|
| 1 | +import { MagnetometerData, MagnetometerDataEvent } from "./magnetometer.js"; |
| 2 | +import { Service } from "./bluetooth-device-wrapper.js"; |
| 3 | +import { profile } from "./bluetooth-profile.js"; |
| 4 | +import { BackgroundErrorEvent, DeviceError } from "./device.js"; |
| 5 | +import { |
| 6 | + CharacteristicDataTarget, |
| 7 | + TypedServiceEvent, |
| 8 | + TypedServiceEventDispatcher, |
| 9 | +} from "./service-events.js"; |
| 10 | + |
| 11 | +export class MagnetometerService implements Service { |
| 12 | + constructor( |
| 13 | + private magnetometerDataCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 14 | + private magnetometerPeriodCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 15 | + private magnetometerBearingCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 16 | + private magnetometerCalibrationCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 17 | + private dispatchTypedEvent: TypedServiceEventDispatcher, |
| 18 | + private queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 19 | + ) { |
| 20 | + this.magnetometerDataCharacteristic.addEventListener( |
| 21 | + "characteristicvaluechanged", |
| 22 | + (event: Event) => { |
| 23 | + const target = event.target as CharacteristicDataTarget; |
| 24 | + const data = this.dataViewToData(target.value); |
| 25 | + this.dispatchTypedEvent( |
| 26 | + "magnetometerdatachanged", |
| 27 | + new MagnetometerDataEvent(data), |
| 28 | + ); |
| 29 | + }, |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + static async createService( |
| 34 | + gattServer: BluetoothRemoteGATTServer, |
| 35 | + dispatcher: TypedServiceEventDispatcher, |
| 36 | + queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 37 | + listenerInit: boolean, |
| 38 | + ): Promise<MagnetometerService | undefined> { |
| 39 | + let magnetometerService: BluetoothRemoteGATTService; |
| 40 | + try { |
| 41 | + magnetometerService = await gattServer.getPrimaryService( |
| 42 | + profile.magnetometer.id, |
| 43 | + ); |
| 44 | + } catch (err) { |
| 45 | + if (listenerInit) { |
| 46 | + dispatcher("backgrounderror", new BackgroundErrorEvent(err as string)); |
| 47 | + return; |
| 48 | + } else { |
| 49 | + throw new DeviceError({ |
| 50 | + code: "service-missing", |
| 51 | + message: err as string, |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + const magnetometerDataCharacteristic = |
| 56 | + await magnetometerService.getCharacteristic( |
| 57 | + profile.magnetometer.characteristics.data.id, |
| 58 | + ); |
| 59 | + const magnetometerPeriodCharacteristic = |
| 60 | + await magnetometerService.getCharacteristic( |
| 61 | + profile.magnetometer.characteristics.period.id, |
| 62 | + ); |
| 63 | + const magnetometerBearingCharacteristic = |
| 64 | + await magnetometerService.getCharacteristic( |
| 65 | + profile.magnetometer.characteristics.bearing.id, |
| 66 | + ); |
| 67 | + const magnetometerCalibrationCharacteristic = |
| 68 | + await magnetometerService.getCharacteristic( |
| 69 | + profile.magnetometer.characteristics.calibration.id, |
| 70 | + ); |
| 71 | + return new MagnetometerService( |
| 72 | + magnetometerDataCharacteristic, |
| 73 | + magnetometerPeriodCharacteristic, |
| 74 | + magnetometerBearingCharacteristic, |
| 75 | + magnetometerCalibrationCharacteristic, |
| 76 | + dispatcher, |
| 77 | + queueGattOperation, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + private dataViewToData(dataView: DataView): MagnetometerData { |
| 82 | + return { |
| 83 | + x: dataView.getInt16(0, true), |
| 84 | + y: dataView.getInt16(2, true), |
| 85 | + z: dataView.getInt16(4, true), |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + async getData(): Promise<MagnetometerData> { |
| 90 | + const dataView = await this.queueGattOperation(() => |
| 91 | + this.magnetometerDataCharacteristic.readValue(), |
| 92 | + ); |
| 93 | + return this.dataViewToData(dataView); |
| 94 | + } |
| 95 | + |
| 96 | + async getPeriod(): Promise<number> { |
| 97 | + const dataView = await this.queueGattOperation(() => |
| 98 | + this.magnetometerPeriodCharacteristic.readValue(), |
| 99 | + ); |
| 100 | + return dataView.getUint16(0, true); |
| 101 | + } |
| 102 | + |
| 103 | + async setPeriod(value: number): Promise<void> { |
| 104 | + if (value === 0) { |
| 105 | + // Writing 0 causes the device to crash. |
| 106 | + return; |
| 107 | + } |
| 108 | + // Allowed values: 10, 20, 50, 100 |
| 109 | + // Values passed are rounded up to the allowed values on device. |
| 110 | + // Documentation for allowed values looks wrong. |
| 111 | + // https://lancaster-university.github.io/microbit-docs/ble/profile/#about-the-magnetometer-service |
| 112 | + const dataView = new DataView(new ArrayBuffer(2)); |
| 113 | + dataView.setUint16(0, value, true); |
| 114 | + return this.queueGattOperation(() => |
| 115 | + this.magnetometerPeriodCharacteristic.writeValue(dataView), |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + async getBearing(): Promise<number> { |
| 120 | + const dataView = await this.queueGattOperation(() => |
| 121 | + this.magnetometerBearingCharacteristic.readValue(), |
| 122 | + ); |
| 123 | + return dataView.getUint16(0, true); |
| 124 | + } |
| 125 | + |
| 126 | + async triggerCalibration(): Promise<void> { |
| 127 | + const dataView = new DataView(new ArrayBuffer(1)); |
| 128 | + dataView.setUint8(0, 1); |
| 129 | + return this.queueGattOperation(() => |
| 130 | + this.magnetometerCalibrationCharacteristic.writeValue(dataView), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + async startNotifications(type: TypedServiceEvent): Promise<void> { |
| 135 | + await this.characteristicForEvent(type)?.startNotifications(); |
| 136 | + } |
| 137 | + |
| 138 | + async stopNotifications(type: TypedServiceEvent): Promise<void> { |
| 139 | + await this.characteristicForEvent(type)?.stopNotifications(); |
| 140 | + } |
| 141 | + |
| 142 | + private characteristicForEvent(type: TypedServiceEvent) { |
| 143 | + switch (type) { |
| 144 | + case "magnetometerdatachanged": { |
| 145 | + return this.magnetometerDataCharacteristic; |
| 146 | + } |
| 147 | + default: { |
| 148 | + return undefined; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments