forked from Blendman974/faceclaw
Add sound demo to Faceclaw; use CFW mode 6 (headerless 4bpp) for images
This commit is contained in:
1 parent
116e8891e7
commit
acda9cdd7e
15 files changed
+262
-22
No files matched your search
@@ -243,6 +243,30 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
/** Play one quantized buzzer tone via CFW load_image_z mode 5 kind 1. */
|
||||
public void playBuzzerNote(int note, int oct, int beat) {
|
||||
synchronized (lock) {
|
||||
if (!running || !sessionReady || !fixedLayoutCreated) {
|
||||
logLine("skip buzzer note; session not ready");
|
||||
return;
|
||||
}
|
||||
byte[] payload = BleProtocol.buildBuzzerNotePayload(note, oct, beat);
|
||||
OutboundMessage message = messageBuilder.imagePayload(
|
||||
DASHBOARD_TILE,
|
||||
nextMapSessionId(),
|
||||
payload,
|
||||
"buzzer note=" + note + " oct=" + oct + " beat=" + beat,
|
||||
connectionOptions.sendImagesToLeft
|
||||
);
|
||||
message.onTimeout = () -> {
|
||||
handleTransportFailure("buzzer note ack timeout");
|
||||
};
|
||||
pendingMessages.addLast(message);
|
||||
logLine("queue " + message.label);
|
||||
}
|
||||
interruptibleSleep.interrupt();
|
||||
}
|
||||
|
||||
public Bitmap createPreviewBitmap(int[] colors, int width, int height) {
|
||||
if (colors == null) {
|
||||
throw new IllegalArgumentException("colors are required");
|
||||
|
||||
+12
-15
@@ -74,7 +74,7 @@ public final class BleImageOptimizer {
|
||||
final int tileIndex;
|
||||
final BleProtocol.ImageTileOptions tile;
|
||||
final byte[] bmp; // raw 4bpp BMP, kept for the displayed-tile dedup cache
|
||||
final byte[] payload; // bytes actually streamed: zlib BMP when it shrinks, else == bmp
|
||||
final byte[] payload; // bytes actually streamed: mode-6 zlib 4bpp when it shrinks, else == bmp
|
||||
final int sessionId;
|
||||
List<BleProtocol.ImageFragment> fragments = Collections.emptyList();
|
||||
|
||||
@@ -88,27 +88,24 @@ public final class BleImageOptimizer {
|
||||
}
|
||||
|
||||
/**
|
||||
* zlib-deflate a tile BMP for transmission when CFW image decompression is
|
||||
* enabled and it actually shrinks the payload. The CFW's load_image_z
|
||||
* inflates any image buffer whose first byte is a zlib stream (CM nibble 8);
|
||||
* a raw BMP (starts 0x42) is loaded unchanged, so the raw fallback is always
|
||||
* safe and mixing raw/compressed tiles is fine. java.util.zip.Deflater's
|
||||
* default is the zlib wrapper (2-byte header + adler32) at windowBits 15,
|
||||
* matching the firmware inflate.
|
||||
* zlib-deflate headerless 4bpp pixels for CFW load_image_z mode 6 when it
|
||||
* shrinks the payload. Wire format: [6][zlib stream]. A raw BMP (starts 'B')
|
||||
* is sent verbatim when compression does not help.
|
||||
*/
|
||||
static byte[] maybeCompress(byte[] bmp) {
|
||||
if (!ConnectionOptions.COMPRESS_IMAGES_ZLIB || bmp == null || bmp.length == 0) {
|
||||
if (bmp == null || bmp.length == 0) {
|
||||
return bmp;
|
||||
}
|
||||
byte[] z = deflate(bmp);
|
||||
// CFW wire format: a compressed payload is [mode byte][zlib stream]. Mode 1
|
||||
// = zlib of a 4bpp BMP (load_image_z inflates it and hands the BMP to the
|
||||
// stock loader). A raw BMP (starts 'B') is still sent verbatim.
|
||||
if (z == null || z.length + 1 >= bmp.length) {
|
||||
byte[] packed = BmpUtil.pack4bppFromBmp(bmp);
|
||||
if (packed.length == 0) {
|
||||
return bmp;
|
||||
}
|
||||
byte[] z = deflate(packed);
|
||||
if (z == null || z.length + 1 >= packed.length) {
|
||||
return bmp;
|
||||
}
|
||||
byte[] out = new byte[z.length + 1];
|
||||
out[0] = 1;
|
||||
out[0] = 6;
|
||||
System.arraycopy(z, 0, out, 1, z.length);
|
||||
return out;
|
||||
}
|
||||
|
||||
@@ -121,6 +121,14 @@ public class BleProtocol {
|
||||
return wrapEvenHub(5, magic, 9, concat(inner));
|
||||
}
|
||||
|
||||
/** CFW load_image_z mode 5 kind 1: play one quantized buzzer note. */
|
||||
public static byte[] buildBuzzerNotePayload(int note, int oct, int beat) {
|
||||
int clampedNote = Math.max(1, Math.min(7, note));
|
||||
int clampedOct = Math.max(0, Math.min(3, oct));
|
||||
int clampedBeat = Math.max(1, Math.min(255, beat));
|
||||
return new byte[] {5, 1, (byte) clampedNote, (byte) clampedOct, (byte) clampedBeat};
|
||||
}
|
||||
|
||||
public static byte[] buildImageRawData(ImageTileOptions tile, int sessionId, int totalSize, ImageFragment fragment, int magic) {
|
||||
List<byte[]> inner = new ArrayList<>();
|
||||
inner.add(encodeVarintField(1, tile.containerId));
|
||||
|
||||
@@ -19,11 +19,6 @@ public class ConnectionOptions {
|
||||
public static final int BATTERY_REFRESH_INTERVAL_MS = 5 * 60_000;
|
||||
public static final int BATTERY_INPUT_QUIET_MS = 5_000;
|
||||
public static final int IMAGE_FRAGMENT_SIZE = 3800;
|
||||
// Stream image tiles as zlib(DEFLATE)-compressed BMPs. REQUIRES the custom
|
||||
// firmware (load_image_z inflates any image buffer that starts with a zlib
|
||||
// stream; a raw BMP is loaded unchanged). Turn OFF when talking to stock
|
||||
// firmware, which would reject the compressed bytes as an invalid BMP.
|
||||
public static final boolean COMPRESS_IMAGES_ZLIB = true;
|
||||
public static final int IMAGE_RETRY_DELAY_MS = 2_000;
|
||||
public static final int MAX_CONSECUTIVE_ACK_TIMEOUTS = 8;
|
||||
public static final int EVEN_APP_WRITE_FAILURE_WINDOW_MS = 15_000;
|
||||
|
||||
@@ -54,6 +54,29 @@ public class MessageBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage imagePayload(
|
||||
BleProtocol.ImageTileOptions tile,
|
||||
int sessionId,
|
||||
byte[] payload,
|
||||
String label,
|
||||
boolean leftArm
|
||||
) {
|
||||
BleProtocol.ImageFragment fragment =
|
||||
new BleProtocol.ImageFragment(0, payload, payload.length);
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"sound",
|
||||
label,
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImageRawData(tile, sessionId, payload.length, fragment, magic),
|
||||
ACK_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage imageFragment(BleProtocol.ImageFragment fragment, BleImageOptimizer.TileImagePlan plan, boolean requestAck, boolean leftArm) {
|
||||
int magic = requestAck ? magicPool.allocate() : 0;
|
||||
return new OutboundMessage(
|
||||
|
||||
@@ -116,4 +116,45 @@ public class BmpUtil {
|
||||
| ((bmp[12] & 0xff) << 16)
|
||||
| ((bmp[13] & 0xff) << 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the BMP header and row padding, producing the headerless 4bpp wire
|
||||
* format expected by CFW load_image_z mode 6: top-down rows, stride =
|
||||
* ceil(width/2), high nibble = left pixel.
|
||||
*/
|
||||
public static byte[] pack4bppFromBmp(byte[] bmp) {
|
||||
if (bmp == null || bmp.length < 0x36 || bmp[0] != 0x42 || bmp[1] != 0x4d) {
|
||||
return new byte[0];
|
||||
}
|
||||
int width = readUint32Le(bmp, 18);
|
||||
int heightSigned = readInt32Le(bmp, 22);
|
||||
int height = heightSigned < 0 ? -heightSigned : heightSigned;
|
||||
int pixelOffset = readBmpPixelOffset(bmp);
|
||||
if (width <= 0 || height <= 0 || pixelOffset < 0 || pixelOffset >= bmp.length) {
|
||||
return new byte[0];
|
||||
}
|
||||
int packedStride = (width + 1) >> 1;
|
||||
int bmpStride = (packedStride + 3) & ~3;
|
||||
if (pixelOffset + (long) bmpStride * height > bmp.length) {
|
||||
return new byte[0];
|
||||
}
|
||||
byte[] out = new byte[packedStride * height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
int srcY = height - 1 - y;
|
||||
int srcRow = pixelOffset + srcY * bmpStride;
|
||||
System.arraycopy(bmp, srcRow, out, y * packedStride, packedStride);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static int readUint32Le(byte[] buf, int offset) {
|
||||
return (buf[offset] & 0xff)
|
||||
| ((buf[offset + 1] & 0xff) << 8)
|
||||
| ((buf[offset + 2] & 0xff) << 16)
|
||||
| ((buf[offset + 3] & 0xff) << 24);
|
||||
}
|
||||
|
||||
private static int readInt32Le(byte[] buf, int offset) {
|
||||
return readUint32Le(buf, offset);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user