-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugPanel.tsx
More file actions
97 lines (89 loc) · 2.71 KB
/
DebugPanel.tsx
File metadata and controls
97 lines (89 loc) · 2.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
/**
* Live introspection of `NitroCompass.getDebugInfo()` rendered as a
* collapsible footer. Intended to make user bug reports
* self-diagnosing: have the user open this panel during a misbehaving
* session and screenshot the values.
*/
import {useEffect, useState} from 'react';
import {Pressable, Text, View} from 'react-native';
import {NitroCompass, type DebugInfo} from 'react-native-nitro-compass';
import {styles} from '../styles';
const POLL_MS = 250;
export function DebugPanel() {
const [open, setOpen] = useState(false);
const [info, setInfo] = useState<DebugInfo | null>(null);
useEffect(() => {
if (!open) return;
const tick = () => {
try {
setInfo(NitroCompass.getDebugInfo());
} catch {
// Native side absent (e.g. web target) or transiently throwing
// — keep the panel mounted, just don't update.
}
};
tick();
const id = setInterval(tick, POLL_MS);
return () => clearInterval(id);
}, [open]);
return (
<View style={styles.debugPanel}>
<Pressable
onPress={() => setOpen(o => !o)}
style={({pressed}) => [
styles.debugToggle,
{opacity: pressed ? 0.6 : 1},
]}>
<Text style={styles.debugToggleText}>
{open ? '▾ Debug' : '▸ Debug'}
</Text>
</Pressable>
{open && info && (
<View style={styles.debugBody}>
<Row k="interferenceActive" v={String(info.interferenceActive)} />
<Row
k="usingUncalibratedMag"
v={String(info.usingUncalibratedMag)}
/>
<Row
k="hasGameRotationVector"
v={String(info.hasGameRotationVector)}
/>
<Row k="lastField (µT)" v={fmtField(info.lastFieldMicroTesla)} />
<Row
k="expectedField (µT)"
v={fmtField(info.expectedFieldMicroTesla)}
/>
<Row k="fusedYaw (°)" v={fmtAngle(info.fusedYawDeg)} />
<Row
k="yawRate (°/s)"
v={info.lastYawRateDegPerS.toFixed(2)}
/>
<Row
k="sinceBiasJump"
v={fmtMs(info.msSinceLastBiasJump)}
/>
</View>
)}
</View>
);
}
function Row({k, v}: {k: string; v: string}) {
return (
<View style={styles.debugRow}>
<Text style={styles.debugKey}>{k}</Text>
<Text style={styles.debugValue}>{v}</Text>
</View>
);
}
function fmtField(n: number): string {
return n === -1 ? '—' : n.toFixed(1);
}
function fmtAngle(n: number): string {
return Number.isNaN(n) ? '—' : n.toFixed(1);
}
function fmtMs(ms: number): string {
if (ms === -1) return '—';
if (ms < 1000) return `${ms} ms`;
return `${(ms / 1000).toFixed(1)} s`;
}