-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathApp.js
156 lines (141 loc) · 4.16 KB
/
App.js
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
import React, {Component} from "react";
import {Text, View, Image, Dimensions} from "react-native";
import {Grid, Col, Row} from "react-native-easy-grid";
import {magnetometer, SensorTypes, setUpdateIntervalForType} from "react-native-sensors";
import LPF from "lpf";
const {height, width} = Dimensions.get("window");
export default class App extends Component {
constructor() {
super();
this.state = {
magnetometer: "0",
};
LPF.init([]);
LPF.smoothing = 0.2;
}
componentDidMount() {
this._toggle();
}
componentWillUnmount() {
this._unsubscribe();
}
_toggle = () => {
if (this._subscription) {
this._unsubscribe();
} else {
this._subscribe();
}
};
_subscribe = async () => {
setUpdateIntervalForType(SensorTypes.magnetometer, 16);
this._subscription = magnetometer.subscribe(
sensorData => this.setState({magnetometer: this._angle(sensorData)}),
error => console.log("The sensor is not available"),
);
};
_unsubscribe = () => {
this._subscription && this._subscription.unsubscribe();
this._subscription = null;
};
_angle = magnetometer => {
let angle = 0;
if (magnetometer) {
let {x, y} = magnetometer;
if (Math.atan2(y, x) >= 0) {
angle = Math.atan2(y, x) * (180 / Math.PI);
} else {
angle = (Math.atan2(y, x) + 2 * Math.PI) * (180 / Math.PI);
}
}
return Math.round(LPF.next(angle));
};
_direction = degree => {
if (degree >= 22.5 && degree < 67.5) {
return "NE";
} else if (degree >= 67.5 && degree < 112.5) {
return "E";
} else if (degree >= 112.5 && degree < 157.5) {
return "SE";
} else if (degree >= 157.5 && degree < 202.5) {
return "S";
} else if (degree >= 202.5 && degree < 247.5) {
return "SW";
} else if (degree >= 247.5 && degree < 292.5) {
return "W";
} else if (degree >= 292.5 && degree < 337.5) {
return "NW";
} else {
return "N";
}
};
// Match the device top with pointer 0° degree. (By default 0° starts from the right of the device.)
_degree = magnetometer => {
return magnetometer - 90 >= 0
? magnetometer - 90
: magnetometer + 271;
};
render() {
return (
<Grid style={{backgroundColor: "black"}}>
<Row style={{alignItems: "center"}} size={0.9}>
<Col style={{alignItems: "center"}}>
<Text
style={{
color: "#fff",
fontSize: height / 26,
fontWeight: "bold",
}}
>
{this._direction(this._degree(this.state.magnetometer))}
</Text>
</Col>
</Row>
<Row style={{alignItems: "center"}} size={0.1}>
<Col style={{alignItems: "center"}}>
<View style={{width: width, alignItems: "center", bottom: 0}}>
<Image
source={require("./assets/compass_pointer.png")}
style={{
height: height / 26,
resizeMode: "contain",
}}
/>
</View>
</Col>
</Row>
<Row style={{alignItems: "center"}} size={2}>
<Text
style={{
color: "#fff",
fontSize: height / 27,
width: width,
position: "absolute",
textAlign: "center",
}}
>
{this._degree(this.state.magnetometer)}°
</Text>
<Col style={{alignItems: "center"}}>
<Image
source={require("./assets/compass_bg.png")}
style={{
height: width - 80,
justifyContent: "center",
alignItems: "center",
resizeMode: "contain",
transform: [
{rotate: 360 - this.state.magnetometer + "deg"},
],
}}
/>
</Col>
</Row>
<Row style={{alignItems: "center"}} size={1}>
<Col style={{alignItems: "center"}}>
<Text style={{color: "#fff"}}>Copyright @RahulHaque</Text>
</Col>
</Row>
</Grid>
);
}
}