diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java index 4c4310c7..63ee1640 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawBleCommunicator.java @@ -28,12 +28,11 @@ import java.util.Map; public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private static final String TAG = "FaceclawComm"; - private static final BleProtocol.ImageTileOptions[] DASHBOARD_TILES = new BleProtocol.ImageTileOptions[] { - new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 288, 144), - new BleProtocol.ImageTileOptions("img10", 11, 288, 0, 288, 144), - new BleProtocol.ImageTileOptions("img01", 12, 0, 144, 288, 144), - new BleProtocol.ImageTileOptions("img11", 13, 288, 144, 288, 144) - }; + // A single full-screen image container. Earlier firmware capped image tiles at + // 288x144, which forced a 2x2 tile layout; the custom firmware lifts that limit, + // so the dashboard is now one 576x288 container. + private static final BleProtocol.ImageTileOptions DASHBOARD_TILE = + new BleProtocol.ImageTileOptions("img00", 10, 0, 0, 576, 288); private final Context appContext; private final FaceclawBleManager bleManager; @@ -90,13 +89,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private volatile FaceclawAudioPacketListener audioPacketListener; private String displayedFingerprint = ""; - private byte[][] displayedTileBmps = emptyTileSet(); + private byte[] displayedBmp = new byte[0]; private final Map imageUpdateStats = new HashMap<>(); private final Object desiredTilesLock = new Object(); private String desiredFingerprint = ""; - private byte[][] desiredTileBmps = emptyTileSet(); - private boolean desiredForceTiledCommit; + private byte[] desiredBmp = new byte[0]; private int desiredPaintMs; private final ArrayDeque pendingMessages = new ArrayDeque<>(); @@ -215,24 +213,20 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } - public void submitDashboardImage4( - byte[] tile0Bmp, - byte[] tile1Bmp, - byte[] tile2Bmp, - byte[] tile3Bmp, + public void submitDashboardImage( + byte[] image8bpp, + int width, + int height, String fingerprint, - boolean forceTiledCommit, int paintMs ) { Log.i(TAG, "Received an updated frame "); + // Convert the full-screen 8bpp grayscale buffer into the BMP wire format here so + // that all framing concerns live on the Java side. + byte[] bmp = image8bpp == null ? new byte[0] : BmpUtil.build4bppBmp(image8bpp, width, height); synchronized (desiredTilesLock) { - desiredTileBmps = emptyTileSet(); - desiredTileBmps[0] = tile0Bmp == null ? new byte[0] : Arrays.copyOf(tile0Bmp, tile0Bmp.length); - desiredTileBmps[1] = tile1Bmp == null ? new byte[0] : Arrays.copyOf(tile1Bmp, tile1Bmp.length); - desiredTileBmps[2] = tile2Bmp == null ? new byte[0] : Arrays.copyOf(tile2Bmp, tile2Bmp.length); - desiredTileBmps[3] = tile3Bmp == null ? new byte[0] : Arrays.copyOf(tile3Bmp, tile3Bmp.length); + desiredBmp = bmp; desiredFingerprint = fingerprint == null ? "" : fingerprint; - desiredForceTiledCommit = forceTiledCommit; desiredPaintMs = paintMs; } interruptibleSleep.interrupt(); @@ -265,7 +259,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = false; warmedUp = false; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; }; message.onTimeout = () -> { handleTransportFailure("shutdown ack timeout"); @@ -414,7 +408,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = false; warmedUp = false; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; clearAllMessagesLocked("firmware exit event"); } } @@ -493,7 +487,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { audioPacketListener = null; clearAllMessagesLocked("connection lost"); displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; } } @@ -521,7 +515,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; clearAllMessagesLocked("session ready"); displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; lastAckAtMs = SystemClock.elapsedRealtime(); lastIncomingAtMs = lastAckAtMs; lastConnectionOrInputAtMs = lastAckAtMs; @@ -1044,13 +1038,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { } private void enqueueCreateLayoutLocked() { - OutboundMessage message = messageBuilder.createLayout(DASHBOARD_TILES); + OutboundMessage message = messageBuilder.createLayout(DASHBOARD_TILE); message.onAck = () -> { startupProbePending = false; clearMessagesOfKindLocked("startup-text-probe"); fixedLayoutCreated = true; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; }; message.onTimeout = () -> { if (startupProbePending) { @@ -1076,7 +1070,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { fixedLayoutCreated = true; warmedUp = false; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; logLine("existing dashboard layout accepted text probe; image warmup still required"); }; message.onTimeout = () -> { @@ -1094,13 +1088,13 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private void enqueueWarmupLocked() { byte[] bmp; synchronized (desiredTilesLock) { - bmp = desiredTileBmps[0]; + bmp = desiredBmp; } if (bmp == null || bmp.length == 0) { bmp = new byte[] {0}; } bmp = BmpUtil.buildBlankWarmupBmp(bmp); - BleProtocol.ImageTileOptions tile = DASHBOARD_TILES[0]; + BleProtocol.ImageTileOptions tile = DASHBOARD_TILE; int sessionId = nextMapSessionId(); List fragments = BleImageOptimizer.planImageFragments(bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE); for (BleProtocol.ImageFragment fragment : fragments) { @@ -1116,7 +1110,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { warmedUp = false; clearMessagesOfKindLocked("warmup"); displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; }; } logLine("queue blank warmup"); @@ -1124,76 +1118,34 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { private void enqueueDesiredImageLocked() { String fingerprint = getDesiredFingerprint(); - List changedTiles = new ArrayList<>(); - byte[][] tileBmps; - boolean forceTiledCommit; + byte[] bmp; int paintMs; synchronized (desiredTilesLock) { - tileBmps = desiredTileBmps; - forceTiledCommit = desiredForceTiledCommit; - desiredForceTiledCommit = false; + bmp = desiredBmp; paintMs = desiredPaintMs; } - for (int i = 0; i < DASHBOARD_TILES.length; i++) { - BleProtocol.ImageTileOptions tile = DASHBOARD_TILES[i]; - byte[] bmp = tileBmps[i]; - if (bmp == null) { - bmp = new byte[0]; - } - byte[] displayedBmp = displayedTileBmps[i]; - if (Arrays.equals(bmp, displayedBmp)) { - continue; - } - changedTiles.add(new BleImageOptimizer.TileImagePlan(i, tile, bmp, nextMapSessionId())); + if (bmp == null) { + bmp = new byte[0]; } - if (changedTiles.isEmpty()) { + if (Arrays.equals(bmp, displayedBmp)) { displayedFingerprint = fingerprint; return; } - //boolean synchronizedCommits = changedTiles.size() > 1; - boolean synchronizedCommits = false; - if (forceTiledCommit) { - synchronizedCommits = false; - } - boolean reserveLastByte = false; - for (BleImageOptimizer.TileImagePlan plan : changedTiles) { - plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE, reserveLastByte); - } + BleImageOptimizer.TileImagePlan plan = + new BleImageOptimizer.TileImagePlan(0, DASHBOARD_TILE, bmp, nextMapSessionId()); + plan.fragments = BleImageOptimizer.planImageFragments(plan.payload, ConnectionOptions.IMAGE_FRAGMENT_SIZE); int updateId = nextImageUpdateId++; - imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, changedTiles.size())); - int messageCount = 0; - for (BleImageOptimizer.TileImagePlan plan : changedTiles) { - messageCount += plan.fragments.size(); - } - int messageNumber = 1; - if (!synchronizedCommits) { - for (BleImageOptimizer.TileImagePlan plan : changedTiles) { - for (int i = 0; i < plan.fragments.size(); i++) { - BleProtocol.ImageFragment fragment = plan.fragments.get(i); - boolean requestAck = true; - boolean isLast = i == plan.fragments.size() - 1; - enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, messageNumber++, messageCount, requestAck, isLast); - } - } - } else { - for (BleImageOptimizer.TileImagePlan plan : changedTiles) { - for (int i = 0; i < plan.fragments.size() - 1; i++) { - boolean requestAck = true; - boolean isLast = false; - enqueueImageFragmentLocked(plan, plan.fragments.get(i), fingerprint, updateId, messageNumber++, messageCount, requestAck, isLast); - } - } - for (BleImageOptimizer.TileImagePlan plan : changedTiles) { - boolean isLast = true; - enqueueImageFragmentLocked(plan, plan.fragments.get(plan.fragments.size() - 1), fingerprint, updateId, messageNumber++, messageCount, true, isLast); - } + int messageCount = plan.fragments.size(); + imageUpdateStats.put(updateId, new BleImageOptimizer.ImageUpdateStats(paintMs, 1)); + for (int i = 0; i < plan.fragments.size(); i++) { + BleProtocol.ImageFragment fragment = plan.fragments.get(i); + enqueueImageFragmentLocked(plan, fragment, fingerprint, updateId, i + 1, messageCount, true); } logLine("queue image update#" + updateId + " fingerprint=" + fingerprint - + " changedTiles=" + changedTiles.size() + " messages=" + messageCount - + " synchronizedCommits=" + synchronizedCommits); + + " messages=" + messageCount); } private void enqueueImageFragmentLocked( @@ -1203,8 +1155,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { int updateId, int messageNumber, int messageCount, - boolean requestAck, - boolean isLast + boolean requestAck ) { OutboundMessage message = messageBuilder.imageFragment(fragment, plan, requestAck, connectionOptions.sendImagesToLeft); message.setImageUpdatePosition(updateId, messageNumber, messageCount); @@ -1212,7 +1163,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { imageRetryAfterMs = 0; logImageUpdateAckLandmarkLocked(message); if (message.tileIndex >= 0 && !hasPendingOrInflightTileLocked(message.tileIndex)) { - displayedTileBmps[message.tileIndex] = BmpUtil.copyTileBmp(plan.bmp); + displayedBmp = BmpUtil.copyTileBmp(plan.bmp); } boolean imageStillInFlight = false; for (OutboundMessage inFlight : inFlightMessages) { @@ -1238,7 +1189,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { imageUpdateStats.remove(message.imageUpdateId); clearMessagesOfKindLocked("image"); displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; imageRetryAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.IMAGE_RETRY_DELAY_MS; }; pendingMessages.addLast(message); @@ -1423,7 +1374,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { shutdownRequested = false; imageRetryAfterMs = 0; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; clearAllMessagesLocked("transport failure: " + reason); reconnectAfterMs = SystemClock.elapsedRealtime() + ConnectionOptions.RECONNECT_DELAY_MS; bleManager.disconnect(rightAddress); @@ -1454,7 +1405,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { audioCaptureActive = false; audioPacketListener = null; displayedFingerprint = ""; - displayedTileBmps = emptyTileSet(); + displayedBmp = new byte[0]; } private boolean hasPendingOrInflightTileLocked(int tileIndex) { @@ -1639,10 +1590,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable { return new String(out); } - private static byte[][] emptyTileSet() { - return new byte[][] {new byte[0], new byte[0], new byte[0], new byte[0]}; - } - private static String safeMessage(Throwable t) { if (t == null) { return "unknown"; diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java index 967e39d9..84fb73db 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/MessageBuilder.java @@ -84,7 +84,7 @@ public class MessageBuilder { ); } - public OutboundMessage createLayout(BleProtocol.ImageTileOptions[] tiles) { + public OutboundMessage createLayout(BleProtocol.ImageTileOptions... tiles) { int magic = magicPool.allocate(); return new OutboundMessage( "create-layout", diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java index ac13bba9..339a634f 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/util/BmpUtil.java @@ -20,6 +20,93 @@ public class BmpUtil { return warmup; } + /** + * Build a 4bpp grayscale BMP (BITMAPINFOHEADER + 16-entry gray palette, + * bottom-up rows padded to a 4-byte stride) from a full-resolution 8bpp + * grayscale buffer. The buffer is row-major, top-to-bottom, one byte per + * pixel with values 0..255; each pixel is quantized to a 4-bit nibble. + * + * Building the wire format here, rather than on the TypeScript side, keeps + * all framing concerns in one place so future firmware changes that drop or + * replace the BMP header (e.g. 8-bit color depth or iframes) only touch Java. + */ + public static byte[] build4bppBmp(byte[] gray8, int width, int height) { + if (width <= 0 || height <= 0) { + return new byte[0]; + } + int bytesPerPixelRow = (width + 1) >> 1; + int rowStride = (bytesPerPixelRow + 3) & ~3; + int pixelDataSize = rowStride * height; + + int fileHeaderSize = 14; + int dibHeaderSize = 40; + int paletteSize = 16 * 4; + int pixelOffset = fileHeaderSize + dibHeaderSize + paletteSize; + int fileSize = pixelOffset + pixelDataSize; + + byte[] buf = new byte[fileSize]; + buf[0] = 0x42; // 'B' + buf[1] = 0x4d; // 'M' + putUint32Le(buf, 2, fileSize); + putUint32Le(buf, 10, pixelOffset); + putUint32Le(buf, 14, dibHeaderSize); + putInt32Le(buf, 18, width); + putInt32Le(buf, 22, height); + putUint16Le(buf, 26, 1); // planes + putUint16Le(buf, 28, 4); // bits per pixel + putUint32Le(buf, 30, 0); // compression (BI_RGB) + putUint32Le(buf, 34, pixelDataSize); + putUint32Le(buf, 46, 16); // palette entry count + + for (int i = 0; i < 16; i++) { + int v = i * 17; + int base = fileHeaderSize + dibHeaderSize + i * 4; + buf[base] = (byte) v; + buf[base + 1] = (byte) v; + buf[base + 2] = (byte) v; + buf[base + 3] = 0; + } + + for (int bmpRow = 0; bmpRow < height; bmpRow++) { + int srcY = height - 1 - bmpRow; // BMP rows run bottom-up + int srcRowOffset = srcY * width; + int rowOffset = pixelOffset + bmpRow * rowStride; + for (int x = 0; x < width; x += 2) { + int hi = grayToNibble(sampleGray(gray8, srcRowOffset + x)); + int lo = (x + 1 < width) ? grayToNibble(sampleGray(gray8, srcRowOffset + x + 1)) : 0; + buf[rowOffset + (x >> 1)] = (byte) ((hi << 4) | lo); + } + } + return buf; + } + + private static int sampleGray(byte[] gray8, int index) { + return (index >= 0 && index < gray8.length) ? (gray8[index] & 0xff) : 0; + } + + private static int grayToNibble(int value) { + if (value <= 0) { + return 0; + } + return Math.min(15, (value + 15) >> 4); + } + + private static void putUint16Le(byte[] buf, int offset, int value) { + buf[offset] = (byte) (value & 0xff); + buf[offset + 1] = (byte) ((value >>> 8) & 0xff); + } + + private static void putUint32Le(byte[] buf, int offset, int value) { + buf[offset] = (byte) (value & 0xff); + buf[offset + 1] = (byte) ((value >>> 8) & 0xff); + buf[offset + 2] = (byte) ((value >>> 16) & 0xff); + buf[offset + 3] = (byte) ((value >>> 24) & 0xff); + } + + private static void putInt32Le(byte[] buf, int offset, int value) { + putUint32Le(buf, offset, value); + } + public static int readBmpPixelOffset(byte[] bmp) { if (bmp == null || bmp.length < 14 || bmp[0] != 0x42 || bmp[1] != 0x4d) { return -1; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index b62ef994..970b3d59 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -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}`; diff --git a/app/graphics/compression-estimator.ts b/app/graphics/compression-estimator.ts deleted file mode 100644 index 908269a4..00000000 --- a/app/graphics/compression-estimator.ts +++ /dev/null @@ -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 | Uint8Array): number { - return value instanceof Uint8Array ? value.byteLength : value.length; -} - -function toUint8Array(value: ArrayLike | 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)}%)`; -} diff --git a/app/graphics/image.ts b/app/graphics/image.ts index 28a8b773..90258595 100644 --- a/app/graphics/image.ts +++ b/app/graphics/image.ts @@ -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))); } diff --git a/app/native/faceclaw-communicator.ts b/app/native/faceclaw-communicator.ts index e3abd898..1546225d 100644 --- a/app/native/faceclaw-communicator.ts +++ b/app/native/faceclaw-communicator.ts @@ -225,20 +225,14 @@ export class FaceclawCommunicatorBridge { await this.enqueueJavaCall(() => this.communicator.start()); } - async submitDashboardImage(tileBmps: Uint8Array[], fingerprint: string, forceTiledCommit = false, paintMs = -1): Promise { - 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 { + 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)), ); }); diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 2d3ed3d0..17ff50c2 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -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 = { id: TId; @@ -221,15 +220,6 @@ export const screenTimeoutSetting = new ConfigSettingEnum( formatValue: screenTimeoutLabel, }); -export const wakeModeSetting = new ConfigSettingEnum({ - 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", diff --git a/app/ui/dashboard.ts b/app/ui/dashboard.ts index 3124cf7e..e7cf007b 100644 --- a/app/ui/dashboard.ts +++ b/app/ui/dashboard.ts @@ -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 { 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; diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index 616df305..fe964388 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -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, );