forked from Blendman974/faceclaw
Merge branch 'main' into evenhub-from-qr
This commit is contained in:
commit
5db6df93dd
6 files changed
+304
-146
No files matched your search
@@ -73,7 +73,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
private boolean ringNotificationsReady;
|
||||
private boolean sessionReady;
|
||||
private boolean fixedLayoutCreated;
|
||||
private boolean warmedUp;
|
||||
private boolean shutdownRequested;
|
||||
// CFW firmware-debug-flags overlay (mode 7). Desired value pushed from TS; the
|
||||
// sub-op last sent this session (-1 = not yet), reset on (re)connect so the
|
||||
@@ -307,7 +306,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
public void setFirmwareDebugFlags(boolean enabled) {
|
||||
// Just record it; the drive loop emits the mode-7 control message when the
|
||||
// session is warmed up and idle, and re-emits when this value changes.
|
||||
// display path is ready and idle, and re-emits when this value changes.
|
||||
firmwareDebugFlagsEnabled = enabled;
|
||||
}
|
||||
|
||||
@@ -317,7 +316,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
int magic;
|
||||
synchronized (lock) {
|
||||
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated || !warmedUp) {
|
||||
if (!running || !sessionReady || shutdownRequested || !fixedLayoutCreated) {
|
||||
logLine("skip G2 mic enable; EvenHub display path not ready");
|
||||
return false;
|
||||
}
|
||||
@@ -383,8 +382,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait until the recreated layout, warmup, and retained compositor frame
|
||||
* have all landed. If this wake came from CFW's deferred double tap, READY
|
||||
* Wait until the recreated layout and retained compositor frame have both
|
||||
* landed. If this wake came from CFW's deferred double tap, READY
|
||||
* is then sent to both arms to cancel their stock-dashboard fallback.
|
||||
*/
|
||||
public boolean awaitEvenHubSessionReady(int timeoutMs) {
|
||||
@@ -397,7 +396,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
frameReady = !desiredFingerprint.isEmpty()
|
||||
&& desiredFingerprint.equals(displayedFingerprint);
|
||||
}
|
||||
if (!shutdownRequested && fixedLayoutCreated && warmedUp && frameReady) {
|
||||
if (!shutdownRequested && fixedLayoutCreated && frameReady) {
|
||||
if (faceclawWakePendingNonce >= 0) {
|
||||
readyGeneration = enqueueFaceclawWakeControlLocked(
|
||||
BleProtocol.FACECLAW_WAKE_OP_READY,
|
||||
@@ -463,7 +462,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
compassEnabled = enable;
|
||||
compassControlLastSent = -1;
|
||||
clearMessagesOfKindLocked("compass-control");
|
||||
if (running && sessionReady && !shutdownRequested && fixedLayoutCreated && warmedUp) {
|
||||
if (running && sessionReady && !shutdownRequested && fixedLayoutCreated) {
|
||||
enqueueCompassControlLocked(true, compassEnabled);
|
||||
} else {
|
||||
logLine("defer compass " + (enable ? "enable" : "disable") + "; display path not ready");
|
||||
@@ -742,8 +741,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
surfaceId, pixels8bpp, rectX, rectY, rectWidth, rectHeight, contentFingerprint, glyphs);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "composite");
|
||||
// Pack the composited 8bpp buffer down to the headerless 4bpp frame
|
||||
// format the wire planners consume; BMP framing is added later only on
|
||||
// the rare paths that still need it (warmup, uncompressed fallback).
|
||||
// format the wire planners consume; BMP framing is added later only for
|
||||
// the uncompressed fallback.
|
||||
FrameTimings.getInstance().spanStart(frameId, "pack-4bpp");
|
||||
byte[] packed = BmpUtil.pack4bppFromGray8(composite.gray, composite.width, composite.height);
|
||||
FrameTimings.getInstance().spanEnd(frameId, "pack-4bpp");
|
||||
@@ -835,7 +834,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return true;
|
||||
}
|
||||
if (!cfwCleanupSupported || !running || !sessionReady
|
||||
|| shutdownRequested || !fixedLayoutCreated || !warmedUp) {
|
||||
|| shutdownRequested || !fixedLayoutCreated) {
|
||||
logLine("skip CFW cleanup; mode 11 unavailable or image path not ready");
|
||||
return false;
|
||||
}
|
||||
@@ -986,7 +985,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
shutdownRequested = false;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
startupProbePending = false;
|
||||
audioCaptureActive = false;
|
||||
clearAllMessagesPreservingWakeLeaseLocked("EvenHub resume");
|
||||
@@ -1021,7 +1019,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
message.onAck = () -> {
|
||||
lastShutdownAckMagic = message.magic;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
displayedFingerprint = "";
|
||||
};
|
||||
message.onTimeout = () -> {
|
||||
@@ -1039,7 +1036,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// this also covers a disable that was wiped by the queue flush above
|
||||
// or whose ack was lost, and the charging-mode/exit paths where the
|
||||
// Compass window never got a chance to release it.
|
||||
if (compassMaybeOn && fixedLayoutCreated && warmedUp) {
|
||||
if (compassMaybeOn && fixedLayoutCreated) {
|
||||
enqueueCompassControlLocked(true, false);
|
||||
}
|
||||
}
|
||||
@@ -1267,7 +1264,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
lastShutdownExitAtMs = SystemClock.elapsedRealtime();
|
||||
}
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
displayedFingerprint = "";
|
||||
clearAllMessagesLocked("firmware exit event");
|
||||
}
|
||||
@@ -1363,7 +1359,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!connected) {
|
||||
sessionReady = false;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
startupProbePending = false;
|
||||
chargingMode = false;
|
||||
audioCaptureActive = false;
|
||||
@@ -1398,7 +1393,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// its page was intentionally suspended.
|
||||
shutdownRequested = false;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
clearAllMessagesLocked("session ready");
|
||||
displayedFingerprint = "";
|
||||
lastAckAtMs = SystemClock.elapsedRealtime();
|
||||
@@ -1692,19 +1686,14 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// Prewrite outside the lock; the logical message remains pending until
|
||||
// its final BLE frame is sent after the current protocol ACK.
|
||||
}
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && !warmedUp && pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
Log.i(TAG, "enqueueing warmup");
|
||||
enqueueWarmupLocked();
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
|
||||
&& (firmwareDebugFlagsEnabled ? 2 : 1) != firmwareDebugFlagsLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
Log.i(TAG, "enqueueing firmware debug flags " + (firmwareDebugFlagsEnabled ? "show" : "hide"));
|
||||
enqueueFirmwareDebugFlagsLocked();
|
||||
}
|
||||
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
|
||||
&& (compassEnabled ? 1 : 0) != compassControlLastSent
|
||||
&& pendingMessages.isEmpty() && inFlightMessages.isEmpty()) {
|
||||
Log.i(TAG, "enqueueing compass " + (compassEnabled ? "enable" : "disable"));
|
||||
@@ -1724,7 +1713,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// window unprotected and a heartbeat that came due there
|
||||
// cost the frame a full ack round trip (measured 124ms on
|
||||
// frame#232 of the 2026-08-20 02:27 capture).
|
||||
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
boolean imageWaiting = !shutdownRequested && fixedLayoutCreated
|
||||
&& !hasPendingOrInflightKindLocked("heartbeat")
|
||||
&& now >= imageRetryAfterMs
|
||||
&& (hasPendingImageLocked()
|
||||
@@ -1737,7 +1726,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (messageToPrewrite == null && sessionReady && windowHasRoom && !pendingMessages.isEmpty()) {
|
||||
messageToWrite = pendingMessages.removeFirst();
|
||||
Log.i(TAG, "sending pending message: " + messageToWrite.label);
|
||||
} else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated && warmedUp
|
||||
} else if (messageToPrewrite == null && !shutdownRequested && fixedLayoutCreated
|
||||
&& windowHasRoom && !hasPendingImageLocked()
|
||||
&& now >= imageRetryAfterMs
|
||||
&& !getDesiredFingerprint().equals(lastEnqueuedFingerprint)) {
|
||||
@@ -1798,7 +1787,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private boolean shouldBlockPrewriteForHeartbeatLocked(long now) {
|
||||
if (shutdownRequested || !warmedUp || !fixedLayoutCreated) {
|
||||
if (shutdownRequested || !fixedLayoutCreated) {
|
||||
return false;
|
||||
}
|
||||
return hasPendingOrInflightKindLocked("heartbeat")
|
||||
@@ -1809,7 +1798,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (message == null || !message.isLeftArmMessage) {
|
||||
return false;
|
||||
}
|
||||
if (!"image".equals(message.kind) && !"warmup".equals(message.kind)) {
|
||||
if (!"image".equals(message.kind)) {
|
||||
return false;
|
||||
}
|
||||
return message.message.length + 2 > 232;
|
||||
@@ -1817,7 +1806,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
|
||||
private boolean handleHeartbeat(boolean imageWaiting) {
|
||||
long now = SystemClock.elapsedRealtime();
|
||||
boolean heartbeatEligible = !shutdownRequested && warmedUp && fixedLayoutCreated;
|
||||
boolean heartbeatEligible = !shutdownRequested && fixedLayoutCreated;
|
||||
boolean heartbeatPending = heartbeatEligible && hasPendingOrInflightKindLocked("heartbeat");
|
||||
long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs;
|
||||
boolean heartbeatReady = heartbeatEligible && heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS;
|
||||
@@ -2092,8 +2081,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
private void enqueueCreateLayoutLocked() {
|
||||
// New session/container: re-assert the firmware-debug-flags overlay once it's
|
||||
// warmed up again (the mode-7 send is gated on this having reset).
|
||||
// New session/container: re-assert the firmware-debug-flags overlay once
|
||||
// the layout is ready (the mode-7 send is gated on this having reset).
|
||||
firmwareDebugFlagsLastSent = -1;
|
||||
OutboundMessage message = messageBuilder.createLayout(DASHBOARD_TILE);
|
||||
message.onAck = () -> {
|
||||
@@ -2124,9 +2113,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
startupProbePending = false;
|
||||
clearMessagesOfKindLocked("create-layout");
|
||||
fixedLayoutCreated = true;
|
||||
warmedUp = false;
|
||||
displayedFingerprint = "";
|
||||
logLine("existing dashboard layout accepted text probe; image warmup still required");
|
||||
logLine("existing dashboard layout accepted text probe");
|
||||
};
|
||||
message.onTimeout = () -> {
|
||||
startupProbePending = false;
|
||||
@@ -2140,36 +2128,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("queue startup text probe");
|
||||
}
|
||||
|
||||
private void enqueueWarmupLocked() {
|
||||
BleProtocol.ImageTileOptions tile = DASHBOARD_TILE;
|
||||
// Warm up the legacy container with a carrier-sized blank BMP. Real
|
||||
// 640x480 frames use mode 6 and are intentionally independent of this
|
||||
// geometry; a mismatched raw BMP would be rejected by the stock loader.
|
||||
byte[] bmp = BmpUtil.build4bppBmpFromPacked(
|
||||
new byte[((tile.width + 1) >> 1) * tile.height], tile.width, tile.height);
|
||||
int sessionId = nextMapSessionId();
|
||||
List<BleProtocol.ImageFragment> fragments = BleImageOptimizer.planImageFragments(bmp, ConnectionOptions.IMAGE_FRAGMENT_SIZE);
|
||||
for (BleProtocol.ImageFragment fragment : fragments) {
|
||||
OutboundMessage message = messageBuilder.imageWarmupFragment(tile, sessionId, fragment, bmp, connectionOptions.sendImagesToLeft);
|
||||
pendingMessages.addLast(message);
|
||||
message.onAck = () -> {
|
||||
// The first tile warmup primes the image path but does not reliably
|
||||
// guarantee that the tile is now visible on-screen, so it must not
|
||||
// update the displayed-tile cache used by image dedupe.
|
||||
warmedUp = true;
|
||||
// Warmup fragments are image messages on the wire, so they reset
|
||||
// the firmware's heartbeat timer just like real image updates.
|
||||
lastHeartbeatAckedAtMs = SystemClock.elapsedRealtime();
|
||||
};
|
||||
message.onTimeout = () -> {
|
||||
warmedUp = false;
|
||||
clearMessagesOfKindLocked("warmup");
|
||||
displayedFingerprint = "";
|
||||
};
|
||||
}
|
||||
logLine("queue blank warmup");
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the CFW mode-7 diagnostic-flag control op to the dashboard container:
|
||||
* [7][2] to show the on-glasses debug-flag overlay, [7][1] to hide it. Uses the
|
||||
@@ -2519,7 +2477,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
chargingMode = true;
|
||||
clearAllMessagesLocked("glasses charging");
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
startupProbePending = false;
|
||||
displayedFingerprint = "";
|
||||
finishDesiredFrameLocked("discarded: glasses charging");
|
||||
@@ -2542,7 +2499,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
* Record, into the frame that is waiting, why it did not go out on this
|
||||
* pass of the send loop. Without this the export shows a bare multi-second
|
||||
* jump between "image submitted as desired frame" and the first BLE
|
||||
* packet, with no hint whether we were blocked on warmup, a heartbeat, the
|
||||
* packet, with no hint whether we were blocked on a heartbeat, the
|
||||
* BLE window, or another message queued ahead. Deduped on (frame, reason),
|
||||
* so a frame stalled for seconds gets one line per state change rather
|
||||
* than one per loop pass.
|
||||
@@ -2586,9 +2543,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
if (!fixedLayoutCreated) {
|
||||
return "display layout not created yet";
|
||||
}
|
||||
if (!warmedUp) {
|
||||
return "waiting for the warmup frame";
|
||||
}
|
||||
if (now < imageRetryAfterMs) {
|
||||
return "image retry backoff (" + (imageRetryAfterMs - now) + "ms left)";
|
||||
}
|
||||
@@ -2624,7 +2578,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
// round trip. Reported explicitly because it is otherwise invisible --
|
||||
// heartbeats belong to no frame.
|
||||
long heartbeatElapsedMs = now - lastHeartbeatAckedAtMs;
|
||||
if (warmedUp && fixedLayoutCreated && !shutdownRequested
|
||||
if (fixedLayoutCreated && !shutdownRequested
|
||||
&& heartbeatElapsedMs >= ConnectionOptions.HEARTBEAT_READY_MS) {
|
||||
return "heartbeat due (" + heartbeatElapsedMs + "ms since the last one acked)";
|
||||
}
|
||||
@@ -2980,7 +2934,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
maybeEmitEvenAppConflictLocked(reason);
|
||||
sessionReady = false;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
startupProbePending = false;
|
||||
shutdownRequested = false;
|
||||
chargingMode = false;
|
||||
@@ -3004,7 +2957,6 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
sessionReady = false;
|
||||
shutdownRequested = false;
|
||||
fixedLayoutCreated = false;
|
||||
warmedUp = false;
|
||||
chargingMode = false;
|
||||
rightConnected = false;
|
||||
leftConnected = false;
|
||||
@@ -3022,7 +2974,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
audioPacketListener = null;
|
||||
compassControlLastSent = -1;
|
||||
// A dead transport orphans any glasses-side compass state; the fresh
|
||||
// session re-asserts the desired state after warmup (lastSent = -1).
|
||||
// session re-asserts the desired state once its layout is ready.
|
||||
compassMaybeOn = false;
|
||||
faceclawWakePendingNonce = -1;
|
||||
lastFaceclawWakeLeaseQueuedAtMs = 0;
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.faceclaw.app;
|
||||
public class MessageBuilder {
|
||||
private static final int ACK_TIMEOUT_MS = 3_500;
|
||||
private static final int HEARTBEAT_TIMEOUT_MS = 1_500;
|
||||
private static final int WARMUP_FRAGMENT_TIMEOUT_MS = 3_000;
|
||||
|
||||
private BleMagicPool magicPool;
|
||||
|
||||
@@ -39,21 +38,6 @@ public class MessageBuilder {
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage imageWarmupFragment(BleProtocol.ImageTileOptions tile, int sessionId, BleProtocol.ImageFragment fragment, byte[] bmp, boolean leftArm) {
|
||||
int magic = magicPool.allocate();
|
||||
return new OutboundMessage(
|
||||
"warmup",
|
||||
"warmup " + tile.name + "#" + fragment.index,
|
||||
BleProtocol.SID_EVENHUB,
|
||||
BleProtocol.FLAG_REQUEST,
|
||||
magic,
|
||||
BleProtocol.buildImageRawData(tile, sessionId, bmp.length, fragment, magic),
|
||||
WARMUP_FRAGMENT_TIMEOUT_MS,
|
||||
-1,
|
||||
leftArm
|
||||
);
|
||||
}
|
||||
|
||||
public OutboundMessage imagePayload(
|
||||
BleProtocol.ImageTileOptions tile,
|
||||
int sessionId,
|
||||
|
||||
@@ -65,9 +65,9 @@ public class BmpUtil {
|
||||
/**
|
||||
* Wrap a packed 4bpp frame in BMP framing (BITMAPINFOHEADER + 16-entry gray
|
||||
* palette, bottom-up rows padded to a 4-byte stride). Only the full-frame
|
||||
* image path still speaks BMP on the wire (warmup, and the fallback when
|
||||
* mode-6 compression does not shrink the frame); everything else uses the
|
||||
* headerless packed format directly.
|
||||
* fallback used when mode-6 compression does not shrink the frame still
|
||||
* speaks BMP on the wire; everything else uses the headerless packed format
|
||||
* directly.
|
||||
*/
|
||||
public static byte[] build4bppBmpFromPacked(byte[] packed, int width, int height) {
|
||||
int packedStride = (width + 1) >> 1;
|
||||
|
||||
+275
-54
@@ -1,4 +1,4 @@
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { getDefaultLargeFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import {
|
||||
COMPASS_CALIBRATION_COMPLETE,
|
||||
@@ -25,10 +25,69 @@ import { CompassCalibrationLayer } from "./calibration-layer";
|
||||
export const COMPASS_WINDOW_ID = "compass";
|
||||
export const COMPASS_SURFACE_ID = "window:compass";
|
||||
const RECONCILE_INTERVAL_MS = 400;
|
||||
/** Vertical breathing room between the rose, the readout and the status line. */
|
||||
/** Vertical breathing room between the readout, the status line and the rose. */
|
||||
const STACK_GAP = 8;
|
||||
/** Inset of the cardinal-label ring from the rose's outer circle. */
|
||||
const LABEL_INSET = 14;
|
||||
/** Padding above the readout and below the rose's near edge. */
|
||||
const TOP_PAD = 2;
|
||||
const BOTTOM_PAD = 4;
|
||||
/** Clearance between the rose's widest point and the viewport edge. */
|
||||
const EDGE_PAD = 8;
|
||||
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
|
||||
const MAX_ROSE_RADIUS = 98;
|
||||
|
||||
/**
|
||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||
* oval — wider and taller at the bottom — and give the rose's points their
|
||||
* foreshortening for free.
|
||||
*/
|
||||
const TILT_SQUASH = 0.42;
|
||||
const TILT_PERSPECTIVE = 0.22;
|
||||
/** Screen height of the projected ring, per unit of radius. */
|
||||
const RING_HEIGHT_PER_RADIUS =
|
||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||
/** Screen half-width of the projected ring, per unit of radius. */
|
||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||
|
||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||
const CARDINAL_TIP = 0.9;
|
||||
const INTERCARDINAL_TIP = 0.54;
|
||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||
const POINT_BASE_RADIUS = 0.17;
|
||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||
/**
|
||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||
* middle into the heading tick.
|
||||
*/
|
||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||
const PLANE_SPOKE_STEP = 30;
|
||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||
const PLANE_NEAR_VALUE = 78;
|
||||
const PLANE_FALLOFF = 0.55;
|
||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||
const PLANE_CUTOFF = 22;
|
||||
/** Height over which the grid fades out as it approaches the readout. */
|
||||
const PLANE_FADE_SPAN = 45;
|
||||
|
||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||
const TICK_HEIGHT = 7;
|
||||
const TICK_HALF_WIDTH = 4;
|
||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||
const DISC_DEPTH = 0.1;
|
||||
/**
|
||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||
*/
|
||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||
const WALL_DARK = 20;
|
||||
const WALL_LIT = 62;
|
||||
|
||||
class CompassLayer implements Layer {
|
||||
private rawHeading: number | null = null;
|
||||
@@ -103,26 +162,43 @@ class CompassLayer implements Layer {
|
||||
const small = getDefaultSmallFont();
|
||||
const large = getDefaultLargeFont();
|
||||
const heading = this.rawHeading === null ? null : calibrateHeading(this.rawHeading);
|
||||
const radius = Math.min(63, Math.round(height * 0.227));
|
||||
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||
const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2);
|
||||
const smallStep = lineStep(small);
|
||||
|
||||
// One column centred on the display's true centre, so the rose's centre
|
||||
// dot sits where the wearer is looking rather than 32px right of it.
|
||||
// One column centred on the display's true centre, so the rose sits where
|
||||
// the wearer is looking rather than 32px right of it.
|
||||
const cx = screenCenterInViewportX();
|
||||
const stackHeight = radius * 2 + STACK_GAP + large.lineHeight + STACK_GAP + statusLines.length * smallStep;
|
||||
const cy = Math.max(0, Math.round((height - stackHeight) / 2)) + radius;
|
||||
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
// The rose is sized and placed first — it hangs off the bottom edge — and
|
||||
// the readout then floats in whatever room is left above it.
|
||||
const textHeight = large.lineHeight + 4 + statusLines.length * smallStep;
|
||||
const radius = Math.max(
|
||||
24,
|
||||
Math.min(
|
||||
MAX_ROSE_RADIUS,
|
||||
Math.floor(
|
||||
(height - BOTTOM_PAD - TOP_PAD - textHeight - STACK_GAP - TICK_HEIGHT)
|
||||
/ (RING_HEIGHT_PER_RADIUS + DISC_DEPTH),
|
||||
),
|
||||
Math.floor((Math.min(cx, width - cx) - EDGE_PAD) / RING_HALF_WIDTH_PER_RADIUS),
|
||||
),
|
||||
);
|
||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||
// from the bottom padding rather than on it.
|
||||
const cy = height - BOTTOM_PAD - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||
|
||||
let y = cy + radius + STACK_GAP;
|
||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||
image.drawText(large, Math.round(cx - large.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||
y += large.lineHeight + STACK_GAP;
|
||||
y += large.lineHeight + 4;
|
||||
for (const line of statusLines) {
|
||||
image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125);
|
||||
y += smallStep;
|
||||
}
|
||||
|
||||
drawPlaneGrid(image, cx, cy, radius, y + 6);
|
||||
drawCompassRose(image, cx, cy, radius, heading);
|
||||
return image;
|
||||
}
|
||||
|
||||
@@ -174,54 +250,199 @@ function cardinalDirection(heading: number): string {
|
||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||
}
|
||||
|
||||
/** Draw a compass rose whose bright needle points toward magnetic north. */
|
||||
interface Point {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||
* and `radius` is a fraction of the rose's outer ring.
|
||||
*/
|
||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||
const radians = (angle * Math.PI) / 180;
|
||||
const across = radius * Math.sin(radians);
|
||||
const away = radius * Math.cos(radians);
|
||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||
return {
|
||||
x: cx + (across * scale) / depth,
|
||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||
};
|
||||
}
|
||||
|
||||
/** Draw the tilted rose, with its brightest point aimed at magnetic north. */
|
||||
function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||
drawCircle(image, cx, cy, radius, 105);
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const cardinals: Array<[string, number]> = [["N", -90], ["E", 0], ["S", 90], ["W", 180]];
|
||||
for (const [label, degrees] of cardinals) {
|
||||
const angle = (degrees - (heading ?? 0)) * Math.PI / 180;
|
||||
const x = cx + Math.cos(angle) * (radius - LABEL_INSET) - medium.measureText(label) / 2;
|
||||
const y = cy + Math.sin(angle) * (radius - LABEL_INSET) - medium.lineHeight / 2;
|
||||
image.drawText(medium, Math.round(x), Math.round(y), label, label === "N" ? 230 : 105);
|
||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||
// orientation, dimmed, rather than an empty ring.
|
||||
const fade = heading === null ? 0.45 : 1;
|
||||
const bearing = heading ?? 0;
|
||||
drawDiscWall(image, cx, cy, radius, fade);
|
||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||
// north is drawn at full brightness: the other seven are there to make the
|
||||
// rose read as a rose, and competing with north would defeat the point.
|
||||
for (const degrees of [45, 135, 225, 315]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||
}
|
||||
const intercardinals: Array<[string, number]> = [["NE", -45], ["SE", 45], ["SW", 135], ["NW", -135]];
|
||||
for (const [label, degrees] of intercardinals) {
|
||||
const angle = (degrees - (heading ?? 0)) * Math.PI / 180;
|
||||
const x = cx + Math.cos(angle) * (radius - LABEL_INSET) - small.measureText(label) / 2;
|
||||
const y = cy + Math.sin(angle) * (radius - LABEL_INSET) - small.lineHeight / 2;
|
||||
image.drawText(small, Math.round(x), Math.round(y), label, 85);
|
||||
for (const degrees of [90, 180, 270]) {
|
||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||
}
|
||||
image.fillRect(cx - 2, cy - 2, 5, 5, 180);
|
||||
if (heading === null) return;
|
||||
const northAngle = (-90 - heading) * Math.PI / 180;
|
||||
// Stop the needle just inside the label ring. The labels keep their font
|
||||
// size as the rose shrinks, so a fixed inset would run the needle into them.
|
||||
const needle = Math.max(8, radius - LABEL_INSET - medium.lineHeight / 2 - 4);
|
||||
const nx = cx + Math.cos(northAngle) * needle;
|
||||
const ny = cy + Math.sin(northAngle) * needle;
|
||||
const sx = cx - Math.cos(northAngle) * (needle - 8);
|
||||
const sy = cy - Math.sin(northAngle) * (needle - 8);
|
||||
drawThickLine(image, cx, cy, nx, ny, 255);
|
||||
image.drawLine(cx, cy, sx, sy, 75);
|
||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||
}
|
||||
|
||||
function drawCircle(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
let x = radius;
|
||||
let y = 0;
|
||||
let error = 1 - x;
|
||||
while (x >= y) {
|
||||
const points = [[x, y], [y, x], [-y, x], [-x, y], [-x, -y], [-y, -x], [y, -x], [x, -y]];
|
||||
for (const [dx, dy] of points) image.setPixel(cx + dx!, cy + dy!, value);
|
||||
y++;
|
||||
if (error < 0) error += 2 * y + 1;
|
||||
else { x--; error += 2 * (y - x) + 1; }
|
||||
/**
|
||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||
* shaded facet, which is what keeps the eight points readable once perspective
|
||||
* has squashed the ones lying across the wearer's line of sight.
|
||||
*/
|
||||
function drawRosePoint(
|
||||
image: GrayImage,
|
||||
cx: number,
|
||||
cy: number,
|
||||
radius: number,
|
||||
angle: number,
|
||||
tipRadius: number,
|
||||
litValue: number,
|
||||
shadedValue: number,
|
||||
): void {
|
||||
const centre = { x: cx, y: cy };
|
||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||
*/
|
||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||
const depth = discDepth(radius);
|
||||
const segments = 48;
|
||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||
const rimValue = 105 * fade;
|
||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||
const point = projectRose(cx, cy, radius, 1, angle);
|
||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||
// runs nearly horizontal across the bottom of the oval.
|
||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||
const top = { x: previous.x, y: previous.y };
|
||||
const next = { x: point.x, y: point.y };
|
||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||
previous = point;
|
||||
}
|
||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||
}
|
||||
|
||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||
function wallFacing(angle: number): number {
|
||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||
* rather than stopping at a hard edge.
|
||||
*/
|
||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||
const shade = (point: Point, ringRadius: number): number => {
|
||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||
return value >= PLANE_CUTOFF ? value : 0;
|
||||
};
|
||||
const trace = (
|
||||
steps: number,
|
||||
at: (step: number) => { point: Point; ringRadius: number },
|
||||
): void => {
|
||||
let previous: Point | null = null;
|
||||
let previousValue = 0;
|
||||
for (let i = 0; i <= steps; i++) {
|
||||
const { point, ringRadius } = at(i);
|
||||
const value = shade(point, ringRadius);
|
||||
if (previous !== null && previousValue > 0 && value > 0) {
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||
}
|
||||
previous = point;
|
||||
previousValue = value;
|
||||
}
|
||||
};
|
||||
|
||||
for (const ringRadius of PLANE_RINGS) {
|
||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||
}
|
||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||
// closes on itself instead of trailing off into loose ends.
|
||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||
trace(24, (step) => {
|
||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function drawThickLine(image: GrayImage, x0: number, y0: number, x1: number, y1: number, value: number): void {
|
||||
image.drawLine(x0, y0, x1, y1, value);
|
||||
image.drawLine(x0 - 1, y0, x1 - 1, y1, value);
|
||||
image.drawLine(x0 + 1, y0, x1 + 1, y1, value);
|
||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||
function discDepth(radius: number): number {
|
||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||
}
|
||||
|
||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const segments = 72;
|
||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||
for (let i = 1; i <= segments; i++) {
|
||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||
previous = point;
|
||||
}
|
||||
}
|
||||
|
||||
/** A fixed mark at the top of the ring: the heading the wearer is facing. */
|
||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||
const top = projectRose(cx, cy, radius, 1, 0);
|
||||
fillTriangle(
|
||||
image,
|
||||
{ x: cx, y: top.y - 1 },
|
||||
{ x: cx - TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
{ x: cx + TICK_HALF_WIDTH, y: top.y - TICK_HEIGHT },
|
||||
value,
|
||||
);
|
||||
}
|
||||
|
||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||
const shade = Math.round(value);
|
||||
for (let y = top; y <= bottom; y++) {
|
||||
for (let x = left; x <= right; x++) {
|
||||
const point = { x: x + 0.5, y: y + 0.5 };
|
||||
const ab = edge(a, b, point);
|
||||
const bc = edge(b, c, point);
|
||||
const ca = edge(c, a, point);
|
||||
// Accept either winding, so callers don't have to order their vertices.
|
||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||
image.setPixel(x, y, shade);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||
function edge(a: Point, b: Point, p: Point): number {
|
||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
}
|
||||
@@ -610,7 +610,7 @@ export class FaceclawCommunicatorBridge {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve only once the recreated layout, image warmup, and retained frame
|
||||
* Resolve only once the recreated layout and retained frame
|
||||
* are visible. CFW's deferred-dashboard READY is emitted from this barrier.
|
||||
*/
|
||||
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@
|
||||
"version": "1.0.0",
|
||||
"private": false,
|
||||
"scripts": {
|
||||
"build": "nativescript build android"
|
||||
"build": "nativescript build android",
|
||||
"clean": "rm -rf platforms"
|
||||
},
|
||||
"dependencies": {
|
||||
"@evenrealities/pretext": "^0.1.4",
|
||||
|
||||
Reference in new issue
Block a user