Send a single large image rather than 4 tiles

This commit is contained in:
James Babcock committed 2026-06-23 10:49:20 -07:00
1 parent 401cf6d4c1
commit 8f28ede647
10 files changed
+150 -430

No files matched your search

+2 -22
View File
@@ -10,11 +10,9 @@ import { onAndroidNotificationPosted } from "../native/notification-icons";
import { openEvenAppSettings, readEvenAppNotificationState } from "../native/even-app-conflict";
import { grayImageToPreviewSource, grayImageToPreviewSourceWithBitmapFactory } from "../native/gray-image-preview";
import { voiceControlBridge } from "../native/voice-control";
import { estimateTileCompression } from "../graphics/compression-estimator";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type ImageTile } from "../graphics/image";
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
import {
applyDashboardScreenTimeout,
consumeDashboardTiledWakePaint,
drawDashboard,
noteDashboardPhoneTextInput,
openAndroidNotificationFromSleep,
@@ -25,7 +23,6 @@ import {
setDashboardActions,
} from "../ui/dashboard";
import {
estimateCompressionRatiosSetting,
nightscoutApiTokenSetting,
nightscoutSiteUrlSetting,
systemCardNameSetting,
@@ -558,20 +555,14 @@ class DashboardController {
const paintStartedAtMs = Date.now();
const image = drawDashboard();
const paintMs = Date.now() - paintStartedAtMs;
const forceTiledCommit = consumeDashboardTiledWakePaint();
const fingerprint = image.fingerprint();
const imageTiles = image.toEvenHubTiles();
if (estimateCompressionRatiosSetting.get()) {
this.logCompressionEstimate(imageTiles);
}
const tiles = imageTiles.map((tile) => tile.bmp);
const updatePreviewAfterTransmit = this.phase === "connected";
if (!updatePreviewAfterTransmit) {
this.updateDisplayPreviewFromImage(image);
}
if (this.communicator) {
console.log("submitDashboardImage");
await this.communicator.submitDashboardImage(tiles, fingerprint, forceTiledCommit, paintMs);
await this.communicator.submitDashboardImage(image.to8bppBuffer(), image.width, image.height, fingerprint, paintMs);
await this.communicator.waitForNextFrameMetrics(FRAME_TRANSMIT_BACKPRESSURE_TIMEOUT_MS);
if (updatePreviewAfterTransmit) {
await this.updateConnectedDisplayPreviewFromImage(image);
@@ -751,17 +742,6 @@ class DashboardController {
return `paint=${Math.round(metrics.paintMs)}ms, transmit=${Math.round(metrics.transmitMs)}ms, tiles=${Math.round(metrics.tileCount)}`;
}
private logCompressionEstimate(tiles: readonly ImageTile[]): void {
try {
const report = estimateTileCompression(tiles);
for (const line of report.tileLines) {
this.appendLog(line);
}
this.appendLog(report.totalLine);
} catch (error) {
this.appendLog(`compression estimate failed: ${this.formatError(error)}`);
}
}
private appendLog(line: string): void {
const stamped = `[${formatTimestamp(new Date())}] ${line}`;
-182
View File
@@ -1,182 +0,0 @@
import { type ImageTile } from "./image";
const lz4 = require("lz4js");
const pako = require("pako");
type CompressionTotals = {
uncompressed: number;
lz4: number;
png: number;
};
export type CompressionEstimateReport = {
tileLines: string[];
totalLine: string;
};
const cumulativeTotals: CompressionTotals = {
uncompressed: 0,
lz4: 0,
png: 0,
};
let reportSequence = 0;
export function estimateTileCompression(tiles: readonly ImageTile[]): CompressionEstimateReport {
const frameTotals: CompressionTotals = {
uncompressed: 0,
lz4: 0,
png: 0,
};
const frameId = ++reportSequence;
const tileLines: string[] = [];
for (let index = 0; index < tiles.length; index++) {
const tile = tiles[index]!;
const uncompressedSize = tile.bmp.byteLength;
const lz4Size = byteLength(lz4.compress(tile.bmp));
const pngSize = encodePng4BitGrayscaleFromBmpTile(tile).byteLength;
frameTotals.uncompressed += uncompressedSize;
frameTotals.lz4 += lz4Size;
frameTotals.png += pngSize;
tileLines.push(
`compression estimate #${frameId} tile ${index + 1}/${tiles.length} ` +
`${tile.x},${tile.y} ${tile.width}x${tile.height}: ` +
`uncompressed=${uncompressedSize}, lz4=${formatSizeWithRatio(lz4Size, uncompressedSize)}, ` +
`png4=${formatSizeWithRatio(pngSize, uncompressedSize)}`,
);
}
cumulativeTotals.uncompressed += frameTotals.uncompressed;
cumulativeTotals.lz4 += frameTotals.lz4;
cumulativeTotals.png += frameTotals.png;
return {
tileLines,
totalLine:
`compression estimate #${frameId} totals: ` +
`frame uncompressed=${frameTotals.uncompressed}, ` +
`lz4=${formatSizeWithRatio(frameTotals.lz4, frameTotals.uncompressed)}, ` +
`png4=${formatSizeWithRatio(frameTotals.png, frameTotals.uncompressed)}; ` +
`running uncompressed=${cumulativeTotals.uncompressed}, ` +
`lz4=${formatSizeWithRatio(cumulativeTotals.lz4, cumulativeTotals.uncompressed)}, ` +
`png4=${formatSizeWithRatio(cumulativeTotals.png, cumulativeTotals.uncompressed)}`,
};
}
function encodePng4BitGrayscaleFromBmpTile(tile: ImageTile): Uint8Array {
const pixelOffset = readUint32Le(tile.bmp, 10);
const sourceStride = (((tile.width + 1) >> 1) + 3) & ~3;
const pngRowBytes = (tile.width + 1) >> 1;
const scanlineStride = 1 + pngRowBytes;
const scanlines = new Uint8Array(scanlineStride * tile.height);
for (let y = 0; y < tile.height; y++) {
const sourceY = tile.height - 1 - y;
const sourceOffset = pixelOffset + sourceY * sourceStride;
const targetOffset = y * scanlineStride;
scanlines[targetOffset] = 0; // PNG filter type 0 keeps the 4-bit samples unchanged.
scanlines.set(tile.bmp.subarray(sourceOffset, sourceOffset + pngRowBytes), targetOffset + 1);
}
const compressed = toUint8Array(pako.deflate(scanlines));
const ihdr = new Uint8Array(13);
const ihdrView = new DataView(ihdr.buffer);
ihdrView.setUint32(0, tile.width, false);
ihdrView.setUint32(4, tile.height, false);
ihdr[8] = 4; // bit depth: 4-bit grayscale
ihdr[9] = 0; // color type: grayscale
ihdr[10] = 0; // compression method
ihdr[11] = 0; // filter method
ihdr[12] = 0; // no interlace
return concatBytes(
PNG_SIGNATURE,
pngChunk("IHDR", ihdr),
pngChunk("IDAT", compressed),
pngChunk("IEND", new Uint8Array(0)),
);
}
function pngChunk(type: string, data: Uint8Array): Uint8Array {
const typeBytes = asciiBytes(type);
const chunk = new Uint8Array(12 + data.byteLength);
const view = new DataView(chunk.buffer);
view.setUint32(0, data.byteLength, false);
chunk.set(typeBytes, 4);
chunk.set(data, 8);
view.setUint32(8 + data.byteLength, crc32(typeBytes, data), false);
return chunk;
}
const PNG_SIGNATURE = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
const CRC_TABLE = buildCrcTable();
function crc32(typeBytes: Uint8Array, data: Uint8Array): number {
let crc = 0xffffffff;
crc = updateCrc32(crc, typeBytes);
crc = updateCrc32(crc, data);
return (crc ^ 0xffffffff) >>> 0;
}
function updateCrc32(crc: number, data: Uint8Array): number {
for (let i = 0; i < data.byteLength; i++) {
crc = CRC_TABLE[(crc ^ data[i]!) & 0xff]! ^ (crc >>> 8);
}
return crc;
}
function buildCrcTable(): Uint32Array {
const table = new Uint32Array(256);
for (let n = 0; n < table.length; n++) {
let c = n;
for (let k = 0; k < 8; k++) {
c = (c & 1) !== 0 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
}
table[n] = c >>> 0;
}
return table;
}
function asciiBytes(value: string): Uint8Array {
const out = new Uint8Array(value.length);
for (let i = 0; i < value.length; i++) {
out[i] = value.charCodeAt(i) & 0x7f;
}
return out;
}
function concatBytes(...parts: Uint8Array[]): Uint8Array {
const totalLength = parts.reduce((sum, part) => sum + part.byteLength, 0);
const out = new Uint8Array(totalLength);
let offset = 0;
for (const part of parts) {
out.set(part, offset);
offset += part.byteLength;
}
return out;
}
function readUint32Le(bytes: Uint8Array, offset: number): number {
return (
(bytes[offset] ?? 0) |
((bytes[offset + 1] ?? 0) << 8) |
((bytes[offset + 2] ?? 0) << 16) |
((bytes[offset + 3] ?? 0) << 24)
) >>> 0;
}
function byteLength(value: ArrayLike<number> | Uint8Array): number {
return value instanceof Uint8Array ? value.byteLength : value.length;
}
function toUint8Array(value: ArrayLike<number> | Uint8Array): Uint8Array {
return value instanceof Uint8Array ? value : Uint8Array.from(value);
}
function formatSizeWithRatio(size: number, uncompressedSize: number): string {
if (uncompressedSize <= 0) return `${size}`;
return `${size} (${((size / uncompressedSize) * 100).toFixed(1)}%)`;
}
+8 -83
View File
@@ -3,18 +3,8 @@ import { wrapText } from "./textwrap";
export const G2_LENS_WIDTH = 576;
export const G2_LENS_HEIGHT = 288;
export const G2_TILE_WIDTH = 288;
export const G2_TILE_HEIGHT = 144;
const DEFAULT_CORNER_RADIUS = 8;
export type ImageTile = {
x: number;
y: number;
width: number;
height: number;
bmp: Uint8Array;
};
export function imageFromAsciiArt(lines: readonly string[], value = 255): GrayImage {
const width = Math.max(0, ...lines.map((line) => line.length));
const image = new GrayImage(width, lines.length, 0);
@@ -255,22 +245,14 @@ export class GrayImage {
return `fnv:${(hash >>> 0).toString(16)}`;
}
toEvenHubTiles(): ImageTile[] {
const tiles: ImageTile[] = [];
for (let ty = 0; ty < this.height; ty += G2_TILE_HEIGHT) {
for (let tx = 0; tx < this.width; tx += G2_TILE_WIDTH) {
const width = Math.min(G2_TILE_WIDTH, this.width - tx);
const height = Math.min(G2_TILE_HEIGHT, this.height - ty);
tiles.push({
x: tx,
y: ty,
width,
height,
bmp: buildEvenHubBmp(width, height, (x, y) => grayToEvenHubNibble(this.getPixel(tx + x, ty + y))),
});
}
}
return tiles;
/**
* Snapshot of the full image as a single 8-bit-per-pixel grayscale buffer,
* row-major and top-to-bottom. The Java side turns this into whatever wire
* format the firmware currently expects (today: a 4bpp BMP), so all framing
* concerns stay on one side of the bridge.
*/
to8bppBuffer(): Uint8Array {
return Uint8Array.from(this.pixels);
}
private drawGlyph(font: BdfFont, glyph: Glyph, x: number, y: number, value: number): void {
@@ -290,63 +272,6 @@ export class GrayImage {
}
}
function buildEvenHubBmp(
width: number,
height: number,
pixel: (x: number, y: number) => number,
): Uint8Array {
const bytesPerPixelRow = (width + 1) >> 1;
const rowStride = (bytesPerPixelRow + 3) & ~3;
const pixelDataSize = rowStride * height;
const fileHeaderSize = 14;
const dibHeaderSize = 40;
const paletteSize = 16 * 4;
const pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize;
const fileSize = pixelOffset + pixelDataSize;
const buf = new Uint8Array(fileSize);
const view = new DataView(buf.buffer);
buf[0] = 0x42;
buf[1] = 0x4d;
view.setUint32(2, fileSize, true);
view.setUint32(10, pixelOffset, true);
view.setUint32(14, dibHeaderSize, true);
view.setInt32(18, width, true);
view.setInt32(22, height, true);
view.setUint16(26, 1, true);
view.setUint16(28, 4, true);
view.setUint32(30, 0, true);
view.setUint32(34, pixelDataSize, true);
view.setUint32(46, 16, true);
for (let i = 0; i < 16; i++) {
const v = i * 17;
const base = fileHeaderSize + dibHeaderSize + i * 4;
buf[base + 0] = v;
buf[base + 1] = v;
buf[base + 2] = v;
buf[base + 3] = 0;
}
for (let bmpRow = 0; bmpRow < height; bmpRow++) {
const srcY = height - 1 - bmpRow;
const rowOffset = pixelOffset + bmpRow * rowStride;
for (let x = 0; x < width; x += 2) {
const hi = pixel(x, srcY) & 0x0f;
const lo = (x + 1 < width ? pixel(x + 1, srcY) : 0) & 0x0f;
buf[rowOffset + (x >> 1)] = (hi << 4) | lo;
}
}
return buf;
}
function grayToEvenHubNibble(value: number): number {
const clamped = clampByte(value);
return clamped === 0 ? 0 : Math.min(15, (clamped + 15) >> 4);
}
function clampByte(value: number): number {
return Math.max(0, Math.min(255, Math.round(value)));
}
+6 -12
View File
@@ -225,20 +225,14 @@ export class FaceclawCommunicatorBridge {
await this.enqueueJavaCall(() => this.communicator.start());
}
async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string, forceTiledCommit = false, paintMs = -1): Promise<void> {
if (tileBmps.length !== 4) {
throw new Error(`expected 4 dashboard tiles, got ${tileBmps.length}`);
}
const tileSnapshots = tileBmps.map((tile) => new Uint8Array(tile));
async submitDashboardImage(image8bpp: Uint8Array, width: number, height: number, fingerprint: string, paintMs = -1): Promise<void> {
const snapshot = new Uint8Array(image8bpp);
await this.enqueueJavaCall(() => {
console.log("submitDashboardImage calling Java function");
this.communicator.submitDashboardImage4(
toJavaByteArray(tileSnapshots[0]!),
toJavaByteArray(tileSnapshots[1]!),
toJavaByteArray(tileSnapshots[2]!),
toJavaByteArray(tileSnapshots[3]!),
this.communicator.submitDashboardImage(
toJavaByteArray(snapshot),
Math.round(width),
Math.round(height),
fingerprint,
forceTiledCommit,
Math.round(nonNegativeNumber(paintMs)),
);
});
-17
View File
@@ -22,7 +22,6 @@ export type SystemCardSettings = {
};
export type SystemCardSettingKey = keyof SystemCardSettings;
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never";
export type WakeModeSetting = "tiled" | "synchronized";
type ConfigSettingOptions<TValue, TId extends string> = {
id: TId;
@@ -221,15 +220,6 @@ export const screenTimeoutSetting = new ConfigSettingEnum<ScreenTimeoutSetting>(
formatValue: screenTimeoutLabel,
});
export const wakeModeSetting = new ConfigSettingEnum<WakeModeSetting>({
id: "wake-mode",
label: "Wake mode",
storageKey: "display.wakeMode",
defaultValue: "tiled",
values: ["tiled", "synchronized"],
formatValue: (value) => value === "tiled" ? "Tiled" : "Synchronized",
});
export const voiceControlEnabledSetting = new ConfigSettingBoolean({
id: "voice-control-enabled",
label: "Enable",
@@ -237,13 +227,6 @@ export const voiceControlEnabledSetting = new ConfigSettingBoolean({
defaultValue: false,
});
export const estimateCompressionRatiosSetting = new ConfigSettingBoolean({
id: "estimate-compression-ratios",
label: "Estimate compression ratios",
storageKey: "debug.estimateCompressionRatios",
defaultValue: false,
});
export const nightscoutSiteUrlSetting = new ConfigSettingString({
id: "nightscout-site-url",
label: "Nightscout site URL",
+1 -13
View File
@@ -5,7 +5,7 @@ import { mediaControllerBridge } from "../native/media-controller";
import { nightscoutBridge } from "../native/nightscout-bridge";
import { EventSourceType, OsEventTypeList } from "../g2/events";
import { getDashboardPlugin, type DashboardPluginCardBounds, type DashboardPluginState } from "./dashboard-plugins";
import { isNightscoutSettingsConfigured, screenTimeoutSettingToMs, screenTimeoutSetting, wakeModeSetting, type DashboardSlotId, DashboardPluginId, bottomRightSlotSetting, bottomLeftSlotSetting, dashboardSlotIds } from "./dashboard-settings";
import { isNightscoutSettingsConfigured, screenTimeoutSettingToMs, screenTimeoutSetting, type DashboardSlotId, DashboardPluginId, bottomRightSlotSetting, bottomLeftSlotSetting, dashboardSlotIds } from "./dashboard-settings";
import { Layer, LayerActions, LayerStack, type DashboardInputEvent, type LayerContext } from "./layers";
import { SingleNotificationLayer } from "./notifications";
import { TelepromptLayer } from "./apps/teleprompt";
@@ -23,7 +23,6 @@ type DashboardState = {
logLines: string[];
screenOn: boolean;
lastInputAtMs: number;
tiledWakePaintPending: boolean;
telepromptDocumentText: string | null;
battery: DashboardBatteryLevels;
};
@@ -32,7 +31,6 @@ export let dashboardState: DashboardState = {
logLines: [] as string[],
screenOn: true,
lastInputAtMs: Date.now(),
tiledWakePaintPending: false,
telepromptDocumentText: null as string | null,
battery: {
headset: null,
@@ -104,7 +102,6 @@ export async function receiveInput(event: RawInputEvent): Promise<void> {
if (!dashboardState.screenOn) {
if (inputEvent.type === "double-click") {
dashboardState.screenOn = true;
dashboardState.tiledWakePaintPending = wakeModeSetting.get() === "tiled";
if (dashboardLayers.isAtBase()) {
dashboardLayers.push(createRootMenuLayer());
}
@@ -145,12 +142,6 @@ export function drawDashboard(): GrayImage {
return dashboardLayers.paint();
}
export function consumeDashboardTiledWakePaint(): boolean {
const value = dashboardState.tiledWakePaintPending;
dashboardState.tiledWakePaintPending = false;
return value;
}
export function applyDashboardScreenTimeout(nowMs = Date.now()): boolean {
const timeoutMs = screenTimeoutSettingToMs(screenTimeoutSetting.get());
if (timeoutMs === null || !dashboardState.screenOn) return false;
@@ -179,7 +170,6 @@ export function openAndroidNotificationFromSleep(notificationKey: string, nowMs
dashboardState.lastInputAtMs = nowMs;
dashboardState.screenOn = true;
dashboardState.tiledWakePaintPending = wakeModeSetting.get() === "tiled";
dashboardLayers.clearToBase();
dashboardLayers.push(createRootMenuLayer());
dashboardLayers.push(new SingleNotificationLayer(notificationKey, {
@@ -191,7 +181,6 @@ export function openAndroidNotificationFromSleep(notificationKey: string, nowMs
function closeNewNotificationTrigger(ctx: LayerContext): void {
dashboardState.screenOn = false;
dashboardState.tiledWakePaintPending = false;
ctx.stack.clearToBase();
}
@@ -200,7 +189,6 @@ export function resetDashboardSleepTimerAndWake(nowMs = Date.now()): boolean {
if (dashboardState.screenOn) return false;
dashboardState.screenOn = true;
dashboardState.tiledWakePaintPending = wakeModeSetting.get() === "tiled";
dashboardLayers.clearToBase();
dashboardLayers.push(createRootMenuLayer());
return true;
+1 -3
View File
@@ -1,5 +1,5 @@
import { dashboardState, TOP_LEFT_MENU_LAYOUT } from "../dashboard";
import { dashboardSlotIds, enumSettingMenuItem, estimateCompressionRatiosSetting, showSignalStrengthSetting, showAndroidNotificationsSetting, showBatteryIndicatorsSetting, showFaceclawLogoSetting, systemCardNameSetting, textSettingMenuItem, toggleSettingMenuItem, voiceControlEnabledSetting, wakeModeSetting, dashboardSlotSettings, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, screenTimeoutSetting } from "../dashboard-settings";
import { dashboardSlotIds, enumSettingMenuItem, showSignalStrengthSetting, showAndroidNotificationsSetting, showBatteryIndicatorsSetting, showFaceclawLogoSetting, systemCardNameSetting, textSettingMenuItem, toggleSettingMenuItem, voiceControlEnabledSetting, dashboardSlotSettings, nightscoutSiteUrlSetting, nightscoutApiTokenSetting, screenTimeoutSetting } from "../dashboard-settings";
import { MenuLayer } from "../menu";
export function createSettingsMenuLayer(): MenuLayer {
@@ -44,8 +44,6 @@ function createDisplaySettingsMenuLayer(): MenuLayer {
dashboardState.lastInputAtMs = Date.now();
},
}),
enumSettingMenuItem(wakeModeSetting),
toggleSettingMenuItem(estimateCompressionRatiosSetting),
],
TOP_LEFT_MENU_LAYOUT,
);