forked from Blendman974/faceclaw
Port Compass to iOS
This commit is contained in:
1 parent
5ae2c245c6
commit
2b3e068a23
32 files changed
+490
-56
No files matched your search
@@ -21,7 +21,7 @@ import {
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
|
||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
@@ -268,6 +268,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
reconfigureSurface: options.reconfigureSurface,
|
||||
onClosed: () => {
|
||||
layer.stop();
|
||||
options.onClosed();
|
||||
|
||||
@@ -2,16 +2,16 @@
|
||||
* Local magnetic declination for the compass, so the app can show true north.
|
||||
*
|
||||
* Declination only varies over tens of kilometres, so the location it needs is
|
||||
* whatever the phone can cheaply produce: the same one-shot coarse lookup the
|
||||
* Weather app uses, refreshed no more than daily. The last fix is persisted so
|
||||
* a fresh start has a usable declination before location comes back, and the
|
||||
* model is re-evaluated at the current time when it does.
|
||||
* whatever the phone can cheaply produce, refreshed no more than daily. The
|
||||
* last fix is persisted so a fresh start has a usable correction immediately.
|
||||
* Android re-evaluates its magnetic model; iOS caches the correction supplied
|
||||
* by Core Location (true heading minus magnetic heading).
|
||||
*/
|
||||
import { hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
import { hasLocationPermission } from "../../native/location-permissions";
|
||||
import { getCurrentDeclination, restoreDeclination } from "../../native/declination";
|
||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||
|
||||
const DEGREES_KEY = "compass.declination.degrees";
|
||||
const LATITUDE_KEY = "compass.declination.latitude";
|
||||
const LONGITUDE_KEY = "compass.declination.longitude";
|
||||
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
|
||||
@@ -40,7 +40,7 @@ function loadStoredFix(): void {
|
||||
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
|
||||
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
|
||||
fix = { latitude, longitude, locatedAtMs };
|
||||
declination = magneticDeclinationDegrees(latitude, longitude);
|
||||
declination = restoreDeclination(latitude, longitude, parseFloat(getStringSetting(DEGREES_KEY, "")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,16 +73,17 @@ export function refreshDeclination(): void {
|
||||
loadStoredFix();
|
||||
if (refreshing || !hasLocationPermission()) return;
|
||||
const now = Date.now();
|
||||
if (fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
|
||||
if (declination !== null && fix !== null && now - fix.locatedAtMs < FIX_MAX_AGE_MS) return;
|
||||
if (now - lastAttemptMs < RETRY_INTERVAL_MS) return;
|
||||
lastAttemptMs = now;
|
||||
refreshing = true;
|
||||
getCurrentLocation()
|
||||
getCurrentDeclination()
|
||||
.then((location) => {
|
||||
const value = magneticDeclinationDegrees(location.latitude, location.longitude);
|
||||
if (value === null) return;
|
||||
const value = location.degrees;
|
||||
if (!Number.isFinite(value)) return;
|
||||
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
|
||||
declination = value;
|
||||
setStringSetting(DEGREES_KEY, String(value));
|
||||
setStringSetting(LATITUDE_KEY, String(location.latitude));
|
||||
setStringSetting(LONGITUDE_KEY, String(location.longitude));
|
||||
setStringSetting(LOCATED_AT_KEY, String(now));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { type GrayImage } from "../../../graphics/image";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||
import { hasLocationPermission } from "../../../g2/android-permissions";
|
||||
import { hasLocationPermission } from "../../../native/location-permissions";
|
||||
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
||||
|
||||
@@ -4,7 +4,6 @@ const unavailable: Record<string, string> = {
|
||||
notifications: 'Phone notification integration is not available on iOS yet.',
|
||||
transcribe: 'Voice capture and transcription are not available on iOS yet.',
|
||||
microphones: 'Glasses microphone capture is not available on iOS yet.',
|
||||
compass: 'Glasses compass streaming is not available on iOS yet.',
|
||||
navigate: 'Navigation location tracking is not available on iOS yet.',
|
||||
weather: 'Weather location access is not available on iOS yet.',
|
||||
calendar: 'Calendar access is not available on iOS yet.',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { type CompassEvent } from '../native/compass-types'
|
||||
/** iOS binary bridge to the same Kotlin protocol used by Android. */
|
||||
import { fromData, toData, nativeArray } from '../native/kotlin-data'
|
||||
import { OsEventTypeList } from './events'
|
||||
@@ -64,3 +65,13 @@ export function packGray4(gray: Uint8Array, width: number, height: number): Uint
|
||||
return fromData(protocol.packDataWidthHeight(toData(gray), width, height))
|
||||
}
|
||||
export const rle4 = (packed: Uint8Array): Uint8Array => fromData(protocol.rleData(toData(packed)))
|
||||
|
||||
export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
|
||||
const event = protocol.compassInputDataSidFlag(toData(message.payload), message.sid, message.flag)
|
||||
return event ? { command: event.command, headingDegrees: event.headingDegrees,
|
||||
...(event.diagnosticFlags >= 0 ? { diagnostics: {
|
||||
magneticAccuracy: event.magneticAccuracy, magneticAnomalies: event.magneticAnomalies,
|
||||
orientationSource: event.orientationSource, flags: event.diagnosticFlags, sampleTimeMs: Number(event.sampleTimeMs),
|
||||
} } : {}),
|
||||
} : null
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { type CompassEvent } from '../native/compass-types'
|
||||
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
|
||||
import { OsEventTypeList } from './events'
|
||||
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||
@@ -178,3 +179,23 @@ export function rle4(packed: Uint8Array): Uint8Array {
|
||||
}
|
||||
return output.slice(0, used)
|
||||
}
|
||||
|
||||
/** Reference decoder for host tests; iOS uses the shared Kotlin implementation. */
|
||||
export function decodeCompassInput(message: ProtocolMessage): CompassEvent | null {
|
||||
if (message.sid !== 8 || ![1, 6].includes(message.flag)) return null
|
||||
const command = readInteger(message.payload, 1, -1)
|
||||
if (command === 16 || command === 17) return { command, headingDegrees: -1 }
|
||||
if (command !== 15) return null
|
||||
const compass = readBytes(message.payload, 10)
|
||||
const headingDegrees = compass ? readInteger(compass, 1, -1) : -1
|
||||
if (headingDegrees < 0 || headingDegrees >= 360) return null
|
||||
const event: CompassEvent = { command, headingDegrees }
|
||||
let d: Uint8Array | undefined
|
||||
try { d = readBytes(message.payload, 100) } catch { return event }
|
||||
if (d?.length === 12 && d[0] === 67 && d[1] === 77 && d[2] === 1 && d[7] === 0 &&
|
||||
(d[3] <= 3 || d[3] === 255) && (d[4] <= 2 || d[4] === 255) && d[5] <= 3) {
|
||||
event.diagnostics = { magneticAccuracy: d[3] === 255 ? -1 : d[3], magneticAnomalies: d[4] === 255 ? -1 : d[4],
|
||||
orientationSource: d[5], flags: d[6], sampleTimeMs: new DataView(d.buffer, d.byteOffset + 8, 4).getUint32(0, true) }
|
||||
}
|
||||
return event
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { type CompassEvent } from '../native/compass-types'
|
||||
import * as protocol from './ble-protocol'
|
||||
import { iosBleTraffic } from './ble-traffic-counters'
|
||||
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
||||
@@ -74,11 +75,16 @@ export class GlassesSession {
|
||||
private microphoneToken = 0
|
||||
private microphoneWork: Promise<void> = Promise.resolve()
|
||||
private microphoneEnabled = false
|
||||
private compassWanted = false
|
||||
private compassSent: boolean | null = null
|
||||
private compassStopping = false
|
||||
private compassWork: Promise<void> = Promise.resolve()
|
||||
constructor(private readonly transport: SessionTransport,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {},
|
||||
private readonly onActivity: () => void = () => {}) {
|
||||
private readonly onActivity: () => void = () => {},
|
||||
private readonly onCompass: (event: CompassEvent) => void = () => {}) {
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
private update(phase: SessionState['phase'], status: string): void {
|
||||
@@ -88,7 +94,7 @@ export class GlassesSession {
|
||||
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
|
||||
const invalid = deviceAddressError(addresses)
|
||||
if (invalid) { this.update('error', invalid); return }
|
||||
this.addresses = { ...addresses }; this.wanted = true
|
||||
this.addresses = { ...addresses }; this.wanted = true; this.compassStopping = false
|
||||
if (!retry) this.retryCount = 0
|
||||
const generation = ++this.generation
|
||||
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
|
||||
@@ -131,6 +137,7 @@ export class GlassesSession {
|
||||
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
|
||||
this.displayed = null; this.retryCount = 0
|
||||
this.update('connected', 'Glasses connected')
|
||||
await this.syncCompass(); this.check(generation)
|
||||
this.schedule()
|
||||
if (addresses.ring && ids.ring) await this.connectRing(generation)
|
||||
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
|
||||
@@ -347,6 +354,10 @@ export class GlassesSession {
|
||||
}
|
||||
if (message.sid === protocol.SID.settings) this.applySettings(message)
|
||||
if (event.identifier === this.ids.right) {
|
||||
if (this.state.phase === 'connected') {
|
||||
const compass = protocol.decodeCompassInput(message)
|
||||
if (compass) this.onCompass(compass)
|
||||
}
|
||||
const input = protocol.decodeGlassesInput(message)
|
||||
if (input) {
|
||||
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
|
||||
@@ -406,6 +417,24 @@ export class GlassesSession {
|
||||
this.microphoneWork = work
|
||||
return work
|
||||
}
|
||||
setCompassEnabled(enabled: boolean): void {
|
||||
this.compassWanted = enabled
|
||||
void this.syncCompass()
|
||||
}
|
||||
/** Serialize changes so a release cannot overtake an unacknowledged enable. */
|
||||
private syncCompass(): Promise<void> {
|
||||
const generation = this.generation
|
||||
const work = this.compassWork.then(async () => {
|
||||
if (generation !== this.generation || this.state.phase !== 'connected') return
|
||||
const enabled = !this.compassStopping && this.compassWanted
|
||||
if (this.compassSent === enabled) return
|
||||
// Same CFW mode 10, 100 ms report interval and zero minimum change as Android.
|
||||
await this.requestCfw('left', new Uint8Array(enabled ? [10, 2, 100, 0, 0, 0] : [10, 0]))
|
||||
if (generation === this.generation) this.compassSent = enabled
|
||||
}).catch(error => { if (generation === this.generation) this.fail(error, true) })
|
||||
this.compassWork = work
|
||||
return work
|
||||
}
|
||||
wake(): void {
|
||||
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
void this.pump()
|
||||
@@ -480,6 +509,7 @@ export class GlassesSession {
|
||||
}
|
||||
private reset(): void {
|
||||
++this.generation
|
||||
this.compassSent = null; this.compassWork = Promise.resolve()
|
||||
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
|
||||
if (this.timer !== null) clearTimeout(this.timer)
|
||||
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
|
||||
@@ -516,6 +546,8 @@ export class GlassesSession {
|
||||
}
|
||||
private async stopInternal(): Promise<void> {
|
||||
this.wanted = false
|
||||
this.compassStopping = true
|
||||
await this.syncCompass()
|
||||
// Stop mic capture before ending the display session. Late enable ACKs
|
||||
// cannot leave a microphone streaming after an explicit Disconnect.
|
||||
if (this.audioListener || this.microphoneEnabled) {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { bindCompassSession, receiveCompassEvent } from '../native/compass.ios'
|
||||
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
|
||||
import { iosBluetooth } from '../native/ios-bluetooth'
|
||||
import { iosVoiceInput } from '../native/ios-voice-input'
|
||||
@@ -378,7 +379,8 @@ export class IosPreviewController {
|
||||
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
|
||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}, message => this.logBluetooth(message), () => this.refreshClock())
|
||||
}, message => this.logBluetooth(message), () => this.refreshClock(), receiveCompassEvent)
|
||||
bindCompassSession(this.session)
|
||||
}
|
||||
await this.session.start(addresses)
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
@@ -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 })
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -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 }
|
||||
}
|
||||
@@ -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')
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { ensureLocationPermission, hasLocationPermission } from '../g2/android-permissions'
|
||||
Reference in new issue
Block a user