|
| 1 | +import { Service } from "./bluetooth-device-wrapper.js"; |
| 2 | +import { profile } from "./bluetooth-profile.js"; |
| 3 | +import { BackgroundErrorEvent, DeviceError } from "./device.js"; |
| 4 | +import { LedMatrix } from "./led.js"; |
| 5 | +import { |
| 6 | + TypedServiceEvent, |
| 7 | + TypedServiceEventDispatcher, |
| 8 | +} from "./service-events.js"; |
| 9 | + |
| 10 | +const createLedMatrix = (): LedMatrix => { |
| 11 | + return [ |
| 12 | + [false, false, false, false, false], |
| 13 | + [false, false, false, false, false], |
| 14 | + [false, false, false, false, false], |
| 15 | + [false, false, false, false, false], |
| 16 | + [false, false, false, false, false], |
| 17 | + ]; |
| 18 | +}; |
| 19 | + |
| 20 | +export class LedService implements Service { |
| 21 | + constructor( |
| 22 | + private matrixStateCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 23 | + private scrollingDelayCharacteristic: BluetoothRemoteGATTCharacteristic, |
| 24 | + private textCharactertistic: BluetoothRemoteGATTCharacteristic, |
| 25 | + private queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 26 | + ) {} |
| 27 | + |
| 28 | + static async createService( |
| 29 | + gattServer: BluetoothRemoteGATTServer, |
| 30 | + dispatcher: TypedServiceEventDispatcher, |
| 31 | + queueGattOperation: <R>(action: () => Promise<R>) => Promise<R>, |
| 32 | + listenerInit: boolean, |
| 33 | + ): Promise<LedService | undefined> { |
| 34 | + let ledService: BluetoothRemoteGATTService; |
| 35 | + try { |
| 36 | + ledService = await gattServer.getPrimaryService(profile.led.id); |
| 37 | + } catch (err) { |
| 38 | + if (listenerInit) { |
| 39 | + dispatcher("backgrounderror", new BackgroundErrorEvent(err as string)); |
| 40 | + return; |
| 41 | + } else { |
| 42 | + throw new DeviceError({ |
| 43 | + code: "service-missing", |
| 44 | + message: err as string, |
| 45 | + }); |
| 46 | + } |
| 47 | + } |
| 48 | + const matrixStateCharacteristic = await ledService.getCharacteristic( |
| 49 | + profile.led.characteristics.matrixState.id, |
| 50 | + ); |
| 51 | + const scrollingDelayCharacteristic = await ledService.getCharacteristic( |
| 52 | + profile.led.characteristics.scrollingDelay.id, |
| 53 | + ); |
| 54 | + const textCharacteristic = await ledService.getCharacteristic( |
| 55 | + profile.led.characteristics.text.id, |
| 56 | + ); |
| 57 | + return new LedService( |
| 58 | + matrixStateCharacteristic, |
| 59 | + scrollingDelayCharacteristic, |
| 60 | + textCharacteristic, |
| 61 | + queueGattOperation, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + async getLedMatrix(): Promise<LedMatrix> { |
| 66 | + const dataView = await this.queueGattOperation(() => |
| 67 | + this.matrixStateCharacteristic.readValue(), |
| 68 | + ); |
| 69 | + return this.dataViewToLedMatrix(dataView); |
| 70 | + } |
| 71 | + |
| 72 | + async setLedMatrix(value: LedMatrix): Promise<void> { |
| 73 | + const dataView = this.ledMatrixToDataView(value); |
| 74 | + return this.queueGattOperation(() => |
| 75 | + this.matrixStateCharacteristic.writeValue(dataView), |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + private dataViewToLedMatrix(dataView: DataView): LedMatrix { |
| 80 | + if (dataView.byteLength !== 5) { |
| 81 | + throw new Error("Unexpected LED matrix byte length"); |
| 82 | + } |
| 83 | + const matrix = createLedMatrix(); |
| 84 | + for (let row = 0; row < 5; ++row) { |
| 85 | + const rowByte = dataView.getUint8(row); |
| 86 | + for (let column = 0; column < 5; ++column) { |
| 87 | + const columnMask = 0x1 << (4 - column); |
| 88 | + matrix[row][column] = (rowByte & columnMask) != 0; |
| 89 | + } |
| 90 | + } |
| 91 | + return matrix; |
| 92 | + } |
| 93 | + |
| 94 | + private ledMatrixToDataView(matrix: LedMatrix): DataView { |
| 95 | + const dataView = new DataView(new ArrayBuffer(5)); |
| 96 | + for (let row = 0; row < 5; ++row) { |
| 97 | + let rowByte = 0; |
| 98 | + for (let column = 0; column < 5; ++column) { |
| 99 | + const columnMask = 0x1 << (4 - column); |
| 100 | + if (matrix[row][column]) { |
| 101 | + rowByte |= columnMask; |
| 102 | + } |
| 103 | + } |
| 104 | + dataView.setUint8(row, rowByte); |
| 105 | + } |
| 106 | + return dataView; |
| 107 | + } |
| 108 | + |
| 109 | + async setText(text: string) { |
| 110 | + const bytes = new TextEncoder().encode(text); |
| 111 | + if (bytes.length > 20) { |
| 112 | + throw new Error("Text must be <= 20 bytes when encoded as UTF-8"); |
| 113 | + } |
| 114 | + return this.queueGattOperation(() => |
| 115 | + this.textCharactertistic.writeValue(bytes), |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + async setScrollingDelay(value: number) { |
| 120 | + const dataView = new DataView(new ArrayBuffer(2)); |
| 121 | + dataView.setUint16(0, value, true); |
| 122 | + return this.queueGattOperation(() => |
| 123 | + this.scrollingDelayCharacteristic.writeValue(dataView), |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + async getScrollingDelay(): Promise<number> { |
| 128 | + const dataView = await this.queueGattOperation(() => |
| 129 | + this.scrollingDelayCharacteristic.readValue(), |
| 130 | + ); |
| 131 | + return dataView.getUint16(0, true); |
| 132 | + } |
| 133 | + |
| 134 | + async startNotifications(type: TypedServiceEvent): Promise<void> {} |
| 135 | + |
| 136 | + async stopNotifications(type: TypedServiceEvent): Promise<void> {} |
| 137 | +} |
0 commit comments