Port Compass to iOS

This commit is contained in:
James Babcock committed 2026-09-18 23:30:07 -07:00
1 parent 5ae2c245c6
commit 2b3e068a23
32 files changed
+490 -56

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
export const COMPASS_CHANGED = 15;
export const COMPASS_CALIBRATION_STARTED = 16;
export const COMPASS_CALIBRATION_COMPLETE = 17;
export type CompassDiagnostics = {
/** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
magneticAccuracy: number;
/** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
magneticAnomalies: number;
/** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
orientationSource: number;
flags: number;
sampleTimeMs: number;
};
export type CompassEvent = {
command: number;
/** Magnetic heading in degrees, or -1 for calibration-only events. */
headingDegrees: number;
/** Absent with stock/older CFW or an unrecognized diagnostic extension. */
diagnostics?: CompassDiagnostics;
};
+29
View File
@@ -0,0 +1,29 @@
import { type CompassEvent } from './compass-types'
export * from './compass-types'
type CompassSession = { setCompassEnabled(enabled: boolean): void }
const owners = new Set<string>()
const listeners = new Set<(event: CompassEvent) => void>()
let session: CompassSession | null = null
/** Holds and listeners survive disconnect/reconnect and opening before Connect. */
export function bindCompassSession(next: CompassSession): void {
if (session && session !== next) session.setCompassEnabled(false)
session = next
session.setCompassEnabled(owners.size > 0)
}
export function setCompassEnabled(enabled: boolean, owner = 'compass'): void {
if (enabled) owners.add(owner)
else owners.delete(owner)
session?.setCompassEnabled(owners.size > 0)
}
export function addCompassListener(listener: (event: CompassEvent) => void): () => void {
listeners.add(listener)
return () => { listeners.delete(listener) }
}
export function receiveCompassEvent(event: CompassEvent): void {
for (const listener of [...listeners]) listener(event)
}
+2 -22
View File
@@ -1,28 +1,8 @@
declare const com: any;
declare const global: any;
export const COMPASS_CHANGED = 15;
export const COMPASS_CALIBRATION_STARTED = 16;
export const COMPASS_CALIBRATION_COMPLETE = 17;
export type CompassDiagnostics = {
/** 0 uncalibrated .. 3 well calibrated; -1 unavailable. */
magneticAccuracy: number;
/** 0 none, 1 small/temperature, 2 large; -1 unavailable. */
magneticAnomalies: number;
/** 0 unknown, 1 GRV (relative), 2 GMRV, 3 RV. */
orientationSource: number;
flags: number;
sampleTimeMs: number;
};
export type CompassEvent = {
command: number;
/** Magnetic heading in degrees, or -1 for calibration-only events. */
headingDegrees: number;
/** Absent with stock/older CFW or an unrecognized diagnostic extension. */
diagnostics?: CompassDiagnostics;
};
import { type CompassEvent } from "./compass-types";
export * from "./compass-types";
function activeCommunicator(): any {
if (!global.isAndroid) return null;
+14
View File
@@ -0,0 +1,14 @@
declare const FaceclawLocation: any
export function restoreDeclination(_latitude: number, _longitude: number, degrees: number): number | null {
return Number.isFinite(degrees) && Math.abs(degrees) <= 180 ? degrees : null
}
export function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> {
return new Promise((resolve, reject) => {
FaceclawLocation.shared().requestDeclination((degrees: number, latitude: number, longitude: number, error: string) => {
if (error) reject(new Error(error))
else resolve({ degrees, latitude, longitude })
})
})
}
+13
View File
@@ -0,0 +1,13 @@
import { magneticDeclinationDegrees } from './geomagnetic'
import { getCurrentLocation } from './location'
export function restoreDeclination(latitude: number, longitude: number, _degrees: number): number | null {
return magneticDeclinationDegrees(latitude, longitude)
}
export async function getCurrentDeclination(): Promise<{ latitude: number; longitude: number; degrees: number }> {
const location = await getCurrentLocation()
const degrees = magneticDeclinationDegrees(location.latitude, location.longitude)
if (degrees === null) throw new Error('Magnetic declination unavailable')
return { ...location, degrees }
}
+4
View File
@@ -32,5 +32,9 @@ export function runKotlinProtocolSmokeTest(): void {
const changed = new Uint8Array(16); changed[0] = 255
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
const compass = protocol.decodeCompassInput({ sid: 8, flag: 1, command: 15, magic: 0,
payload: new Uint8Array([8, 15, 16, 0, 82, 3, 8, 231, 2, 162, 6, 12, 67, 77, 1, 3, 2, 3, 140, 0, 152, 186, 220, 254]) })
if (compass?.headingDegrees !== 359 || compass.diagnostics?.sampleTimeMs !== 0xfedcba98)
throw new Error('Kotlin compass notification mismatch')
console.log('FACECLAW_KOTLIN_PROTOCOL_PASS iOS')
}
+5
View File
@@ -0,0 +1,5 @@
declare const FaceclawLocation: any
export const hasLocationPermission = (): boolean => FaceclawLocation.shared().hasPermission()
export function ensureLocationPermission(): Promise<boolean> {
return new Promise(resolve => FaceclawLocation.shared().requestPermission(resolve))
}
+1
View File
@@ -0,0 +1 @@
export { ensureLocationPermission, hasLocationPermission } from '../g2/android-permissions'