import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter, } from 'react-native'; import { Grid, Col, Row } from "react-native-easy-grid"; import { magnetometer, SensorTypes, setUpdateIntervalForType } from "react-native-sensors"; import LPF from "lpf"; import { colors, handleVibration, height, px2dp } from '../../utils'; interface Props { } interface State { magnetometer: any, show: boolean } export default class KitCompass extends React.Component { constructor(props: Props) { super(props); LPF.init([]); LPF.smoothing = 0.2; }; public switchShowBigCompassListener = null; public tempShow = false; public readonly state: Readonly = { magnetometer: "0", show: false, }; componentDidMount() { this._toggle(); this.switchShowBigCompassListener = DeviceEventEmitter.addListener('switch-big-compass-show', (a) => { this.setState({ show: a.value }); this.tempShow = a.value; }) } componentWillUnmount() { this._unsubscribe(); this.switchShowBigCompassListener && this.switchShowBigCompassListener?.remove(); } _toggle = () => { //@ts-ignore if (this._subscription) { this._unsubscribe(); } else { this._subscribe(); } }; _subscribe = async () => { // const { show } = this.state; setUpdateIntervalForType(SensorTypes.magnetometer, 16); //@ts-ignore this._subscription = magnetometer.subscribe( sensorData => { this.setState({ magnetometer: this._angle(sensorData) }) const degree = this._degree(this._angle(sensorData)); // console.log("show", this.tempShow) if (degree == '360' && this.tempShow) { handleVibration(100) } }, error => console.log("The sensor is not available"), ); }; _unsubscribe = () => { //@ts-ignore this._subscription && this._subscription.unsubscribe(); //@ts-ignore 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() { const { show } = this.state; if(!show){ return <> } return ( {/* {this._direction(this._degree(this.state.magnetometer))} */} {/* */} {this._degree(this.state.magnetometer)}° ) } } const styles = StyleSheet.create({ overLayer: { position: 'absolute', top: 0, left: 0, bottom: 0, width: '100%', height: '100%', zIndex: 999, backgroundColor: 'rgba(0,0,0,0.2)', justifyContent: 'center', alignItems: 'center' }, container: { position: 'relative', width: '100%', height: '100%', // backgroundColor: 'red' }, content: { position: 'absolute', top: 0, left: 0, bottom: 0, width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center' } })