diff --git a/app/graphics/image.ts b/app/graphics/image.ts index daa7674a..c6796a53 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -42,6 +42,12 @@ export class GrayImage { this.clear(fill); } + clone(): GrayImage { + const clone = new GrayImage(this.width, this.height, 0); + clone.pixels.set(this.pixels); + return clone; + } + clear(value = 0): void { this.pixels.fill(clampByte(value)); } diff --git a/app/native/faceclaw-ble.ts b/app/native/faceclaw-ble.ts deleted file mode 100644 index 4170d762..00000000 --- a/app/native/faceclaw-ble.ts +++ /dev/null @@ -1,177 +0,0 @@ -import { Utils } from "@nativescript/core"; - -declare const com: any; - -type NotificationListener = (address: string, characteristicUuid: string, data: Uint8Array) => void; -type ConnectionListener = (address: string, connected: boolean) => void; - -function toUint8Array(bytes: ArrayLike | null | undefined): Uint8Array { - if (!bytes) return new Uint8Array(0); - const out = new Uint8Array(bytes.length); - for (let i = 0; i < bytes.length; i++) { - out[i] = bytes[i]! & 0xff; - } - return out; -} - -function toJavaByteArray(bytes: Uint8Array): number[] { - const out = Array.create("byte", bytes.length) as number[]; - for (let i = 0; i < bytes.length; i++) { - const value = bytes[i]!; - out[i] = value > 127 ? value - 256 : value; - } - return out; -} - -function toJavaByteList(frames: Uint8Array[]): any { - const list = new java.util.ArrayList(); - for (const frame of frames) { - list.add(toJavaByteArray(frame)); - } - return list; -} - -export class FaceclawBleBridge { - private readonly manager: any; - private readonly notificationListeners = new Set(); - private readonly connectionListeners = new Set(); - private readonly listenerProxy: any; - - constructor() { - const context = Utils.android.getApplicationContext(); - if (!context) throw new Error("Android application context unavailable"); - - this.manager = new com.faceclaw.app.FaceclawBleManager(context); - this.listenerProxy = new com.faceclaw.app.FaceclawBleListener({ - onNotification: (address: string, characteristicUuid: string, data: ArrayLike) => { - const bytes = toUint8Array(data); - for (const listener of this.notificationListeners) { - listener(String(address), String(characteristicUuid), bytes); - } - }, - onConnectionStateChange: (address: string, connected: boolean) => { - for (const listener of this.connectionListeners) { - listener(String(address), Boolean(connected)); - } - }, - }); - this.manager.setListener(this.listenerProxy); - } - - onNotification(listener: NotificationListener): () => void { - this.notificationListeners.add(listener); - return () => { - this.notificationListeners.delete(listener); - }; - } - - onConnectionStateChange(listener: ConnectionListener): () => void { - this.connectionListeners.add(listener); - return () => { - this.connectionListeners.delete(listener); - }; - } - - async connect(address: string, timeoutMs: number): Promise { - if (!this.manager.connect(address, timeoutMs)) { - throw new Error(`connect failed: ${address}`); - } - } - - async requestMtu(address: string, mtu: number, timeoutMs: number): Promise { - if (!this.manager.requestMtu(address, mtu, timeoutMs)) { - throw new Error(`requestMtu failed: ${address}`); - } - } - - async requestConnectionPriority(address: string, priority: number): Promise { - if (!this.manager.requestConnectionPriority(address, priority)) { - throw new Error(`requestConnectionPriority failed: ${address} ${priority}`); - } - } - - async discoverServices(address: string, timeoutMs: number): Promise { - if (!this.manager.discoverServices(address, timeoutMs)) { - throw new Error(`discoverServices failed: ${address}`); - } - } - - async enableNotifications( - address: string, - characteristicUuid: string, - enable: boolean, - timeoutMs: number, - ): Promise { - if (!this.manager.enableNotifications(address, characteristicUuid, enable, timeoutMs)) { - throw new Error(`enableNotifications failed: ${address} ${characteristicUuid}`); - } - } - - async writeCharacteristic( - address: string, - characteristicUuid: string, - data: Uint8Array, - writeType: number, - timeoutMs: number, - ): Promise { - if (!this.manager.write(address, characteristicUuid, toJavaByteArray(data), writeType, timeoutMs)) { - throw new Error(`write failed: ${address} ${characteristicUuid}`); - } - } - - async writeCharacteristicFrames( - address: string, - characteristicUuid: string, - frames: Uint8Array[], - writeType: number, - timeoutMs: number, - allowFallback: boolean, - interFrameDelayMs: number, - ): Promise { - if ( - !this.manager.writeFrames( - address, - characteristicUuid, - toJavaByteList(frames), - writeType, - timeoutMs, - allowFallback, - interFrameDelayMs, - ) - ) { - throw new Error(`write frames failed: ${address} ${characteristicUuid}`); - } - } - - async queueCharacteristicFrames( - address: string, - characteristicUuid: string, - frames: Uint8Array[], - writeType: number, - timeoutMs: number, - allowFallback: boolean, - interFrameDelayMs: number, - ): Promise { - if ( - !this.manager.queueWriteFrames( - address, - characteristicUuid, - toJavaByteList(frames), - writeType, - timeoutMs, - allowFallback, - interFrameDelayMs, - ) - ) { - throw new Error(`queue write frames failed: ${address} ${characteristicUuid}`); - } - } - - async disconnect(address: string): Promise { - this.manager.disconnect(address); - } - - async close(): Promise { - this.manager.close(); - } -} diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index 73dd3e7d..8c4590cf 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -1,4 +1,5 @@ import { GrayImage } from "../graphics/image"; +import { toUint8Array } from "../util/array-util"; declare const com: any; @@ -13,7 +14,7 @@ export function readActiveNotificationIcons(maxIcons: number): GrayImage[] { const now = Date.now(); if (now - cachedAtMs < ICON_CACHE_MS) { - return cachedIcons.map(cloneGrayImage); + return cachedIcons.map(icon => icon.clone()); } const bytes = toUint8Array( @@ -33,20 +34,5 @@ export function readActiveNotificationIcons(maxIcons: number): GrayImage[] { cachedIcons = icons; cachedAtMs = now; - return icons.map(cloneGrayImage); -} - -function toUint8Array(bytes: ArrayLike | null | undefined): Uint8Array { - if (!bytes) return new Uint8Array(0); - const out = new Uint8Array(bytes.length); - for (let i = 0; i < bytes.length; i++) { - out[i] = bytes[i]! & 0xff; - } - return out; -} - -function cloneGrayImage(image: GrayImage): GrayImage { - const clone = new GrayImage(image.width, image.height, 0); - clone.pixels.set(image.pixels); - return clone; + return icons.map(icon => icon.clone()); } diff --git a/app/native/system-status-icons.ts b/app/native/system-status-icons.ts index 50150070..e0e782e5 100644 --- a/app/native/system-status-icons.ts +++ b/app/native/system-status-icons.ts @@ -1,6 +1,6 @@ import { Utils } from "@nativescript/core"; - import { GrayImage } from "../graphics/image"; +import { toUint8Array } from "~/util/array-util"; declare const com: any; @@ -15,7 +15,7 @@ export function readSystemStatusIcons(): GrayImage[] { const now = Date.now(); if (now - cachedAtMs < ICON_CACHE_MS) { - return cachedIcons.map(cloneGrayImage); + return cachedIcons.map(cachedIcon => cachedIcon.clone()); } const context = Utils.android.getApplicationContext(); @@ -35,20 +35,5 @@ export function readSystemStatusIcons(): GrayImage[] { cachedIcons = icons; cachedAtMs = now; - return icons.map(cloneGrayImage); -} - -function toUint8Array(bytes: ArrayLike | null | undefined): Uint8Array { - if (!bytes) return new Uint8Array(0); - const out = new Uint8Array(bytes.length); - for (let i = 0; i < bytes.length; i++) { - out[i] = bytes[i]! & 0xff; - } - return out; -} - -function cloneGrayImage(image: GrayImage): GrayImage { - const clone = new GrayImage(image.width, image.height, 0); - clone.pixels.set(image.pixels); - return clone; + return icons.map(icon => icon.clone()); } diff --git a/app/ui/apps/stopwatch.ts b/app/ui/apps/stopwatch.ts new file mode 100644 index 00000000..30ee91c9 --- /dev/null +++ b/app/ui/apps/stopwatch.ts @@ -0,0 +1,71 @@ +import { loadEmbeddedTerminus32 } from "../../graphics/bdffont"; +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; +import { Layer, type DashboardInputEvent, type LayerContext } from "../layers"; + +const stopwatchFont = loadEmbeddedTerminus32(); + +export class StopwatchLayer implements Layer { + private readonly startedAtMs = Date.now(); + private pausedAtMs: number | null = null; + private pausedDurationMs = 0; + + paint(ctx: LayerContext): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const elapsedMs = this.elapsedMs(); + const timeLabel = formatStopwatchElapsed(elapsedMs); + const stateLabel = this.pausedAtMs === null ? "Running" : "Paused"; + + image.drawText(ctx.font, 24, 24, "Stopwatch", 180); + + const quadrantWidth = G2_LENS_WIDTH / 2; + const quadrantHeight = G2_LENS_HEIGHT / 2; + const timeX = Math.max(0, Math.round((quadrantWidth - stopwatchFont.measureText(timeLabel)) / 2)); + const timeY = Math.max(0, Math.round((quadrantHeight - stopwatchFont.lineHeight) / 2)); + image.drawText(stopwatchFont, timeX, timeY, timeLabel, 245); + image.drawText(ctx.font, 90, 142, stateLabel, 150); + image.drawText(ctx.font, 90, 160, "Click: pause / resume", 150); + image.drawText(ctx.font, 90, 178, "Double-click: back", 150); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + if (event.type === "double-click") { + void ctx.actions.setStopwatchRenderActive(false); + ctx.stack.pop(); + return; + } + if (event.type === "click") { + if (this.pausedAtMs === null) { + this.pausedAtMs = Date.now(); + void ctx.actions.setStopwatchRenderActive(false); + } else { + this.pausedDurationMs += Date.now() - this.pausedAtMs; + this.pausedAtMs = null; + void ctx.actions.setStopwatchRenderActive(true); + } + } + } + + private elapsedMs(): number { + const now = this.pausedAtMs ?? Date.now(); + return Math.max(0, now - this.startedAtMs - this.pausedDurationMs); + } +} + +function pad2(value: number): string { + return String(value).padStart(2, "0"); +} + +function formatStopwatchElapsed(elapsedMs: number): string { + const totalTenths = Math.floor(elapsedMs / 100); + const tenths = totalTenths % 10; + const totalSeconds = Math.floor(totalTenths / 10); + const seconds = totalSeconds % 60; + const totalMinutes = Math.floor(totalSeconds / 60); + const minutes = totalMinutes % 60; + const hours = Math.floor(totalMinutes / 60); + if (hours > 0) { + return `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`; + } + return `${pad2(minutes)}:${pad2(seconds)}.${tenths}`; +} diff --git a/app/ui/apps/transcribe.ts b/app/ui/apps/transcribe.ts new file mode 100644 index 00000000..6822c450 --- /dev/null +++ b/app/ui/apps/transcribe.ts @@ -0,0 +1,120 @@ +import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../../graphics/image"; +import { type BdfFont } from "../../graphics/bdffont"; +import { voiceControlBridge, type VoiceTranscriptEvent } from "../../native/voice-control"; +import { Layer, type DashboardInputEvent, type LayerContext } from "../layers"; + +export class TranscribeLayer implements Layer { + private mode: "off" | "wakeword" | "full" = "full"; + private status = "Starting transcription..."; + private transcript = ""; + private unsubscribeTranscript: (() => void) | null = null; + private unsubscribeStatus: (() => void) | null = null; + + start(ctx: LayerContext): void { + this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event)); + this.unsubscribeStatus = voiceControlBridge.onStatus((state) => { + this.status = state.status; + }); + void ctx.actions.setTranscribeRenderActive(true); + void Promise.resolve(ctx.actions.startDedicatedVoiceInput("full")).catch((error) => { + this.status = `Could not start transcription: ${error instanceof Error ? error.message : String(error)}`; + }); + } + + paint(ctx: LayerContext): GrayImage { + const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); + const modeLabel = this.mode === "full" ? "Full" : this.mode === "wakeword" ? "Wakeword" : "Off"; + const text = this.transcript || "Listening..."; + const wrapped = wrapTranscribeText(ctx.font, text, G2_LENS_WIDTH - 64); + + image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52); + image.drawText(ctx.font, 24, 24, "Transcribe", 200); + image.drawText(ctx.font, G2_LENS_WIDTH - 108, 24, modeLabel, this.mode === "full" ? 220 : 130); + //image.drawText(ctx.font, 24, 46, truncateTranscribeLine(this.status, 74), 110); + image.drawText(ctx.font, 24, 46, this.status, 110); + + const firstLine = Math.max(0, wrapped.length - 10); + for (let index = firstLine; index < wrapped.length; index++) { + const y = 72 + (index - firstLine) * 16; + image.drawText(ctx.font, 32, y, wrapped[index]!, 230); + } + + image.drawText(ctx.font, 24, 252, "Click: mode / Double-click: back", 110); + return image; + } + + handleInput(event: DashboardInputEvent, ctx: LayerContext): void { + if (event.type === "double-click") { + this.stop(ctx); + ctx.stack.pop(); + return; + } + if (event.type === "click") { + this.cycleMode(ctx); + } + } + + private cycleMode(ctx: LayerContext): void { + if (this.mode === "full") { + this.mode = "wakeword"; + void ctx.actions.startDedicatedVoiceInput("wakeword"); + } else if (this.mode === "wakeword") { + this.mode = "off"; + void ctx.actions.stopDedicatedVoiceInput(); + } else { + this.mode = "full"; + void ctx.actions.startDedicatedVoiceInput("full"); + } + } + + private stop(ctx: LayerContext): void { + this.unsubscribeTranscript?.(); + this.unsubscribeTranscript = null; + this.unsubscribeStatus?.(); + this.unsubscribeStatus = null; + void ctx.actions.setTranscribeRenderActive(false); + void ctx.actions.stopDedicatedVoiceInput(); + } + + private onTranscript(event: VoiceTranscriptEvent): void { + if (event.isFinal) { + if (this.transcript && !this.transcript.endsWith("\n")) { + this.transcript = `${this.transcript}\n`; + } + return; + } + this.transcript = appendTranscriptChunk(this.transcript, event.text); + } +} + +function appendTranscriptChunk(transcript: string, chunk: string): string { + const trimmed = chunk.replace(/\s+/g, " ").trim(); + if (!trimmed) return transcript; + if (!transcript || transcript.endsWith("\n") || /^[.,!?;:]/.test(trimmed)) { + return `${transcript}${trimmed}`; + } + return `${transcript} ${trimmed}`; +} + +function wrapTranscribeText(font: BdfFont, text: string, maxWidth: number): string[] { + const words = text.split(/\s+/).filter(Boolean); + const lines: string[] = []; + let line = ""; + for (const word of words) { + const candidate = line ? `${line} ${word}` : word; + if (line && font.measureText(candidate) > maxWidth) { + lines.push(line); + line = word; + } else { + line = candidate; + } + } + if (line) { + lines.push(line); + } + return lines.length ? lines : [""]; +} + +function truncateTranscribeLine(text: string, maxChars: number): string { + return text.length <= maxChars ? text : `${text.slice(0, maxChars - 3)}...`; +} diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index f932f0d3..504a6453 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -49,6 +49,8 @@ import { } from "./dashboard-settings"; import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers"; import { drawRightValueMenuItem, drawToggleMenuItem, MenuLayer } from "./menu"; +import { StopwatchLayer } from "./apps/stopwatch"; +import { TranscribeLayer } from "./apps/transcribe"; type DashboardCardId = "system" | DashboardSlotId; export type DashboardBatteryLevels = { @@ -92,7 +94,6 @@ const dashboardActions: LayerActions = { }; const dashboardFont = loadEmbeddedTerminus12(); const dashboardSystemFont = loadEmbeddedTerminus16(); -const stopwatchFont = loadEmbeddedTerminus32(); const NOTIFICATION_ICON_SIZE = 24; const SYSTEM_CARD_ITEM_HEIGHT = 38; const SYSTEM_CARD_ITEM_GAP = 2; @@ -507,185 +508,6 @@ function createAppsMenuLayer(): MenuLayer { ); } -class StopwatchLayer implements Layer { - private readonly startedAtMs = Date.now(); - private pausedAtMs: number | null = null; - private pausedDurationMs = 0; - - paint(ctx: LayerContext): GrayImage { - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - const elapsedMs = this.elapsedMs(); - const timeLabel = formatStopwatchElapsed(elapsedMs); - const stateLabel = this.pausedAtMs === null ? "Running" : "Paused"; - - image.drawText(ctx.font, 24, 24, "Stopwatch", 180); - image.drawText(ctx.font, G2_LENS_WIDTH - 114, 24, stateLabel, this.pausedAtMs === null ? 200 : 120); - - const quadrantWidth = G2_LENS_WIDTH / 2; - const quadrantHeight = G2_LENS_HEIGHT / 2; - const timeX = Math.max(0, Math.round((quadrantWidth - stopwatchFont.measureText(timeLabel)) / 2)); - const timeY = Math.max(0, Math.round((quadrantHeight - stopwatchFont.lineHeight) / 2)); - image.drawText(stopwatchFont, timeX, timeY, timeLabel, 245); - image.drawText(ctx.font, 126, 160, "Click: pause / resume", 150); - image.drawText(ctx.font, 126, 178, "Double-click: back", 120); - image.drawText(ctx.font, 126, 214, "Renders requested every 100ms", 110); - return image; - } - - handleInput(event: DashboardInputEvent, ctx: LayerContext): void { - if (event.type === "double-click") { - void ctx.actions.setStopwatchRenderActive(false); - ctx.stack.pop(); - return; - } - if (event.type === "click") { - if (this.pausedAtMs === null) { - this.pausedAtMs = Date.now(); - void ctx.actions.setStopwatchRenderActive(false); - } else { - this.pausedDurationMs += Date.now() - this.pausedAtMs; - this.pausedAtMs = null; - void ctx.actions.setStopwatchRenderActive(true); - } - } - } - - private elapsedMs(): number { - const now = this.pausedAtMs ?? Date.now(); - return Math.max(0, now - this.startedAtMs - this.pausedDurationMs); - } -} - -class TranscribeLayer implements Layer { - private mode: "off" | "wakeword" | "full" = "full"; - private status = "Starting transcription..."; - private transcript = ""; - private unsubscribeTranscript: (() => void) | null = null; - private unsubscribeStatus: (() => void) | null = null; - - start(ctx: LayerContext): void { - this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event)); - this.unsubscribeStatus = voiceControlBridge.onStatus((state) => { - this.status = state.status; - }); - void ctx.actions.setTranscribeRenderActive(true); - void Promise.resolve(ctx.actions.startDedicatedVoiceInput("full")).catch((error) => { - this.status = `Could not start transcription: ${error instanceof Error ? error.message : String(error)}`; - }); - } - - paint(ctx: LayerContext): GrayImage { - const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); - const modeLabel = this.mode === "full" ? "Full" : this.mode === "wakeword" ? "Wakeword" : "Off"; - const text = this.transcript || "Listening..."; - const wrapped = wrapTranscribeText(ctx.font, text, G2_LENS_WIDTH - 64); - - image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52); - image.drawText(ctx.font, 24, 24, "Transcribe", 200); - image.drawText(ctx.font, G2_LENS_WIDTH - 108, 24, modeLabel, this.mode === "full" ? 220 : 130); - //image.drawText(ctx.font, 24, 46, truncateTranscribeLine(this.status, 74), 110); - image.drawText(ctx.font, 24, 46, this.status, 110); - - const firstLine = Math.max(0, wrapped.length - 10); - for (let index = firstLine; index < wrapped.length; index++) { - const y = 72 + (index - firstLine) * 16; - image.drawText(ctx.font, 32, y, wrapped[index]!, 230); - } - - image.drawText(ctx.font, 24, 252, "Click: mode / Double-click: back", 110); - return image; - } - - handleInput(event: DashboardInputEvent, ctx: LayerContext): void { - if (event.type === "double-click") { - this.stop(ctx); - ctx.stack.pop(); - return; - } - if (event.type === "click") { - this.cycleMode(ctx); - } - } - - private cycleMode(ctx: LayerContext): void { - if (this.mode === "full") { - this.mode = "wakeword"; - void ctx.actions.startDedicatedVoiceInput("wakeword"); - } else if (this.mode === "wakeword") { - this.mode = "off"; - void ctx.actions.stopDedicatedVoiceInput(); - } else { - this.mode = "full"; - void ctx.actions.startDedicatedVoiceInput("full"); - } - } - - private stop(ctx: LayerContext): void { - this.unsubscribeTranscript?.(); - this.unsubscribeTranscript = null; - this.unsubscribeStatus?.(); - this.unsubscribeStatus = null; - void ctx.actions.setTranscribeRenderActive(false); - void ctx.actions.stopDedicatedVoiceInput(); - } - - private onTranscript(event: VoiceTranscriptEvent): void { - if (event.isFinal) { - if (this.transcript && !this.transcript.endsWith("\n")) { - this.transcript = `${this.transcript}\n`; - } - return; - } - this.transcript = appendTranscriptChunk(this.transcript, event.text); - } -} - -function appendTranscriptChunk(transcript: string, chunk: string): string { - const trimmed = chunk.replace(/\s+/g, " ").trim(); - if (!trimmed) return transcript; - if (!transcript || transcript.endsWith("\n") || /^[.,!?;:]/.test(trimmed)) { - return `${transcript}${trimmed}`; - } - return `${transcript} ${trimmed}`; -} - -function wrapTranscribeText(font: typeof dashboardFont, text: string, maxWidth: number): string[] { - const words = text.split(/\s+/).filter(Boolean); - const lines: string[] = []; - let line = ""; - for (const word of words) { - const candidate = line ? `${line} ${word}` : word; - if (line && font.measureText(candidate) > maxWidth) { - lines.push(line); - line = word; - } else { - line = candidate; - } - } - if (line) { - lines.push(line); - } - return lines.length ? lines : [""]; -} - -function truncateTranscribeLine(text: string, maxChars: number): string { - return text.length <= maxChars ? text : `${text.slice(0, maxChars - 3)}...`; -} - -function formatStopwatchElapsed(elapsedMs: number): string { - const totalTenths = Math.floor(elapsedMs / 100); - const tenths = totalTenths % 10; - const totalSeconds = Math.floor(totalTenths / 10); - const seconds = totalSeconds % 60; - const totalMinutes = Math.floor(totalSeconds / 60); - const minutes = totalMinutes % 60; - const hours = Math.floor(totalMinutes / 60); - if (hours > 0) { - return `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`; - } - return `${pad2(minutes)}:${pad2(seconds)}.${tenths}`; -} - function createSystemMenuLayer(): MenuLayer { return new MenuLayer( "System", diff --git a/app/util/array-util.ts b/app/util/array-util.ts new file mode 100644 index 00000000..8f007a59 --- /dev/null +++ b/app/util/array-util.ts @@ -0,0 +1,9 @@ + +export function toUint8Array(bytes: ArrayLike | null | undefined): Uint8Array { + if (!bytes) return new Uint8Array(0); + const out = new Uint8Array(bytes.length); + for (let i = 0; i < bytes.length; i++) { + out[i] = bytes[i]! & 0xff; + } + return out; +}