-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAcce.js
More file actions
116 lines (106 loc) · 2.82 KB
/
Copy pathAcce.js
File metadata and controls
116 lines (106 loc) · 2.82 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
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { Accelerometer } from 'expo-sensors';
import { Audio } from 'expo-av';
export default function Acce() {
const [data, setData] = useState({
x: 0,
y: 0,
z: 0,
});
const [subscription, setSubscription] = useState(null);
const _slow = () => {
Accelerometer.setUpdateInterval(1000);
};
const _fast = () => {
Accelerometer.setUpdateInterval(16);
};
const _subscribe = () => {
setSubscription(
Accelerometer.addListener(async(accelerometerData) => {
setData(accelerometerData);
console.log("Zoom zoom: ", accelerometerData)
if (accelerometerData.y <= -1.6) {
try {
const { sound } = await Audio.Sound.createAsync(
require('./assets/Sounds/swiftly.mp3')
);
await sound.playAsync();
} catch {
console.log("Error playing sound!")
}
setShowMessage(true)
} else {
setShowMessage(false)
}
})
);
};
const _unsubscribe = () => {
subscription && subscription.remove();
setSubscription(null);
};
const [showMessage, setShowMessage] = useState(false)
useEffect(() => {
_subscribe();
return () => _unsubscribe();
}, [showMessage]);
const { x, y, z } = data;
return (
<View style={styles.container}>
{
showMessage ?
<Text style={styles.text}>Pothole detected!</Text> : null
}
<Text style={styles.text}>Accelerometer: (in Gs where 1 G = 9.81 m s^-2)</Text>
<Text style={styles.text}>
x: {round(x)} y: {round(y)} z: {round(z)}
</Text>
<View style={styles.buttonContainer}>
<TouchableOpacity onPress={subscription ? _unsubscribe : _subscribe} style={styles.button}>
<Text>{subscription ? 'On' : 'Off'}</Text>
</TouchableOpacity>
<TouchableOpacity onPress={_slow} style={[styles.button, styles.middleButton]}>
<Text>Slow</Text>
</TouchableOpacity>
<TouchableOpacity onPress={_fast} style={styles.button}>
<Text>Fast</Text>
</TouchableOpacity>
</View>
</View>
);
}
function round(n) {
if (!n) {
return 0;
}
return Math.floor(n * 100) / 100;
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingHorizontal: 10,
backgroundColor: 'white'
},
text: {
textAlign: 'center',
},
buttonContainer: {
flexDirection: 'row',
alignItems: 'stretch',
marginTop: 15,
},
button: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#eee',
padding: 10,
},
middleButton: {
borderLeftWidth: 1,
borderRightWidth: 1,
borderColor: '#ccc',
},
});