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'; import FastImage from 'react-native-fast-image'; interface Props { } interface State { magnetometer: any showBjgCompass: boolean } export default class KitSmallCompass extends React.Component { constructor(props: Props) { super(props); LPF.init([]); LPF.smoothing = 0.2; }; public readonly state: Readonly = { magnetometer: "0", showBjgCompass: false, }; componentDidMount() { this._toggle(); } componentWillUnmount() { this._unsubscribe(); } _toggle = () => { //@ts-ignore if (this._subscription) { this._unsubscribe(); } else { this._subscribe(); } }; _subscribe = async () => { setUpdateIntervalForType(SensorTypes.magnetometer, 16); //@ts-ignore this._subscription = magnetometer.subscribe( sensorData => { this.setState({ magnetometer: this._angle(sensorData) }) }, 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; }; switchShowBjgCompass() { let { showBjgCompass } = this.state; this.setState({ showBjgCompass: !showBjgCompass }) DeviceEventEmitter.emit('switch-big-compass-show', { value: !showBjgCompass }) }; render() { const { showBjgCompass } = this.state; return ( { showBjgCompass ? : } ) } } const styles = StyleSheet.create({ btnContainer: { position: 'absolute', bottom: px2dp(178), left: px2dp(13), width: px2dp(36), height: px2dp(38), zIndex: 999, backgroundColor: 'rgba(255,255,255, 0.88)', justifyContent: 'center', alignItems: 'center', elevation: 8, borderRadius: px2dp(4), }, 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' } })