feat(companion): live location sharing, Locator geofencing, trail auto-pause

Squash merge of feat/location-beacon-alerts-autopause (v1.21).

Features:
- Live Location Sharing — broadcast position as movement-gated [LOC]
  messages to a channel or contact; live shares show as map pins with
  distance/bearing in Nearby Nodes and a status-bar indicator. [LOC] is
  parsed in DMs, channel messages and room messages; DM shares name the
  sender.
- Locator (geofence) — arm a geofence around a saved waypoint or a person
  (live [LOC] or last-known position), alert on arrive/leave or near/far,
  with an optional homing beeper (gated to arrive/both modes). Arm from
  Tools > Locator, Nearby Nodes, or Waypoints; target picker lists
  favourites first, clearable via a "None" entry. Active target is drawn
  as a flag on the map.
- One active target shared across Locator / Navigate / Map via a single
  resolver (resolvePersonPos / activeTargetPos) that prefers a live
  [LOC] share over the last-advertised GPS fix.
- Follow live contacts — Navigate to a live-sharing contact follows them
  as they move and adds an ETA line; quick-share your own position from
  the Map.
- Map & status-bar upgrades — home mini-map gets a north marker, scale
  tick, and a connected trail line (was disconnected dots); status line
  shows tracked-node count, an arrow + distance to the active Locator/Nav
  target (falling back to the nearest live-tracked contact); GPS fix icon
  in the top status bar, shown only on GPS boards with GPS enabled.
- Trail auto-pause — recording freezes on stops (banking elapsed time,
  breaking the map line across the idle gap) and resumes on movement
  without ending the session.
- Streaming trail simplification — GPS points are simplified in-stream
  via a fixed-corridor (Reumann-Witkam) pass tuned for fidelity: straight
  runs collapse to their endpoints, curves stay bounded to the Min-dist
  tolerance, so the 512-point buffer covers a far longer route than a
  flat point budget would suggest.
- Collapsible Tools (Location / Comms / System sections, fold-in-place
  like Settings) and page-indicator icons on the home carousel.
- Waypoint coordinate editor — add a waypoint by scroll-editing lat/lon
  digit by digit.

Fixes:
- Critical: low-heap hang and contact loss on RAM-tight builds. Halved
  message-history scrollback rings (recovering ~14 KB free heap) and
  made contacts/channels/prefs persistence atomic (temp file + rename),
  so an interrupted save can no longer corrupt or wipe the store.
- Serial.write() bounded so a stalled USB host can't hang the device.
- Nearby Nodes: live [LOC] senders respect the type filter, sort by
  shared position, and the list refreshes so live shares bubble up.
- Map: live contacts are labelled before waypoints.
- GPS status icon hidden when GPS is off in Settings.
- Splash screen no longer truncates a pre-release tag's own dash (e.g.
  v1.21-rc1) when stripping the build's commit-hash suffix.
- Null-guarded the Locator target picker; clamped loc-share channel
  index on load.

Under the hood:
- -Os size optimisation on the e-ink and GAT562 30S solo envs.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
MarekZegare4
2026-06-25 19:04:21 +02:00
parent c935287627
commit 57774d41f3
80 changed files with 2886 additions and 340 deletions

View File

@@ -1,4 +1,9 @@
#include "ArduinoSerialInterface.h"
#include "StreamUtils.h"
// Worst-case time we'll let writeFrame() block waiting for the host to drain
// the link, before giving up on the rest of the frame. See StreamUtils.h.
#define SERIAL_WRITE_TIMEOUT_MS 300
#define RECV_STATE_IDLE 0
#define RECV_STATE_HDR_FOUND 1
@@ -32,8 +37,11 @@ size_t ArduinoSerialInterface::writeFrame(const uint8_t src[], size_t len) {
hdr[1] = (len & 0xFF); // LSB
hdr[2] = (len >> 8); // MSB
_serial->write(hdr, 3);
return _serial->write(src, len);
// Single deadline shared across header + payload, so a stalled host costs
// at most SERIAL_WRITE_TIMEOUT_MS total, not that much per write() call.
uint32_t deadline = millis() + SERIAL_WRITE_TIMEOUT_MS;
if (streamWriteUntil(*_serial, hdr, 3, deadline) < 3) return 0;
return streamWriteUntil(*_serial, src, len, deadline);
}
size_t ArduinoSerialInterface::checkRecvFrame(uint8_t dest[]) {

40
src/helpers/StreamUtils.h Normal file
View File

@@ -0,0 +1,40 @@
#pragma once
// A USB-CDC serial monitor or companion app can leave the port open (DTR
// asserted) without ever draining it. Stream::write() then blocks inside the
// platform's "TX FIFO full" wait loop for as long as that holds true — which
// is forever in practice, since the main loop()'s hardware watchdog is only
// re-armed once per iteration. streamWriteUntil() caps that wait instead of
// trusting the platform to give up.
//
// Under normal conditions (host actually reading) this is a no-op compared to
// a plain write(): writes are never asked for more than availableForWrite()
// already reports free, so they return immediately without entering the
// platform's internal wait/yield loop at all. Only a genuinely stalled host
// (zero throughput) hits the deadline; a slow-but-alive one just takes longer,
// same as a plain write() would.
#include <Arduino.h>
// Returns the number of bytes actually written; less than `len` only when the
// host stopped draining the link before `deadline_ms` (an absolute millis()
// value — share one deadline across multiple calls that make up one logical
// message, so a stall costs a fixed total budget, not one per call).
static inline size_t streamWriteUntil(Stream& s, const uint8_t* buf, size_t len, uint32_t deadline_ms) {
size_t sent = 0;
while (sent < len) {
int avail = s.availableForWrite();
if (avail <= 0) {
if ((int32_t)(millis() - deadline_ms) >= 0) break; // host stalled — give up
yield(); // let the USB/BLE background task run so it can drain the FIFO
continue;
}
size_t chunk = (size_t)avail < (len - sent) ? (size_t)avail : (len - sent);
size_t w = s.write(buf + sent, chunk);
if (w == 0) {
if ((int32_t)(millis() - deadline_ms) >= 0) break;
yield();
continue;
}
sent += w;
}
return sent;
}