Skip to content

Commit

Permalink
Better buffer transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
thegecko committed Jul 21, 2024
1 parent 9194781 commit 8016a9b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/services/device-information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ export class DeviceInformationService {
private async readStringCharacteristic(uuid: BluetoothCharacteristicUUID): Promise<string | undefined> {
try {
const view = await this.helper.getCharacteristicValue(uuid);
const buffer = view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
return String.fromCharCode.apply(null, Array.from(new Uint8Array(buffer)));
const decoder = new TextDecoder();
return decoder.decode(view.buffer);
} catch (_e) {
return undefined;
}
Expand Down
10 changes: 3 additions & 7 deletions src/services/led.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class LedService {

/**
* Write text to the LED matrix
* @param text Te text to display
* @param text The text to display
*/
public async writeText(text: string): Promise<void> {
const encoded = this.encodeString(text);
Expand Down Expand Up @@ -116,12 +116,8 @@ export class LedService {
}

private encodeString(text: string): ArrayBuffer {
const buffer = new ArrayBuffer(text.length);
const view = new Uint8Array(buffer);
for (let i = 0; i < text.length; i++) {
view[i] = text.charCodeAt(i);
}
return buffer;
const encoder = new TextEncoder();
return encoder.encode(text);
}

private viewToLedMatrix(view: DataView): LedMatrix {
Expand Down

0 comments on commit 8016a9b

Please sign in to comment.