forked from Blendman974/faceclaw
Refactor: Organize files by app
This commit is contained in:
1 parent
1ec6b3ff83
commit
9b3832d7a7
40 files changed
+175
-175
No files matched your search
@@ -0,0 +1,143 @@
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||
import { GESTURE_DOUBLE_CLICK } from "../../ui/gestures";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
|
||||
// IMU report pace. This is NOT a literal Hz — the firmware only accepts the
|
||||
// ImuReportPace codes 100..1000 (step 100); the real delivery rate is
|
||||
// device-defined. An out-of-range value makes the firmware stream empty
|
||||
// (all-zero) samples. 200 matches the reference EvenHub running-tracker app.
|
||||
const IMU_REPORT_PACE = 200;
|
||||
// How often to reconcile the IMU on/off state with window visibility.
|
||||
const RECONCILE_INTERVAL_MS = 400;
|
||||
|
||||
/**
|
||||
* Accelerometer demo: turns the IMU stream on while this page is visible (the
|
||||
* Debug tests window is foregrounded and the screen is on), off otherwise, and
|
||||
* shows the latest x/y/z reading with a simple per-axis bar.
|
||||
*
|
||||
* The IMU stream is experimental — the firmware may never populate it — so the
|
||||
* page also reports how many samples have arrived.
|
||||
*/
|
||||
export class AccelerometerDemoLayer implements Layer {
|
||||
private reading: ImuReading | null = null;
|
||||
private sampleCount = 0;
|
||||
private peak = 0;
|
||||
private enabled = false;
|
||||
private removed = false;
|
||||
private reconcileTimer: ReturnType<typeof setInterval> | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
|
||||
constructor(
|
||||
private readonly windowId: string,
|
||||
private readonly requestRender: () => void,
|
||||
) {
|
||||
this.start();
|
||||
}
|
||||
|
||||
private start(): void {
|
||||
this.unsubscribe = addImuListener((reading) => {
|
||||
if (this.removed) return;
|
||||
this.reading = reading;
|
||||
this.sampleCount++;
|
||||
this.peak = Math.max(this.peak, Math.abs(reading.x), Math.abs(reading.y), Math.abs(reading.z));
|
||||
this.requestRender();
|
||||
});
|
||||
this.reconcileTimer = setInterval(() => this.reconcile(), RECONCILE_INTERVAL_MS);
|
||||
this.reconcile();
|
||||
}
|
||||
|
||||
/** Match the IMU on/off state to whether this page is currently visible. */
|
||||
private reconcile(): void {
|
||||
if (this.removed) return;
|
||||
const visible = shell.isWindowVisible(this.windowId);
|
||||
if (visible === this.enabled) return;
|
||||
this.enabled = visible;
|
||||
setImuReportEnabled(visible, IMU_REPORT_PACE);
|
||||
this.requestRender();
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.removed = true;
|
||||
if (this.reconcileTimer !== null) {
|
||||
clearInterval(this.reconcileTimer);
|
||||
this.reconcileTimer = null;
|
||||
}
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
if (this.enabled) {
|
||||
setImuReportEnabled(false, IMU_REPORT_PACE);
|
||||
this.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
image.drawText(small, 20, 8, "Accelerometer", 220);
|
||||
const status = this.enabled ? "IMU on" : "IMU off";
|
||||
image.drawText(small, width - 20 - small.measureText(status), 8, status, 150);
|
||||
|
||||
if (this.reading) {
|
||||
const axes: Array<[string, number]> = [
|
||||
["X", this.reading.x],
|
||||
["Y", this.reading.y],
|
||||
["Z", this.reading.z],
|
||||
];
|
||||
const barX = 118;
|
||||
const barW = width - barX - 24;
|
||||
const scale = Math.max(this.peak, 1e-6);
|
||||
let y = 40;
|
||||
for (const [label, value] of axes) {
|
||||
image.drawText(medium, 22, y, label, 210);
|
||||
image.drawText(medium, 44, y, value.toFixed(3), 235);
|
||||
drawCenteredBar(image, barX, y + 2, barW, 12, clamp(value / scale, -1, 1));
|
||||
y += 30;
|
||||
}
|
||||
image.drawText(
|
||||
small,
|
||||
22,
|
||||
y + 6,
|
||||
`source: ${imuSourceLabel(this.reading.source)} samples: ${this.sampleCount}`,
|
||||
130,
|
||||
);
|
||||
} else {
|
||||
const message = this.enabled
|
||||
? "Waiting for IMU data…"
|
||||
: "Paused (page not visible).";
|
||||
image.drawText(small, 22, 44, message, 190);
|
||||
image.drawText(small, 22, 66, "The firmware may not report the", 120);
|
||||
image.drawText(small, 22, 82, "accelerometer at all — experimental.", 120);
|
||||
}
|
||||
|
||||
image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Horizontal bar centered at its midpoint; frac in [-1, 1] fills left/right. */
|
||||
function drawCenteredBar(image: GrayImage, x: number, y: number, width: number, height: number, frac: number): void {
|
||||
const mid = x + Math.round(width / 2);
|
||||
image.fillRect(x, y, width, height, 20);
|
||||
image.setPixel(mid, y - 1, 90);
|
||||
image.setPixel(mid, y + height, 90);
|
||||
const half = width / 2;
|
||||
const px = Math.round(clamp(frac, -1, 1) * half);
|
||||
if (px >= 0) {
|
||||
image.fillRect(mid, y, Math.max(1, px), height, 200);
|
||||
} else {
|
||||
image.fillRect(mid + px, y, -px, height, 200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../../ui/layers";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../../ui/gestures";
|
||||
import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../../ui/menu";
|
||||
import {
|
||||
buildSoundSequencePayload,
|
||||
CFW_SEQ_MAX,
|
||||
effectPhrases,
|
||||
phraseDurationMs,
|
||||
SOUND_EFFECTS,
|
||||
type SoundEffect,
|
||||
} from "../../ui/sound-effects";
|
||||
|
||||
const HEADER_HEIGHT = 30;
|
||||
const ROW_HEIGHT = 16;
|
||||
const LIST_X = 20;
|
||||
const FOOTER_HEIGHT = 22;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Buzzer demo: pick a sound effect from the catalog and play it on the
|
||||
* glasses' piezo. Multi-phrase effects are paced phrase-by-phrase so each
|
||||
* sequencer message lands as the previous phrase finishes.
|
||||
*/
|
||||
export class BuzzerDemoLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private playing: string | null = null;
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
|
||||
image.drawText(font, 20, 8, "Buzzer demo", 220);
|
||||
const status = this.playing ? `Playing: ${this.playing}` : `${GESTURE_CLICK} play`;
|
||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
||||
|
||||
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
|
||||
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
|
||||
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length);
|
||||
|
||||
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const effect = SOUND_EFFECTS[index]!;
|
||||
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
|
||||
}
|
||||
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
|
||||
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
|
||||
}
|
||||
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_SCROLL} select ${GESTURE_CLICK} play ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1);
|
||||
return;
|
||||
case "click": {
|
||||
const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)];
|
||||
if (effect && !this.playing) {
|
||||
// Fire-and-forget: playback paces itself with sleeps between
|
||||
// phrases; awaiting it here would stall input for seconds.
|
||||
void this.playEffect(effect, ctx.actions).catch((error) => {
|
||||
console.warn(`buzzer effect failed: ${error}`);
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private async playEffect(effect: SoundEffect, actions: LayerActions): Promise<void> {
|
||||
this.playing = effect.name;
|
||||
actions.requestRender();
|
||||
try {
|
||||
for (const phrase of effectPhrases(effect.make())) {
|
||||
// Safety net: chop any over-long phrase into <=48-step messages.
|
||||
for (let index = 0; index < phrase.length; index += CFW_SEQ_MAX) {
|
||||
const chunk = phrase.slice(index, index + CFW_SEQ_MAX);
|
||||
await actions.playBuzzerSequence(buildSoundSequencePayload(chunk));
|
||||
await sleep(phraseDurationMs(chunk));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
this.playing = null;
|
||||
actions.requestRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { MenuLayer } from "../../ui/menu";
|
||||
import { ScreenTestLayer } from "./screen-test";
|
||||
import { BuzzerDemoLayer } from "./buzzer-demo";
|
||||
import { AccelerometerDemoLayer } from "./accelerometer-demo";
|
||||
import { appViewportSize } from "../../ui/shell/geometry";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "../../ui/shell/in-process-window";
|
||||
|
||||
export const DEBUG_TESTS_WINDOW_ID = "debug-tests";
|
||||
export const DEBUG_TESTS_SURFACE_ID = "window:debug-tests";
|
||||
|
||||
/** Debug/diagnostic pages (screen test, buzzer demo) as an in-process app. */
|
||||
export function createDebugTestsAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
const menu = new MenuLayer(
|
||||
"Debug tests",
|
||||
[
|
||||
{
|
||||
label: "Screen test",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new ScreenTestLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Buzzer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new BuzzerDemoLayer());
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Accelerometer demo",
|
||||
onSelect: (ctx) => {
|
||||
ctx.stack.push(new AccelerometerDemoLayer(DEBUG_TESTS_WINDOW_ID, ctx.actions.requestRender));
|
||||
},
|
||||
},
|
||||
],
|
||||
{
|
||||
x: 8,
|
||||
y: 8,
|
||||
width: 272,
|
||||
minHeight: 0,
|
||||
maxHeight: appViewportSize("min").height - 16,
|
||||
},
|
||||
);
|
||||
return createInProcessWindow({
|
||||
appId: "debug-tests",
|
||||
windowId: DEBUG_TESTS_WINDOW_ID,
|
||||
title: "Debug tests",
|
||||
iconLetter: "Db",
|
||||
icon: "flask-conical",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(menu),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: options.onClosed,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../../ui/layers";
|
||||
|
||||
const COLOR_VALUE_COUNT = 256;
|
||||
const COLOR_VALUE_STEP = 1;
|
||||
const GRID_COLUMNS = 16;
|
||||
const CELL_HEIGHT = 30;
|
||||
const LABEL_Y_OFFSET = 0;
|
||||
const SWATCH_Y_OFFSET = 12;
|
||||
const SWATCH_WIDTH = 24;
|
||||
const SWATCH_HEIGHT = 16;
|
||||
|
||||
export class ScreenTestLayer implements Layer {
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
// Sized to the hosting stack (the Debug tests app viewport).
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const font = getDefaultSmallFont();
|
||||
const cellWidth = Math.floor(width / GRID_COLUMNS);
|
||||
const gridX = Math.floor((width - cellWidth * GRID_COLUMNS) / 2);
|
||||
|
||||
for (let value = 0; value < COLOR_VALUE_COUNT; value += COLOR_VALUE_STEP) {
|
||||
const slot = value / COLOR_VALUE_STEP;
|
||||
const col = slot % GRID_COLUMNS;
|
||||
const row = Math.floor(slot / GRID_COLUMNS);
|
||||
const cellX = gridX + col * cellWidth;
|
||||
const cellY = row * CELL_HEIGHT;
|
||||
const label = String(value);
|
||||
const labelX = cellX + Math.max(0, Math.floor((cellWidth - font.measureText(label)) / 2));
|
||||
const swatchX = cellX + Math.floor((cellWidth - SWATCH_WIDTH) / 2);
|
||||
|
||||
image.drawText(font, labelX, cellY + LABEL_Y_OFFSET, label, 185);
|
||||
fillSwatch(image, swatchX, cellY + SWATCH_Y_OFFSET, SWATCH_WIDTH, SWATCH_HEIGHT, value);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function fillSwatch(image: GrayImage, x: number, y: number, width: number, height: number, value: number): void {
|
||||
// Values will ultimately be rounded up to the nearest multiple of 16 (since they're transmitted as 4-bit)
|
||||
// grayscale). So, for intermediate values, dither.
|
||||
if (value % 16 !== 0) {
|
||||
const baseValue = value & 0xf0;
|
||||
const fillFraction = value & 0x0f;
|
||||
for (let i = 0; i < height; i++) {
|
||||
for (let j = 0; j < width; j++) {
|
||||
const ditheredValue = ditherPattern(j, i, fillFraction)
|
||||
image.setPixel(x + j, y + i, ditheredValue ? baseValue + 16 : baseValue);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
image.fillRect(x, y, width, height, value);
|
||||
}
|
||||
}
|
||||
|
||||
function ditherPattern(x: number, y: number, fillFraction: number): number {
|
||||
const pattern = [
|
||||
[0, 8, 2, 10],
|
||||
[12, 4, 14, 6],
|
||||
[3, 11, 1, 9],
|
||||
[15, 7, 13, 5],
|
||||
];
|
||||
const patternValue = pattern[y % 4][x % 4];
|
||||
return (fillFraction > patternValue) ? 1 : 0;
|
||||
}
|
||||
Reference in new issue
Block a user