Skip to content

Commit 8016a9b

Browse files
committed
Better buffer transforms
1 parent 9194781 commit 8016a9b

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

src/services/device-information.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export class DeviceInformationService {
115115
private async readStringCharacteristic(uuid: BluetoothCharacteristicUUID): Promise<string | undefined> {
116116
try {
117117
const view = await this.helper.getCharacteristicValue(uuid);
118-
const buffer = view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
119-
return String.fromCharCode.apply(null, Array.from(new Uint8Array(buffer)));
118+
const decoder = new TextDecoder();
119+
return decoder.decode(view.buffer);
120120
} catch (_e) {
121121
return undefined;
122122
}

src/services/led.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class LedService {
7373

7474
/**
7575
* Write text to the LED matrix
76-
* @param text Te text to display
76+
* @param text The text to display
7777
*/
7878
public async writeText(text: string): Promise<void> {
7979
const encoded = this.encodeString(text);
@@ -116,12 +116,8 @@ export class LedService {
116116
}
117117

118118
private encodeString(text: string): ArrayBuffer {
119-
const buffer = new ArrayBuffer(text.length);
120-
const view = new Uint8Array(buffer);
121-
for (let i = 0; i < text.length; i++) {
122-
view[i] = text.charCodeAt(i);
123-
}
124-
return buffer;
119+
const encoder = new TextEncoder();
120+
return encoder.encode(text);
125121
}
126122

127123
private viewToLedMatrix(view: DataView): LedMatrix {

0 commit comments

Comments
 (0)