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
@@ -22,6 +22,8 @@
|
|||||||
<string>Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures.</string>
|
<string>Faceclaw uses Bluetooth to connect to your glasses and ring, display your apps, and receive gestures.</string>
|
||||||
<key>NSSpeechRecognitionUsageDescription</key>
|
<key>NSSpeechRecognitionUsageDescription</key>
|
||||||
<string>Faceclaw turns audio from your glasses microphone into text for the app you choose. Dictation uses on-device recognition.</string>
|
<string>Faceclaw turns audio from your glasses microphone into text for the app you choose. Dictation uses on-device recognition.</string>
|
||||||
|
<key>NSLocationWhenInUseUsageDescription</key>
|
||||||
|
<string>Faceclaw uses your location to correct the glasses compass from magnetic north to true north.</string>
|
||||||
<key>UIBackgroundModes</key>
|
<key>UIBackgroundModes</key>
|
||||||
<array>
|
<array>
|
||||||
<string>bluetooth-central</string>
|
<string>bluetooth-central</string>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ TARGETED_DEVICE_FAMILY = 1;
|
|||||||
CODE_SIGN_STYLE = Automatic;
|
CODE_SIGN_STYLE = Automatic;
|
||||||
|
|
||||||
// Select the local signing team in Xcode or pass --teamId to the iOS scripts.
|
// Select the local signing team in Xcode or pass --teamId to the iOS scripts.
|
||||||
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers
|
OTHER_LDFLAGS = $(inherited) -framework CoreBluetooth -framework CoreLocation -framework CoreText -framework Speech -framework AVFoundation -framework WebKit -framework UniformTypeIdentifiers
|
||||||
|
|
||||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#import <Foundation/Foundation.h>
|
||||||
|
|
||||||
|
/** Short, foreground Core Location lookups for the glasses' true-north correction. */
|
||||||
|
@interface FaceclawLocation : NSObject
|
||||||
|
+ (instancetype)shared;
|
||||||
|
- (BOOL)hasPermission;
|
||||||
|
- (void)requestPermission:(void (^)(BOOL granted))completion;
|
||||||
|
- (void)requestDeclination:(void (^)(double degrees, double latitude, double longitude, NSString *error))completion;
|
||||||
|
@end
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#import "FaceclawLocation.h"
|
||||||
|
#import <CoreLocation/CoreLocation.h>
|
||||||
|
#import <math.h>
|
||||||
|
|
||||||
|
@interface FaceclawLocation () <CLLocationManagerDelegate>
|
||||||
|
@property(nonatomic, strong) CLLocationManager *manager;
|
||||||
|
@property(nonatomic, strong) NSMutableArray *permissionCallbacks;
|
||||||
|
@property(nonatomic, strong) NSMutableArray *declinationCallbacks;
|
||||||
|
@property(nonatomic, strong) NSTimer *timer;
|
||||||
|
@property(nonatomic, strong) CLLocation *location;
|
||||||
|
@property(nonatomic, strong) CLHeading *heading;
|
||||||
|
@end
|
||||||
|
|
||||||
|
@implementation FaceclawLocation
|
||||||
|
+ (instancetype)shared {
|
||||||
|
static FaceclawLocation *instance;
|
||||||
|
static dispatch_once_t once;
|
||||||
|
dispatch_once(&once, ^{ instance = [FaceclawLocation new]; });
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
- (instancetype)init {
|
||||||
|
if ((self = [super init])) {
|
||||||
|
_permissionCallbacks = [NSMutableArray new];
|
||||||
|
_declinationCallbacks = [NSMutableArray new];
|
||||||
|
_manager = [CLLocationManager new];
|
||||||
|
_manager.delegate = self;
|
||||||
|
_manager.desiredAccuracy = kCLLocationAccuracyKilometer;
|
||||||
|
}
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
- (BOOL)hasPermission {
|
||||||
|
CLAuthorizationStatus status = self.manager.authorizationStatus;
|
||||||
|
return status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse;
|
||||||
|
}
|
||||||
|
- (void)requestPermission:(void (^)(BOOL))completion {
|
||||||
|
if (self.manager.authorizationStatus != kCLAuthorizationStatusNotDetermined) {
|
||||||
|
completion([self hasPermission]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[self.permissionCallbacks addObject:[completion copy]];
|
||||||
|
[self.manager requestWhenInUseAuthorization];
|
||||||
|
}
|
||||||
|
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
|
||||||
|
if (manager.authorizationStatus == kCLAuthorizationStatusNotDetermined) return;
|
||||||
|
NSArray *callbacks = [self.permissionCallbacks copy];
|
||||||
|
[self.permissionCallbacks removeAllObjects];
|
||||||
|
for (void (^callback)(BOOL) in callbacks) callback([self hasPermission]);
|
||||||
|
if (![self hasPermission]) [self finishWithError:@"Location permission is required for true north"];
|
||||||
|
}
|
||||||
|
- (void)requestDeclination:(void (^)(double, double, double, NSString *))completion {
|
||||||
|
if (![self hasPermission] || ![CLLocationManager headingAvailable]) {
|
||||||
|
completion(0, 0, 0, @"Location or heading unavailable for true north");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
[self.declinationCallbacks addObject:[completion copy]];
|
||||||
|
if (self.timer) return;
|
||||||
|
self.location = nil;
|
||||||
|
self.heading = nil;
|
||||||
|
// trueHeading is valid only while the same manager receives location updates.
|
||||||
|
// Subtracting magneticHeading cancels the phone's orientation: the result is
|
||||||
|
// a local correction to the GLASSES heading, never the phone's heading.
|
||||||
|
[self.manager startUpdatingLocation];
|
||||||
|
[self.manager startUpdatingHeading];
|
||||||
|
self.timer = [NSTimer scheduledTimerWithTimeInterval:20 repeats:NO block:^(NSTimer *timer) {
|
||||||
|
[self finishWithError:@"Timed out obtaining the true-north correction"];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
|
||||||
|
CLLocation *location = locations.lastObject;
|
||||||
|
if (!location || location.horizontalAccuracy < 0 || fabs(location.timestamp.timeIntervalSinceNow) > 60) return;
|
||||||
|
self.location = location;
|
||||||
|
[self finishIfReady];
|
||||||
|
}
|
||||||
|
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
|
||||||
|
if (heading.headingAccuracy < 0 || heading.trueHeading < 0 || heading.magneticHeading < 0 ||
|
||||||
|
fabs(heading.timestamp.timeIntervalSinceNow) > 60) return;
|
||||||
|
self.heading = heading;
|
||||||
|
[self finishIfReady];
|
||||||
|
}
|
||||||
|
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager { return NO; }
|
||||||
|
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
|
||||||
|
if (error.code != kCLErrorLocationUnknown) [self finishWithError:error.localizedDescription];
|
||||||
|
}
|
||||||
|
- (void)finishIfReady {
|
||||||
|
if (self.location && self.heading) [self finishWithError:@""];
|
||||||
|
}
|
||||||
|
- (void)finishWithError:(NSString *)error {
|
||||||
|
double degrees = self.heading.trueHeading - self.heading.magneticHeading;
|
||||||
|
if (degrees > 180) degrees -= 360;
|
||||||
|
if (degrees < -180) degrees += 360;
|
||||||
|
CLLocationCoordinate2D coordinate = self.location.coordinate;
|
||||||
|
NSArray *callbacks = [self.declinationCallbacks copy];
|
||||||
|
[self.declinationCallbacks removeAllObjects];
|
||||||
|
[self.timer invalidate]; self.timer = nil;
|
||||||
|
[self.manager stopUpdatingLocation];
|
||||||
|
[self.manager stopUpdatingHeading];
|
||||||
|
self.location = nil; self.heading = nil;
|
||||||
|
for (void (^callback)(double, double, double, NSString *) in callbacks)
|
||||||
|
callback(degrees, coordinate.latitude, coordinate.longitude, error);
|
||||||
|
}
|
||||||
|
@end
|
||||||
@@ -5,6 +5,7 @@ module FaceclawGraphics {
|
|||||||
header "FaceclawSocket.h"
|
header "FaceclawSocket.h"
|
||||||
header "FaceclawLc3Decoder.h"
|
header "FaceclawLc3Decoder.h"
|
||||||
header "FaceclawSpeech.h"
|
header "FaceclawSpeech.h"
|
||||||
|
header "FaceclawLocation.h"
|
||||||
header "FaceclawConfigPort.h"
|
header "FaceclawConfigPort.h"
|
||||||
header "FaceclawEvenHubWebView.h"
|
header "FaceclawEvenHubWebView.h"
|
||||||
export *
|
export *
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import {
|
|||||||
type InProcessWindow,
|
type InProcessWindow,
|
||||||
} from "../../ui/shell/in-process-window";
|
} from "../../ui/shell/in-process-window";
|
||||||
import { shell } from "../../ui/shell/shell";
|
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 { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||||
@@ -268,6 +268,7 @@ export function createCompassAppWindow(options: InProcessAppOptions): InProcessW
|
|||||||
submitFrame: options.submitFrame,
|
submitFrame: options.submitFrame,
|
||||||
setSurfaceVisible: options.setSurfaceVisible,
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
removeSurface: options.removeSurface,
|
removeSurface: options.removeSurface,
|
||||||
|
reconfigureSurface: options.reconfigureSurface,
|
||||||
onClosed: () => {
|
onClosed: () => {
|
||||||
layer.stop();
|
layer.stop();
|
||||||
options.onClosed();
|
options.onClosed();
|
||||||
|
|||||||
@@ -2,16 +2,16 @@
|
|||||||
* Local magnetic declination for the compass, so the app can show true north.
|
* 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
|
* 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
|
* whatever the phone can cheaply produce, refreshed no more than daily. The
|
||||||
* Weather app uses, refreshed no more than daily. The last fix is persisted so
|
* last fix is persisted so a fresh start has a usable correction immediately.
|
||||||
* a fresh start has a usable declination before location comes back, and the
|
* Android re-evaluates its magnetic model; iOS caches the correction supplied
|
||||||
* model is re-evaluated at the current time when it does.
|
* by Core Location (true heading minus magnetic heading).
|
||||||
*/
|
*/
|
||||||
import { hasLocationPermission } from "../../g2/android-permissions";
|
import { hasLocationPermission } from "../../native/location-permissions";
|
||||||
import { magneticDeclinationDegrees } from "../../native/geomagnetic";
|
import { getCurrentDeclination, restoreDeclination } from "../../native/declination";
|
||||||
import { getCurrentLocation } from "../../native/location";
|
|
||||||
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
import { getStringSetting, setStringSetting } from "../../native/settings-store";
|
||||||
|
|
||||||
|
const DEGREES_KEY = "compass.declination.degrees";
|
||||||
const LATITUDE_KEY = "compass.declination.latitude";
|
const LATITUDE_KEY = "compass.declination.latitude";
|
||||||
const LONGITUDE_KEY = "compass.declination.longitude";
|
const LONGITUDE_KEY = "compass.declination.longitude";
|
||||||
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
|
const LOCATED_AT_KEY = "compass.declination.locatedAtMs";
|
||||||
@@ -40,7 +40,7 @@ function loadStoredFix(): void {
|
|||||||
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
|
const locatedAtMs = parseInt(getStringSetting(LOCATED_AT_KEY, ""), 10);
|
||||||
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
|
if (Number.isFinite(latitude) && Number.isFinite(longitude) && Number.isFinite(locatedAtMs)) {
|
||||||
fix = { latitude, longitude, 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();
|
loadStoredFix();
|
||||||
if (refreshing || !hasLocationPermission()) return;
|
if (refreshing || !hasLocationPermission()) return;
|
||||||
const now = Date.now();
|
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;
|
if (now - lastAttemptMs < RETRY_INTERVAL_MS) return;
|
||||||
lastAttemptMs = now;
|
lastAttemptMs = now;
|
||||||
refreshing = true;
|
refreshing = true;
|
||||||
getCurrentLocation()
|
getCurrentDeclination()
|
||||||
.then((location) => {
|
.then((location) => {
|
||||||
const value = magneticDeclinationDegrees(location.latitude, location.longitude);
|
const value = location.degrees;
|
||||||
if (value === null) return;
|
if (!Number.isFinite(value)) return;
|
||||||
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
|
fix = { latitude: location.latitude, longitude: location.longitude, locatedAtMs: now };
|
||||||
declination = value;
|
declination = value;
|
||||||
|
setStringSetting(DEGREES_KEY, String(value));
|
||||||
setStringSetting(LATITUDE_KEY, String(location.latitude));
|
setStringSetting(LATITUDE_KEY, String(location.latitude));
|
||||||
setStringSetting(LONGITUDE_KEY, String(location.longitude));
|
setStringSetting(LONGITUDE_KEY, String(location.longitude));
|
||||||
setStringSetting(LOCATED_AT_KEY, String(now));
|
setStringSetting(LOCATED_AT_KEY, String(now));
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { type GrayImage } from "../../../graphics/image";
|
import { type GrayImage } from "../../../graphics/image";
|
||||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
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 { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||||
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||||
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
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.',
|
notifications: 'Phone notification integration is not available on iOS yet.',
|
||||||
transcribe: 'Voice capture and transcription are 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.',
|
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.',
|
navigate: 'Navigation location tracking is not available on iOS yet.',
|
||||||
weather: 'Weather location access 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.',
|
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. */
|
/** iOS binary bridge to the same Kotlin protocol used by Android. */
|
||||||
import { fromData, toData, nativeArray } from '../native/kotlin-data'
|
import { fromData, toData, nativeArray } from '../native/kotlin-data'
|
||||||
import { OsEventTypeList } from './events'
|
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))
|
return fromData(protocol.packDataWidthHeight(toData(gray), width, height))
|
||||||
}
|
}
|
||||||
export const rle4 = (packed: Uint8Array): Uint8Array => fromData(protocol.rleData(toData(packed)))
|
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. */
|
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
|
||||||
import { OsEventTypeList } from './events'
|
import { OsEventTypeList } from './events'
|
||||||
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||||
@@ -178,3 +179,23 @@ export function rle4(packed: Uint8Array): Uint8Array {
|
|||||||
}
|
}
|
||||||
return output.slice(0, used)
|
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 * as protocol from './ble-protocol'
|
||||||
import { iosBleTraffic } from './ble-traffic-counters'
|
import { iosBleTraffic } from './ble-traffic-counters'
|
||||||
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
import { buildBoundingBoxPayload, buildFullFrameBands } from './ble-image-optimizer'
|
||||||
@@ -74,11 +75,16 @@ export class GlassesSession {
|
|||||||
private microphoneToken = 0
|
private microphoneToken = 0
|
||||||
private microphoneWork: Promise<void> = Promise.resolve()
|
private microphoneWork: Promise<void> = Promise.resolve()
|
||||||
private microphoneEnabled = false
|
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,
|
constructor(private readonly transport: SessionTransport,
|
||||||
private readonly onState: (state: SessionState) => void,
|
private readonly onState: (state: SessionState) => void,
|
||||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||||
private readonly log: (message: string) => 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))
|
this.off = transport.onEvent(event => this.receive(event))
|
||||||
}
|
}
|
||||||
private update(phase: SessionState['phase'], status: string): void {
|
private update(phase: SessionState['phase'], status: string): void {
|
||||||
@@ -88,7 +94,7 @@ export class GlassesSession {
|
|||||||
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
|
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
|
||||||
const invalid = deviceAddressError(addresses)
|
const invalid = deviceAddressError(addresses)
|
||||||
if (invalid) { this.update('error', invalid); return }
|
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
|
if (!retry) this.retryCount = 0
|
||||||
const generation = ++this.generation
|
const generation = ++this.generation
|
||||||
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, charging: null, ring: false }
|
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.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
|
||||||
this.displayed = null; this.retryCount = 0
|
this.displayed = null; this.retryCount = 0
|
||||||
this.update('connected', 'Glasses connected')
|
this.update('connected', 'Glasses connected')
|
||||||
|
await this.syncCompass(); this.check(generation)
|
||||||
this.schedule()
|
this.schedule()
|
||||||
if (addresses.ring && ids.ring) await this.connectRing(generation)
|
if (addresses.ring && ids.ring) await this.connectRing(generation)
|
||||||
else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
|
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 (message.sid === protocol.SID.settings) this.applySettings(message)
|
||||||
if (event.identifier === this.ids.right) {
|
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)
|
const input = protocol.decodeGlassesInput(message)
|
||||||
if (input) {
|
if (input) {
|
||||||
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
|
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
|
this.microphoneWork = work
|
||||||
return 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 {
|
wake(): void {
|
||||||
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
if (this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||||
void this.pump()
|
void this.pump()
|
||||||
@@ -480,6 +509,7 @@ export class GlassesSession {
|
|||||||
}
|
}
|
||||||
private reset(): void {
|
private reset(): void {
|
||||||
++this.generation
|
++this.generation
|
||||||
|
this.compassSent = null; this.compassWork = Promise.resolve()
|
||||||
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
|
++this.microphoneToken; this.audioListener = null; this.microphoneEnabled = false
|
||||||
if (this.timer !== null) clearTimeout(this.timer)
|
if (this.timer !== null) clearTimeout(this.timer)
|
||||||
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
|
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
|
||||||
@@ -516,6 +546,8 @@ export class GlassesSession {
|
|||||||
}
|
}
|
||||||
private async stopInternal(): Promise<void> {
|
private async stopInternal(): Promise<void> {
|
||||||
this.wanted = false
|
this.wanted = false
|
||||||
|
this.compassStopping = true
|
||||||
|
await this.syncCompass()
|
||||||
// Stop mic capture before ending the display session. Late enable ACKs
|
// Stop mic capture before ending the display session. Late enable ACKs
|
||||||
// cannot leave a microphone streaming after an explicit Disconnect.
|
// cannot leave a microphone streaming after an explicit Disconnect.
|
||||||
if (this.audioListener || this.microphoneEnabled) {
|
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 { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
|
||||||
import { iosBluetooth } from '../native/ios-bluetooth'
|
import { iosBluetooth } from '../native/ios-bluetooth'
|
||||||
import { iosVoiceInput } from '../native/ios-voice-input'
|
import { iosVoiceInput } from '../native/ios-voice-input'
|
||||||
@@ -378,7 +379,8 @@ export class IosPreviewController {
|
|||||||
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
|
input.kind === 'display-wake' && input.eventType === OsEventTypeList.HEAD_UP_EVENT)
|
||||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||||
}).catch(error => this.fail(error))
|
}).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)
|
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 com: any;
|
||||||
declare const global: any;
|
declare const global: any;
|
||||||
|
|
||||||
export const COMPASS_CHANGED = 15;
|
import { type CompassEvent } from "./compass-types";
|
||||||
export const COMPASS_CALIBRATION_STARTED = 16;
|
export * from "./compass-types";
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
function activeCommunicator(): any {
|
function activeCommunicator(): any {
|
||||||
if (!global.isAndroid) return null;
|
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
|
const changed = new Uint8Array(16); changed[0] = 255
|
||||||
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
|
assert(buildBoundingBoxPayload(new Uint8Array(16), changed, 8, 4, 1)?.[0] === 3, 'image update')
|
||||||
assert(lvglMetrics('/nonexistent/faceclaw-font').length === 0, 'font bridge')
|
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')
|
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'
|
||||||
+8
-7
@@ -1027,13 +1027,14 @@ class BleProtocol {
|
|||||||
/** Decode the stock firmware's compass navigation notifications (sid 0x08). */
|
/** Decode the stock firmware's compass navigation notifications (sid 0x08). */
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun parseCompassEvent(frame: ParsedFrame?): CompassEvent? {
|
fun parseCompassEvent(frame: ParsedFrame?): CompassEvent? {
|
||||||
if (
|
if (frame == null || !frame.ok) return null
|
||||||
((((frame == null) || !frame.ok) || (frame.sid != SID_NAVIGATION)) ||
|
return parseCompassPayload(frame.sid, frame.flag, stripTrailingCrc(frame.pb))
|
||||||
((frame.flag != FLAG_NOTIFY) && (frame.flag != FLAG_NOTIFY_ALT)))
|
}
|
||||||
) {
|
|
||||||
return null
|
/** MessageReceiver has already validated and removed the CRC. */
|
||||||
}
|
@JvmStatic
|
||||||
var root: ByteArray = stripTrailingCrc(frame.pb)
|
fun parseCompassPayload(sid: Int, flag: Int, root: ByteArray): CompassEvent? {
|
||||||
|
if (sid != SID_NAVIGATION || (flag != FLAG_NOTIFY && flag != FLAG_NOTIFY_ALT)) return null
|
||||||
var command: Int = readVarintFieldValue(root, 1, -1)
|
var command: Int = readVarintFieldValue(root, 1, -1)
|
||||||
if ((command == NAV_CMD_COMPASS_CHANGED)) {
|
if ((command == NAV_CMD_COMPASS_CHANGED)) {
|
||||||
var compass: ByteArray? = readFieldBytes(root, 10)
|
var compass: ByteArray? = readFieldBytes(root, 10)
|
||||||
|
|||||||
@@ -132,6 +132,9 @@ class IosProtocol {
|
|||||||
G2Event.decodePayload(sid, data.byteArray())
|
G2Event.decodePayload(sid, data.byteArray())
|
||||||
else null
|
else null
|
||||||
|
|
||||||
|
fun compassInput(data: NSData, sid: Int, flag: Int): BleProtocol.CompassEvent? =
|
||||||
|
BleProtocol.parseCompassPayload(sid, flag, data.byteArray())
|
||||||
|
|
||||||
fun ringInput(data: NSData): G2Event? = FaceclawRingEventDecoder.decode(data.byteArray())?.event
|
fun ringInput(data: NSData): G2Event? = FaceclawRingEventDecoder.decode(data.byteArray())?.event
|
||||||
|
|
||||||
fun pack(data: NSData, width: Int, height: Int): NSData {
|
fun pack(data: NSData, width: Int, height: Int): NSData {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ function compass(width = 576, height = 260) {
|
|||||||
createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; },
|
createInProcessWindow: (options) => { window = options; return { requestRender: () => ++renders }; },
|
||||||
},
|
},
|
||||||
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
||||||
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
'../../native/location-permissions': { hasLocationPermission: () => false },
|
||||||
'./calibration': calibration,
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ function compass({ style, width = 576, height = 260 } = {}) {
|
|||||||
createInProcessWindow: (options) => { window = options; return { requestRender: () => renders++ }; },
|
createInProcessWindow: (options) => { window = options; return { requestRender: () => renders++ }; },
|
||||||
},
|
},
|
||||||
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
||||||
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
'../../native/location-permissions': { hasLocationPermission: () => false },
|
||||||
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
||||||
'./calibration-layer': {},
|
'./calibration-layer': {},
|
||||||
'./compass-rose': load('app/apps/compass/compass-rose.ts', {
|
'./compass-rose': load('app/apps/compass/compass-rose.ts', {
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ const compass = load('app/apps/compass/compass-app.ts', {
|
|||||||
'../../ui/shell/geometry': { screenCenterInViewportX: () => viewportWidth - 320 },
|
'../../ui/shell/geometry': { screenCenterInViewportX: () => viewportWidth - 320 },
|
||||||
'../../ui/shell/in-process-window': {},
|
'../../ui/shell/in-process-window': {},
|
||||||
'../../ui/shell/shell': {},
|
'../../ui/shell/shell': {},
|
||||||
'../../g2/android-permissions': {},
|
'../../native/location-permissions': {},
|
||||||
'./calibration': calibration,
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {},
|
'./calibration-layer': {},
|
||||||
'./compass-rose': rose,
|
'./compass-rose': rose,
|
||||||
|
|||||||
@@ -89,6 +89,7 @@ test('background glasses input still composites frames; phone resume preserves t
|
|||||||
}
|
}
|
||||||
const settings = { onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
|
const settings = { onAnySettingChanged: () => () => {}, previewColorSetting: { get: () => 'white' } };
|
||||||
const modules = {
|
const modules = {
|
||||||
|
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||||
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) }, knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
||||||
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {} } },
|
'../native/ios-voice-input': { iosVoiceInput: { handleSessionEnded() {} } },
|
||||||
'../native/ios-bluetooth': { iosBluetooth: () => ({}) }, './glasses-session': { GlassesSession: Session },
|
'../native/ios-bluetooth': { iosBluetooth: () => ({}) }, './glasses-session': { GlassesSession: Session },
|
||||||
@@ -156,6 +157,7 @@ test('iOS bandwidth footer toggles live, polls only in foreground and resets its
|
|||||||
on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) },
|
on: (key, fn) => appEvents.set(key, fn), off: key => appEvents.delete(key) },
|
||||||
Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View };
|
Button: View, Color: class {}, Dialogs: {}, GridLayout: View, Image: View, Label: View, Page: View, StackLayout: View };
|
||||||
const modules = {
|
const modules = {
|
||||||
|
'../native/compass.ios': { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||||
'@nativescript/core': core,
|
'@nativescript/core': core,
|
||||||
'../apps/all-apps': { ALL_APPS: [] },
|
'../apps/all-apps': { ALL_APPS: [] },
|
||||||
'../g2/ios-preview-controller': { IosPreviewController: class { resume() {} pause() {} } },
|
'../g2/ios-preview-controller': { IosPreviewController: class { resume() {} pause() {} } },
|
||||||
|
|||||||
+49
-5
@@ -159,10 +159,10 @@ async function until(predicate) {
|
|||||||
assert.fail('Timed out waiting for test state');
|
assert.fail('Timed out waiting for test state');
|
||||||
}
|
}
|
||||||
function harness(t, transport = new FakeTransport()) {
|
function harness(t, transport = new FakeTransport()) {
|
||||||
const states = [], input = [];
|
const states = [], input = [], compass = [];
|
||||||
const session = new GlassesSession(transport, state => states.push(state), event => input.push(event));
|
const session = new GlassesSession(transport, state => states.push(state), event => input.push(event), undefined, undefined, event => compass.push(event));
|
||||||
t.after(async () => { transport.hold = false; await session.stop(); });
|
t.after(async () => { transport.hold = false; await session.stop(); });
|
||||||
return { transport, session, states, input };
|
return { transport, session, states, input, compass };
|
||||||
}
|
}
|
||||||
test('session authenticates both arms, checks firmware and sends acknowledged display data to L', async t => {
|
test('session authenticates both arms, checks firmware and sends acknowledged display data to L', async t => {
|
||||||
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
|
const h = harness(t); await h.session.start({ ...addresses, ring: 'AA:BB:CC:DD:EE:03' });
|
||||||
@@ -171,7 +171,7 @@ test('session authenticates both arms, checks firmware and sends acknowledged di
|
|||||||
assert.equal(h.session.state.charging, false);
|
assert.equal(h.session.state.charging, false);
|
||||||
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
|
assert.deepEqual(h.transport.sent.filter(s => s.message.sid === 128).map(s => s.id).sort(), ['L', 'R']);
|
||||||
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
|
h.session.setFrame(new Uint8Array(640 * 480)); await until(() => h.session.state.frames === 1);
|
||||||
const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw);
|
const image = h.transport.sent.find(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 6);
|
||||||
assert.equal(image.id, 'L');
|
assert.equal(image.id, 'L');
|
||||||
assert.equal(image.message.payload[0], 6);
|
assert.equal(image.message.payload[0], 6);
|
||||||
const layout = h.transport.sent.find(s => s.message.sid === p.SID.hub && s.message.command === 0);
|
const layout = h.transport.sent.find(s => s.message.sid === p.SID.hub && s.message.command === 0);
|
||||||
@@ -293,7 +293,7 @@ test('release while microphone enable awaits ACK sends disable last and rejects
|
|||||||
assert.equal(p.readInteger(p.readBytes(mic.at(-1).message.payload, 18), 1), 0);
|
assert.equal(p.readInteger(p.readBytes(mic.at(-1).message.payload, 18), 1), 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
const imageMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] !== 11);
|
const imageMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && ![10, 11].includes(s.message.payload[0]));
|
||||||
const imageBody = sent => sent.message.payload;
|
const imageBody = sent => sent.message.payload;
|
||||||
const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message, overrides);
|
const ack = (transport, sent, overrides) => transport.ack(sent.id, sent.message, overrides);
|
||||||
const tick = () => new Promise(resolve => setImmediate(resolve));
|
const tick = () => new Promise(resolve => setImmediate(resolve));
|
||||||
@@ -467,3 +467,47 @@ test('delta ids skip reserved values and advance only on emitted deltas; reconne
|
|||||||
await h.session.stop(); await h.session.start(addresses);
|
await h.session.stop(); await h.session.start(addresses);
|
||||||
assert.equal((await submit(base))[0], 6); // Even the same pixels need a trusted full base.
|
assert.equal((await submit(base))[0], 6); // Even the same pixels need a trusted full base.
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const compassMessages = transport => transport.sent.filter(s => s.message.sid === p.SID.cfw && s.message.payload[0] === 10);
|
||||||
|
test('compass desired state survives connecting and reconnecting; explicit stop disables it first', async t => {
|
||||||
|
const h = harness(t);
|
||||||
|
h.session.setCompassEnabled(true);
|
||||||
|
await h.session.start(addresses);
|
||||||
|
assert.equal(compassMessages(h.transport).length, 1);
|
||||||
|
assert.equal(compassMessages(h.transport)[0].id, 'L');
|
||||||
|
assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 2, 100, 0, 0, 0]);
|
||||||
|
await h.session.stop();
|
||||||
|
assert.deepEqual([...compassMessages(h.transport).at(-1).message.payload], [10, 0]);
|
||||||
|
assert.equal(h.transport.sent.at(-1).message.payload[0], 11);
|
||||||
|
await h.session.start(addresses);
|
||||||
|
assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
|
||||||
|
h.transport.emit({ kind: 'disconnected', identifier: 'R' });
|
||||||
|
assert.equal(h.session.state.phase, 'retrying');
|
||||||
|
await h.session.start(addresses, true);
|
||||||
|
assert.equal(compassMessages(h.transport).at(-1).message.payload[1], 2);
|
||||||
|
});
|
||||||
|
test('release during pending compass enable sends disable after the enable ACK', async t => {
|
||||||
|
const h = harness(t); await h.session.start(addresses);
|
||||||
|
assert.deepEqual([...compassMessages(h.transport)[0].message.payload], [10, 0]);
|
||||||
|
h.transport.hold = true;
|
||||||
|
h.session.setCompassEnabled(true);
|
||||||
|
await until(() => h.transport.held.length === 1);
|
||||||
|
h.session.setCompassEnabled(false);
|
||||||
|
const enabling = h.transport.held.shift();
|
||||||
|
h.transport.hold = false; h.transport.ack(enabling.id, enabling.message);
|
||||||
|
await until(() => compassMessages(h.transport).length === 3);
|
||||||
|
assert.deepEqual(compassMessages(h.transport).map(s => s.message.payload[1]), [0, 2, 0]);
|
||||||
|
});
|
||||||
|
test('compass notifications only come from the right arm and preserve diagnostics and calibration', async t => {
|
||||||
|
const h = harness(t); await h.session.start(addresses);
|
||||||
|
const payload = new Uint8Array(Buffer.from('080f1000520308e702a2060c434d010302038c0098badcfe', 'hex'));
|
||||||
|
const emit = (id, sid = 8, flag = 1, body = payload) => h.transport.emit({ kind: 'notification', identifier: id,
|
||||||
|
characteristic: p.G2_NOTIFY, data: hex(p.concat(...p.frameMessage(body, sid, flag, 20))) });
|
||||||
|
emit('L'); emit('other'); emit('R', 9); emit('R', 8, 0x20);
|
||||||
|
assert.equal(h.compass.length, 0);
|
||||||
|
emit('R'); emit('R', 8, 6, new Uint8Array([8, 16, 16, 0]));
|
||||||
|
emit('R', 8, 1, new Uint8Array([8, 17, 16, 0]));
|
||||||
|
assert.deepEqual(h.compass, [{ command: 15, headingDegrees: 359, diagnostics: {
|
||||||
|
magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 140, sampleTimeMs: 0xfedcba98,
|
||||||
|
} }, { command: 16, headingDegrees: -1 }, { command: 17, headingDegrees: -1 }]);
|
||||||
|
});
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const vm = require('node:vm');
|
||||||
|
const ts = require('typescript');
|
||||||
|
const path = require('node:path');
|
||||||
|
function load(file, context = {}) {
|
||||||
|
const sandbox = { exports: {}, console, ...context };
|
||||||
|
vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||||
|
}).outputText, sandbox);
|
||||||
|
return sandbox.exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('iOS compass owners and listeners work before connection and across session replacement', () => {
|
||||||
|
const api = load('app/native/compass.ios.ts', { require: () => ({}) });
|
||||||
|
const states = [], nextStates = [], events = [];
|
||||||
|
const off = api.addCompassListener(event => events.push(event));
|
||||||
|
api.setCompassEnabled(true, 'compass');
|
||||||
|
api.setCompassEnabled(true, 'glanceboard');
|
||||||
|
api.bindCompassSession({ setCompassEnabled: enabled => states.push(enabled) });
|
||||||
|
api.setCompassEnabled(false, 'compass');
|
||||||
|
assert.deepEqual(states, [true, true]);
|
||||||
|
api.bindCompassSession({ setCompassEnabled: enabled => nextStates.push(enabled) });
|
||||||
|
assert.equal(states.at(-1), false);
|
||||||
|
api.setCompassEnabled(false, 'glanceboard');
|
||||||
|
assert.deepEqual(nextStates, [true, false]);
|
||||||
|
api.receiveCompassEvent({ command: 15, headingDegrees: 271 });
|
||||||
|
off(); api.receiveCompassEvent({ command: 15, headingDegrees: 90 });
|
||||||
|
assert.deepEqual(events, [{ command: 15, headingDegrees: 271 }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('shared declination cache handles iOS fixes, persisted corrections and unavailable sensors', async () => {
|
||||||
|
const store = new Map(), listeners = [];
|
||||||
|
let permission = false, lookups = 0, fail = false;
|
||||||
|
let now = 1_800_000_000_000;
|
||||||
|
const ios = load('app/native/declination.ios.ts');
|
||||||
|
const context = {
|
||||||
|
Date: { now: () => now }, console: { warn() {} },
|
||||||
|
require: id => ({
|
||||||
|
'../../native/location-permissions': { hasLocationPermission: () => permission },
|
||||||
|
'../../native/declination': { restoreDeclination: ios.restoreDeclination, getCurrentDeclination: async () => {
|
||||||
|
lookups++; if (fail) throw new Error('No heading sensor');
|
||||||
|
return { latitude: 37.7, longitude: -122.4, degrees: 12.5 };
|
||||||
|
} },
|
||||||
|
'../../native/settings-store': { getStringSetting: (key, fallback) => store.get(key) ?? fallback,
|
||||||
|
setStringSetting: (key, value) => store.set(key, value) },
|
||||||
|
})[id],
|
||||||
|
};
|
||||||
|
const api = load('app/apps/compass/declination.ts', context);
|
||||||
|
api.onDeclinationChanged(() => listeners.push(api.getDeclinationDegrees()));
|
||||||
|
assert.equal(api.getDeclinationAvailability(), 'no-permission');
|
||||||
|
api.refreshDeclination(); assert.equal(lookups, 0);
|
||||||
|
permission = true;
|
||||||
|
assert.equal(api.getDeclinationAvailability(), 'no-fix');
|
||||||
|
api.refreshDeclination(); api.refreshDeclination();
|
||||||
|
await new Promise(resolve => setImmediate(resolve));
|
||||||
|
assert.equal(lookups, 1); assert.deepEqual(listeners, [12.5]);
|
||||||
|
assert.equal(api.getDeclinationAvailability(), 'available');
|
||||||
|
const restarted = load('app/apps/compass/declination.ts', context);
|
||||||
|
assert.equal(restarted.getDeclinationDegrees(), 12.5);
|
||||||
|
restarted.refreshDeclination(); assert.equal(lookups, 1);
|
||||||
|
now += 25 * 60 * 60 * 1000; fail = true;
|
||||||
|
restarted.refreshDeclination(); await new Promise(resolve => setImmediate(resolve));
|
||||||
|
assert.equal(restarted.getDeclinationDegrees(), 12.5);
|
||||||
|
restarted.refreshDeclination(); assert.equal(lookups, 2);
|
||||||
|
// Existing Android preferences have coordinates but no cached correction.
|
||||||
|
store.delete('compass.declination.degrees'); fail = false;
|
||||||
|
store.set('compass.declination.locatedAtMs', String(now));
|
||||||
|
const migrated = load('app/apps/compass/declination.ts', context);
|
||||||
|
assert.equal(migrated.getDeclinationDegrees(), null);
|
||||||
|
migrated.refreshDeclination(); await new Promise(resolve => setImmediate(resolve));
|
||||||
|
assert.equal(migrated.getDeclinationDegrees(), 12.5);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('iOS native correction callbacks resolve values or propagate unavailable errors', async () => {
|
||||||
|
let unavailable = false;
|
||||||
|
const api = load('app/native/declination.ios.ts', { FaceclawLocation: { shared: () => ({
|
||||||
|
requestDeclination: callback => callback(-8.25, 51, 0, unavailable ? 'Heading unavailable' : ''),
|
||||||
|
}) } });
|
||||||
|
assert.deepEqual({ ...await api.getCurrentDeclination() }, { latitude: 51, longitude: 0, degrees: -8.25 });
|
||||||
|
unavailable = true;
|
||||||
|
await assert.rejects(api.getCurrentDeclination(), /Heading unavailable/);
|
||||||
|
assert.equal(api.restoreDeclination(0, 0, NaN), null);
|
||||||
|
assert.equal(api.restoreDeclination(0, 0, 360), null);
|
||||||
|
});
|
||||||
@@ -68,6 +68,7 @@ function fixture() {
|
|||||||
paint: () => { boardStats.paints++; return new images.GrayImage(100, 80, 200); } }),
|
paint: () => { boardStats.paints++; return new images.GrayImage(100, 80, 200); } }),
|
||||||
};
|
};
|
||||||
const modules = {
|
const modules = {
|
||||||
|
"../native/compass.ios": { bindCompassSession() {}, receiveCompassEvent() {} },
|
||||||
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
|
'@nativescript/core': { File: { fromPath: () => ({ writeTextSync() {} }) },
|
||||||
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
knownFolders: { documents: () => ({ path: '/tmp' }) }, path },
|
||||||
'../native/ios-bluetooth': { iosBluetooth: () => ({}) },
|
'../native/ios-bluetooth': { iosBluetooth: () => ({}) },
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package com.faceclaw.app
|
||||||
|
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
class CompassTest {
|
||||||
|
private fun hex(text: String) = text.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun receiverPayloadAndAndroidFrameDecodeTheSameCompassSample() {
|
||||||
|
val payload = hex("080f1000520308e702a2060c434d010302038c0098badcfe")
|
||||||
|
for (flag in listOf(1, 6)) {
|
||||||
|
val frame = BleProtocol.framePb(payload, 8, flag, 15).single()
|
||||||
|
val received = MessageReceiver().receive("R", frame, 0).single()
|
||||||
|
val shared = assertNotNull(BleProtocol.parseCompassPayload(received.sid, received.flag, received.payload))
|
||||||
|
val android = assertNotNull(BleProtocol.parseCompassEvent(BleProtocol.parseFrame(frame)))
|
||||||
|
assertEquals(359, shared.headingDegrees)
|
||||||
|
assertEquals(15, shared.command)
|
||||||
|
assertEquals(3, shared.magneticAccuracy)
|
||||||
|
assertEquals(2, shared.magneticAnomalies)
|
||||||
|
assertEquals(3, shared.orientationSource)
|
||||||
|
assertEquals(0x8c, shared.diagnosticFlags)
|
||||||
|
assertEquals(0xfedcba98L, shared.sampleTimeMs)
|
||||||
|
assertEquals(android.sampleTimeMs, shared.sampleTimeMs)
|
||||||
|
}
|
||||||
|
assertNull(BleProtocol.parseCompassPayload(9, 1, payload))
|
||||||
|
assertNull(BleProtocol.parseCompassPayload(8, 0x20, payload))
|
||||||
|
assertNull(BleProtocol.parseCompassPayload(8, 1, hex("080f1000520308e802")))
|
||||||
|
assertNull(BleProtocol.parseCompassPayload(8, 1, byteArrayOf()))
|
||||||
|
for (size in 9 until payload.size) {
|
||||||
|
val legacy = assertNotNull(BleProtocol.parseCompassPayload(8, 1, payload.copyOf(size)))
|
||||||
|
assertEquals(359, legacy.headingDegrees)
|
||||||
|
assertEquals(-1, legacy.diagnosticFlags)
|
||||||
|
}
|
||||||
|
for (command in listOf(16, 17)) {
|
||||||
|
val calibration = assertNotNull(BleProtocol.parseCompassPayload(8, 1, byteArrayOf(8, command.toByte(), 16, 0)))
|
||||||
|
assertEquals(command, calibration.command)
|
||||||
|
assertEquals(-1, calibration.headingDegrees)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,6 +22,14 @@ class IosNativeApiTest {
|
|||||||
assertContentEquals(auth.byteArray(), result.payload.byteArray())
|
assertContentEquals(auth.byteArray(), result.payload.byteArray())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun compassFacadePreservesTheEndOfTheCrcFreePayload() {
|
||||||
|
val protocol = IosProtocol()
|
||||||
|
val payload = byteArrayOf(8, 15, 16, 0, 82, 3, 8, -25, 2).data()
|
||||||
|
assertEquals(359, protocol.compassInput(payload, 8, 1)?.headingDegrees)
|
||||||
|
assertNull(protocol.compassInput(payload, 8, 0))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun nativeCompositorClipsPartialUpdatesBeforeCoalescing() {
|
fun nativeCompositorClipsPartialUpdatesBeforeCoalescing() {
|
||||||
val compositor = IosSurfaceCompositor(3, 2)
|
val compositor = IosSurfaceCompositor(3, 2)
|
||||||
|
|||||||
Reference in new issue
Block a user