-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
172 lines (165 loc) · 4.71 KB
/
App.js
File metadata and controls
172 lines (165 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import React, { Component } from 'react';
import {
Alert,
StyleSheet,
View,
Text,
TouchableOpacity,
StatusBar,
} from 'react-native';
import {
gyroscope,
setUpdateIntervalForType,
SensorTypes
} from "react-native-sensors";
import DialogProgress from 'react-native-dialog-progress'
let calibrate;
export default class App extends Component {
constructor() {
super();
setUpdateIntervalForType(SensorTypes.gyroscope, 2000); // defaults to 100ms
this.state = {
updatesEnabled: false,
x: 0,
y: 0,
z: 0,
x_low: 0,
y_low: 0,
z_low: 0,
x_high: 0,
y_high: 0,
z_high: 0
};
}
calibrateGyroscope = async () => {
const promise = new Promise((resolve, reject) => {
const options = {
title: "กำลังเก็บค่าไจโรสโคป",
message: "โปรดรอสักครู่...",
isCancelable: false
}
DialogProgress.show(options)
this.setState({ updatesEnabled: true })
let test = 0;
calibrate = gyroscope.subscribe(({ x, y, z, timestamp }) => {
if (test === 0) {
this.setState({
x_low: x,
y_low: y,
z_low: z,
x_high: x,
y_high: y,
z_high: z,
})
this.setState({
x: x,
y: y,
z: z
})
test++;
} else {
if (x <= this.state.x_low)
this.setState({ x_low: x })
else if (x >= this.state.x_high)
this.setState({ x_high: x })
if (y <= this.state.y_low)
this.setState({ y_low: y })
else if (y >= this.state.y_high)
this.setState({ y_high: y })
if (z <= this.state.z_low)
this.setState({ z_low: z })
else if (z >= this.state.z_high)
this.setState({ z_high: z })
this.setState({
x: x,
y: y,
z: z
})
test++;
if (test === 30) {
this.stopCalibrate();
DialogProgress.hide()
this.setState({ updatesEnabled: false })
resolve(test);
}
}
}
);
});
const result = await promise;
}
stopCalibrate = () => {
calibrate.unsubscribe()
}
render() {
const {
updatesEnabled
} = this.state;
return (
<View style={styles.MainContainer}>
<StatusBar barStyle="dark-content" />
<Text style={{ fontSize: 32, textAlign: 'center' }}>โปรแกรมเก็บค่าการ{"\n"}ทดสอบไจโรสโคป</Text>
<Text style={{ fontSize: 24 }}>X: {this.state.x}{"\n"}Y: {this.state.y}{"\n"}Z: {this.state.z}</Text>
<Text style={{ fontSize: 24 }}>X low: {this.state.x_low}{"\n"}Y low: {this.state.y_low}{"\n"}Z low: {this.state.z_low}</Text>
<Text style={{ fontSize: 24 }}>X high: {this.state.x_high}{"\n"}Y high: {this.state.y_high}{"\n"}Z high: {this.state.z_high}</Text>
{!updatesEnabled ?
<TouchableOpacity
onPress={async () => {
Alert.alert(
'ยืนยัน',
'กดปุ่มยืนยันเพื่อเริ่มการเก็บค่า',
[
{ text: 'ยกเลิก', style: 'cancel' },
{
text: 'ยืนยัน', onPress: async () => {
const calib = await this.calibrateGyroscope()
}
},
],
{ cancelable: true })
}
} disabled={updatesEnabled}
style={styles.button}>
<Text style={{ color: '#FFF', fontSize: 24 }}>
เริ่มการทำงาน
</Text>
</TouchableOpacity> :
<TouchableOpacity
onPress={() => this.removeLocationUpdates()} disabled={!updatesEnabled}
style={styles.button_red}>
<Text style={{ color: '#FFF', fontSize: 24 }}>
หยุดการทำงาน
</Text>
</TouchableOpacity>}
</View>
);
}
};
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
paddingTop: (Platform.OS) === 'ios' ? 20 : 0,
alignItems: 'center',
justifyContent: 'center',
},
QR_text: {
color: '#000',
fontSize: 19,
padding: 8,
marginTop: 12
},
button: {
backgroundColor: '#2979FF',
alignItems: 'center',
padding: 12,
width: 300,
marginTop: 14
},
button_red: {
backgroundColor: 'red',
alignItems: 'center',
padding: 12,
width: 300,
marginTop: 14
},
});