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
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user