This commit is contained in:
James Babcock committed 2026-05-12 12:41:52 -07:00
1 parent adb93ae441
commit bcb162ecaf
8 files changed
+214 -392

No files matched your search

+71
View File
@@ -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}`;
}
+120
View File
@@ -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)}...`;
}
+2 -180
View File
@@ -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",