Compare commits

...

9 Commits

Author SHA1 Message Date
498b89d7ba Clear tap status in dashboard after 2s display window.
formatLastTap no longer falls back to server last_tap once the
ephemeral tapDisplay timer expires.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 21:25:03 +02:00
31e539052a Unify cache polling on CACHE_STATUS and split API docs.
Replace separate accel/tap snapshot UART commands with one clients[] response
that omits unsubscribed fields; remove snapshot handlers and CLI commands.
Add goTool/docs for WebSocket streams and REST; tap-snapshot REST uses CACHE_STATUS.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 21:23:09 +02:00
a85d48320e Add list_clients WebSocket command to external API.
Lets API clients discover slave IDs and stream/notify flags before configuring per-slave accel or tap.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 21:02:41 +02:00
f512936d97 Add CACHE_STATUS UART poll and dashboard live stream.
Combine cached accel and tap in one low-overhead master command for ~16 ms
host polling. The dashboard uses a single live-stream toggle plus per-slave
accel-stream controls; fix live_stream state so polling is not cleared every
slow client refresh.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 20:57:54 +02:00
a8d4d42920 Add BMA456 tap detection with ESP-NOW notify and host snapshot API.
Slaves forward configured tap kinds to the master; goTool exposes CLI, dashboard, REST, and WebSocket with separate notify vs receive and 2s display cache.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 20:42:57 +02:00
3cb0b5bbe9 Add LiPo battery monitoring with ESP-NOW cache and dashboard API.
Slaves report pack voltages every 30s; the master caches them for fast
BATTERY_STATUS reads. goTool exposes REST/WebSocket and shows values in
the dashboard, with a nanopb fix so optional lipo submessages encode.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 20:14:28 +02:00
eb67a46158 Add LED ring control per client and broadcast over REST and WebSocket.
Solid color mode fills all ring LEDs; master routes UART commands to slaves
via ESPNOW_LED_RING. goTool exposes POST /api/led-ring, WebSocket set_led_ring,
and a dashboard LED panel with master/slave/all targets.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 19:24:55 +02:00
47c75110c9 Stream slave accel via ESP-NOW with master snapshot cache.
Slaves push BMA456 samples at 16ms when enabled; the master caches per
client and exposes ACCEL_SNAPSHOT and ACCEL_STREAM over UART. goTool adds
dashboard stream controls, HTTP accel-stream routes, and an external
WebSocket API with per-connection receive/interval and slave stream commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 19:11:36 +02:00
ba20544762 Add UART ACCEL_READ command for on-demand BMA456 samples.
Expose MessageType 24 with protobuf response (success, x, y, z in raw LSB),
firmware handler with mutex-safe I2C read, goTool `accel` CLI, and docs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-28 19:55:02 +02:00
59 changed files with 8243 additions and 317 deletions

View File

@ -24,12 +24,15 @@ go run . -port /dev/ttyUSB0 clients
|---------|--------------|-------------| |---------|--------------|-------------|
| `version` | `0x03` | Prints `version` and `git_hash` from firmware | | `version` | `0x03` | Prints `version` and `git_hash` from firmware |
| `clients` | `0x04` | Lists slaves registered on the master via ESP-NOW | | `clients` | `0x04` | Lists slaves registered on the master via ESP-NOW |
| `deadzone` | `0x06` | Get/set accelerometer deadzone LSB (`-set`, `-value`, `-client`, `-all`) |
| `tap-notify` | `0x1b` | Get/set which tap kinds (single/double/triple) notify via ESP-NOW (`-set`, `-client`, `-all`, `-single`, `-double`, `-triple`) |
| `cache-status` | `0x1d` | Subscribed accel + tap cache (`CACHE_STATUS`); one UART round-trip for 16 ms polling |
| `unicast-test` | `0x07` | Sends ESP-NOW unicast test to one slave (`-client`, `-seq`) | | `unicast-test` | `0x07` | Sends ESP-NOW unicast test to one slave (`-client`, `-seq`) |
| `test` | — | Run an automated scenario (JSON configs under `testdata/`) | | `test` | — | Run an automated scenario (JSON configs under `testdata/`) |
| `serve` | — | Web dashboard at `http://localhost:8080` (WebSocket live updates) | | `serve` | — | Web dashboard at `http://localhost:8080` (WebSocket live updates) |
| `ota` | 1619 | UART firmware upload to master; firmware then pushes to slaves via ESP-NOW | | `ota` | 1619 | UART firmware upload to master; firmware then pushes to slaves via ESP-NOW |
| `ota-progress` | 21 | Query per-slave ESP-NOW OTA progress on the master (`-client N`, default all) | | `ota-progress` | 21 | Query per-slave ESP-NOW OTA progress on the master (`-client N`, default all) |
| `led-ring` | 8 | LED ring: `-mode clear\|progress\|digit\|blink\|find-me`, … | | `led-ring` | 8 | LED ring: `-mode clear\|color\|progress\|digit\|blink\|find-me`, `-client`, `-all` |
| `find-me` | 22 | Locate pod (`-client 0` master, `>0` slave via ESP-NOW) | | `find-me` | 22 | Locate pod (`-client 0` master, `>0` slave via ESP-NOW) |
| `restart` | 23 | Reboot master or slave (`-client 0` / `>0`) | | `restart` | 23 | Reboot master or slave (`-client 0` / `>0`) |
@ -60,28 +63,43 @@ Polls the master over UART and pushes state to the browser via WebSocket (Alpine
```bash ```bash
go run . -port /dev/ttyUSB0 serve go run . -port /dev/ttyUSB0 serve
go run . -port /dev/ttyUSB0 serve -addr :8080 -interval 2s go run . -port /dev/ttyUSB0 serve -addr :8080 -interval 2s
go run . -port /dev/ttyUSB0 serve -api-addr :8081 -accel-interval 16ms
make gotool-serve PORT=/dev/ttyUSB0 make gotool-serve PORT=/dev/ttyUSB0
``` ```
Open [http://localhost:8080](http://localhost:8080) — shows master firmware info and the ESP-NOW client table from `CLIENT_INFO`. Open [http://localhost:8080](http://localhost:8080) — shows master firmware info and the ESP-NOW client table from `CLIENT_INFO`.
**Tap (dashboard):** two independent controls per slave:
| Column | Meaning |
|--------|---------|
| Tap-Notify (S/D/T) | Which tap kinds the **slave** sends to the master over ESP-NOW (UART `TAP_NOTIFY`) — does **not** poll UART |
| Tap (An/Aus) | Host **receive**: poll master tap cache (~16 ms) and show last tap for **≥2 s** |
Enable notify first, then turn receive on to see events. Same split as the external WebSocket API (`set_tap_notify` vs `set_tap_stream`).
If the UART device is unplugged or the port disappears, `serve` keeps running and retries on each poll interval; the UI shows **UART off** until the port is available again. If the UART device is unplugged or the port disappears, `serve` keeps running and retries on each poll interval; the UI shows **UART off** until the port is available again.
The dashboard can configure nodes using the same UART commands as the CLI: ### HTTP / WebSocket API
| UI action | CLI equivalent | `serve` also listens on **`:8081`** for external programs (`-api-addr`, empty to disable). Same UART as the dashboard.
|-----------|------------------|
| Nur Master | `deadzone -set -value N -client 0` |
| Einzelner Slave | `deadzone -set -value N -client ID` |
| Alle Slaves | per-slave ESP-NOW (Master bleibt unverändert; CLI `-all` setzt auch den Master) |
| Unicast test | `unicast-test -client ID` |
HTTP API (used by the web UI): `GET/POST /api/deadzone`, `POST /api/unicast-test`, `POST /api/find-me`, `POST /api/restart`, `POST /api/ota` (multipart field `firmware`, max 2 MiB). | Doc | Content |
|-----|---------|
| **[docs/API_WEBSOCKET.md](docs/API_WEBSOCKET.md)** | `ws://…:8081/ws` commands, **`accel` / `tap` push stream** format, dashboard `ws://…:8080/ws` |
| **[docs/API_REST.md](docs/API_REST.md)** | REST on `:8080` (dashboard) and `:8081` (battery, LED, service info) |
CLI:
```bash
go run . -port /dev/ttyUSB0 tap-notify -client 16 -set -single
go run . -port /dev/ttyUSB0 cache-status
```
| UI / API | Behaviour | | UI / API | Behaviour |
|----------|-----------| |----------|-----------|
| Firmware OTA card | Same as `ota` CLI; WebSocket `ota_progress` with `step` `master` (UART) then `slaves` (ESP-NOW) | | Firmware OTA card | Same as `ota` CLI; dashboard WebSocket `ota_progress` ([REST doc](docs/API_REST.md)) |
| `POST /api/ota` | Upload `.bin` to master only — slaves are updated by firmware over ESP-NOW after `OTA_END` | | `POST /api/ota` | Upload `.bin` to master — slaves updated by firmware over ESP-NOW after `OTA_END` |
```bash ```bash
go run . -port /dev/ttyUSB0 ota build/powerpod.bin go run . -port /dev/ttyUSB0 ota build/powerpod.bin

View File

@ -0,0 +1,40 @@
package main
import "sync"
// accelStreamCtl tracks which slaves the host wants to poll for accel (mirrors firmware).
type accelStreamCtl struct {
mu sync.Mutex
enabled map[uint32]struct{}
}
func newAccelStreamCtl() *accelStreamCtl {
return &accelStreamCtl{enabled: make(map[uint32]struct{})}
}
func (c *accelStreamCtl) Set(clientID uint32, on bool) {
c.mu.Lock()
defer c.mu.Unlock()
if on {
c.enabled[clientID] = struct{}{}
} else {
delete(c.enabled, clientID)
}
}
func (c *accelStreamCtl) Any() bool {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.enabled) > 0
}
func (c *accelStreamCtl) SyncFromClients(clients []ClientView) {
c.mu.Lock()
defer c.mu.Unlock()
c.enabled = make(map[uint32]struct{})
for _, cl := range clients {
if cl.AccelStream {
c.enabled[cl.ID] = struct{}{}
}
}
}

220
goTool/api_accel_stream.go Normal file
View File

@ -0,0 +1,220 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"powerpod/gotool/pb"
)
type accelStreamAPIRequest struct {
Write bool `json:"write"`
Enable bool `json:"enable"`
ClientID uint32 `json:"client_id"`
AllClients bool `json:"all_clients"`
}
type accelStreamAPIResponse struct {
Enabled bool `json:"enabled"`
ClientID uint32 `json:"client_id"`
Success bool `json:"success"`
SlavesUpdated uint32 `json:"slaves_updated"`
Error string `json:"error,omitempty"`
}
type clientAccelStreamBody struct {
Enable bool `json:"enable"`
}
func mountAccelStreamAPI(mux *http.ServeMux, link *managedSerial, hub *wsHub, ctl *accelStreamCtl) {
mux.HandleFunc("GET /api/clients/{clientID}/accel-stream", func(w http.ResponseWriter, r *http.Request) {
clientID, err := parsePathClientID(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: err.Error()})
return
}
serveAccelStreamGet(w, clientID, link, hub, ctl)
})
mux.HandleFunc("PUT /api/clients/{clientID}/accel-stream", func(w http.ResponseWriter, r *http.Request) {
clientID, err := parsePathClientID(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: err.Error()})
return
}
serveClientAccelStreamPut(w, r, clientID, link, hub, ctl)
})
mux.HandleFunc("/api/accel-stream", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
serveAccelStreamGetQuery(w, r, link, hub, ctl)
case http.MethodPost:
serveAccelStreamPost(w, r, link, hub, ctl)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
})
}
func parsePathClientID(r *http.Request) (uint32, error) {
s := r.PathValue("clientID")
if s == "" {
return 0, fmt.Errorf("client_id required")
}
v, err := strconv.ParseUint(s, 10, 32)
if err != nil || v == 0 {
return 0, fmt.Errorf("invalid client_id")
}
return uint32(v), nil
}
func applyAccelStreamClient(link *managedSerial, hub *wsHub, ctl *accelStreamCtl, clientID uint32, enable bool) accelStreamAPIResponse {
resp, err := link.AccelStream(&pb.AccelStreamRequest{
Write: true,
Enable: enable,
ClientId: clientID,
})
if err != nil {
return accelStreamAPIResponse{
ClientID: clientID,
Error: err.Error(),
}
}
out := accelStreamAPIResponse{
Enabled: enable,
ClientID: resp.GetClientId(),
Success: resp.GetSuccess(),
SlavesUpdated: resp.GetSlavesUpdated(),
}
if resp.GetSuccess() {
if ctl != nil {
ctl.Set(clientID, enable)
}
if hub != nil {
hub.patchClientAccelStream(clientID, enable)
}
} else {
out.Enabled = resp.GetEnabled()
}
return out
}
func serveAccelStreamGet(w http.ResponseWriter, clientID uint32, link *managedSerial, hub *wsHub, ctl *accelStreamCtl) {
resp, err := link.AccelStreamPoll(&pb.AccelStreamRequest{
Write: false,
ClientId: clientID,
})
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, accelStreamAPIResponse{
ClientID: clientID,
Error: err.Error(),
})
return
}
if ctl != nil {
ctl.Set(clientID, resp.GetEnabled())
}
writeJSON(w, http.StatusOK, accelStreamAPIResponse{
Enabled: resp.GetEnabled(),
ClientID: resp.GetClientId(),
Success: resp.GetSuccess(),
})
}
func serveAccelStreamGetQuery(w http.ResponseWriter, r *http.Request, link *managedSerial, hub *wsHub, ctl *accelStreamCtl) {
clientID, err := parseUintQuery(r, "client_id", 0)
if err != nil || clientID == 0 {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: "client_id required"})
return
}
serveAccelStreamGet(w, clientID, link, hub, ctl)
}
func serveClientAccelStreamPut(w http.ResponseWriter, r *http.Request, clientID uint32, link *managedSerial, hub *wsHub, ctl *accelStreamCtl) {
var body clientAccelStreamBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: "invalid JSON"})
return
}
out := applyAccelStreamClient(link, hub, ctl, clientID, body.Enable)
status := http.StatusOK
if out.Error != "" {
status = http.StatusServiceUnavailable
} else if !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}
func serveAccelStreamPost(w http.ResponseWriter, r *http.Request, link *managedSerial, hub *wsHub, ctl *accelStreamCtl) {
var body accelStreamAPIRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: "invalid JSON"})
return
}
if body.AllClients {
updated, err := applyAccelStreamAll(link, body.Enable)
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, accelStreamAPIResponse{Error: err.Error()})
return
}
clients, _ := link.listClientsPoll()
for _, c := range clients {
if ctl != nil {
ctl.Set(c.GetId(), body.Enable)
}
if hub != nil {
hub.patchClientAccelStream(c.GetId(), body.Enable)
}
}
writeJSON(w, http.StatusOK, accelStreamAPIResponse{
Enabled: body.Enable,
Success: updated > 0,
SlavesUpdated: updated,
})
return
}
if body.ClientID == 0 {
writeJSON(w, http.StatusBadRequest, accelStreamAPIResponse{Error: "client_id required"})
return
}
out := applyAccelStreamClient(link, hub, ctl, body.ClientID, body.Enable)
status := http.StatusOK
if out.Error != "" || !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}
func applyAccelStreamAll(link *managedSerial, enable bool) (uint32, error) {
clients, err := link.listClients()
if err != nil {
return 0, err
}
var updated uint32
for _, c := range clients {
resp, err := link.AccelStream(&pb.AccelStreamRequest{
Write: true,
Enable: enable,
ClientId: c.GetId(),
})
if err != nil {
continue
}
if resp.GetSuccess() {
updated++
}
}
if len(clients) == 0 {
return 0, fmt.Errorf("no slaves registered")
}
if updated == 0 {
return 0, fmt.Errorf("accel stream not applied to any slave")
}
return updated, nil
}

60
goTool/api_battery.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"encoding/json"
"net/http"
"strconv"
)
func mountBatteryAPI(mux *http.ServeMux, link *managedSerial) {
mux.HandleFunc("/api/battery", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
serveBatteryGet(w, r, link)
case http.MethodPost:
serveBatteryPost(w, r, link)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
})
}
func serveBatteryGet(w http.ResponseWriter, r *http.Request, link *managedSerial) {
req := batteryAPIRequest{}
if v := r.URL.Query().Get("all_clients"); v == "1" || v == "true" {
req.AllClients = true
}
if s := r.URL.Query().Get("client_id"); s != "" {
id, err := strconv.ParseUint(s, 10, 32)
if err != nil {
writeJSON(w, http.StatusBadRequest, batteryAPIResponse{Error: "invalid client_id"})
return
}
req.ClientID = uint32(id)
} else if !req.AllClients {
req.AllClients = true
}
out := applyBatteryStatus(link, req)
status := http.StatusOK
if out.Error != "" || !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}
func serveBatteryPost(w http.ResponseWriter, r *http.Request, link *managedSerial) {
var body batteryAPIRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, batteryAPIResponse{Error: "invalid JSON"})
return
}
if !body.AllClients && body.ClientID == 0 {
body.AllClients = true
}
out := applyBatteryStatus(link, body)
status := http.StatusOK
if out.Error != "" || !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}

36
goTool/api_led_ring.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"encoding/json"
"net/http"
)
func mountLedRingAPI(mux *http.ServeMux, link *managedSerial) {
mux.HandleFunc("/api/led-ring", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
serveLedRingPost(w, r, link)
})
}
func serveLedRingPost(w http.ResponseWriter, r *http.Request, link *managedSerial) {
var body ledRingAPIRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, ledRingAPIResponse{Error: "invalid JSON"})
return
}
if body.Mode == "" {
writeJSON(w, http.StatusBadRequest, ledRingAPIResponse{Error: "mode required"})
return
}
out := applyLedRing(link, body)
status := http.StatusOK
if out.Error != "" {
status = http.StatusServiceUnavailable
} else if !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}

52
goTool/api_live_stream.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"encoding/json"
"net/http"
)
type liveStreamAPIResponse struct {
Enabled bool `json:"enabled"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
func mountLiveStreamAPI(mux *http.ServeMux, hub *wsHub) {
mux.HandleFunc("/api/live-stream", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
serveLiveStreamGet(w, hub)
case http.MethodPut:
serveLiveStreamPut(w, r, hub)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
})
}
func serveLiveStreamGet(w http.ResponseWriter, hub *wsHub) {
enabled := false
if hub != nil {
enabled = hub.liveStreamEnabled()
}
writeJSON(w, http.StatusOK, liveStreamAPIResponse{Enabled: enabled, Success: true})
}
func serveLiveStreamPut(w http.ResponseWriter, r *http.Request, hub *wsHub) {
var body struct {
Enable bool `json:"enable"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, liveStreamAPIResponse{Error: "invalid JSON"})
return
}
if hub != nil {
hub.patchLiveStream(body.Enable)
}
writeJSON(w, http.StatusOK, liveStreamAPIResponse{
Enabled: body.Enable,
Success: true,
})
}

View File

@ -67,7 +67,12 @@ type otaAPIResponse struct {
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
func mountServeAPI(mux *http.ServeMux, link *managedSerial, hub *wsHub) { func mountServeAPI(mux *http.ServeMux, link *managedSerial, hub *wsHub, streamCtl *accelStreamCtl, tapCtl *tapNotifyCtl) {
mountLiveStreamAPI(mux, hub)
mountAccelStreamAPI(mux, link, hub, streamCtl)
mountTapAPI(mux, link, hub, tapCtl)
mountLedRingAPI(mux, link)
mountBatteryAPI(mux, link)
mux.HandleFunc("/api/deadzone", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/api/deadzone", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:

943
goTool/api_stream.go Normal file
View File

@ -0,0 +1,943 @@
package main
import (
"context"
"encoding/json"
"errors"
"log"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"powerpod/gotool/pb"
)
const (
defaultAccelStreamInterval = 16 * time.Millisecond
minAPIStreamInterval = 1 * time.Millisecond
maxAPIStreamInterval = 10 * time.Second
// How long tap events stay in API push/cache after first sight (matches dashboard).
apiTapDisplayMinMs = 2000
)
// AccelClientSample is one slave's cached accel on the master.
type AccelClientSample struct {
ClientID uint32 `json:"client_id"`
Valid bool `json:"valid"`
X int32 `json:"x,omitempty"`
Y int32 `json:"y,omitempty"`
Z int32 `json:"z,omitempty"`
AgeMs uint32 `json:"age_ms,omitempty"`
}
// AccelStreamMessage is sent to external WebSocket clients (hello + accel samples).
type AccelStreamMessage struct {
Type string `json:"type"` // "hello" | "accel"
Serial string `json:"serial_port,omitempty"`
IntervalMs int `json:"interval_ms,omitempty"`
TapDisplayMinMs int `json:"tap_display_min_ms,omitempty"`
Commands []string `json:"commands,omitempty"`
Note string `json:"note,omitempty"`
T int64 `json:"t,omitempty"` // Unix nanoseconds
Success bool `json:"success,omitempty"`
Clients []AccelClientSample `json:"clients,omitempty"`
Error string `json:"error,omitempty"`
}
// StreamStatusMessage is the reply to set_stream / get_stream (this connection).
type StreamStatusMessage struct {
Type string `json:"type"` // "stream_status"
ReceiveAccel bool `json:"receive_accel"`
IntervalMs int `json:"interval_ms"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
// AccelStreamStatusMessage is the reply to set_accel_stream / get_accel_stream (slave).
type AccelStreamStatusMessage struct {
Type string `json:"type"` // "accel_stream_status"
ClientID uint32 `json:"client_id"`
Enabled bool `json:"enabled"`
Success bool `json:"success"`
SlavesUpdated uint32 `json:"slaves_updated,omitempty"`
Error string `json:"error,omitempty"`
}
// TapClientEvent is one tap visible to API clients (fresh or within tap_display_min_ms).
type TapClientEvent struct {
ClientID uint32 `json:"client_id"`
Valid bool `json:"valid"`
Kind string `json:"kind,omitempty"` // single | double | triple
AgeMs uint32 `json:"age_ms,omitempty"`
ShownAtMs int64 `json:"shown_at_ms,omitempty"` // Unix ms when API first saw this tap
}
// TapStreamMessage is pushed to external WebSocket clients when receive_tap is on.
type TapStreamMessage struct {
Type string `json:"type"` // "tap"
T int64 `json:"t,omitempty"`
Success bool `json:"success,omitempty"`
Events []TapClientEvent `json:"events,omitempty"`
Error string `json:"error,omitempty"`
}
// TapStreamStatusMessage is the reply to set_tap_stream / get_tap_stream (this connection).
type TapStreamStatusMessage struct {
Type string `json:"type"` // "tap_stream_status"
ReceiveTap bool `json:"receive_tap"`
IntervalMs int `json:"interval_ms"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
// APIClientInfo is one registered slave (or slot) from CLIENT_INFO.
type APIClientInfo struct {
ID uint32 `json:"id"`
MAC string `json:"mac"`
Version uint32 `json:"version"`
Available bool `json:"available"`
Used bool `json:"used"`
LastPing uint32 `json:"last_ping"`
LastSuccessPing uint32 `json:"last_success_ping"`
AccelStream bool `json:"accel_stream"`
TapNotifySingle bool `json:"tap_notify_single"`
TapNotifyDouble bool `json:"tap_notify_double"`
TapNotifyTriple bool `json:"tap_notify_triple"`
}
// ClientListMessage is the reply to list_clients.
type ClientListMessage struct {
Type string `json:"type"` // "client_list"
Success bool `json:"success"`
Clients []APIClientInfo `json:"clients,omitempty"`
Error string `json:"error,omitempty"`
}
// TapNotifyStatusMessage is the reply to set_tap_notify / get_tap_notify (slave).
type TapNotifyStatusMessage struct {
Type string `json:"type"` // "tap_notify_status"
ClientID uint32 `json:"client_id"`
Single bool `json:"single"`
DoubleTap bool `json:"double_tap"`
Triple bool `json:"triple"`
Success bool `json:"success"`
SlavesUpdated uint32 `json:"slaves_updated,omitempty"`
Error string `json:"error,omitempty"`
}
type accelWSCommand struct {
Type string `json:"type"`
ClientID uint32 `json:"client_id"`
Enable *bool `json:"enable"`
IntervalMs *int `json:"interval_ms"`
Single *bool `json:"single"`
DoubleTap *bool `json:"double_tap"`
Triple *bool `json:"triple"`
AllClients bool `json:"all_clients"`
}
type APIInfoResponse struct {
Name string `json:"name"`
Version string `json:"version"`
SerialPort string `json:"serial_port"`
WebSocket string `json:"websocket"`
DefaultIntervalMs int `json:"default_interval_ms"`
MinIntervalMs int `json:"min_interval_ms"`
MaxIntervalMs int `json:"max_interval_ms"`
TapDisplayMinMs int `json:"tap_display_min_ms"`
Description string `json:"description"`
}
type cachedTapEvent struct {
kind string
shownAt time.Time
}
type wsSubscriber struct {
conn *websocket.Conn
receiveAccel bool
receiveTap bool
interval time.Duration
lastAccelSent time.Time
lastTapSent time.Time
}
type accelStreamHub struct {
mu sync.RWMutex
clients map[*websocket.Conn]*wsSubscriber
defaultInterval time.Duration
configChanged chan struct{}
recentTaps map[uint32]cachedTapEvent
}
func newAccelStreamHub(defaultInterval time.Duration) *accelStreamHub {
return &accelStreamHub{
clients: make(map[*websocket.Conn]*wsSubscriber),
defaultInterval: defaultInterval,
configChanged: make(chan struct{}, 1),
}
}
func (h *accelStreamHub) notifyConfigChanged() {
select {
case h.configChanged <- struct{}{}:
default:
}
}
func clampAPIInterval(d time.Duration) time.Duration {
if d < minAPIStreamInterval {
return minAPIStreamInterval
}
if d > maxAPIStreamInterval {
return maxAPIStreamInterval
}
return d
}
func (h *accelStreamHub) register(conn *websocket.Conn, portName string) *wsSubscriber {
sub := &wsSubscriber{
conn: conn,
receiveAccel: false,
interval: h.defaultInterval,
}
h.mu.Lock()
h.clients[conn] = sub
h.mu.Unlock()
hello := AccelStreamMessage{
Type: "hello",
Serial: portName,
IntervalMs: int(h.defaultInterval / time.Millisecond),
TapDisplayMinMs: apiTapDisplayMinMs,
Note: "set_tap_notify configures slave S/D/T only; set_tap_stream enables tap polling/push",
Commands: []string{
"list_clients",
"set_stream", "get_stream", "set_accel_stream", "get_accel_stream",
"set_tap_stream", "get_tap_stream", "set_tap_notify", "get_tap_notify",
"set_led_ring", "get_battery",
},
}
if data, err := json.Marshal(hello); err == nil {
_ = conn.WriteMessage(websocket.TextMessage, data)
}
return sub
}
func (h *accelStreamHub) unregister(conn *websocket.Conn) {
h.mu.Lock()
delete(h.clients, conn)
anyTap := false
for _, sub := range h.clients {
if sub.receiveTap {
anyTap = true
break
}
}
if !anyTap {
h.recentTaps = nil
}
h.mu.Unlock()
h.notifyConfigChanged()
}
func (h *accelStreamHub) anyWantsAccel() bool {
h.mu.RLock()
defer h.mu.RUnlock()
for _, sub := range h.clients {
if sub.receiveAccel {
return true
}
}
return false
}
func (h *accelStreamHub) anyWantsTap() bool {
h.mu.RLock()
defer h.mu.RUnlock()
for _, sub := range h.clients {
if sub.receiveTap {
return true
}
}
return false
}
func (h *accelStreamHub) minWantedInterval() time.Duration {
h.mu.RLock()
defer h.mu.RUnlock()
var min time.Duration
for _, sub := range h.clients {
if !sub.receiveAccel && !sub.receiveTap {
continue
}
if min == 0 || sub.interval < min {
min = sub.interval
}
}
if min == 0 {
return h.defaultInterval
}
return min
}
func (h *accelStreamHub) setStream(sub *wsSubscriber, enable bool, intervalMs *int) StreamStatusMessage {
h.mu.Lock()
sub.receiveAccel = enable
if intervalMs != nil {
sub.interval = clampAPIInterval(time.Duration(*intervalMs) * time.Millisecond)
}
ms := int(sub.interval / time.Millisecond)
h.mu.Unlock()
h.notifyConfigChanged()
return StreamStatusMessage{
Type: "stream_status",
ReceiveAccel: enable,
IntervalMs: ms,
Success: true,
}
}
func (h *accelStreamHub) getStream(sub *wsSubscriber) StreamStatusMessage {
h.mu.RLock()
defer h.mu.RUnlock()
return StreamStatusMessage{
Type: "stream_status",
ReceiveAccel: sub.receiveAccel,
IntervalMs: int(sub.interval / time.Millisecond),
Success: true,
}
}
func (h *accelStreamHub) setTapStream(sub *wsSubscriber, enable bool, intervalMs *int) TapStreamStatusMessage {
h.mu.Lock()
sub.receiveTap = enable
if !enable {
h.recentTaps = nil
}
if intervalMs != nil {
sub.interval = clampAPIInterval(time.Duration(*intervalMs) * time.Millisecond)
}
ms := int(sub.interval / time.Millisecond)
h.mu.Unlock()
h.notifyConfigChanged()
return TapStreamStatusMessage{
Type: "tap_stream_status",
ReceiveTap: enable,
IntervalMs: ms,
Success: true,
}
}
func (h *accelStreamHub) getTapStream(sub *wsSubscriber) TapStreamStatusMessage {
h.mu.RLock()
defer h.mu.RUnlock()
return TapStreamStatusMessage{
Type: "tap_stream_status",
ReceiveTap: sub.receiveTap,
IntervalMs: int(sub.interval / time.Millisecond),
Success: true,
}
}
func (h *accelStreamHub) ingestTapEvents(incoming []TapClientEvent) []TapClientEvent {
h.mu.Lock()
defer h.mu.Unlock()
now := time.Now()
if h.recentTaps == nil {
h.recentTaps = make(map[uint32]cachedTapEvent)
}
for _, e := range incoming {
if !e.Valid || e.Kind == "" {
continue
}
h.recentTaps[e.ClientID] = cachedTapEvent{kind: e.Kind, shownAt: now}
}
return h.activeTapEventsLocked(now)
}
func (h *accelStreamHub) activeTapEventsLocked(now time.Time) []TapClientEvent {
if len(h.recentTaps) == 0 {
return nil
}
cutoff := now.Add(-apiTapDisplayMinMs * time.Millisecond)
out := make([]TapClientEvent, 0, len(h.recentTaps))
for id, ev := range h.recentTaps {
if ev.shownAt.Before(cutoff) {
delete(h.recentTaps, id)
continue
}
shownAtMs := ev.shownAt.UnixMilli()
out = append(out, TapClientEvent{
ClientID: id,
Valid: true,
Kind: ev.kind,
AgeMs: uint32(now.Sub(ev.shownAt).Milliseconds()),
ShownAtMs: shownAtMs,
})
}
return out
}
func (h *accelStreamHub) deliver(msg AccelStreamMessage) {
data, err := json.Marshal(msg)
if err != nil {
return
}
now := time.Now()
h.mu.Lock()
defer h.mu.Unlock()
for conn, sub := range h.clients {
if !sub.receiveAccel {
continue
}
if !sub.lastAccelSent.IsZero() && now.Sub(sub.lastAccelSent) < sub.interval {
continue
}
sub.lastAccelSent = now
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
delete(h.clients, conn)
_ = conn.Close()
}
}
}
func (h *accelStreamHub) deliverTap(msg TapStreamMessage) {
data, err := json.Marshal(msg)
if err != nil {
return
}
now := time.Now()
h.mu.Lock()
defer h.mu.Unlock()
for conn, sub := range h.clients {
if !sub.receiveTap {
continue
}
if !sub.lastTapSent.IsZero() && now.Sub(sub.lastTapSent) < sub.interval {
continue
}
sub.lastTapSent = now
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
delete(h.clients, conn)
_ = conn.Close()
}
}
}
func runAccelStreamer(link *managedSerial, hub *accelStreamHub, dash *wsHub, ctl *accelStreamCtl, tapCtl *tapNotifyCtl, stop <-chan struct{}) {
var ticker *time.Ticker
var tick <-chan time.Time
resetTicker := func() {
if ticker != nil {
ticker.Stop()
}
interval := hub.minWantedInterval()
ticker = time.NewTicker(interval)
tick = ticker.C
}
resetTicker()
defer func() {
if ticker != nil {
ticker.Stop()
}
}()
for {
select {
case <-stop:
return
case <-hub.configChanged:
resetTicker()
case <-tick:
wantAccel := hub.anyWantsAccel() && accelStreamPollingActive(dash, ctl)
wantTap := hub.anyWantsTap()
if !wantAccel && !wantTap {
continue
}
now := time.Now().UnixNano()
cache, err := link.readCacheStatusPoll()
if errors.Is(err, errUARTBusy) {
if wantAccel {
hub.deliver(AccelStreamMessage{
Type: "accel",
T: now,
Success: false,
Error: "uart busy",
})
}
if wantTap {
hub.deliverTap(TapStreamMessage{
Type: "tap",
T: now,
Success: false,
Error: "uart busy",
})
}
continue
}
if err != nil {
if wantAccel {
hub.deliver(AccelStreamMessage{
Type: "accel",
T: now,
Success: false,
Error: err.Error(),
})
}
if wantTap {
hub.deliverTap(TapStreamMessage{
Type: "tap",
T: now,
Success: false,
Error: err.Error(),
})
}
continue
}
if wantAccel {
samples := accelSamplesFromCacheStatus(cache)
clients := make([]AccelClientSample, 0, len(samples))
for _, s := range samples {
clients = append(clients, AccelClientSample{
ClientID: s.GetClientId(),
Valid: s.GetValid(),
X: s.GetX(),
Y: s.GetY(),
Z: s.GetZ(),
AgeMs: s.GetAgeMs(),
})
}
hub.deliver(AccelStreamMessage{
Type: "accel",
T: now,
Success: true,
Clients: clients,
})
}
if wantTap {
events := tapEventsFromCacheStatus(cache)
fresh := make([]TapClientEvent, 0, len(events))
for _, e := range events {
if !e.GetValid() {
continue
}
fresh = append(fresh, TapClientEvent{
ClientID: e.GetClientId(),
Valid: true,
Kind: tapKindLabelPB(e.GetKind()),
AgeMs: e.GetAgeMs(),
})
}
visible := hub.ingestTapEvents(fresh)
if len(visible) > 0 {
hub.deliverTap(TapStreamMessage{
Type: "tap",
T: now,
Success: true,
Events: visible,
})
}
}
}
}
}
func accelStreamPollingActive(dash *wsHub, ctl *accelStreamCtl) bool {
if ctl != nil && ctl.Any() {
return true
}
return dash != nil && dash.anyAccelStreamEnabled()
}
func writeStreamStatus(conn *websocket.Conn, msg StreamStatusMessage) {
data, err := json.Marshal(msg)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func writeBatteryStatus(conn *websocket.Conn, out batteryAPIResponse) {
out.Type = "battery_status"
data, err := json.Marshal(out)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func writeLedRingStatus(conn *websocket.Conn, out ledRingAPIResponse) {
out.Type = "led_ring_status"
data, err := json.Marshal(out)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func writeAccelStreamStatus(conn *websocket.Conn, out accelStreamAPIResponse) {
msg := AccelStreamStatusMessage{
Type: "accel_stream_status",
ClientID: out.ClientID,
Enabled: out.Enabled,
Success: out.Success,
SlavesUpdated: out.SlavesUpdated,
Error: out.Error,
}
data, err := json.Marshal(msg)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func writeTapStreamStatus(conn *websocket.Conn, msg TapStreamStatusMessage) {
data, err := json.Marshal(msg)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func clientInfoToAPI(c *pb.ClientInfo) APIClientInfo {
return APIClientInfo{
ID: c.GetId(),
MAC: formatMAC(c.GetMac()),
Version: c.GetVersion(),
Available: c.GetAvailable(),
Used: c.GetUsed(),
LastPing: c.GetLastPing(),
LastSuccessPing: c.GetLastSuccessPing(),
AccelStream: c.GetAccelStreamEnabled(),
TapNotifySingle: c.GetTapNotifySingle(),
TapNotifyDouble: c.GetTapNotifyDouble(),
TapNotifyTriple: c.GetTapNotifyTriple(),
}
}
func writeClientList(conn *websocket.Conn, msg ClientListMessage) {
data, err := json.Marshal(msg)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func writeTapNotifyStatus(conn *websocket.Conn, out tapNotifyAPIResponse) {
msg := TapNotifyStatusMessage{
Type: "tap_notify_status",
ClientID: out.ClientID,
Single: out.Single,
DoubleTap: out.DoubleTap,
Triple: out.Triple,
Success: out.Success,
SlavesUpdated: out.SlavesUpdated,
Error: out.Error,
}
data, err := json.Marshal(msg)
if err != nil {
return
}
_ = conn.WriteMessage(websocket.TextMessage, data)
}
func applyTapNotifyClientWS(link *managedSerial, dash *wsHub, tapCtl *tapNotifyCtl, clientID uint32, single, doubleTap, triple bool) tapNotifyAPIResponse {
resp, err := link.TapNotify(&pb.TapNotifyRequest{
Write: true,
ClientId: clientID,
Single: single,
DoubleTap: doubleTap,
Triple: triple,
})
if err != nil {
return tapNotifyAPIResponse{ClientID: clientID, Error: err.Error()}
}
out := tapNotifyAPIResponse{
ClientID: resp.GetClientId(),
Success: resp.GetSuccess(),
SlavesUpdated: resp.GetSlavesUpdated(),
Single: resp.GetSingle(),
DoubleTap: resp.GetDoubleTap(),
Triple: resp.GetTriple(),
}
if resp.GetSuccess() {
if tapCtl != nil {
tapCtl.Set(clientID, single, doubleTap, triple)
}
if dash != nil {
dash.patchClientTapNotify(clientID, single, doubleTap, triple)
}
}
return out
}
func handleAccelWSCommand(conn *websocket.Conn, sub *wsSubscriber, data []byte, link *managedSerial, dash *wsHub, ctl *accelStreamCtl, tapCtl *tapNotifyCtl, hub *accelStreamHub) {
var cmd accelWSCommand
if err := json.Unmarshal(data, &cmd); err != nil {
writeStreamStatus(conn, StreamStatusMessage{Type: "stream_status", Error: "invalid JSON"})
return
}
switch cmd.Type {
case "list_clients":
clients, err := link.listClientsPoll()
if err != nil {
writeClientList(conn, ClientListMessage{
Type: "client_list",
Error: err.Error(),
})
return
}
out := make([]APIClientInfo, 0, len(clients))
for _, c := range clients {
out = append(out, clientInfoToAPI(c))
}
writeClientList(conn, ClientListMessage{
Type: "client_list",
Success: true,
Clients: out,
})
case "set_stream":
if cmd.Enable == nil {
writeStreamStatus(conn, StreamStatusMessage{
Type: "stream_status",
Error: "enable required",
})
return
}
writeStreamStatus(conn, hub.setStream(sub, *cmd.Enable, cmd.IntervalMs))
case "get_stream":
writeStreamStatus(conn, hub.getStream(sub))
case "set_accel_stream":
if cmd.ClientID == 0 {
writeAccelStreamStatus(conn, accelStreamAPIResponse{Error: "client_id required"})
return
}
if cmd.Enable == nil {
writeAccelStreamStatus(conn, accelStreamAPIResponse{
ClientID: cmd.ClientID,
Error: "enable required",
})
return
}
writeAccelStreamStatus(conn, applyAccelStreamClient(link, dash, ctl, cmd.ClientID, *cmd.Enable))
case "get_accel_stream":
if cmd.ClientID == 0 {
writeAccelStreamStatus(conn, accelStreamAPIResponse{Error: "client_id required"})
return
}
resp, err := link.AccelStreamPoll(&pb.AccelStreamRequest{
Write: false,
ClientId: cmd.ClientID,
})
if err != nil {
writeAccelStreamStatus(conn, accelStreamAPIResponse{
ClientID: cmd.ClientID,
Error: err.Error(),
})
return
}
if ctl != nil {
ctl.Set(cmd.ClientID, resp.GetEnabled())
}
writeAccelStreamStatus(conn, accelStreamAPIResponse{
Enabled: resp.GetEnabled(),
ClientID: resp.GetClientId(),
Success: resp.GetSuccess(),
})
case "set_tap_stream":
if cmd.Enable == nil {
writeTapStreamStatus(conn, TapStreamStatusMessage{
Type: "tap_stream_status",
Error: "enable required",
})
return
}
writeTapStreamStatus(conn, hub.setTapStream(sub, *cmd.Enable, cmd.IntervalMs))
case "get_tap_stream":
writeTapStreamStatus(conn, hub.getTapStream(sub))
case "set_tap_notify":
if cmd.AllClients {
if cmd.Single == nil || cmd.DoubleTap == nil || cmd.Triple == nil {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{Error: "single, double_tap, triple required"})
return
}
updated, err := applyTapNotifyAll(link, dash, tapCtl, *cmd.Single, *cmd.DoubleTap, *cmd.Triple)
if err != nil {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{Error: err.Error()})
return
}
writeTapNotifyStatus(conn, tapNotifyAPIResponse{
Success: updated > 0,
SlavesUpdated: updated,
Single: *cmd.Single,
DoubleTap: *cmd.DoubleTap,
Triple: *cmd.Triple,
})
return
}
if cmd.ClientID == 0 {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{Error: "client_id required"})
return
}
if cmd.Single == nil || cmd.DoubleTap == nil || cmd.Triple == nil {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{
ClientID: cmd.ClientID,
Error: "single, double_tap, triple required",
})
return
}
writeTapNotifyStatus(conn, applyTapNotifyClientWS(link, dash, tapCtl, cmd.ClientID, *cmd.Single, *cmd.DoubleTap, *cmd.Triple))
case "get_tap_notify":
if cmd.ClientID == 0 {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{Error: "client_id required"})
return
}
resp, err := link.TapNotifyPoll(&pb.TapNotifyRequest{
Write: false,
ClientId: cmd.ClientID,
})
if err != nil {
writeTapNotifyStatus(conn, tapNotifyAPIResponse{
ClientID: cmd.ClientID,
Error: err.Error(),
})
return
}
if tapCtl != nil {
tapCtl.Set(cmd.ClientID, resp.GetSingle(), resp.GetDoubleTap(), resp.GetTriple())
}
writeTapNotifyStatus(conn, tapNotifyAPIResponse{
ClientID: cmd.ClientID,
Success: resp.GetSuccess(),
Single: resp.GetSingle(),
DoubleTap: resp.GetDoubleTap(),
Triple: resp.GetTriple(),
})
case "set_led_ring":
var body ledRingAPIRequest
if err := json.Unmarshal(data, &body); err != nil {
writeLedRingStatus(conn, ledRingAPIResponse{Error: "invalid JSON"})
return
}
if body.Mode == "" {
writeLedRingStatus(conn, ledRingAPIResponse{Error: "mode required"})
return
}
writeLedRingStatus(conn, applyLedRing(link, body))
case "get_battery":
var body batteryAPIRequest
if err := json.Unmarshal(data, &body); err != nil {
writeBatteryStatus(conn, batteryAPIResponse{Error: "invalid JSON"})
return
}
if !body.AllClients && body.ClientID == 0 {
body.AllClients = true
}
writeBatteryStatus(conn, applyBatteryStatus(link, body))
default:
writeStreamStatus(conn, StreamStatusMessage{
Type: "stream_status",
Error: "unknown type (list_clients, set_stream, get_stream, set_accel_stream, get_accel_stream, set_tap_stream, get_tap_stream, set_tap_notify, get_tap_notify, set_led_ring, get_battery)",
})
}
}
func serveExternalWS(conn *websocket.Conn, link *managedSerial, dash *wsHub, ctl *accelStreamCtl, tapCtl *tapNotifyCtl, portName string, hub *accelStreamHub) {
sub := hub.register(conn, portName)
defer hub.unregister(conn)
defer conn.Close()
for {
_, data, err := conn.ReadMessage()
if err != nil {
return
}
handleAccelWSCommand(conn, sub, data, link, dash, ctl, tapCtl, hub)
}
}
func mountExternalAPI(mux *http.ServeMux, portName string, defaultInterval time.Duration, hub *accelStreamHub, link *managedSerial, dash *wsHub, ctl *accelStreamCtl, tapCtl *tapNotifyCtl) {
defMs := int(defaultInterval / time.Millisecond)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && r.URL.Path != "/api/v1" && r.URL.Path != "/api/v1/" {
http.NotFound(w, r)
return
}
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
writeJSON(w, http.StatusOK, APIInfoResponse{
Name: "powerpod-external-api",
Version: "1",
SerialPort: portName,
WebSocket: "/ws",
DefaultIntervalMs: defMs,
MinIntervalMs: int(minAPIStreamInterval / time.Millisecond),
MaxIntervalMs: int(maxAPIStreamInterval / time.Millisecond),
TapDisplayMinMs: apiTapDisplayMinMs,
Description: "WebSocket: set_accel_stream + set_stream for accel; set_tap_notify (slave S/D/T) then set_tap_stream for tap events (shown ≥2s)",
})
})
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("api websocket upgrade: %v", err)
return
}
serveExternalWS(conn, link, dash, ctl, tapCtl, portName, hub)
})
}
func runAPIServer(portName string, link *managedSerial, addr string, defaultInterval time.Duration, dash *wsHub, ctl *accelStreamCtl, tapCtl *tapNotifyCtl, stop <-chan struct{}) *http.Server {
hub := newAccelStreamHub(defaultInterval)
go runAccelStreamer(link, hub, dash, ctl, tapCtl, stop)
mux := http.NewServeMux()
mountExternalAPI(mux, portName, defaultInterval, hub, link, dash, ctl, tapCtl)
mountLedRingAPI(mux, link)
mountBatteryAPI(mux, link)
srv := &http.Server{Addr: addr, Handler: mux}
go func() {
log.Printf("external API http://localhost%s WebSocket ws://localhost%s/ws (default stream interval %s, per-client via set_stream / set_tap_stream)",
addr, addr, defaultInterval.String())
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("external API server: %v", err)
}
}()
return srv
}
func shutdownAPIServer(srv *http.Server) {
if srv == nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
_ = srv.Shutdown(ctx)
}

219
goTool/api_tap.go Normal file
View File

@ -0,0 +1,219 @@
package main
import (
"encoding/json"
"fmt"
"net/http"
"powerpod/gotool/pb"
)
type tapNotifyAPIRequest struct {
Write bool `json:"write"`
ClientID uint32 `json:"client_id"`
AllClients bool `json:"all_clients"`
Single bool `json:"single"`
DoubleTap bool `json:"double_tap"`
Triple bool `json:"triple"`
}
type tapNotifyAPIResponse struct {
ClientID uint32 `json:"client_id"`
Success bool `json:"success"`
SlavesUpdated uint32 `json:"slaves_updated"`
Single bool `json:"single"`
DoubleTap bool `json:"double_tap"`
Triple bool `json:"triple"`
Error string `json:"error,omitempty"`
}
type tapSnapshotAPIResponse struct {
Events []tapEventView `json:"events"`
Error string `json:"error,omitempty"`
}
type tapEventView struct {
ClientID uint32 `json:"client_id"`
Kind string `json:"kind"`
AgeMs uint32 `json:"age_ms"`
}
func mountTapAPI(mux *http.ServeMux, link *managedSerial, hub *wsHub, tapCtl *tapNotifyCtl) {
mux.HandleFunc("GET /api/clients/{clientID}/tap-notify", func(w http.ResponseWriter, r *http.Request) {
clientID, err := parsePathClientID(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: err.Error()})
return
}
serveTapNotifyGet(w, clientID, link)
})
mux.HandleFunc("PUT /api/clients/{clientID}/tap-notify", func(w http.ResponseWriter, r *http.Request) {
clientID, err := parsePathClientID(r)
if err != nil {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: err.Error()})
return
}
serveClientTapNotifyPut(w, r, clientID, link, hub, tapCtl)
})
mux.HandleFunc("/api/tap-notify", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
serveTapNotifyGetQuery(w, r, link)
case http.MethodPost:
serveTapNotifyPost(w, r, link, hub, tapCtl)
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
})
mux.HandleFunc("/api/tap-snapshot", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
serveTapSnapshotGet(w, r, link)
})
}
func applyTapNotifyClient(link *managedSerial, hub *wsHub, tapCtl *tapNotifyCtl, clientID uint32, single, doubleTap, triple bool) tapNotifyAPIResponse {
return applyTapNotifyClientWS(link, hub, tapCtl, clientID, single, doubleTap, triple)
}
func serveTapNotifyGet(w http.ResponseWriter, clientID uint32, link *managedSerial) {
resp, err := link.TapNotifyPoll(&pb.TapNotifyRequest{
Write: false,
ClientId: clientID,
})
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, tapNotifyAPIResponse{
ClientID: clientID,
Error: err.Error(),
})
return
}
writeJSON(w, http.StatusOK, tapNotifyAPIResponse{
ClientID: resp.GetClientId(),
Success: resp.GetSuccess(),
Single: resp.GetSingle(),
DoubleTap: resp.GetDoubleTap(),
Triple: resp.GetTriple(),
})
}
func serveTapNotifyGetQuery(w http.ResponseWriter, r *http.Request, link *managedSerial) {
clientID, err := parseUintQuery(r, "client_id", 0)
if err != nil || clientID == 0 {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: "client_id required"})
return
}
serveTapNotifyGet(w, clientID, link)
}
type clientTapNotifyBody struct {
Single bool `json:"single"`
DoubleTap bool `json:"double_tap"`
Triple bool `json:"triple"`
}
func serveClientTapNotifyPut(w http.ResponseWriter, r *http.Request, clientID uint32, link *managedSerial, hub *wsHub, tapCtl *tapNotifyCtl) {
var body clientTapNotifyBody
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: "invalid JSON"})
return
}
out := applyTapNotifyClient(link, hub, tapCtl, clientID, body.Single, body.DoubleTap, body.Triple)
status := http.StatusOK
if out.Error != "" || !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}
func serveTapNotifyPost(w http.ResponseWriter, r *http.Request, link *managedSerial, hub *wsHub, tapCtl *tapNotifyCtl) {
var body tapNotifyAPIRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: "invalid JSON"})
return
}
if body.AllClients {
updated, err := applyTapNotifyAll(link, hub, tapCtl, body.Single, body.DoubleTap, body.Triple)
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, tapNotifyAPIResponse{Error: err.Error()})
return
}
writeJSON(w, http.StatusOK, tapNotifyAPIResponse{
Success: updated > 0,
SlavesUpdated: updated,
Single: body.Single,
DoubleTap: body.DoubleTap,
Triple: body.Triple,
})
return
}
if body.ClientID == 0 {
writeJSON(w, http.StatusBadRequest, tapNotifyAPIResponse{Error: "client_id required"})
return
}
out := applyTapNotifyClient(link, hub, tapCtl, body.ClientID, body.Single, body.DoubleTap, body.Triple)
status := http.StatusOK
if out.Error != "" || !out.Success {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, out)
}
func applyTapNotifyAll(link *managedSerial, hub *wsHub, tapCtl *tapNotifyCtl, single, doubleTap, triple bool) (uint32, error) {
resp, err := link.TapNotify(&pb.TapNotifyRequest{
Write: true,
AllClients: true,
Single: single,
DoubleTap: doubleTap,
Triple: triple,
})
if err != nil {
return 0, err
}
if !resp.GetSuccess() {
return 0, fmt.Errorf("tap notify not applied to any slave")
}
if hub != nil || tapCtl != nil {
clients, _ := link.listClientsPoll()
for _, c := range clients {
if tapCtl != nil {
tapCtl.Set(c.GetId(), single, doubleTap, triple)
}
if hub != nil {
hub.patchClientTapNotify(c.GetId(), single, doubleTap, triple)
}
}
}
return resp.GetSlavesUpdated(), nil
}
func serveTapSnapshotGet(w http.ResponseWriter, r *http.Request, link *managedSerial) {
clientID, err := parseUintQuery(r, "client_id", 0)
if err != nil {
writeJSON(w, http.StatusBadRequest, tapSnapshotAPIResponse{Error: err.Error()})
return
}
cache, err := link.readCacheStatusPoll()
if err != nil {
writeJSON(w, http.StatusServiceUnavailable, tapSnapshotAPIResponse{Error: err.Error()})
return
}
out := tapSnapshotAPIResponse{Events: make([]tapEventView, 0)}
for _, e := range tapEventsFromCacheStatus(cache) {
if clientID != 0 && e.GetClientId() != clientID {
continue
}
out.Events = append(out.Events, tapEventView{
ClientID: e.GetClientId(),
Kind: tapKindLabel(e.GetKind()),
AgeMs: e.GetAgeMs(),
})
}
writeJSON(w, http.StatusOK, out)
}

View File

@ -347,6 +347,8 @@ func ledRingModeValue(mode string) (uint32, error) {
return 3, nil return 3, nil
case "find_me", "findme": case "find_me", "findme":
return 4, nil return 4, nil
case "color", "solid", "fill":
return 5, nil
default: default:
return 0, fmt.Errorf("unknown led_ring mode %q", mode) return 0, fmt.Errorf("unknown led_ring mode %q", mode)
} }

120
goTool/battery_api.go Normal file
View File

@ -0,0 +1,120 @@
package main
import (
"powerpod/gotool/pb"
)
const (
lipoMinMv = 3000
lipoMaxMv = 4200
)
type lipoReadingJSON struct {
Valid bool `json:"valid"`
VoltageMv uint32 `json:"voltage_mv"`
Percent int `json:"percent,omitempty"`
}
type batterySampleJSON struct {
ClientID uint32 `json:"client_id"`
Lipo1 lipoReadingJSON `json:"lipo1"`
Lipo2 lipoReadingJSON `json:"lipo2"`
AgeMs uint32 `json:"age_ms,omitempty"`
}
type batteryAPIRequest struct {
ClientID uint32 `json:"client_id"`
AllClients bool `json:"all_clients"`
}
type batteryAPIResponse struct {
Type string `json:"type,omitempty"` // battery_status (WebSocket)
Success bool `json:"success"`
Samples []batterySampleJSON `json:"samples,omitempty"`
Error string `json:"error,omitempty"`
}
func lipoPercent(mv uint32) int {
if mv <= lipoMinMv {
return 0
}
if mv >= lipoMaxMv {
return 100
}
return int((mv - lipoMinMv) * 100 / (lipoMaxMv - lipoMinMv))
}
func lipoFromPBMsg(l *pb.LipoReading) lipoReadingJSON {
if l == nil {
return lipoReadingJSON{}
}
return lipoFromPB(l.GetValid(), l.GetVoltageMv())
}
func lipoFromPB(valid bool, mv uint32) lipoReadingJSON {
out := lipoReadingJSON{Valid: valid, VoltageMv: mv}
if valid {
out.Percent = lipoPercent(mv)
}
return out
}
func batterySamplesFromPB(samples []*pb.BatterySample) []batterySampleJSON {
out := make([]batterySampleJSON, 0, len(samples))
for _, s := range samples {
out = append(out, batterySampleJSON{
ClientID: s.GetClientId(),
Lipo1: lipoFromPBMsg(s.GetLipo1()),
Lipo2: lipoFromPBMsg(s.GetLipo2()),
AgeMs: s.GetAgeMs(),
})
}
return out
}
func applyBatteryStatus(link *managedSerial, in batteryAPIRequest) batteryAPIResponse {
resp, err := link.BatteryStatus(&pb.BatteryStatusRequest{
ClientId: in.ClientID,
AllClients: in.AllClients,
})
if err != nil {
return batteryAPIResponse{Error: err.Error()}
}
samples := batterySamplesFromPB(resp.GetSamples())
out := batteryAPIResponse{
Success: resp.GetSuccess() || len(samples) > 0,
Samples: samples,
}
if len(samples) == 0 && out.Error == "" {
out.Error = "battery status unavailable"
}
return out
}
func findBatterySample(samples []batterySampleJSON, clientID uint32) (batterySampleJSON, bool) {
for _, s := range samples {
if s.ClientID == clientID {
return s, true
}
}
return batterySampleJSON{}, false
}
// applyBatterySamplesToState merges UART/REST battery samples into dashboard views.
func applyBatterySamplesToState(st *DashboardState, samples []batterySampleJSON) {
if st == nil || len(samples) == 0 {
return
}
if m, ok := findBatterySample(samples, 0); ok {
st.Master.Lipo1 = m.Lipo1
st.Master.Lipo2 = m.Lipo2
st.Master.BatteryAgeMs = m.AgeMs
}
for i := range st.Clients {
if s, ok := findBatterySample(samples, st.Clients[i].ID); ok {
st.Clients[i].Lipo1 = s.Lipo1
st.Clients[i].Lipo2 = s.Lipo2
st.Clients[i].BatteryAgeMs = s.AgeMs
}
}
}

49
goTool/cache_status.go Normal file
View File

@ -0,0 +1,49 @@
package main
import "powerpod/gotool/pb"
// accelSamplesFromCacheStatus maps combined CACHE_STATUS entries to AccelSample
// (for dashboard / WebSocket accel push).
func accelSamplesFromCacheStatus(r *pb.CacheStatusResponse) []*pb.AccelSample {
if r == nil {
return nil
}
out := make([]*pb.AccelSample, 0, len(r.GetClients()))
for _, c := range r.GetClients() {
if c.GetAccel() == nil {
continue
}
a := c.GetAccel()
out = append(out, &pb.AccelSample{
ClientId: c.GetClientId(),
Valid: a.GetValid(),
X: a.GetX(),
Y: a.GetY(),
Z: a.GetZ(),
AgeMs: a.GetAgeMs(),
})
}
return out
}
// tapEventsFromCacheStatus maps combined CACHE_STATUS entries to TapEvent
// (only clients with a consumed pending tap).
func tapEventsFromCacheStatus(r *pb.CacheStatusResponse) []*pb.TapEvent {
if r == nil {
return nil
}
out := make([]*pb.TapEvent, 0, len(r.GetClients()))
for _, c := range r.GetClients() {
if c.GetTap() == nil {
continue
}
t := c.GetTap()
out = append(out, &pb.TapEvent{
ClientId: c.GetClientId(),
Valid: true,
Kind: t.GetKind(),
AgeMs: t.GetAgeMs(),
})
}
return out
}

View File

@ -40,6 +40,182 @@ func (m *managedSerial) listClientsPoll() ([]*pb.ClientInfo, error) {
return decodeClientsPayload(payload) return decodeClientsPayload(payload)
} }
func decodeBatteryStatusPayload(payload []byte) (*pb.BatteryStatusResponse, error) {
if len(payload) < 2 {
return nil, fmt.Errorf("short battery response")
}
if payload[0] != byte(pb.MessageType_BATTERY_STATUS) {
return nil, fmt.Errorf("unexpected command id 0x%02x (want 0x%02x)",
payload[0], byte(pb.MessageType_BATTERY_STATUS))
}
var msg pb.UartMessage
if err := proto.Unmarshal(payload[1:], &msg); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if msg.GetType() != pb.MessageType_BATTERY_STATUS {
return nil, fmt.Errorf("unexpected type %v", msg.GetType())
}
r := msg.GetBatteryStatusResponse()
if r == nil {
return nil, fmt.Errorf("missing battery_status_response")
}
return r, nil
}
func (m *managedSerial) BatteryStatus(req *pb.BatteryStatusRequest) (*pb.BatteryStatusResponse, error) {
var resp *pb.BatteryStatusResponse
err := m.withPort(func(sp *serialPort) error {
var e error
resp, e = sp.batteryStatus(req)
return e
})
return resp, err
}
func (m *managedSerial) BatteryStatusPoll(req *pb.BatteryStatusRequest) (*pb.BatteryStatusResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_BATTERY_STATUS,
Payload: &pb.UartMessage_BatteryStatusRequest{
BatteryStatusRequest: req,
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_BATTERY_STATUS)}, body...)
respPayload, err := m.batteryStatusPayloadPoll(payload)
if err != nil {
return nil, err
}
return decodeBatteryStatusPayload(respPayload)
}
func (m *managedSerial) batteryStatusPayloadPoll(payload []byte) ([]byte, error) {
var resp []byte
err := m.withPortPoll(func(sp *serialPort) error {
var e error
resp, e = sp.exchangePayloadForBattery(payload, "BATTERY_STATUS")
return e
})
return resp, err
}
func (s *serialPort) batteryStatus(req *pb.BatteryStatusRequest) (*pb.BatteryStatusResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_BATTERY_STATUS,
Payload: &pb.UartMessage_BatteryStatusRequest{
BatteryStatusRequest: req,
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_BATTERY_STATUS)}, body...)
respPayload, err := s.exchangePayloadForBattery(payload, "BATTERY_STATUS")
if err != nil {
return nil, err
}
return decodeBatteryStatusPayload(respPayload)
}
func (m *managedSerial) AccelStream(req *pb.AccelStreamRequest) (*pb.AccelStreamResponse, error) {
return m.accelStreamVia(m.withPort, req)
}
func (m *managedSerial) AccelStreamPoll(req *pb.AccelStreamRequest) (*pb.AccelStreamResponse, error) {
return m.accelStreamVia(m.withPortPoll, req)
}
// SetAccelStream enables or disables the ESP-NOW accel stream for one slave (master UART).
func (m *managedSerial) SetAccelStream(clientID uint32, enable bool) (*pb.AccelStreamResponse, error) {
return m.AccelStream(&pb.AccelStreamRequest{
Write: true,
Enable: enable,
ClientId: clientID,
})
}
// GetAccelStream returns whether the accel stream is enabled for a slave on the master.
func (m *managedSerial) GetAccelStream(clientID uint32) (bool, error) {
resp, err := m.AccelStreamPoll(&pb.AccelStreamRequest{
Write: false,
ClientId: clientID,
})
if err != nil {
return false, err
}
if !resp.GetSuccess() {
return false, fmt.Errorf("accel stream read failed for client %d", clientID)
}
return resp.GetEnabled(), nil
}
func (m *managedSerial) TapNotify(req *pb.TapNotifyRequest) (*pb.TapNotifyResponse, error) {
return m.tapNotifyVia(m.withPort, req)
}
func (m *managedSerial) TapNotifyPoll(req *pb.TapNotifyRequest) (*pb.TapNotifyResponse, error) {
return m.tapNotifyVia(m.withPortPoll, req)
}
func (m *managedSerial) tapNotifyVia(
portFn func(func(*serialPort) error) error,
req *pb.TapNotifyRequest,
) (*pb.TapNotifyResponse, error) {
var resp *pb.TapNotifyResponse
err := portFn(func(sp *serialPort) error {
var e error
resp, e = sp.TapNotify(req)
return e
})
return resp, err
}
func (m *managedSerial) readCacheStatusPoll() (*pb.CacheStatusResponse, error) {
payload, err := m.exchangePoll(byte(pb.MessageType_CACHE_STATUS), "CACHE_STATUS")
if err != nil {
return nil, err
}
return decodeCacheStatusPayload(payload)
}
func decodeCacheStatusPayload(payload []byte) (*pb.CacheStatusResponse, error) {
if len(payload) < 1 {
return nil, fmt.Errorf("empty response payload")
}
if payload[0] != byte(pb.MessageType_CACHE_STATUS) {
return nil, fmt.Errorf("unexpected command id 0x%02x (want 0x%02x)",
payload[0], byte(pb.MessageType_CACHE_STATUS))
}
var msg pb.UartMessage
if err := proto.Unmarshal(payload[1:], &msg); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
if msg.GetType() != pb.MessageType_CACHE_STATUS {
return nil, fmt.Errorf("unexpected type %v", msg.GetType())
}
r := msg.GetCacheStatusResponse()
if r == nil {
return nil, fmt.Errorf("missing cache_status_response")
}
return r, nil
}
func (m *managedSerial) accelStreamVia(
portFn func(func(*serialPort) error) error,
req *pb.AccelStreamRequest,
) (*pb.AccelStreamResponse, error) {
var resp *pb.AccelStreamResponse
err := portFn(func(sp *serialPort) error {
var e error
resp, e = sp.AccelStream(req)
return e
})
return resp, err
}
func (m *managedSerial) AccelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) { func (m *managedSerial) AccelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) {
return m.accelDeadzoneVia(m.withPort, req) return m.accelDeadzoneVia(m.withPort, req)
} }
@ -117,6 +293,68 @@ func (s *serialPort) listClients() ([]*pb.ClientInfo, error) {
return decodeClientsPayload(payload) return decodeClientsPayload(payload)
} }
func (s *serialPort) AccelStream(req *pb.AccelStreamRequest) (*pb.AccelStreamResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_ACCEL_STREAM,
Payload: &pb.UartMessage_AccelStreamRequest{
AccelStreamRequest: req,
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_ACCEL_STREAM)}, body...)
respPayload, err := s.exchangePayload(payload, "ACCEL_STREAM")
if err != nil {
return nil, err
}
var respMsg pb.UartMessage
if err := proto.Unmarshal(respPayload[1:], &respMsg); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
r := respMsg.GetAccelStreamResponse()
if r == nil {
return nil, fmt.Errorf("missing accel_stream_response")
}
return r, nil
}
func (s *serialPort) TapNotify(req *pb.TapNotifyRequest) (*pb.TapNotifyResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_TAP_NOTIFY,
Payload: &pb.UartMessage_TapNotifyRequest{
TapNotifyRequest: req,
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_TAP_NOTIFY)}, body...)
respPayload, err := s.exchangePayload(payload, "TAP_NOTIFY")
if err != nil {
return nil, err
}
var respMsg pb.UartMessage
if err := proto.Unmarshal(respPayload[1:], &respMsg); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
r := respMsg.GetTapNotifyResponse()
if r == nil {
return nil, fmt.Errorf("missing tap_notify_response")
}
return r, nil
}
func (s *serialPort) readCacheStatus() (*pb.CacheStatusResponse, error) {
payload, err := s.exchange(byte(pb.MessageType_CACHE_STATUS), "CACHE_STATUS")
if err != nil {
return nil, err
}
return decodeCacheStatusPayload(payload)
}
func (s *serialPort) accelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) { func (s *serialPort) accelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) {
msg := &pb.UartMessage{ msg := &pb.UartMessage{
Type: pb.MessageType_ACCEL_DEADZONE, Type: pb.MessageType_ACCEL_DEADZONE,
@ -215,6 +453,28 @@ func (s *serialPort) GetVersion() (*pb.VersionResponse, error) { return s.getVer
func (s *serialPort) ListClients() ([]*pb.ClientInfo, error) { return s.listClients() } func (s *serialPort) ListClients() ([]*pb.ClientInfo, error) { return s.listClients() }
func (s *serialPort) SetAccelStream(clientID uint32, enable bool) (*pb.AccelStreamResponse, error) {
return s.AccelStream(&pb.AccelStreamRequest{
Write: true,
Enable: enable,
ClientId: clientID,
})
}
func (s *serialPort) GetAccelStream(clientID uint32) (bool, error) {
resp, err := s.AccelStream(&pb.AccelStreamRequest{
Write: false,
ClientId: clientID,
})
if err != nil {
return false, err
}
if !resp.GetSuccess() {
return false, fmt.Errorf("accel stream read failed for client %d", clientID)
}
return resp.GetEnabled(), nil
}
func (s *serialPort) AccelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) { func (s *serialPort) AccelDeadzone(req *pb.AccelDeadzoneRequest) (*pb.AccelDeadzoneResponse, error) {
return s.accelDeadzone(req) return s.accelDeadzone(req)
} }
@ -227,6 +487,16 @@ func (s *serialPort) LedRing(req *pb.LedRingProgressRequest) (*pb.LedRingProgres
return s.ledRingProgress(req) return s.ledRingProgress(req)
} }
func (m *managedSerial) LedRing(req *pb.LedRingProgressRequest) (*pb.LedRingProgressResponse, error) {
var resp *pb.LedRingProgressResponse
err := m.withPort(func(sp *serialPort) error {
var e error
resp, e = sp.LedRing(req)
return e
})
return resp, err
}
func (s *serialPort) FindMe(clientID uint32) (*pb.EspNowFindMeResponse, error) { func (s *serialPort) FindMe(clientID uint32) (*pb.EspNowFindMeResponse, error) {
return s.espnowFindMe(clientID) return s.espnowFindMe(clientID)
} }

View File

@ -0,0 +1,33 @@
package main
import (
"fmt"
)
func runCacheStatus(sp *serialPort) error {
r, err := sp.readCacheStatus()
if err != nil {
return err
}
clients := r.GetClients()
if len(clients) == 0 {
fmt.Println("(no slaves with accel stream or tap notify enabled)")
return nil
}
for _, c := range clients {
id := c.GetClientId()
if a := c.GetAccel(); a != nil {
if !a.GetValid() {
fmt.Printf("client %d accel: no sample yet\n", id)
} else {
fmt.Printf("client %d accel: x=%d y=%d z=%d (age %d ms)\n",
id, a.GetX(), a.GetY(), a.GetZ(), a.GetAgeMs())
}
}
if t := c.GetTap(); t != nil {
fmt.Printf("client %d tap: %s (age %d ms)\n",
id, tapKindLabel(t.GetKind()), t.GetAgeMs())
}
}
return nil
}

View File

@ -18,9 +18,17 @@ func runClients(sp *serialPort) error {
fmt.Printf("clients (%d):\n", len(clients)) fmt.Printf("clients (%d):\n", len(clients))
for i, c := range clients { for i, c := range clients {
mac := hex.EncodeToString(c.GetMac()) mac := hex.EncodeToString(c.GetMac())
fmt.Printf(" [%d] id=%d mac=%s ver=%d available=%v used=%v last_ping=%d last_success_ping=%d\n", fmt.Printf(" [%d] id=%d mac=%s ver=%d available=%v used=%v last_ping=%d last_success_ping=%d tap=%s/%s/%s\n",
i, c.GetId(), mac, c.GetVersion(), c.GetAvailable(), c.GetUsed(), i, c.GetId(), mac, c.GetVersion(), c.GetAvailable(), c.GetUsed(),
c.GetLastPing(), c.GetLastSuccessPing()) c.GetLastPing(), c.GetLastSuccessPing(),
boolFlag(c.GetTapNotifySingle()), boolFlag(c.GetTapNotifyDouble()), boolFlag(c.GetTapNotifyTriple()))
} }
return nil return nil
} }
func boolFlag(v bool) string {
if v {
return "on"
}
return "off"
}

View File

@ -7,17 +7,12 @@ import (
"powerpod/gotool/pb" "powerpod/gotool/pb"
) )
const (
ledRingModeClear = 0
ledRingModeProgress = 1
ledRingModeDigit = 2
ledRingModeBlink = 3
ledRingModeFindMe = 4
)
func runLedRing(sp *serialPort, args []string) error { func runLedRing(sp *serialPort, args []string) error {
fs := flag.NewFlagSet("led-ring", flag.ExitOnError) fs := flag.NewFlagSet("led-ring", flag.ExitOnError)
mode := fs.String("mode", "progress", "clear, progress, digit, blink, or find-me") mode := fs.String("mode", "progress", "clear, color, progress, digit, blink, or find-me")
clientID := fs.Uint("client", 0, "0=master ring, >0=slave via ESP-NOW")
allClients := fs.Bool("all", false, "broadcast to all slaves")
slavesOnly := fs.Bool("slaves-only", false, "with -all: do not change master ring")
progress := fs.Uint("progress", 0, "fill level 0100 (mode=progress)") progress := fs.Uint("progress", 0, "fill level 0100 (mode=progress)")
digit := fs.Uint("digit", 0, "digit 010 (mode=digit)") digit := fs.Uint("digit", 0, "digit 010 (mode=digit)")
r := fs.Uint("r", 0, "red 0255") r := fs.Uint("r", 0, "red 0255")
@ -30,20 +25,9 @@ func runLedRing(sp *serialPort, args []string) error {
return err return err
} }
var modeVal uint32 modeVal, err := ledRingModeFromString(*mode)
switch *mode { if err != nil {
case "clear": return err
modeVal = ledRingModeClear
case "progress":
modeVal = ledRingModeProgress
case "digit":
modeVal = ledRingModeDigit
case "blink":
modeVal = ledRingModeBlink
case "find-me", "find_me", "findme":
modeVal = ledRingModeFindMe
default:
return fmt.Errorf("unknown -mode %q (clear, progress, digit, blink, find-me)", *mode)
} }
resp, err := sp.ledRingProgress(&pb.LedRingProgressRequest{ resp, err := sp.ledRingProgress(&pb.LedRingProgressRequest{
@ -56,11 +40,15 @@ func runLedRing(sp *serialPort, args []string) error {
Intensity: uint32(*intensity), Intensity: uint32(*intensity),
BlinkMs: uint32(*blinkMs), BlinkMs: uint32(*blinkMs),
BlinkCount: uint32(*blinkCount), BlinkCount: uint32(*blinkCount),
ClientId: uint32(*clientID),
AllClients: *allClients,
SlavesOnly: *slavesOnly,
}) })
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("success=%v mode=%d progress=%d digit=%d\n", fmt.Printf("success=%v mode=%d progress=%d digit=%d client_id=%d slaves_updated=%d\n",
resp.GetSuccess(), resp.GetMode(), resp.GetProgress(), resp.GetDigit()) resp.GetSuccess(), resp.GetMode(), resp.GetProgress(), resp.GetDigit(),
resp.GetClientId(), resp.GetSlavesUpdated())
return nil return nil
} }

View File

@ -21,7 +21,9 @@ var wsUpgrader = websocket.Upgrader{
func runServe(portName string, baud int, args []string) error { func runServe(portName string, baud int, args []string) error {
serveFlags := flag.NewFlagSet("serve", flag.ExitOnError) serveFlags := flag.NewFlagSet("serve", flag.ExitOnError)
addr := serveFlags.String("addr", ":8080", "HTTP listen address") addr := serveFlags.String("addr", ":8080", "dashboard HTTP listen address")
apiAddr := serveFlags.String("api-addr", ":8081", "external API HTTP listen address (empty to disable)")
accelInterval := serveFlags.Duration("accel-interval", defaultAccelStreamInterval, "accel WebSocket sample period on API server")
interval := serveFlags.Duration("interval", 2*time.Second, "UART poll interval") interval := serveFlags.Duration("interval", 2*time.Second, "UART poll interval")
if err := serveFlags.Parse(args); err != nil { if err := serveFlags.Parse(args); err != nil {
return err return err
@ -35,12 +37,22 @@ func runServe(portName string, baud int, args []string) error {
defer link.Close() defer link.Close()
hub := newWSHub() hub := newWSHub()
streamCtl := newAccelStreamCtl()
tapCtl := newTapNotifyCtl()
stop := make(chan struct{}) stop := make(chan struct{})
defer close(stop) defer close(stop)
go runPoller(link, portName, hub, *interval, stop) go runPoller(link, portName, hub, streamCtl, tapCtl, *interval, stop)
go runBatteryPoller(link, hub, 5*time.Second, stop)
go runCacheStatusDashboardPoller(link, hub, *accelInterval, stop)
var apiSrv *http.Server
if *apiAddr != "" {
apiSrv = runAPIServer(portName, link, *apiAddr, *accelInterval, hub, streamCtl, tapCtl, stop)
defer shutdownAPIServer(apiSrv)
}
mux := http.NewServeMux() mux := http.NewServeMux()
mountServeAPI(mux, link, hub) mountServeAPI(mux, link, hub, streamCtl, tapCtl)
mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
conn, err := wsUpgrader.Upgrade(w, r, nil) conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -64,7 +76,10 @@ func runServe(portName string, baud int, args []string) error {
} }
mux.Handle("/", http.FileServer(http.FS(ui))) mux.Handle("/", http.FileServer(http.FS(ui)))
log.Printf("dashboard http://localhost%s (UART %s @ %d baud, poll %s, auto-reconnect)", log.Printf("dashboard http://localhost%s (UART %s @ %d baud, poll %s, live-stream %s, auto-reconnect)",
*addr, portName, baud, interval.String()) *addr, portName, baud, interval.String(), accelInterval.String())
if *apiAddr == "" {
log.Printf("external API disabled (-api-addr \"\")")
}
return http.ListenAndServe(*addr, mux) return http.ListenAndServe(*addr, mux)
} }

58
goTool/cmd_tap.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"flag"
"fmt"
"powerpod/gotool/pb"
)
func runTapNotify(sp *serialPort, args []string) error {
fs := flag.NewFlagSet("tap-notify", flag.ExitOnError)
write := fs.Bool("set", false, "write tap notify flags (default: read)")
clientID := fs.Uint("client", 0, "client id (>0 required for read/set one slave)")
all := fs.Bool("all", false, "apply to all registered slaves (with -set)")
single := fs.Bool("single", false, "notify on single tap")
doubleTap := fs.Bool("double", false, "notify on double tap")
triple := fs.Bool("triple", false, "notify on triple tap")
if err := fs.Parse(args); err != nil {
return err
}
if !*write && (*all || *clientID == 0) {
return fmt.Errorf("read requires -client <id>")
}
if *write && !*all && *clientID == 0 {
return fmt.Errorf("set requires -client <id> or -all")
}
r, err := sp.TapNotify(&pb.TapNotifyRequest{
Write: *write,
ClientId: uint32(*clientID),
AllClients: *all,
Single: *single,
DoubleTap: *doubleTap,
Triple: *triple,
})
if err != nil {
return err
}
fmt.Printf("client_id=%d success=%v slaves_updated=%d single=%v double=%v triple=%v\n",
r.GetClientId(), r.GetSuccess(), r.GetSlavesUpdated(),
r.GetSingle(), r.GetDoubleTap(), r.GetTriple())
return nil
}
func tapKindLabel(k pb.TapKind) string {
switch k {
case pb.TapKind_TAP_SINGLE:
return "single"
case pb.TapKind_TAP_DOUBLE:
return "double"
case pb.TapKind_TAP_TRIPLE:
return "triple"
default:
return "none"
}
}

View File

@ -19,6 +19,9 @@ type MasterView struct {
GitHash string `json:"git_hash"` GitHash string `json:"git_hash"`
RunningPartition string `json:"running_partition,omitempty"` RunningPartition string `json:"running_partition,omitempty"`
Deadzone uint32 `json:"deadzone,omitempty"` Deadzone uint32 `json:"deadzone,omitempty"`
Lipo1 lipoReadingJSON `json:"lipo1"`
Lipo2 lipoReadingJSON `json:"lipo2"`
BatteryAgeMs uint32 `json:"battery_age_ms,omitempty"`
OK bool `json:"ok"` OK bool `json:"ok"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }
@ -32,6 +35,20 @@ type ClientView struct {
Used bool `json:"used"` Used bool `json:"used"`
LastPing uint32 `json:"last_ping"` LastPing uint32 `json:"last_ping"`
LastSuccessPing uint32 `json:"last_success_ping"` LastSuccessPing uint32 `json:"last_success_ping"`
AccelValid bool `json:"accel_valid"`
AccelX int32 `json:"accel_x"`
AccelY int32 `json:"accel_y"`
AccelZ int32 `json:"accel_z"`
AccelAgeMs uint32 `json:"accel_age_ms"`
AccelStream bool `json:"accel_stream"`
TapNotifySingle bool `json:"tap_notify_single"`
TapNotifyDouble bool `json:"tap_notify_double"`
TapNotifyTriple bool `json:"tap_notify_triple"`
LastTap string `json:"last_tap,omitempty"`
LastTapAt int64 `json:"last_tap_at,omitempty"`
Lipo1 lipoReadingJSON `json:"lipo1"`
Lipo2 lipoReadingJSON `json:"lipo2"`
BatteryAgeMs uint32 `json:"battery_age_ms,omitempty"`
} }
type DashboardState struct { type DashboardState struct {
@ -40,6 +57,8 @@ type DashboardState struct {
UARTConnected bool `json:"uart_connected"` UARTConnected bool `json:"uart_connected"`
SerialOK bool `json:"serial_ok"` SerialOK bool `json:"serial_ok"`
SerialError string `json:"serial_error,omitempty"` SerialError string `json:"serial_error,omitempty"`
/** Host: fast CACHE_STATUS poll (~16 ms) for accel + tap. */
LiveStream bool `json:"live_stream"`
Master MasterView `json:"master"` Master MasterView `json:"master"`
Clients []ClientView `json:"clients"` Clients []ClientView `json:"clients"`
} }
@ -48,6 +67,7 @@ type wsHub struct {
mu sync.RWMutex mu sync.RWMutex
clients map[*websocket.Conn]struct{} clients map[*websocket.Conn]struct{}
state DashboardState state DashboardState
liveStream bool
} }
func newWSHub() *wsHub { func newWSHub() *wsHub {
@ -56,6 +76,19 @@ func newWSHub() *wsHub {
func (h *wsHub) setState(st DashboardState) { func (h *wsHub) setState(st DashboardState) {
h.mu.Lock() h.mu.Lock()
prev := h.state
st.LiveStream = prev.LiveStream
st.Clients = preserveClientAccel(st.Clients, prev.Clients, st.LiveStream)
st.Clients = preserveClientBattery(st.Clients, prev.Clients)
st.Clients = preserveClientTap(st.Clients, prev.Clients)
if !st.Master.Lipo1.Valid && !st.Master.Lipo2.Valid {
if prev.Master.Lipo1.Valid || prev.Master.Lipo2.Valid {
st.Master.Lipo1 = prev.Master.Lipo1
st.Master.Lipo2 = prev.Master.Lipo2
st.Master.BatteryAgeMs = prev.Master.BatteryAgeMs
}
}
h.liveStream = st.LiveStream
h.state = st h.state = st
conns := make([]*websocket.Conn, 0, len(h.clients)) conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients { for c := range h.clients {
@ -89,6 +122,367 @@ func (h *wsHub) unregister(c *websocket.Conn) {
h.mu.Unlock() h.mu.Unlock()
} }
func applyAccelSamples(clients []ClientView, samples []*pb.AccelSample) []ClientView {
if len(samples) == 0 {
return clients
}
byID := make(map[uint32]*pb.AccelSample, len(samples))
for _, s := range samples {
byID[s.GetClientId()] = s
}
out := make([]ClientView, len(clients))
for i, c := range clients {
out[i] = c
if !c.AccelStream {
out[i].AccelValid = false
continue
}
s, ok := byID[c.ID]
if !ok {
continue
}
out[i].AccelValid = s.GetValid()
if s.GetValid() {
out[i].AccelX = s.GetX()
out[i].AccelY = s.GetY()
out[i].AccelZ = s.GetZ()
out[i].AccelAgeMs = s.GetAgeMs()
}
}
return out
}
func preserveClientAccel(newClients, oldClients []ClientView, liveStream bool) []ClientView {
if len(oldClients) == 0 {
return newClients
}
oldByID := make(map[uint32]ClientView, len(oldClients))
for _, c := range oldClients {
oldByID[c.ID] = c
}
out := make([]ClientView, len(newClients))
for i, c := range newClients {
out[i] = c
if !liveStream && !c.AccelStream {
continue
}
if liveStream && !c.AccelStream {
out[i].AccelValid = false
out[i].AccelX = 0
out[i].AccelY = 0
out[i].AccelZ = 0
out[i].AccelAgeMs = 0
continue
}
prev, ok := oldByID[c.ID]
if !ok || !prev.AccelValid {
continue
}
if !c.AccelValid {
out[i].AccelValid = prev.AccelValid
out[i].AccelX = prev.AccelX
out[i].AccelY = prev.AccelY
out[i].AccelZ = prev.AccelZ
out[i].AccelAgeMs = prev.AccelAgeMs
}
}
return out
}
func preserveClientBattery(newClients, oldClients []ClientView) []ClientView {
if len(oldClients) == 0 {
return newClients
}
oldByID := make(map[uint32]ClientView, len(oldClients))
for _, c := range oldClients {
oldByID[c.ID] = c
}
out := make([]ClientView, len(newClients))
for i, c := range newClients {
out[i] = c
if c.Lipo1.Valid || c.Lipo2.Valid {
continue
}
prev, ok := oldByID[c.ID]
if !ok {
continue
}
if prev.Lipo1.Valid || prev.Lipo2.Valid {
out[i].Lipo1 = prev.Lipo1
out[i].Lipo2 = prev.Lipo2
out[i].BatteryAgeMs = prev.BatteryAgeMs
}
}
return out
}
func anyClientAccelStream(clients []ClientView) bool {
for _, c := range clients {
if c.AccelStream {
return true
}
}
return false
}
func anyClientTapNotify(clients []ClientView) bool {
for _, c := range clients {
if c.TapNotifySingle || c.TapNotifyDouble || c.TapNotifyTriple {
return true
}
}
return false
}
func tapKindLabelPB(k pb.TapKind) string {
switch k {
case pb.TapKind_TAP_SINGLE:
return "single"
case pb.TapKind_TAP_DOUBLE:
return "double"
case pb.TapKind_TAP_TRIPLE:
return "triple"
default:
return ""
}
}
func applyTapEvents(clients []ClientView, events []*pb.TapEvent) []ClientView {
if len(events) == 0 {
return clients
}
byID := make(map[uint32]*pb.TapEvent, len(events))
for _, e := range events {
if e.GetValid() {
byID[e.GetClientId()] = e
}
}
if len(byID) == 0 {
return clients
}
now := time.Now().UnixMilli()
out := make([]ClientView, len(clients))
for i, c := range clients {
out[i] = c
if !clientTapNotifyAny(c) {
continue
}
e, ok := byID[c.ID]
if !ok {
continue
}
out[i].LastTap = tapKindLabelPB(e.GetKind())
out[i].LastTapAt = now
}
return out
}
const clientTapDisplayMinMs = 2000
func clientTapNotifyAny(c ClientView) bool {
return c.TapNotifySingle || c.TapNotifyDouble || c.TapNotifyTriple
}
func preserveClientTap(newClients, oldClients []ClientView) []ClientView {
if len(oldClients) == 0 {
return newClients
}
oldByID := make(map[uint32]ClientView, len(oldClients))
for _, c := range oldClients {
oldByID[c.ID] = c
}
cutoff := time.Now().Add(-clientTapDisplayMinMs * time.Millisecond).UnixMilli()
out := make([]ClientView, len(newClients))
for i, c := range newClients {
out[i] = c
if c.LastTap != "" {
continue
}
prev, ok := oldByID[c.ID]
if !ok || prev.LastTap == "" || prev.LastTapAt < cutoff {
continue
}
out[i].LastTap = prev.LastTap
out[i].LastTapAt = prev.LastTapAt
}
return out
}
// patchClientAccelStream updates stream flag immediately (e.g. after REST) and pushes WS.
func (h *wsHub) patchClientAccelStream(clientID uint32, enabled bool) {
h.mu.Lock()
for i := range h.state.Clients {
if h.state.Clients[i].ID != clientID {
continue
}
h.state.Clients[i].AccelStream = enabled
if !enabled {
h.state.Clients[i].AccelValid = false
h.state.Clients[i].AccelX = 0
h.state.Clients[i].AccelY = 0
h.state.Clients[i].AccelZ = 0
h.state.Clients[i].AccelAgeMs = 0
}
break
}
st := h.state
st.UpdatedAt = time.Now().Format(time.RFC3339)
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
func (h *wsHub) anyAccelStreamEnabled() bool {
h.mu.RLock()
defer h.mu.RUnlock()
return anyClientAccelStream(h.state.Clients)
}
func (h *wsHub) anyTapNotifyEnabled() bool {
h.mu.RLock()
defer h.mu.RUnlock()
return anyClientTapNotify(h.state.Clients)
}
func (h *wsHub) liveStreamEnabled() bool {
h.mu.RLock()
defer h.mu.RUnlock()
return h.liveStream
}
func (h *wsHub) snapshotClients() []ClientView {
h.mu.RLock()
defer h.mu.RUnlock()
out := make([]ClientView, len(h.state.Clients))
copy(out, h.state.Clients)
return out
}
// patchLiveStream toggles host CACHE_STATUS polling (~16 ms).
func (h *wsHub) patchLiveStream(enabled bool) {
h.mu.Lock()
h.liveStream = enabled
st := h.state
st.LiveStream = enabled
if !enabled {
for i := range st.Clients {
st.Clients[i].AccelValid = false
st.Clients[i].AccelX = 0
st.Clients[i].AccelY = 0
st.Clients[i].AccelZ = 0
st.Clients[i].AccelAgeMs = 0
st.Clients[i].LastTap = ""
st.Clients[i].LastTapAt = 0
}
}
h.state = st
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
// patchClientTapNotify updates tap notify flags immediately (e.g. after REST) and pushes WS.
func (h *wsHub) patchClientTapNotify(clientID uint32, single, doubleTap, triple bool) {
h.mu.Lock()
for i := range h.state.Clients {
if h.state.Clients[i].ID != clientID {
continue
}
h.state.Clients[i].TapNotifySingle = single
h.state.Clients[i].TapNotifyDouble = doubleTap
h.state.Clients[i].TapNotifyTriple = triple
if !single && !doubleTap && !triple {
h.state.Clients[i].LastTap = ""
h.state.Clients[i].LastTapAt = 0
}
break
}
st := h.state
st.UpdatedAt = time.Now().Format(time.RFC3339)
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
// mergeAccel updates cached accel on clients and pushes state to dashboard WebSockets.
func (h *wsHub) mergeAccel(samples []*pb.AccelSample) {
if !h.liveStreamEnabled() {
return
}
h.mu.Lock()
st := h.state
st.Clients = applyAccelSamples(st.Clients, samples)
st.UpdatedAt = time.Now().Format(time.RFC3339)
h.state = st
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
func (h *wsHub) mergeTap(events []*pb.TapEvent) {
if len(events) == 0 || !h.liveStreamEnabled() {
return
}
h.mu.Lock()
st := h.state
st.Clients = applyTapEvents(st.Clients, events)
st.UpdatedAt = time.Now().Format(time.RFC3339)
h.state = st
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
func (h *wsHub) broadcastRaw(v any) { func (h *wsHub) broadcastRaw(v any) {
h.mu.RLock() h.mu.RLock()
conns := make([]*websocket.Conn, 0, len(h.clients)) conns := make([]*websocket.Conn, 0, len(h.clients))
@ -106,7 +500,7 @@ func (h *wsHub) broadcastRaw(v any) {
} }
} }
func pollDashboard(link *managedSerial, portName string, last *DashboardState) DashboardState { func pollDashboard(link *managedSerial, portName string, last *DashboardState, streamCtl *accelStreamCtl, tapCtl *tapNotifyCtl) DashboardState {
st := DashboardState{ st := DashboardState{
UpdatedAt: time.Now().Format(time.RFC3339), UpdatedAt: time.Now().Format(time.RFC3339),
SerialPort: portName, SerialPort: portName,
@ -152,15 +546,116 @@ func pollDashboard(link *managedSerial, portName string, last *DashboardState) D
Used: c.GetUsed(), Used: c.GetUsed(),
LastPing: c.GetLastPing(), LastPing: c.GetLastPing(),
LastSuccessPing: c.GetLastSuccessPing(), LastSuccessPing: c.GetLastSuccessPing(),
} AccelStream: c.GetAccelStreamEnabled(),
if dz, err := readDeadzonePoll(link, c.GetId()); err == nil { TapNotifySingle: c.GetTapNotifySingle(),
cv.Deadzone = dz TapNotifyDouble: c.GetTapNotifyDouble(),
TapNotifyTriple: c.GetTapNotifyTriple(),
} }
st.Clients = append(st.Clients, cv) st.Clients = append(st.Clients, cv)
} }
applyBatteryToState(link, &st)
if last == nil || !last.LiveStream {
for i, c := range clients {
if dz, err := readDeadzonePoll(link, c.GetId()); err == nil {
st.Clients[i].Deadzone = dz
}
}
}
if last != nil {
st.LiveStream = last.LiveStream
}
if streamCtl != nil {
streamCtl.SyncFromClients(st.Clients)
}
if tapCtl != nil {
tapCtl.SyncFromClients(st.Clients)
}
return st return st
} }
func applyBatteryToState(link *managedSerial, st *DashboardState) {
bat, err := link.BatteryStatusPoll(&pb.BatteryStatusRequest{AllClients: true})
if err != nil {
log.Printf("battery poll: %v", err)
return
}
applyBatterySamplesToState(st, batterySamplesFromPB(bat.GetSamples()))
}
func (h *wsHub) mergeBattery(samples []batterySampleJSON) {
if len(samples) == 0 {
return
}
h.mu.Lock()
st := h.state
applyBatterySamplesToState(&st, samples)
st.UpdatedAt = time.Now().Format(time.RFC3339)
h.state = st
conns := make([]*websocket.Conn, 0, len(h.clients))
for c := range h.clients {
conns = append(conns, c)
}
h.mu.Unlock()
data, err := json.Marshal(st)
if err != nil {
return
}
for _, c := range conns {
_ = c.WriteMessage(websocket.TextMessage, data)
}
}
func runBatteryPoller(link *managedSerial, hub *wsHub, interval time.Duration, stop <-chan struct{}) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
if hub.clientCount() == 0 {
continue
}
bat, err := link.BatteryStatusPoll(&pb.BatteryStatusRequest{AllClients: true})
if err != nil {
continue
}
hub.mergeBattery(batterySamplesFromPB(bat.GetSamples()))
}
}
}
func runCacheStatusDashboardPoller(link *managedSerial, hub *wsHub, interval time.Duration, stop <-chan struct{}) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
if !hub.liveStreamEnabled() {
continue
}
cache, err := link.readCacheStatusPoll()
if err != nil {
continue
}
hub.mergeAccel(accelSamplesFromCacheStatus(cache))
hub.mergeTap(tapEventsFromCacheStatus(cache))
}
}
}
func (h *wsHub) clientCount() int {
h.mu.RLock()
n := len(h.clients)
h.mu.RUnlock()
return n
}
func pausedPollState(portName string, last *DashboardState) DashboardState { func pausedPollState(portName string, last *DashboardState) DashboardState {
if last != nil && last.UARTConnected { if last != nil && last.UARTConnected {
st := *last st := *last
@ -208,22 +703,25 @@ func formatMAC(mac []byte) string {
return hex.EncodeToString(mac) return hex.EncodeToString(mac)
} }
func runPoller(link *managedSerial, portName string, hub *wsHub, interval time.Duration, stop <-chan struct{}) { func runPoller(link *managedSerial, portName string, hub *wsHub, streamCtl *accelStreamCtl, tapCtl *tapNotifyCtl, interval time.Duration, stop <-chan struct{}) {
// streamCtl / tapCtl kept for external API; dashboard uses hub.state flags.
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
defer ticker.Stop() defer ticker.Stop()
uartUp := false uartUp := false
var lastGood DashboardState var lastGood DashboardState
publish := func() { publish := func() {
st := pollDashboard(link, portName, &lastGood) st := pollDashboard(link, portName, &lastGood, streamCtl, tapCtl)
hub.setState(st)
if st.UARTConnected && st.SerialOK { if st.UARTConnected && st.SerialOK {
lastGood = st hub.mu.RLock()
lastGood = hub.state
hub.mu.RUnlock()
} }
if st.UARTConnected && !uartUp { if st.UARTConnected && !uartUp {
log.Printf("UART %s connected", portName) log.Printf("UART %s connected", portName)
} }
uartUp = st.UARTConnected uartUp = st.UARTConnected
hub.setState(st)
} }
publish() publish()

284
goTool/docs/API_REST.md Normal file
View File

@ -0,0 +1,284 @@
# REST API
`go run . -port /dev/ttyUSB0 serve` starts two HTTP servers on the same UART link:
| Base URL | Flag | Used by |
|----------|------|---------|
| `http://localhost:8080` | `-addr` (default `:8080`) | Web dashboard + automation on the UI routes |
| `http://localhost:8081` | `-api-addr` (default `:8081`, `""` disables) | External programs; subset of routes + service info |
WebSocket streaming (accel/tap push): [`API_WEBSOCKET.md`](API_WEBSOCKET.md).
All JSON responses use `Content-Type: application/json`. On UART errors many routes return **503** with `"error"` in the body.
---
## External API (`:8081`)
### Service info
```http
GET /
GET /api/v1/
```
```json
{
"name": "powerpod-external-api",
"version": "1",
"serial_port": "/dev/ttyUSB0",
"websocket": "/ws",
"default_interval_ms": 16,
"min_interval_ms": 1,
"max_interval_ms": 10000,
"tap_display_min_ms": 2000,
"description": "..."
}
```
### Battery
```http
GET /api/battery?all_clients=true
GET /api/battery?client_id=16
POST /api/battery
Content-Type: application/json
```
POST body:
```json
{"all_clients": true}
{"client_id": 0}
{"client_id": 16}
```
Response:
```json
{
"success": true,
"samples": [
{
"client_id": 16,
"lipo1": {"valid": true, "voltage_mv": 3850, "percent": 71},
"lipo2": {"valid": false},
"age_ms": 1200
}
]
}
```
Slaves push battery to the master every **30 s**; these routes read the master cache.
WebSocket equivalent: `get_battery` on `ws://localhost:8081/ws` (reply type `battery_status`).
### LED ring
```http
POST /api/led-ring
Content-Type: application/json
```
Body:
```json
{"mode":"color","client_id":16,"r":255,"g":0,"b":0,"intensity":128}
{"mode":"digit","client_id":0,"digit":3,"r":0,"g":255,"b":0}
{"mode":"find-me","all_clients":true,"slaves_only":true}
```
| `mode` | Notes |
|--------|--------|
| `clear` | Turn off |
| `color` | Full ring RGB + `intensity` |
| `progress` | `progress` 0100 |
| `digit` | `digit` 010 |
| `blink` | `blink_ms`, `blink_count` |
| `find-me` | Locate pod |
Use `client_id` (`0` = master) or `all_clients` (+ optional `slaves_only`) for broadcast.
Response: `success`, `slaves_updated`, optional `error`.
WebSocket: `set_led_ring` with the same fields plus `"type":"set_led_ring"``led_ring_status`.
---
## Dashboard API (`:8080`)
Used by the web UI; safe for scripts that drive the same features.
### Live stream (host `CACHE_STATUS` poll ~16 ms)
```http
GET /api/live-stream
PUT /api/live-stream
Content-Type: application/json
{"enable": true}
```
```json
{"enabled": true, "success": true}
```
Enables fast UART polling for dashboard accel/tap display. Per-slave accel still requires accel-stream (below).
### Accel stream (firmware ESP-NOW, per slave)
```http
GET /api/clients/16/accel-stream
PUT /api/clients/16/accel-stream
Content-Type: application/json
{"enable": true}
```
```json
{"enabled": true, "client_id": 16, "success": true}
```
All slaves:
```http
POST /api/accel-stream
Content-Type: application/json
{"write": true, "enable": true, "all_clients": true}
```
Polling on the host runs only while at least one slave has streaming enabled (here or via external WebSocket / dashboard).
### Tap notify (firmware; does not start host tap polling)
```http
GET /api/clients/16/tap-notify
PUT /api/clients/16/tap-notify
Content-Type: application/json
{"single": true, "double_tap": false, "triple": false}
```
```json
{
"client_id": 16,
"success": true,
"slaves_updated": 1,
"single": true,
"double_tap": false,
"triple": false
}
```
All slaves:
```http
POST /api/tap-notify
Content-Type: application/json
{"single": true, "double_tap": false, "triple": false, "all_clients": true}
```
Host tap display / external `set_tap_stream` is separate.
### Tap snapshot (one-shot, via `CACHE_STATUS`)
```http
GET /api/tap-snapshot?client_id=16
```
Reads the combined cache (`CACHE_STATUS`); optional `client_id` filters pending tap events. Pending taps are consumed on read.
```json
{
"events": [
{"client_id": 16, "kind": "single", "age_ms": 4}
]
}
```
### Deadzone
```http
GET /api/deadzone?client_id=0
POST /api/deadzone
Content-Type: application/json
{"write": true, "deadzone": 128, "client_id": 0}
```
With `all_clients` + `slaves_only`: push to ESP-NOW slaves only (master BMA456 unchanged).
```json
{"deadzone": 128, "client_id": 0, "success": true, "slaves_updated": 2}
```
### Unicast test
```http
POST /api/unicast-test
Content-Type: application/json
{"client_id": 16, "seq": 42}
```
### Find me
```http
POST /api/find-me
Content-Type: application/json
{"client_id": 16}
```
`client_id` `0` = master LED ring.
### Restart
```http
POST /api/restart
Content-Type: application/json
{"client_id": 16}
```
### OTA (master UART upload)
```http
POST /api/ota
Content-Type: multipart/form-data
```
Form field **`firmware`**: binary image, max **2 MiB**.
```json
{"success": true, "bytes_written": 123456, "target_slot": 1}
```
Firmware distributes to slaves over ESP-NOW after `OTA_END`. Progress also appears on dashboard WebSocket as `ota_progress` messages.
CLI equivalent: `go run . -port /dev/ttyUSB0 ota build/powerpod.bin`
### LED ring and battery
Same as external API:
- `POST /api/led-ring`
- `GET` / `POST` `/api/battery`
---
## Dashboard vs external
| Feature | Dashboard `:8080` | External `:8081` |
|---------|-------------------|------------------|
| Client list | Via dashboard WebSocket state / CLI `clients` | WebSocket `list_clients` |
| Accel/tap **push stream** | WebSocket state when live-stream on | WebSocket `set_stream` / `set_tap_stream` |
| Accel stream enable | REST `PUT .../accel-stream` | WebSocket `set_accel_stream` |
| Tap notify | REST `PUT .../tap-notify` | WebSocket `set_tap_notify` |
| LED / battery | REST | REST + WebSocket on `:8081` |
---
## UI mapping
| UI action | REST / CLI |
|-----------|------------|
| Nur Master deadzone | `POST /api/deadzone` `client_id: 0` or CLI `deadzone -set -client 0` |
| Einzelner Slave | `client_id: <id>` |
| Alle Slaves deadzone | `all_clients` + `slaves_only` on POST |
| Unicast test | `POST /api/unicast-test` |
| Tap notify S/D/T | `PUT /api/clients/{id}/tap-notify` |
| Tap receive (UI) | Live stream + tap notify; see WebSocket doc for external API |

View File

@ -0,0 +1,348 @@
# WebSocket API
`go run . -port /dev/ttyUSB0 serve` exposes two WebSocket endpoints. They share the same UART link but serve different purposes.
| URL | Port (default) | Role |
|-----|----------------|------|
| `ws://localhost:8080/ws` | Dashboard (`-addr`) | Server → client only: full `DashboardState` JSON (~2 s poll + live-stream accel/tap) |
| `ws://localhost:8081/ws` | External API (`-api-addr`) | Request/response commands + optional **accel** / **tap** push streams |
Disable the external server with `-api-addr ""`.
CLI overview and UART commands: [`../README.md`](../README.md). HTTP endpoints: [`API_REST.md`](API_REST.md).
---
## External API (`:8081/ws`)
### Connection flow
1. Connect → server sends **`hello`** (receive off; lists available commands).
2. Send JSON commands → server replies with a matching `*_status` or `client_list` message (one reply per command).
3. After `set_stream` / `set_tap_stream` with `enable: true`, the server may send **`accel`** and/or **`tap`** messages **without** a prior command (push stream).
Commands and stream pushes are multiplexed on one socket. While streaming, always parse `type` and branch (status vs sample vs error).
### Two layers (accel and tap)
| Layer | Commands | Effect |
|-------|----------|--------|
| **Firmware (ESP-NOW)** | `set_accel_stream`, `set_tap_notify` | Per `client_id`: slave sends accel or tap kinds to the master |
| **This connection (host)** | `set_stream`, `set_tap_stream` | Whether **you** receive push JSON and at what rate (`interval_ms`, 1 ms … 10 s) |
- **Accel UART polling** runs only if at least one connection has `receive_accel: true` **and** at least one slave streams accel (`set_accel_stream` or dashboard).
- **Tap UART polling** runs only if at least one connection has `receive_tap: true` (`set_tap_stream`). `set_tap_notify` alone does **not** poll.
Typical sequence:
1. `list_clients` → slave IDs
2. Per slave: `set_accel_stream` / `set_tap_notify` as needed
3. `set_stream` and/or `set_tap_stream` with `"enable": true`
4. Read push messages in a loop
There is **no per-slave filter** on push messages: each `accel` contains all cached slaves; each `tap` contains all visible events. Filter by `client_id` in your app.
---
## Push stream messages
These are the samples you get after enabling receive. Interval is per WebSocket connection; the server UART poll uses the **minimum** `interval_ms` among all subscribers that want accel or tap.
### `accel` (type `"accel"`)
Sent only when `set_stream` has `enable: true`, a slave streams accel, and the poll tick fires for this connection.
**Success** — all slaves with a cache entry on the master (not only those with `valid: true`):
```json
{
"type": "accel",
"t": 1716900123456789012,
"success": true,
"clients": [
{
"client_id": 16,
"valid": true,
"x": 12,
"y": -34,
"z": 16384,
"age_ms": 8
},
{
"client_id": 42,
"valid": false
}
]
}
```
| Field | Meaning |
|-------|---------|
| `t` | Unix timestamp in **nanoseconds** when the host read the cache |
| `success` | `true` if `CACHE_STATUS` succeeded |
| `clients[]` | One entry per slave slot in the master cache |
| `client_id` | ESP-NOW client id (same as `list_clients`) |
| `valid` | `false` if no sample yet or stale; omit `x`/`y`/`z` when false |
| `x`, `y`, `z` | Raw accelerometer LSB (BMA456, ±2 g scale on the pod) |
| `age_ms` | Milliseconds since the master received this sample |
**Failure** (e.g. UART busy):
```json
{
"type": "accel",
"t": 1716900123456789012,
"success": false,
"error": "uart busy"
}
```
No `clients` array on failure.
### `tap` (type `"tap"`)
Sent only when `set_tap_stream` has `enable: true` and there is at least one event to show.
Events appear when the master cache reports a new tap. Each event stays in push payloads for **`tap_display_min_ms`** (2000 ms, also in `hello`) after the API first saw it, even if the hardware age grows.
**Success**:
```json
{
"type": "tap",
"t": 1716900123456789012,
"success": true,
"events": [
{
"client_id": 16,
"valid": true,
"kind": "single",
"age_ms": 3,
"shown_at_ms": 1717000000123
}
]
}
```
| Field | Meaning |
|-------|---------|
| `t` | Unix timestamp in **nanoseconds** (poll time) |
| `events[]` | All taps currently “on screen” for the API |
| `client_id` | Slave that tapped |
| `kind` | `"single"`, `"double"`, or `"triple"` |
| `age_ms` | Age in the master cache when read |
| `shown_at_ms` | Unix **milliseconds** when this host first included the event |
If no events are visible, **no** `tap` message is sent on that tick (unlike accel, which can send empty `clients` only on success with cache data).
**Failure**:
```json
{
"type": "tap",
"t": 1716900123456789012,
"success": false,
"error": "uart busy"
}
```
---
## Commands (request → response)
Send one JSON object per message. Field `type` selects the command.
### `hello` (server → client, on connect)
```json
{
"type": "hello",
"serial_port": "/dev/ttyUSB0",
"interval_ms": 16,
"tap_display_min_ms": 2000,
"note": "set_tap_notify configures slave S/D/T only; set_tap_stream enables tap polling/push",
"commands": [
"list_clients",
"set_stream", "get_stream",
"set_accel_stream", "get_accel_stream",
"set_tap_stream", "get_tap_stream",
"set_tap_notify", "get_tap_notify",
"set_led_ring", "get_battery"
]
}
```
### `list_clients`
Request: `{"type":"list_clients"}`
Response `client_list`:
```json
{
"type": "client_list",
"success": true,
"clients": [
{
"id": 16,
"mac": "aa:bb:cc:dd:ee:10",
"version": 1,
"available": true,
"used": true,
"last_ping": 1234,
"last_success_ping": 1200,
"accel_stream": false,
"tap_notify_single": false,
"tap_notify_double": false,
"tap_notify_triple": false
}
]
}
```
### `set_stream` / `get_stream` (receive accel on this connection)
```json
{"type":"set_stream","enable":true,"interval_ms":32}
{"type":"get_stream"}
```
Response `stream_status`:
```json
{"type":"stream_status","receive_accel":true,"interval_ms":32,"success":true}
```
### `set_accel_stream` / `get_accel_stream` (firmware, per slave)
`client_id` required (> 0).
```json
{"type":"set_accel_stream","client_id":16,"enable":true}
{"type":"get_accel_stream","client_id":16}
```
Response `accel_stream_status`:
```json
{"type":"accel_stream_status","client_id":16,"enabled":true,"success":true}
```
### `set_tap_stream` / `get_tap_stream` (receive tap on this connection)
```json
{"type":"set_tap_stream","enable":true,"interval_ms":16}
{"type":"get_tap_stream"}
```
Response `tap_stream_status`:
```json
{"type":"tap_stream_status","receive_tap":true,"interval_ms":16,"success":true}
```
### `set_tap_notify` / `get_tap_notify` (firmware, per slave)
Per client: `single`, `double_tap`, `triple` required on set.
```json
{"type":"set_tap_notify","client_id":16,"single":true,"double_tap":false,"triple":false}
```
Broadcast: `"all_clients": true` with the three booleans.
Response `tap_notify_status`:
```json
{
"type": "tap_notify_status",
"client_id": 16,
"success": true,
"single": true,
"double_tap": false,
"triple": false
}
```
### `set_led_ring`
Same JSON body as [`POST /api/led-ring`](API_REST.md#led-ring) with `"type":"set_led_ring"` added. Reply: `led_ring_status`.
### `get_battery`
Body: `{"type":"get_battery","all_clients":true}` or `"client_id":16`. Default if omitted: all clients.
Reply: `battery_status` with `samples[]` (see REST doc).
---
## Examples
### Accel stream
```python
import asyncio, json, websockets
async def main():
async with websockets.connect("ws://127.0.0.1:8081/ws") as ws:
print(await ws.recv()) # hello
await ws.send(json.dumps({"type": "list_clients"}))
clients = json.loads(await ws.recv())["clients"]
for c in clients:
if not c.get("available"):
continue
await ws.send(json.dumps({
"type": "set_accel_stream", "client_id": c["id"], "enable": True
}))
await ws.recv() # accel_stream_status
await ws.send(json.dumps({"type": "set_stream", "enable": True, "interval_ms": 16}))
await ws.recv() # stream_status
while True:
msg = json.loads(await ws.recv())
if msg.get("type") != "accel":
continue
if not msg.get("success"):
print("error:", msg.get("error"))
continue
for c in msg.get("clients", []):
if c.get("valid"):
print(c["client_id"], c["x"], c["y"], c["z"], "age", c.get("age_ms"))
asyncio.run(main())
```
### Tap stream
```python
import asyncio, json, websockets
async def main():
async with websockets.connect("ws://127.0.0.1:8081/ws") as ws:
print(await ws.recv()) # hello
await ws.send(json.dumps({
"type": "set_tap_notify", "client_id": 16,
"single": True, "double_tap": False, "triple": False
}))
await ws.recv() # tap_notify_status
await ws.send(json.dumps({"type": "set_tap_stream", "enable": True, "interval_ms": 16}))
await ws.recv() # tap_stream_status
while True:
msg = json.loads(await ws.recv())
if msg.get("type") == "tap" and msg.get("events"):
for e in msg["events"]:
print(e["client_id"], e["kind"], "age", e.get("age_ms"))
asyncio.run(main())
```
---
## Dashboard WebSocket (`:8080/ws`)
Read-only from the browsers perspective: the server pushes JSON whenever state changes. Clients do not send commands on this socket (messages are ignored).
Payload shape: `DashboardState``updated_at`, `serial_port`, `uart_connected`, `live_stream`, `master`, `clients[]` (id, mac, accel, tap notify flags, battery, etc.). Accel/tap samples appear here when **Live stream** is enabled in the UI (`PUT /api/live-stream`).
During OTA, additional messages with `"type":"ota_progress"` may appear on the same socket.
Configure slaves via REST on `:8080` ([`API_REST.md`](API_REST.md)), not via this WebSocket.

106
goTool/led_ring_api.go Normal file
View File

@ -0,0 +1,106 @@
package main
import (
"fmt"
"strings"
"powerpod/gotool/pb"
)
const (
ledRingModeClear = 0
ledRingModeProgress = 1
ledRingModeDigit = 2
ledRingModeBlink = 3
ledRingModeFindMe = 4
ledRingModeColor = 5
)
type ledRingAPIRequest struct {
Mode string `json:"mode"`
ClientID uint32 `json:"client_id"`
AllClients bool `json:"all_clients"`
SlavesOnly bool `json:"slaves_only"`
Progress uint32 `json:"progress"`
Digit uint32 `json:"digit"`
R uint32 `json:"r"`
G uint32 `json:"g"`
B uint32 `json:"b"`
Intensity uint32 `json:"intensity"`
BlinkMs uint32 `json:"blink_ms"`
BlinkCount uint32 `json:"blink_count"`
}
type ledRingAPIResponse struct {
Type string `json:"type,omitempty"` // led_ring_status (WebSocket)
Success bool `json:"success"`
Mode uint32 `json:"mode,omitempty"`
Progress uint32 `json:"progress,omitempty"`
Digit uint32 `json:"digit,omitempty"`
ClientID uint32 `json:"client_id,omitempty"`
SlavesUpdated uint32 `json:"slaves_updated,omitempty"`
Error string `json:"error,omitempty"`
}
func ledRingModeFromString(s string) (uint32, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "clear", "":
return ledRingModeClear, nil
case "color", "solid", "fill":
return ledRingModeColor, nil
case "progress":
return ledRingModeProgress, nil
case "digit":
return ledRingModeDigit, nil
case "blink":
return ledRingModeBlink, nil
case "find-me", "find_me", "findme":
return ledRingModeFindMe, nil
default:
return 0, fmt.Errorf("unknown mode %q (clear, color, progress, digit, blink, find-me)", s)
}
}
func ledRingPBFromAPI(in ledRingAPIRequest) (*pb.LedRingProgressRequest, error) {
mode, err := ledRingModeFromString(in.Mode)
if err != nil {
return nil, err
}
return &pb.LedRingProgressRequest{
Mode: mode,
Progress: in.Progress,
Digit: in.Digit,
R: in.R,
G: in.G,
B: in.B,
Intensity: in.Intensity,
BlinkMs: in.BlinkMs,
BlinkCount: in.BlinkCount,
ClientId: in.ClientID,
AllClients: in.AllClients,
SlavesOnly: in.SlavesOnly,
}, nil
}
func applyLedRing(link *managedSerial, in ledRingAPIRequest) ledRingAPIResponse {
req, err := ledRingPBFromAPI(in)
if err != nil {
return ledRingAPIResponse{Error: err.Error()}
}
resp, err := link.LedRing(req)
if err != nil {
return ledRingAPIResponse{Error: err.Error()}
}
out := ledRingAPIResponse{
Success: resp.GetSuccess(),
Mode: resp.GetMode(),
Progress: resp.GetProgress(),
Digit: resp.GetDigit(),
ClientID: resp.GetClientId(),
SlavesUpdated: resp.GetSlavesUpdated(),
}
if !out.Success && out.Error == "" {
out.Error = "led ring command rejected"
}
return out
}

View File

@ -16,6 +16,8 @@ func usage() {
fmt.Fprintf(os.Stderr, " version firmware version and git hash\n") fmt.Fprintf(os.Stderr, " version firmware version and git hash\n")
fmt.Fprintf(os.Stderr, " clients registered ESP-NOW slaves on the master\n") fmt.Fprintf(os.Stderr, " clients registered ESP-NOW slaves on the master\n")
fmt.Fprintf(os.Stderr, " deadzone get/set accelerometer deadzone (LSB)\n") fmt.Fprintf(os.Stderr, " deadzone get/set accelerometer deadzone (LSB)\n")
fmt.Fprintf(os.Stderr, " tap-notify get/set which tap kinds notify via ESP-NOW\n")
fmt.Fprintf(os.Stderr, " cache-status subscribed accel + tap cache (one UART round-trip)\n")
fmt.Fprintf(os.Stderr, " unicast-test send ESP-NOW unicast test to one slave\n") fmt.Fprintf(os.Stderr, " unicast-test send ESP-NOW unicast test to one slave\n")
fmt.Fprintf(os.Stderr, " test run automated scenario (see testdata/)\n") fmt.Fprintf(os.Stderr, " test run automated scenario (see testdata/)\n")
fmt.Fprintf(os.Stderr, " serve web dashboard (Bootstrap + WebSocket)\n") fmt.Fprintf(os.Stderr, " serve web dashboard (Bootstrap + WebSocket)\n")
@ -50,7 +52,7 @@ func main() {
os.Exit(2) os.Exit(2)
} }
runErr = runServe(*portName, *baud, flag.Args()[1:]) runErr = runServe(*portName, *baud, flag.Args()[1:])
case "version", "clients", "client-info", "deadzone", "accel-deadzone", "unicast-test", "unicast_test", "led-ring", "led_ring", "find-me", "find_me", "restart", "ota", "ota-progress", "ota_progress": case "version", "clients", "client-info", "deadzone", "accel-deadzone", "tap-notify", "tap_notify", "cache-status", "cache_status", "unicast-test", "unicast_test", "led-ring", "led_ring", "find-me", "find_me", "restart", "ota", "ota-progress", "ota_progress":
if *portName == "" { if *portName == "" {
fmt.Fprintf(os.Stderr, "command %q requires -port\n\n", cmd) fmt.Fprintf(os.Stderr, "command %q requires -port\n\n", cmd)
usage() usage()
@ -68,6 +70,10 @@ func main() {
runErr = runClients(sp) runErr = runClients(sp)
case "deadzone", "accel-deadzone": case "deadzone", "accel-deadzone":
runErr = runDeadzone(sp, flag.Args()[1:]) runErr = runDeadzone(sp, flag.Args()[1:])
case "tap-notify", "tap_notify":
runErr = runTapNotify(sp, flag.Args()[1:])
case "cache-status", "cache_status":
runErr = runCacheStatus(sp)
case "unicast-test", "unicast_test": case "unicast-test", "unicast_test":
runErr = runUnicastTest(sp, flag.Args()[1:]) runErr = runUnicastTest(sp, flag.Args()[1:])
case "led-ring", "led_ring": case "led-ring", "led_ring":

File diff suppressed because it is too large Load Diff

View File

@ -119,7 +119,7 @@ func (m *managedSerial) exchangePayloadVia(
var resp []byte var resp []byte
err := portFn(func(sp *serialPort) error { err := portFn(func(sp *serialPort) error {
var e error var e error
resp, e = sp.exchangePayloadLocked(payload, cmdName) resp, e = sp.exchangePayloadLocked(payload, cmdName, readTimeout)
return e return e
}) })
return resp, err return resp, err

View File

@ -12,6 +12,9 @@ import (
const readTimeout = 3 * time.Second const readTimeout = 3 * time.Second
// batteryReadTimeout: master may query each slave over ESP-NOW (~400 ms each).
const batteryReadTimeout = 12 * time.Second
type serialPort struct { type serialPort struct {
port serial.Port port serial.Port
mu sync.Mutex mu sync.Mutex
@ -44,10 +47,16 @@ func (s *serialPort) Close() error {
func (s *serialPort) exchangePayload(payload []byte, cmdName string) ([]byte, error) { func (s *serialPort) exchangePayload(payload []byte, cmdName string) ([]byte, error) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
return s.exchangePayloadLocked(payload, cmdName) return s.exchangePayloadLocked(payload, cmdName, readTimeout)
} }
func (s *serialPort) exchangePayloadLocked(payload []byte, cmdName string) ([]byte, error) { func (s *serialPort) exchangePayloadForBattery(payload []byte, cmdName string) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.exchangePayloadLocked(payload, cmdName, batteryReadTimeout)
}
func (s *serialPort) exchangePayloadLocked(payload []byte, cmdName string, timeout time.Duration) ([]byte, error) {
if len(payload) == 0 { if len(payload) == 0 {
return nil, fmt.Errorf("empty payload") return nil, fmt.Errorf("empty payload")
} }
@ -63,6 +72,14 @@ func (s *serialPort) exchangePayloadLocked(payload []byte, cmdName string) ([]by
return nil, fmt.Errorf("write: %w", err) return nil, fmt.Errorf("write: %w", err)
} }
if timeout <= 0 {
timeout = readTimeout
}
if err := s.port.SetReadTimeout(timeout); err != nil {
return nil, fmt.Errorf("set read timeout: %w", err)
}
defer func() { _ = s.port.SetReadTimeout(readTimeout) }()
respPayload, err := uartframe.ReadFrame(s.port, nil) respPayload, err := uartframe.ReadFrame(s.port, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("read response: %w", err) return nil, fmt.Errorf("read response: %w", err)

50
goTool/tap_notify_ctl.go Normal file
View File

@ -0,0 +1,50 @@
package main
import "sync"
// tapNotifyCtl tracks which slaves have tap notify enabled (mirrors firmware / dashboard).
type tapNotifyCtl struct {
mu sync.Mutex
flags map[uint32]tapNotifyFlags
}
type tapNotifyFlags struct {
single bool
doubleTap bool
triple bool
}
func newTapNotifyCtl() *tapNotifyCtl {
return &tapNotifyCtl{flags: make(map[uint32]tapNotifyFlags)}
}
func (c *tapNotifyCtl) Set(clientID uint32, single, doubleTap, triple bool) {
c.mu.Lock()
defer c.mu.Unlock()
if !single && !doubleTap && !triple {
delete(c.flags, clientID)
return
}
c.flags[clientID] = tapNotifyFlags{single: single, doubleTap: doubleTap, triple: triple}
}
func (c *tapNotifyCtl) Any() bool {
c.mu.Lock()
defer c.mu.Unlock()
return len(c.flags) > 0
}
func (c *tapNotifyCtl) SyncFromClients(clients []ClientView) {
c.mu.Lock()
defer c.mu.Unlock()
c.flags = make(map[uint32]tapNotifyFlags)
for _, cl := range clients {
if cl.TapNotifySingle || cl.TapNotifyDouble || cl.TapNotifyTriple {
c.flags[cl.ID] = tapNotifyFlags{
single: cl.TapNotifySingle,
doubleTap: cl.TapNotifyDouble,
triple: cl.TapNotifyTriple,
}
}
}
}

View File

@ -55,11 +55,31 @@
.badge-offline { background: #5c6570; color: #f0f3f5; } .badge-offline { background: #5c6570; color: #f0f3f5; }
.badge.bg-secondary { background: #4a5560 !important; color: #f0f3f5; } .badge.bg-secondary { background: #4a5560 !important; color: #f0f3f5; }
.mac { .mac, .accel {
font-family: ui-monospace, monospace; font-family: ui-monospace, monospace;
font-size: 0.85rem; font-size: 0.85rem;
color: var(--pp-accent); color: var(--pp-accent);
} }
.accel-stale { color: var(--pp-text-muted); }
.tap-toggle {
display: inline-flex;
align-items: center;
gap: 0.15rem;
font-size: 0.72rem;
color: var(--pp-text-secondary);
white-space: nowrap;
}
.tap-toggle input { margin: 0; }
.tap-hit {
color: #ffd166;
font-weight: 600;
animation: tap-flash 2s ease-out;
}
@keyframes tap-flash {
from { color: #fff; transform: scale(1.08); }
to { color: #ffd166; transform: scale(1); }
}
.pp-table { .pp-table {
--bs-table-color: var(--pp-text); --bs-table-color: var(--pp-text);
@ -199,6 +219,10 @@
<dd class="col-7" x-text="state.master.running_partition || '—'"></dd> <dd class="col-7" x-text="state.master.running_partition || '—'"></dd>
<dt class="col-5 text-muted">Deadzone</dt> <dt class="col-5 text-muted">Deadzone</dt>
<dd class="col-7" x-text="state.master.deadzone != null ? state.master.deadzone + ' LSB' : '—'"></dd> <dd class="col-7" x-text="state.master.deadzone != null ? state.master.deadzone + ' LSB' : '—'"></dd>
<dt class="col-5 text-muted">LiPo 1</dt>
<dd class="col-7" x-text="formatLipo(state.master?.lipo1)"></dd>
<dt class="col-5 text-muted">LiPo 2</dt>
<dd class="col-7" x-text="formatLipo(state.master?.lipo2)"></dd>
</dl> </dl>
</template> </template>
<template x-if="state.master && !state.master.ok"> <template x-if="state.master && !state.master.ok">
@ -265,8 +289,29 @@
<span class="badge bg-secondary" x-text="(state.clients || []).length + ' registered'"></span> <span class="badge bg-secondary" x-text="(state.clients || []).length + ' registered'"></span>
</div> </div>
</div> </div>
<p class="text-muted small px-3 pt-2 mb-0">Slaves per ESP-NOW — Master-Deadzone bleibt separat.</p> <p class="text-muted small px-3 pt-2 mb-0">
<div class="card-body p-0 pt-2"> <strong>Live-Stream</strong> startet die schnelle <code>CACHE_STATUS</code>-Abfrage (~16&nbsp;ms).
Pro Slave <strong>Accel</strong> aktiviert den ESP-NOW-Accel-Stream; Tap-Notify (S/D/T) steuert
die Tap-Arten auf dem Slave.
</p>
<div class="px-3 pb-2 d-flex flex-wrap gap-2 align-items-center">
<button type="button"
class="btn btn-sm"
:class="state.live_stream ? 'btn-warning' : 'btn-success'"
@click="setLiveStream(!state.live_stream)"
:disabled="busy || !state.uart_connected || !(state.clients || []).length"
x-text="state.live_stream ? 'Live-Stream aus' : 'Live-Stream an'"></button>
<span class="text-muted small">Tap alle Slaves:</span>
<label class="tap-toggle"><input type="checkbox" x-model="allTapSingle" :disabled="busy"> S</label>
<label class="tap-toggle"><input type="checkbox" x-model="allTapDouble" :disabled="busy"> D</label>
<label class="tap-toggle"><input type="checkbox" x-model="allTapTriple" :disabled="busy"> T</label>
<button type="button" class="btn btn-outline-secondary btn-sm"
@click="setTapNotifyAll(allTapSingle, allTapDouble, allTapTriple)"
:disabled="busy || !state.uart_connected">
Tap setzen
</button>
</div>
<div class="card-body p-0 pt-1">
<div class="table-responsive"> <div class="table-responsive">
<table class="table pp-table table-hover"> <table class="table pp-table table-hover">
<thead> <thead>
@ -276,12 +321,17 @@
<th>Ver</th> <th>Ver</th>
<th>Status</th> <th>Status</th>
<th>Deadzone</th> <th>Deadzone</th>
<th>Accel (LSB)</th>
<th>Akku</th>
<th>Accel</th>
<th>Tap-Notify</th>
<th>Tap</th>
<th>Aktion</th> <th>Aktion</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<template x-if="!(state.clients || []).length"> <template x-if="!(state.clients || []).length">
<tr><td colspan="6" class="text-muted text-center py-4">No clients</td></tr> <tr><td colspan="11" class="text-muted text-center py-4">No clients</td></tr>
</template> </template>
<template x-for="c in (state.clients || [])" :key="c.id + c.mac"> <template x-for="c in (state.clients || [])" :key="c.id + c.mac">
<tr> <tr>
@ -294,6 +344,47 @@
x-text="c.available ? 'available' : 'inactive'"></span> x-text="c.available ? 'available' : 'inactive'"></span>
</td> </td>
<td x-text="c.deadzone != null ? c.deadzone : '—'"></td> <td x-text="c.deadzone != null ? c.deadzone : '—'"></td>
<td>
<span class="accel"
:class="accelCellClass(c)"
x-text="formatAccel(c)"
:title="accelTitle(c)"></span>
</td>
<td class="small" x-text="formatLipoPair(c)" :title="lipoTitle(c)"></td>
<td>
<button type="button"
class="btn btn-sm"
:class="c.accel_stream ? 'btn-warning' : 'btn-outline-success'"
@click="setAccelStream(c.id, !c.accel_stream)"
:disabled="busy || !state.uart_connected || !c.available"
x-text="c.accel_stream ? 'Aus' : 'An'"
title="ESP-NOW Accel-Stream auf Slave"></button>
</td>
<td>
<div class="d-flex flex-wrap gap-1 align-items-center">
<label class="tap-toggle" title="Single tap">
<input type="checkbox"
:checked="c.tap_notify_single"
@change="setTapNotify(c.id, $event.target.checked, c.tap_notify_double, c.tap_notify_triple)"
:disabled="busy || !state.uart_connected || !c.available"> S
</label>
<label class="tap-toggle" title="Double tap">
<input type="checkbox"
:checked="c.tap_notify_double"
@change="setTapNotify(c.id, c.tap_notify_single, $event.target.checked, c.tap_notify_triple)"
:disabled="busy || !state.uart_connected || !c.available"> D
</label>
<label class="tap-toggle" title="Triple tap">
<input type="checkbox"
:checked="c.tap_notify_triple"
@change="setTapNotify(c.id, c.tap_notify_single, c.tap_notify_double, $event.target.checked)"
:disabled="busy || !state.uart_connected || !c.available"> T
</label>
</div>
</td>
<td>
<span :class="tapCellClass(c)" x-text="formatLastTap(c)" :title="tapTitle(c)"></span>
</td>
<td> <td>
<div class="d-flex flex-wrap gap-1 align-items-center"> <div class="d-flex flex-wrap gap-1 align-items-center">
<input type="number" class="form-control form-control-sm dz-input" <input type="number" class="form-control form-control-sm dz-input"
@ -312,11 +403,17 @@
title="ESP-NOW Unicast-Test"> title="ESP-NOW Unicast-Test">
Test Test
</button> </button>
<button type="button" class="btn btn-outline-warning btn-sm" <button type="button" class="btn btn-outline-info btn-sm"
@click="findMe(c.id)" @click="ledRing({ clientId: c.id })"
:disabled="busy || !state.uart_connected || !c.available" :disabled="busy || !state.uart_connected || !c.available"
title="LED-Ring Find me (ESP-NOW)"> title="LED-Ring (aktueller Modus)">
Find me LED
</button>
<button type="button" class="btn btn-outline-warning btn-sm"
@click="ledRing({ clientId: c.id, mode: 'find-me' })"
:disabled="busy || !state.uart_connected || !c.available"
title="Find me">
Find
</button> </button>
<button type="button" class="btn btn-outline-secondary btn-sm" <button type="button" class="btn btn-outline-secondary btn-sm"
@click="restart(c.id)" @click="restart(c.id)"
@ -335,6 +432,82 @@
</div> </div>
</section> </section>
<section class="col-12">
<div class="card">
<div class="card-header">LED-Ring</div>
<div class="card-body">
<p class="text-muted small mb-3">
Modi: <code>clear</code>, <code>color</code> (ganzer Ring), <code>progress</code> (0100&nbsp;%),
<code>digit</code> (010), <code>blink</code>, <code>find-me</code>.
Ziel: Master (<code>client_id=0</code>), ein Slave oder alle Slaves (Broadcast).
</p>
<div class="row g-3 align-items-end">
<div class="col-md-2">
<label class="form-label small text-muted">Modus</label>
<select class="form-select form-select-sm" x-model="led.mode" :disabled="busy">
<option value="color">Farbe (alle LEDs)</option>
<option value="clear">Aus (clear)</option>
<option value="progress">Progress</option>
<option value="digit">Ziffer/Symbol</option>
<option value="blink">Blink</option>
<option value="find-me">Find me</option>
</select>
</div>
<div class="col-md-4">
<label class="form-label small text-muted">RGB / Intensität</label>
<div class="d-flex flex-wrap gap-2">
<input type="number" class="form-control form-control-sm" style="width:4rem" min="0" max="255"
placeholder="R" x-model.number="led.r" :disabled="busy">
<input type="number" class="form-control form-control-sm" style="width:4rem" min="0" max="255"
placeholder="G" x-model.number="led.g" :disabled="busy">
<input type="number" class="form-control form-control-sm" style="width:4rem" min="0" max="255"
placeholder="B" x-model.number="led.b" :disabled="busy">
<input type="number" class="form-control form-control-sm" style="width:5rem" min="0" max="255"
title="0 = Geräte-Default (~5 %)"
placeholder="Int." x-model.number="led.intensity" :disabled="busy">
</div>
</div>
<div class="col-md-2" x-show="led.mode === 'progress'">
<label class="form-label small text-muted">Progress %</label>
<input type="number" class="form-control form-control-sm" min="0" max="100"
x-model.number="led.progress" :disabled="busy">
</div>
<div class="col-md-2" x-show="led.mode === 'digit'">
<label class="form-label small text-muted">Ziffer 010</label>
<input type="number" class="form-control form-control-sm" min="0" max="10"
x-model.number="led.digit" :disabled="busy">
</div>
<div class="col-md-2" x-show="led.mode === 'blink'">
<label class="form-label small text-muted">Blink ms × Anzahl</label>
<div class="d-flex gap-1">
<input type="number" class="form-control form-control-sm" min="1"
x-model.number="led.blinkMs" :disabled="busy">
<input type="number" class="form-control form-control-sm" min="1"
x-model.number="led.blinkCount" :disabled="busy">
</div>
</div>
<div class="col-md-4 d-flex flex-wrap gap-2">
<button type="button" class="btn btn-primary btn-sm"
@click="ledRing({ clientId: 0 })"
:disabled="busy || !state.uart_connected">
Master
</button>
<button type="button" class="btn btn-outline-primary btn-sm"
@click="ledRing({ allClients: true, slavesOnly: true })"
:disabled="busy || !state.uart_connected">
Alle Slaves
</button>
<button type="button" class="btn btn-outline-secondary btn-sm"
@click="ledRing({ allClients: true })"
:disabled="busy || !state.uart_connected">
Alle + Master
</button>
</div>
</div>
</div>
</div>
</section>
<section class="col-12"> <section class="col-12">
<div class="card"> <div class="card">
<div class="card-header">Firmware OTA (A/B)</div> <div class="card-header">Firmware OTA (A/B)</div>
@ -449,7 +622,13 @@
wsConnected: false, wsConnected: false,
masterDz: 100, masterDz: 100,
allDz: 100, allDz: 100,
allTapSingle: false,
allTapDouble: false,
allTapTriple: false,
slaveDz: {}, slaveDz: {},
TAP_DISPLAY_MS: 2000,
tapDisplay: {},
_tapClock: 0,
otaFile: null, otaFile: null,
ota: { ota: {
active: false, phase: '', step: '', percent: 0, active: false, phase: '', step: '', percent: 0,
@ -461,12 +640,30 @@
busy: false, busy: false,
configMsg: '', configMsg: '',
configMsgOk: false, configMsgOk: false,
led: {
mode: 'color',
r: 0,
g: 120,
b: 255,
intensity: 0,
progress: 50,
digit: 0,
blinkMs: 350,
blinkCount: 1
},
connect() { connect() {
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:'; const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
const url = proto + '//' + location.host + '/ws'; const url = proto + '//' + location.host + '/ws';
if (this._batteryTimer) clearInterval(this._batteryTimer);
this._batteryTimer = setInterval(() => this.refreshBattery(), 5000);
if (this._tapTimer) clearInterval(this._tapTimer);
this._tapTimer = setInterval(() => { this._tapClock++; }, 250);
const connect = () => { const connect = () => {
this.ws = new WebSocket(url); this.ws = new WebSocket(url);
this.ws.onopen = () => { this.wsConnected = true; }; this.ws.onopen = () => {
this.wsConnected = true;
this.refreshBattery();
};
this.ws.onclose = () => { this.ws.onclose = () => {
this.wsConnected = false; this.wsConnected = false;
setTimeout(connect, 2000); setTimeout(connect, 2000);
@ -478,7 +675,14 @@
this.applyOTAProgress(msg); this.applyOTAProgress(msg);
return; return;
} }
if (msg.type === 'battery_status') {
if (msg.samples?.length) this.applyBatterySamples(msg.samples);
return;
}
const prev = this.state;
this.state = msg; this.state = msg;
this.preserveBatteryInState(prev, this.state);
this.syncTapDisplay(msg.clients || []);
if (msg.master?.deadzone != null) { if (msg.master?.deadzone != null) {
this.masterDz = msg.master.deadzone; this.masterDz = msg.master.deadzone;
} }
@ -492,10 +696,154 @@
}; };
connect(); connect();
}, },
preserveBatteryInState(prev, next) {
if (!prev || !next) return;
const keepLipo = (oldL, newL) => {
if (newL?.valid) return newL;
if (oldL?.valid) return oldL;
return newL ?? oldL;
};
const keepAge = (oldAge, newAge, hasValid) => {
if (hasValid && newAge != null) return newAge;
if (oldAge != null && !hasValid) return oldAge;
return newAge ?? oldAge;
};
if (next.master) {
const pm = prev.master || {};
const l1 = keepLipo(pm.lipo1, next.master.lipo1);
const l2 = keepLipo(pm.lipo2, next.master.lipo2);
next.master.lipo1 = l1;
next.master.lipo2 = l2;
next.master.battery_age_ms = keepAge(
pm.battery_age_ms, next.master.battery_age_ms, !!(l1?.valid || l2?.valid));
}
if (!Array.isArray(next.clients)) return;
const prevById = Object.fromEntries((prev.clients || []).map((c) => [c.id, c]));
next.clients = next.clients.map((c) => {
const p = prevById[c.id];
if (!p) return c;
const l1 = keepLipo(p.lipo1, c.lipo1);
const l2 = keepLipo(p.lipo2, c.lipo2);
return {
...c,
lipo1: l1,
lipo2: l2,
battery_age_ms: keepAge(p.battery_age_ms, c.battery_age_ms, !!(l1?.valid || l2?.valid))
};
});
},
applyBatterySamples(samples) {
if (!samples?.length) return;
for (const s of samples) {
if (s.client_id === 0) {
if (!this.state.master) this.state.master = {};
this.state.master.lipo1 = s.lipo1;
this.state.master.lipo2 = s.lipo2;
this.state.master.battery_age_ms = s.age_ms;
continue;
}
const c = (this.state.clients || []).find((x) => x.id === s.client_id);
if (c) {
c.lipo1 = s.lipo1;
c.lipo2 = s.lipo2;
c.battery_age_ms = s.age_ms;
}
}
},
async refreshBattery() {
if (!this.state?.uart_connected) return;
try {
const r = await fetch('/api/battery?all_clients=1');
if (!r.ok) return;
const data = await r.json();
if (data.samples?.length) this.applyBatterySamples(data.samples);
} catch (_) {}
},
formatMac(hex) { formatMac(hex) {
if (!hex || hex.length !== 12) return hex || ''; if (!hex || hex.length !== 12) return hex || '';
return hex.match(/.{2}/g).join(':'); return hex.match(/.{2}/g).join(':');
}, },
formatLipo(l) {
if (!l?.valid) return '—';
const v = (l.voltage_mv / 1000).toFixed(2);
return l.percent != null ? `${v} V (${l.percent}%)` : `${v} V`;
},
formatLipoPair(c) {
return `1: ${this.formatLipo(c?.lipo1)} · 2: ${this.formatLipo(c?.lipo2)}`;
},
lipoTitle(c) {
if (!c?.lipo1?.valid && !c?.lipo2?.valid) return 'Keine ADC-Daten (Cache ~30 s)';
let t = `LiPo1 ${c.lipo1?.voltage_mv ?? '—'} mV, LiPo2 ${c.lipo2?.voltage_mv ?? '—'} mV`;
if (c.battery_age_ms != null) t += `, Alter ${c.battery_age_ms} ms`;
return t;
},
formatAccel(c) {
if (!this.state?.live_stream) return '—';
if (!c?.accel_stream) return '—';
if (!c?.accel_valid) return '…';
return `${c.accel_x} / ${c.accel_y} / ${c.accel_z}`;
},
accelTitle(c) {
if (!this.state?.live_stream) return 'Live-Stream aus — oben einschalten';
if (!c?.accel_stream) return 'Accel-Stream für diesen Slave nicht aktiv';
if (!c?.accel_valid) return 'Warte auf erste ESP-NOW Samples…';
const age = c.accel_age_ms != null ? `${c.accel_age_ms} ms alt` : '';
return `x=${c.accel_x} y=${c.accel_y} z=${c.accel_z} (raw LSB, ±2g)${age ? ' · ' + age : ''}`;
},
accelCellClass(c) {
if (!c?.accel_valid) return 'accel-stale';
if (c.accel_age_ms != null && c.accel_age_ms > 200) return 'accel-stale';
return '';
},
tapNotifyAny(c) {
return !!(c?.tap_notify_single || c?.tap_notify_double || c?.tap_notify_triple);
},
syncTapDisplay(clients) {
const now = Date.now();
for (const c of clients) {
if (!c?.last_tap || !c?.last_tap_at) continue;
const prev = this.tapDisplay[c.id];
if (!prev || c.last_tap_at >= prev.shownAt) {
this.tapDisplay[c.id] = { kind: c.last_tap, shownAt: c.last_tap_at };
}
}
for (const id of Object.keys(this.tapDisplay)) {
if (now - this.tapDisplay[id].shownAt > this.TAP_DISPLAY_MS + 500) {
delete this.tapDisplay[id];
}
}
},
activeTapDisplay(c) {
void this._tapClock;
const d = this.tapDisplay[c?.id];
if (!d) return null;
if (Date.now() - d.shownAt >= this.TAP_DISPLAY_MS) return null;
return d;
},
formatLastTap(c) {
if (!this.state?.live_stream) return '—';
if (!this.tapNotifyAny(c)) return '—';
const labels = { single: 'Single', double: 'Double', triple: 'Triple' };
const d = this.activeTapDisplay(c);
if (d) return labels[d.kind] || d.kind;
if (!c?.last_tap) return '…';
return '—';
},
tapTitle(c) {
if (!this.state?.live_stream) return 'Live-Stream aus';
if (!this.tapNotifyAny(c)) return 'Tap-Notify nicht konfiguriert (S/D/T)';
const d = this.activeTapDisplay(c);
if (d) {
const age = (Date.now() - d.shownAt) + ' ms her';
return `Tap: ${d.kind} · ${age}`;
}
if (!c?.last_tap) return 'Warte auf Tap-Event…';
return 'Bereit — letzter Tap ausgeblendet';
},
tapCellClass(c) {
if (this.activeTapDisplay(c)) return 'tap-hit';
return 'text-muted';
},
formatSize(n) { formatSize(n) {
if (n == null) return ''; if (n == null) return '';
if (n < 1024) return n + ' B'; if (n < 1024) return n + ' B';
@ -732,6 +1080,144 @@
async setMasterDeadzone() { async setMasterDeadzone() {
await this.setDeadzone(0, this.masterDz); await this.setDeadzone(0, this.masterDz);
}, },
patchLiveStream(enabled) {
let clients = this.state.clients || [];
if (!enabled) {
clients = clients.map((c) => ({
...c,
accel_valid: false,
accel_x: 0,
accel_y: 0,
accel_z: 0,
accel_age_ms: 0,
last_tap: '',
last_tap_at: 0
}));
this.tapDisplay = {};
}
this.state = { ...this.state, live_stream: enabled, clients };
},
async setLiveStream(enable) {
this.busy = true;
try {
const r = await fetch('/api/live-stream', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enable })
});
const data = await r.json();
if (!r.ok || !data.success) {
this.flash(data.error || 'Live-Stream fehlgeschlagen', false);
return;
}
this.patchLiveStream(!!data.enabled);
this.flash(`Live-Stream ${data.enabled ? 'an' : 'aus'}`, true);
} catch (e) {
this.flash(String(e), false);
} finally {
this.busy = false;
}
},
patchClientAccelStream(clientId, enabled) {
const clients = (this.state.clients || []).map((c) => {
if (c.id !== clientId) return c;
const next = { ...c, accel_stream: enabled };
if (!enabled) {
next.accel_valid = false;
next.accel_x = 0;
next.accel_y = 0;
next.accel_z = 0;
next.accel_age_ms = 0;
}
return next;
});
this.state = { ...this.state, clients };
},
async setAccelStream(clientId, enable) {
this.busy = true;
try {
const r = await fetch(`/api/clients/${clientId}/accel-stream`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ enable })
});
const data = await r.json();
if (!r.ok || !data.success) {
this.flash(data.error || `Accel-Stream Slave ${clientId} fehlgeschlagen`, false);
return;
}
this.patchClientAccelStream(clientId, !!data.enabled);
this.flash(`Slave ${clientId}: Accel-Stream ${data.enabled ? 'an' : 'aus'}`, true);
} catch (e) {
this.flash(String(e), false);
} finally {
this.busy = false;
}
},
patchClientTapNotify(clientId, single, doubleTap, triple) {
const clients = (this.state.clients || []).map((c) => {
if (c.id !== clientId) return c;
const next = {
...c,
tap_notify_single: single,
tap_notify_double: doubleTap,
tap_notify_triple: triple
};
if (!single && !doubleTap && !triple) {
next.last_tap = '';
next.last_tap_at = 0;
delete this.tapDisplay[c.id];
}
return next;
});
this.state = { ...this.state, clients };
},
async setTapNotify(clientId, single, doubleTap, triple) {
this.busy = true;
try {
const r = await fetch(`/api/clients/${clientId}/tap-notify`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ single, double_tap: doubleTap, triple })
});
const data = await r.json();
if (!r.ok || !data.success) {
this.flash(data.error || `Tap-Notify Slave ${clientId} fehlgeschlagen`, false);
return;
}
this.patchClientTapNotify(clientId, !!data.single, !!data.double_tap, !!data.triple);
const on = [data.single && 'S', data.double_tap && 'D', data.triple && 'T'].filter(Boolean).join('/') || 'aus';
this.flash(`Slave ${clientId}: Tap-Notify ${on}`, true);
} catch (e) {
this.flash(String(e), false);
} finally {
this.busy = false;
}
},
async setTapNotifyAll(single, doubleTap, triple) {
this.busy = true;
try {
const r = await fetch('/api/tap-notify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ all_clients: true, single, double_tap: doubleTap, triple })
});
const data = await r.json();
if (!r.ok || !data.success) {
this.flash(data.error || 'Tap-Notify für alle Slaves fehlgeschlagen', false);
return;
}
for (const c of (this.state.clients || [])) {
this.patchClientTapNotify(c.id, !!single, !!doubleTap, !!triple);
}
const on = [single && 'S', doubleTap && 'D', triple && 'T'].filter(Boolean).join('/') || 'aus';
this.flash(`Alle Slaves: Tap-Notify ${on} (${data.slaves_updated} aktualisiert)`, true);
} catch (e) {
this.flash(String(e), false);
} finally {
this.busy = false;
}
},
async setDeadzoneAll(deadzone) { async setDeadzoneAll(deadzone) {
if (deadzone == null || deadzone < 0) { if (deadzone == null || deadzone < 0) {
this.flash('Ungültiger Deadzone-Wert', false); this.flash('Ungültiger Deadzone-Wert', false);
@ -780,6 +1266,49 @@
this.busy = false; this.busy = false;
} }
}, },
async ledRing(opts = {}) {
const clientId = opts.clientId ?? 0;
const mode = opts.mode ?? this.led.mode;
this.busy = true;
try {
const r = await fetch('/api/led-ring', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
mode,
client_id: clientId,
all_clients: !!opts.allClients,
slaves_only: !!opts.slavesOnly,
r: this.led.r,
g: this.led.g,
b: this.led.b,
intensity: this.led.intensity,
progress: this.led.progress,
digit: this.led.digit,
blink_ms: this.led.blinkMs,
blink_count: this.led.blinkCount
})
});
const data = await r.json();
if (!r.ok || !data.success) {
this.flash(data.error || 'LED-Ring fehlgeschlagen', false);
return;
}
let label = 'Master';
if (opts.allClients) {
label = opts.slavesOnly
? `Alle Slaves (${data.slaves_updated})`
: `Alle + Master (${data.slaves_updated} Slaves)`;
} else if (clientId > 0) {
label = `Slave ${clientId}`;
}
this.flash(`LED ${mode} → ${label}`, true);
} catch (e) {
this.flash(String(e), false);
} finally {
this.busy = false;
}
},
async findMe(clientId = 0) { async findMe(clientId = 0) {
this.busy = true; this.busy = true;
try { try {

View File

@ -18,11 +18,15 @@ idf_component_register(
"cmd/cmd_version.c" "cmd/cmd_version.c"
"cmd/cmd_client_info.c" "cmd/cmd_client_info.c"
"cmd/cmd_accel_deadzone.c" "cmd/cmd_accel_deadzone.c"
"cmd/cmd_accel_stream.c"
"cmd/cmd_tap_notify.c"
"cmd/cmd_cache_status.c"
"cmd/cmd_espnow_unicast_test.c" "cmd/cmd_espnow_unicast_test.c"
"cmd/cmd_espnow_find_me.c" "cmd/cmd_espnow_find_me.c"
"cmd/cmd_restart.c" "cmd/cmd_restart.c"
"pod_reboot.c" "pod_reboot.c"
"cmd/cmd_led_ring.c" "cmd/cmd_led_ring.c"
"cmd/cmd_battery.c"
"cmd/cmd_ota.c" "cmd/cmd_ota.c"
"cmd/cmd_ota_slave_progress.c" "cmd/cmd_ota_slave_progress.c"
"ota_uart.c" "ota_uart.c"

View File

@ -115,6 +115,10 @@ Schema: `proto/esp_now_messages.proto`. Encode/decode: `esp_now_proto.c`. The ES
| `ESPNOW_UNICAST_TEST` | Master → slave | `EspNowUnicastTest` (`seq`) | | `ESPNOW_UNICAST_TEST` | Master → slave | `EspNowUnicastTest` (`seq`) |
| `ESPNOW_FIND_ME` | Master → slave | `EspNowFindMe` (`client_id` filter) — LED locate sequence | | `ESPNOW_FIND_ME` | Master → slave | `EspNowFindMe` (`client_id` filter) — LED locate sequence |
| `ESPNOW_RESTART` | Master → slave | `EspNowRestart` (`client_id` filter) — reboot slave | | `ESPNOW_RESTART` | Master → slave | `EspNowRestart` (`client_id` filter) — reboot slave |
| `ESPNOW_ACCEL_SAMPLE` | Slave → master | `EspNowAccelSample` (`slave_id`, `x`, `y`, `z` raw LSB) — ~every 16 ms |
| `ESPNOW_SET_TAP_NOTIFY` | Master → slave | `EspNowTapNotify` (`client_id`, `single`, `double_tap`, `triple`) — which tap kinds to forward |
| `ESPNOW_TAP_EVENT` | Slave → master | `EspNowTapEvent` (`client_id`, `kind`) — on BMA456 tap interrupt if notify enabled |
| `ESPNOW_BATTERY_REPORT` | Slave → master | `EspNowBatteryReport` (`client_id`, `lipo1/2` mV) — ~every 30 s; cached in `client_registry` |
| `ESPNOW_OTA_START` | Master → slave (unicast) | `EspNowOtaStart` (`total_size`) | | `ESPNOW_OTA_START` | Master → slave (unicast) | `EspNowOtaStart` (`total_size`) |
| `ESPNOW_OTA_PAYLOAD` | Master → slave | `EspNowOtaPayload` (`seq`, up to 200 B `data`) | | `ESPNOW_OTA_PAYLOAD` | Master → slave | `EspNowOtaPayload` (`seq`, up to 200 B `data`) |
| `ESPNOW_OTA_END` | Master → slave | `EspNowOtaEnd` | | `ESPNOW_OTA_END` | Master → slave | `EspNowOtaEnd` |
@ -209,6 +213,7 @@ Host and master speak nanopb-encoded `UartMessage` inside UART frames (byte 0 =
| 6 | `ACCEL_DEADZONE` | Implemented (`cmd/cmd_accel_deadzone.c`) — get/set accel filter LSB | | 6 | `ACCEL_DEADZONE` | Implemented (`cmd/cmd_accel_deadzone.c`) — get/set accel filter LSB |
| 7 | `ESPNOW_UNICAST_TEST` | Implemented (`cmd/cmd_espnow_unicast_test.c`) | | 7 | `ESPNOW_UNICAST_TEST` | Implemented (`cmd/cmd_espnow_unicast_test.c`) |
| 8 | `LED_RING` | Implemented (`cmd/cmd_led_ring.c`) — ring progress bar (0100 %, RGB, intensity) | | 8 | `LED_RING` | Implemented (`cmd/cmd_led_ring.c`) — ring progress bar (0100 %, RGB, intensity) |
| 26 | `BATTERY_STATUS` | Implemented (`cmd/cmd_battery.c`) — cached LiPo 1/2 per pod from `client_registry` (UART read, no slave round-trip) |
| 16 | `OTA_START` | Implemented (`cmd/cmd_ota.c`) — begin UART OTA on inactive slot | | 16 | `OTA_START` | Implemented (`cmd/cmd_ota.c`) — begin UART OTA on inactive slot |
| 17 | `OTA_PAYLOAD` | Implemented — up to 200 B per frame; device buffers 4 KiB | | 17 | `OTA_PAYLOAD` | Implemented — up to 200 B per frame; device buffers 4 KiB |
| 18 | `OTA_END` | Implemented — flush, `esp_ota_end`, push image to slaves via ESP-NOW, set boot | | 18 | `OTA_END` | Implemented — flush, `esp_ota_end`, push image to slaves via ESP-NOW, set boot |
@ -217,6 +222,9 @@ Host and master speak nanopb-encoded `UartMessage` inside UART frames (byte 0 =
| 21 | `OTA_SLAVE_PROGRESS` | Implemented (`cmd/cmd_ota_slave_progress.c`) — query per-slave ESP-NOW OTA progress | | 21 | `OTA_SLAVE_PROGRESS` | Implemented (`cmd/cmd_ota_slave_progress.c`) — query per-slave ESP-NOW OTA progress |
| 22 | `FIND_ME` | Implemented (`cmd/cmd_espnow_find_me.c`) — `client_id=0` local ring, `>0` ESP-NOW to slave | | 22 | `FIND_ME` | Implemented (`cmd/cmd_espnow_find_me.c`) — `client_id=0` local ring, `>0` ESP-NOW to slave |
| 23 | `RESTART` | Implemented (`cmd/cmd_restart.c`) — `client_id=0` reboot master, `>0` ESP-NOW reboot slave | | 23 | `RESTART` | Implemented (`cmd/cmd_restart.c`) — `client_id=0` reboot master, `>0` ESP-NOW reboot slave |
| 25 | `ACCEL_STREAM` | Implemented — enable/disable slave ESP-NOW accel stream to master |
| 27 | `TAP_NOTIFY` | Implemented (`cmd/cmd_tap_notify.c`) — get/set which tap kinds notify via ESP-NOW |
| 29 | `CACHE_STATUS` | Implemented (`cmd/cmd_cache_status.c`) — subscribed accel + tap cache (one UART round-trip) |
Regenerate C code: Regenerate C code:
@ -310,6 +318,52 @@ Sets the **software** deadzone used by `bosch456.c` when logging accel (see [BMA
**Response:** `accel_deadzone_response` with applied `deadzone`, `success`, and `slaves_updated` (ESP-NOW count). **Response:** `accel_deadzone_response` with applied `deadzone`, `success`, and `slaves_updated` (ESP-NOW count).
### TAP_NOTIFY command
Configure which BMA456 tap kinds a **slave** forwards to the master over ESP-NOW. The slave only sends `ESPNOW_TAP_EVENT` when the matching notify flag is enabled (set locally on the slave via ESP-NOW).
**Request:** framed `1b` (`0x1b`) + `tap_notify_request`:
| Field | Meaning |
|-------|---------|
| `write` | `false` = read, `true` = write |
| `single`, `double_tap`, `triple` | Which tap kinds to notify (write) |
| `client_id` | Slave id (read/write one slave) |
| `all_clients` | Master: ESP-NOW unicast to every registered slave |
**Response:** `tap_notify_response` (`client_id`, `success`, `slaves_updated`, `single`, `double_tap`, `triple`).
Notify flags are mirrored in `ClientInfo` (`tap_notify_single/double/triple`) for the dashboard.
```bash
go run . -port /dev/ttyUSB0 tap-notify -client 16 -set -single
go run . -port /dev/ttyUSB0 tap-notify -client 16
```
### CACHE_STATUS command
Read **cached** accel and/or tap data on the **master** in one UART round-trip. Slaves send `ESPNOW_ACCEL_SAMPLE` every **16 ms** when streaming; tap events arrive via `ESPNOW_TAP_EVENT` and are held up to **16 ms** (`CLIENT_REGISTRY_TAP_MAX_AGE_MS`). Pending taps are **consumed** on read (like the former `TAP_SNAPSHOT`).
**Request:** framed `1d` (`0x1d`) only — no body (`CacheStatusRequest` empty).
**Response:** `cache_status_response.clients[]` — one entry per slave with `accel_stream_enabled` and/or any tap-notify flag:
| Field | When present |
|-------|----------------|
| `client_id` | Always (for listed slaves) |
| `accel` | Slave has accel stream on (`valid`, `x`/`y`/`z`, `age_ms` when sample fresh) |
| `tap` | Tap notify on **and** a pending tap was consumed (`kind`, `age_ms`) |
Unsubscribed submessages are omitted on the wire (proto3 defaults). The master walks `client_registry` once per request (`cmd/cmd_cache_status.c`).
Host tools poll this at **16 ms** when live-stream / WebSocket receive is enabled. Tap events stay visible for **2 s** in the UI/API after first sight.
```bash
go run . -port /dev/ttyUSB0 cache-status
```
External API (`serve -api-addr :8081`) uses the same command for WebSocket `accel` / `tap` push.
### ESPNOW_UNICAST_TEST command ### ESPNOW_UNICAST_TEST command
Minimal master→slave ESP-NOW unicast check (no BMA456). Use this before debugging `ACCEL_DEADZONE` unicast. Minimal master→slave ESP-NOW unicast check (no BMA456). Use this before debugging `ACCEL_DEADZONE` unicast.
@ -345,6 +399,18 @@ go run . -port /dev/ttyUSB0 restart
go run . -port /dev/ttyUSB0 restart -client 16 go run . -port /dev/ttyUSB0 restart -client 16
``` ```
### BATTERY_STATUS command
Read **cached** LiPo ADC values on the **master** (master local + one entry per registered slave). Slaves push `ESPNOW_BATTERY_REPORT` every **30 s**; the master stores them in `client_registry` (`lipo1/2_valid`, `lipo1/2_mv`, `battery_updated_at`). The master refreshes its own pack on the same interval in `master_monitor_task`.
**Request:** framed `26` + optional `battery_status_request` (`client_id`, `all_clients`).
**Response:** `battery_status_response` with `samples[]` (`client_id`, `lipo1`, `lipo2`, `age_ms`).
```bash
# Host / goTool: all_clients returns master (id 0) + slaves from cache
```
### LED_RING command ### LED_RING command
Control the 95-LED ring from the host. The firmware **does not** animate digits locally; only UART updates the display. Control the 95-LED ring from the host. The firmware **does not** animate digits locally; only UART updates the display.
@ -353,14 +419,19 @@ Control the 95-LED ring from the host. The firmware **does not** animate digits
| Field | Meaning | | Field | Meaning |
|-------|---------| |-------|---------|
| `mode` | `0` = clear, `1` = progress bar, `2` = digit, `3` = blink full ring, `4` = find-me (R/G/B ×3 @ full brightness) | | `mode` | `0` = clear, `1` = progress, `2` = digit (010), `3` = blink, `4` = find-me, `5` = solid color (all LEDs) |
| `progress` | 0100 (% of ring lit, mode `1`) | | `progress` | 0100 (% of ring lit, mode `1`) |
| `digit` | 010 (mode `2`, same segment maps as built-in digits) | | `digit` | 010 (mode `2`, segment maps in `led_ring.c`) |
| `r`, `g`, `b` | Color 0255 | | `r`, `g`, `b` | Color 0255 |
| `intensity` | Brightness 0255 (scaled into RGB; `0` → firmware default ~5 %) | | `intensity` | Brightness 0255 (scaled into RGB; `0` → firmware default ~5 %) |
| `blink_ms`, `blink_count` | Pulse length and count (mode `3`; defaults 350 ms, 1) | | `blink_ms`, `blink_count` | Pulse length and count (mode `3`; defaults 350 ms, 1) |
| `client_id` | `0` = master ring only; `>0` = ESP-NOW unicast to one slave |
| `all_clients` | Broadcast to all registered slaves |
| `slaves_only` | With `all_clients`: do not change master ring |
**Response:** `led_ring_progress_response` (`success`, `mode`, `progress`, `digit`). **Response:** `led_ring_progress_response` (`success`, `mode`, `progress`, `digit`, `client_id`, `slaves_updated`).
Slaves receive the same command via ESP-NOW `ESPNOW_LED_RING` and run it locally.
```bash ```bash
go run . -port /dev/ttyUSB0 led-ring -mode progress -progress 75 -g 80 -b 255 go run . -port /dev/ttyUSB0 led-ring -mode progress -progress 75 -g 80 -b 255
@ -370,6 +441,8 @@ go run . -port /dev/ttyUSB0 led-ring -mode blink -g 255 -blink-count 2
go run . -port /dev/ttyUSB0 find-me go run . -port /dev/ttyUSB0 find-me
go run . -port /dev/ttyUSB0 find-me -client 16 go run . -port /dev/ttyUSB0 find-me -client 16
go run . -port /dev/ttyUSB0 led-ring -mode find-me go run . -port /dev/ttyUSB0 led-ring -mode find-me
go run . -port /dev/ttyUSB0 led-ring -mode color -r 255 -g 0 -b 0 -client 16
go run . -port /dev/ttyUSB0 led-ring -mode digit -digit 5 -all
``` ```
### CLIENT_INFO command ### CLIENT_INFO command
@ -378,7 +451,7 @@ go run . -port /dev/ttyUSB0 led-ring -mode find-me
**Response:** payload `04` + nanopb `UartMessage` with `client_info_response.clients` — one `ClientInfo` per registered slave (from ESP-NOW `SLAVE_INFO`). **Response:** payload `04` + nanopb `UartMessage` with `client_info_response.clients` — one `ClientInfo` per registered slave (from ESP-NOW `SLAVE_INFO`).
Fields per client: `id`, `mac`, `version`, `available`, `used`, `last_ping`, `last_success_ping`**milliseconds since** the last packet / last successful heartbeat (computed when `CLIENT_INFO` is answered; typically 01000 while the slave is heartbeating every 1 s). Fields per client: `id`, `mac`, `version`, `available`, `used`, `last_ping`, `last_success_ping`, `tap_notify_single`, `tap_notify_double`, `tap_notify_triple`**milliseconds since** the last packet / last successful heartbeat (computed when `CLIENT_INFO` is answered; typically 01000 while the slave is heartbeating every 1 s).
## Client registry ## Client registry
@ -389,6 +462,7 @@ Fields per client: `id`, `mac`, `version`, `available`, `used`, `last_ping`, `la
| `client_registry_heartbeat(mac, id, version, …)` | Same as upsert for heartbeats; reactivates inactive clients | | `client_registry_heartbeat(mac, id, version, …)` | Same as upsert for heartbeats; reactivates inactive clients |
| `client_registry_check_timeouts(timeout_ms)` | Mark stale clients inactive (master monitor task) | | `client_registry_check_timeouts(timeout_ms)` | Mark stale clients inactive (master monitor task) |
| `client_registry_count()` / `client_registry_at(i)` | Iterate for UART encoding | | `client_registry_count()` / `client_registry_at(i)` | Iterate for UART encoding |
| `client_registry_set_tap_notify()` / `client_registry_take_tap()` | Tap notify flags + short-lived tap cache (16 ms) |
Slaves register when the master receives `SLAVE_INFO` on the matching network; `HEARTBEAT` keeps them marked available. The registry **MAC is always the ESP-NOW source address** (`recv_info.src_addr`), not the optional `mac` bytes in the protobuf (used only on the wire for debugging). Slaves register when the master receives `SLAVE_INFO` on the matching network; `HEARTBEAT` keeps them marked available. The registry **MAC is always the ESP-NOW source address** (`recv_info.src_addr`), not the optional `mac` bytes in the protobuf (used only on the wire for debugging).
@ -454,7 +528,9 @@ Target: ESP32-S3. Close serial monitor on the UART adapter port before running `
| `cmd/cmd_version.c/h` | VERSION handler | | `cmd/cmd_version.c/h` | VERSION handler |
| `cmd/cmd_client_info.c/h` | CLIENT_INFO handler | | `cmd/cmd_client_info.c/h` | CLIENT_INFO handler |
| `client_registry.c/h` | Registered slave table | | `client_registry.c/h` | Registered slave table |
| `bosch456.c/h` | BMA456H I2C driver, accel poll, tap INT, deadzone filter | | `bosch456.c/h` | BMA456H I2C driver, accel poll, on-demand read, tap INT, deadzone filter |
| `cmd/cmd_tap_notify.c` | UART `TAP_NOTIFY` — ESP-NOW tap notify config |
| `cmd/cmd_cache_status.c` | UART `CACHE_STATUS` — subscribed accel + tap cache poll |
| `board_input.c/h` | Taster GPIO12, LiPo ADC on GPIO1 / GPIO12 | | `board_input.c/h` | Taster GPIO12, LiPo ADC on GPIO1 / GPIO12 |
| `pod_settings.c/h` | NVS persistence (accel deadzone, …) | | `pod_settings.c/h` | NVS persistence (accel deadzone, …) |
| `led_ring.c/h` | LED ring (digit display, progress bar) | | `led_ring.c/h` | LED ring (digit display, progress bar) |

View File

@ -6,7 +6,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/idf_additions.h" #include "freertos/idf_additions.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include <stdint.h> #include <string.h>
static const char *TAG_BTN = "[BTN]"; static const char *TAG_BTN = "[BTN]";
static const char *TAG_LIPO = "[LIPO]"; static const char *TAG_LIPO = "[LIPO]";
@ -14,6 +14,8 @@ static const char *TAG_LIPO = "[LIPO]";
#define LIPO_SAMPLE_INTERVAL_MS 10000 #define LIPO_SAMPLE_INTERVAL_MS 10000
#define BUTTON_QUEUE_LEN 4 #define BUTTON_QUEUE_LEN 4
#define BUTTON_DEBOUNCE_MS 80 #define BUTTON_DEBOUNCE_MS 80
#define LIPO_ADC_FULL_SCALE_MV 3300
#define LIPO_ADC_MAX_RAW 4095
static QueueHandle_t s_button_queue; static QueueHandle_t s_button_queue;
static adc_oneshot_unit_handle_t s_adc; static adc_oneshot_unit_handle_t s_adc;
@ -47,33 +49,51 @@ static esp_err_t adc_init_channel(int gpio, adc_channel_t *out_ch, bool *out_ok)
return ESP_OK; return ESP_OK;
} }
static uint32_t raw_to_mv(int raw) {
if (raw < 0) {
return 0;
}
return (uint32_t)((raw * LIPO_ADC_FULL_SCALE_MV) / LIPO_ADC_MAX_RAW);
}
static void sample_one_channel(adc_channel_t ch, bool ok, uint32_t *mv_out,
bool *valid_out) {
*valid_out = false;
*mv_out = 0;
if (!ok || s_adc == NULL) {
return;
}
int raw = 0;
if (adc_oneshot_read(s_adc, ch, &raw) == ESP_OK) {
*valid_out = true;
*mv_out = raw_to_mv(raw);
}
}
void board_input_read_lipo(board_lipo_reading_t *out) {
if (out == NULL) {
return;
}
memset(out, 0, sizeof(*out));
sample_one_channel(s_lipo1_ch, s_lipo1_ok, &out->lipo1_mv, &out->lipo1_valid);
sample_one_channel(s_lipo2_ch, s_lipo2_ok, &out->lipo2_mv, &out->lipo2_valid);
}
static void lipo_monitor_task(void *param) { static void lipo_monitor_task(void *param) {
(void)param; (void)param;
ESP_LOGI(TAG_LIPO, "monitor task (interval %d ms)", LIPO_SAMPLE_INTERVAL_MS); ESP_LOGI(TAG_LIPO, "monitor task (interval %d ms)", LIPO_SAMPLE_INTERVAL_MS);
while (1) { while (1) {
int raw1 = -1; board_lipo_reading_t reading;
int raw2 = -1; board_input_read_lipo(&reading);
int mv1 = -1;
int mv2 = -1;
if (s_lipo1_ok) {
raw1 = 0;
if (adc_oneshot_read(s_adc, s_lipo1_ch, &raw1) == ESP_OK) {
mv1 = (raw1 * 3300) / 4095;
}
}
if (s_lipo2_ok) {
raw2 = 0;
if (adc_oneshot_read(s_adc, s_lipo2_ch, &raw2) == ESP_OK) {
mv2 = (raw2 * 3300) / 4095;
}
}
ESP_LOGI(TAG_LIPO, ESP_LOGI(TAG_LIPO,
"LIPO1 GPIO%d raw=%d (~%d mV) LIPO2 GPIO%d raw=%d (~%d mV)", "LIPO1 GPIO%d %s %lu mV LIPO2 GPIO%d %s %lu mV",
V_LIPO_1_GPIO, raw1, mv1, V_LIPO_2_GPIO, raw2, mv2); V_LIPO_1_GPIO, reading.lipo1_valid ? "ok" : "n/a",
(unsigned long)reading.lipo1_mv, V_LIPO_2_GPIO,
reading.lipo2_valid ? "ok" : "n/a",
(unsigned long)reading.lipo2_mv);
vTaskDelay(pdMS_TO_TICKS(LIPO_SAMPLE_INTERVAL_MS)); vTaskDelay(pdMS_TO_TICKS(LIPO_SAMPLE_INTERVAL_MS));
} }

View File

@ -1,12 +1,25 @@
#ifndef BOARD_INPUT_H #ifndef BOARD_INPUT_H
#define BOARD_INPUT_H #define BOARD_INPUT_H
#include <stdbool.h>
#include <stdint.h>
#include "esp_err.h" #include "esp_err.h"
typedef struct {
bool lipo1_valid;
bool lipo2_valid;
uint32_t lipo1_mv;
uint32_t lipo2_mv;
} board_lipo_reading_t;
/** /**
* Button (log on press) and LiPo ADC sampling (log every 10 s). * Button (log on press) and LiPo ADC sampling (background log every 10 s).
* TODO: Pin assignments come from powerpod.h and may not match final hardware yet. * TODO: Pin assignments come from powerpod.h and may not match final hardware yet.
*/ */
esp_err_t board_input_init(void); esp_err_t board_input_init(void);
/** On-demand ADC read of both LiPo sense inputs (if configured). */
void board_input_read_lipo(board_lipo_reading_t *out);
#endif #endif

View File

@ -2,7 +2,7 @@
* BMA456H integration for Powerpod (ESP-IDF I2C master + Bosch SensorAPI). * BMA456H integration for Powerpod (ESP-IDF I2C master + Bosch SensorAPI).
* *
* Polls accelerometer at 10 Hz; tap events arrive on BMA456_INT_GPIO. * Polls accelerometer at 10 Hz; tap events arrive on BMA456_INT_GPIO.
* Accel logging is filtered in software (deadzone); see ACCEL_DEADZONE UART command. * Accel logging is filtered in software (deadzone); slaves stream samples via ESP-NOW.
*/ */
#include "bosch456.h" #include "bosch456.h"
@ -14,6 +14,7 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "freertos/idf_additions.h" #include "freertos/idf_additions.h"
#include "freertos/semphr.h"
#include <rom/ets_sys.h> #include <rom/ets_sys.h>
#include <string.h> #include <string.h>
@ -34,6 +35,9 @@ static int16_t s_last_z;
static bool s_have_last_sample; static bool s_have_last_sample;
static volatile bool s_int_pending; static volatile bool s_int_pending;
static SemaphoreHandle_t s_accel_mutex;
static bma456_tap_handler_t s_tap_handler;
static void *s_tap_handler_ctx;
static esp_err_t check_bma4(const char *api_name, int8_t rslt); static esp_err_t check_bma4(const char *api_name, int8_t rslt);
@ -121,6 +125,35 @@ void bma456_set_accel_deadzone(uint32_t deadzone_lsb) {
uint32_t bma456_get_accel_deadzone(void) { return s_accel_deadzone; } uint32_t bma456_get_accel_deadzone(void) { return s_accel_deadzone; }
void bma456_set_tap_handler(bma456_tap_handler_t handler, void *ctx) {
s_tap_handler = handler;
s_tap_handler_ctx = ctx;
}
esp_err_t bma456_read_accel(int16_t *x, int16_t *y, int16_t *z) {
if (!s_bma456_ready || x == NULL || y == NULL || z == NULL) {
return ESP_ERR_INVALID_STATE;
}
if (s_accel_mutex == NULL ||
xSemaphoreTake(s_accel_mutex, pdMS_TO_TICKS(500)) != pdTRUE) {
return ESP_ERR_TIMEOUT;
}
struct bma4_accel sens_data = {0};
int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456);
xSemaphoreGive(s_accel_mutex);
if (ret != BMA4_OK) {
bma4_error_codes_print_result("bma4_read_accel_xyz", ret);
return ESP_FAIL;
}
*x = sens_data.x;
*y = sens_data.y;
*z = sens_data.z;
return ESP_OK;
}
void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z) { void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z) {
if (!s_bma456_ready || !sample_exceeds_deadzone(x, y, z)) { if (!s_bma456_ready || !sample_exceeds_deadzone(x, y, z)) {
return; return;
@ -157,10 +190,19 @@ static void handle_tap_interrupt(void) {
if (tap_out.single_tap) { if (tap_out.single_tap) {
ESP_LOGI(TAG, "tap: single"); ESP_LOGI(TAG, "tap: single");
if (s_tap_handler != NULL) {
s_tap_handler(BMA456_TAP_SINGLE, s_tap_handler_ctx);
}
} else if (tap_out.double_tap) { } else if (tap_out.double_tap) {
ESP_LOGI(TAG, "tap: double"); ESP_LOGI(TAG, "tap: double");
if (s_tap_handler != NULL) {
s_tap_handler(BMA456_TAP_DOUBLE, s_tap_handler_ctx);
}
} else if (tap_out.triple_tap) { } else if (tap_out.triple_tap) {
ESP_LOGI(TAG, "tap: triple"); ESP_LOGI(TAG, "tap: triple");
if (s_tap_handler != NULL) {
s_tap_handler(BMA456_TAP_TRIPLE, s_tap_handler_ctx);
}
} }
} }
@ -187,12 +229,20 @@ static void read_sensor_task(void *param) {
struct bma4_accel sens_data = {0}; struct bma4_accel sens_data = {0};
while (1) { while (1) {
bool got_sample = false;
if (s_accel_mutex != NULL &&
xSemaphoreTake(s_accel_mutex, pdMS_TO_TICKS(500)) == pdTRUE) {
int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456); int8_t ret = bma4_read_accel_xyz(&sens_data, &s_bma456);
xSemaphoreGive(s_accel_mutex);
if (ret == BMA4_OK) { if (ret == BMA4_OK) {
bma456_report_accel_if_changed(sens_data.x, sens_data.y, sens_data.z); got_sample = true;
} else { } else {
bma4_error_codes_print_result("bma4_read_accel_xyz", ret); bma4_error_codes_print_result("bma4_read_accel_xyz", ret);
} }
}
if (got_sample) {
bma456_report_accel_if_changed(sens_data.x, sens_data.y, sens_data.z);
}
if (s_int_pending) { if (s_int_pending) {
s_int_pending = false; s_int_pending = false;
@ -343,6 +393,13 @@ esp_err_t init_bma456(i2c_master_bus_handle_t bus_handle) {
goto fail; goto fail;
} }
if (s_accel_mutex == NULL) {
s_accel_mutex = xSemaphoreCreateMutex();
if (s_accel_mutex == NULL) {
goto fail;
}
}
if (xTaskCreate(read_sensor_task, "bma456_poll", 4096, NULL, 1, NULL) != if (xTaskCreate(read_sensor_task, "bma456_poll", 4096, NULL, 1, NULL) !=
pdPASS) { pdPASS) {
goto fail; goto fail;

View File

@ -35,4 +35,19 @@ uint32_t bma456_get_accel_deadzone(void);
/** Log accel when any axis moved more than deadzone since last reported sample. */ /** Log accel when any axis moved more than deadzone since last reported sample. */
void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z); void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z);
/** Tap kinds from BMA456H multitap output. */
typedef enum {
BMA456_TAP_SINGLE = 1,
BMA456_TAP_DOUBLE = 2,
BMA456_TAP_TRIPLE = 3,
} bma456_tap_kind_t;
typedef void (*bma456_tap_handler_t)(bma456_tap_kind_t kind, void *ctx);
/** Optional callback invoked from sensor task on tap interrupt (may be NULL). */
void bma456_set_tap_handler(bma456_tap_handler_t handler, void *ctx);
/** On-demand read of current accel XYZ (raw LSB). Returns ESP_ERR_INVALID_STATE if sensor not ready. */
esp_err_t bma456_read_accel(int16_t *x, int16_t *y, int16_t *z);
#endif #endif

View File

@ -14,6 +14,11 @@ typedef struct {
static client_slot_t s_clients[CLIENT_REGISTRY_MAX]; static client_slot_t s_clients[CLIENT_REGISTRY_MAX];
static struct {
board_lipo_reading_t reading;
uint32_t updated_at;
} s_master_battery;
uint32_t client_registry_now_ms(void) { uint32_t client_registry_now_ms(void) {
return (uint32_t)(xTaskGetTickCount() * portTICK_PERIOD_MS); return (uint32_t)(xTaskGetTickCount() * portTICK_PERIOD_MS);
} }
@ -241,6 +246,286 @@ size_t client_registry_set_accel_deadzone_all(uint32_t deadzone) {
return n; return n;
} }
static void clear_client_accel(client_slot_t *slot) {
if (slot == NULL) {
return;
}
slot->info.accel_valid = false;
slot->info.accel_x = 0;
slot->info.accel_y = 0;
slot->info.accel_z = 0;
slot->info.accel_updated_at = 0;
}
static void clear_client_tap(client_slot_t *slot) {
if (slot == NULL) {
return;
}
slot->info.tap_valid = false;
slot->info.tap_kind = 0;
slot->info.tap_updated_at = 0;
}
static bool tap_kind_enabled(const client_info_t *info, uint32_t kind) {
if (info == NULL) {
return false;
}
switch (kind) {
case 1:
return info->tap_notify_single;
case 2:
return info->tap_notify_double;
case 3:
return info->tap_notify_triple;
default:
return false;
}
}
esp_err_t client_registry_set_accel_stream(uint32_t client_id, bool enabled) {
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active || s_clients[i].info.id != client_id) {
continue;
}
s_clients[i].info.accel_stream_enabled = enabled;
if (!enabled) {
clear_client_accel(&s_clients[i]);
}
return ESP_OK;
}
return ESP_ERR_NOT_FOUND;
}
esp_err_t client_registry_get_accel_stream(uint32_t client_id,
bool *enabled_out) {
if (enabled_out == NULL) {
return ESP_ERR_INVALID_ARG;
}
const client_info_t *info = client_registry_find_by_id(client_id);
if (info == NULL) {
return ESP_ERR_NOT_FOUND;
}
*enabled_out = info->accel_stream_enabled;
return ESP_OK;
}
size_t client_registry_set_accel_stream_all(bool enabled) {
size_t n = 0;
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active) {
continue;
}
s_clients[i].info.accel_stream_enabled = enabled;
if (!enabled) {
clear_client_accel(&s_clients[i]);
}
n++;
}
return n;
}
esp_err_t client_registry_update_accel(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, int16_t x, int16_t y,
int16_t z) {
if (mac == NULL) {
return ESP_ERR_INVALID_ARG;
}
client_slot_t *slot = find_slot(mac);
if (slot == NULL) {
return ESP_ERR_NOT_FOUND;
}
if (slot->info.id != slave_id) {
return ESP_ERR_INVALID_ARG;
}
if (!slot->info.accel_stream_enabled) {
return ESP_ERR_INVALID_STATE;
}
slot->info.accel_x = x;
slot->info.accel_y = y;
slot->info.accel_z = z;
slot->info.accel_valid = true;
slot->info.accel_updated_at = now_ms();
return ESP_OK;
}
esp_err_t client_registry_set_tap_notify(uint32_t client_id, bool single,
bool double_tap, bool triple) {
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active || s_clients[i].info.id != client_id) {
continue;
}
s_clients[i].info.tap_notify_single = single;
s_clients[i].info.tap_notify_double = double_tap;
s_clients[i].info.tap_notify_triple = triple;
if (!single && !double_tap && !triple) {
clear_client_tap(&s_clients[i]);
}
return ESP_OK;
}
return ESP_ERR_NOT_FOUND;
}
esp_err_t client_registry_get_tap_notify(uint32_t client_id, bool *single_out,
bool *double_tap_out,
bool *triple_out) {
if (single_out == NULL || double_tap_out == NULL || triple_out == NULL) {
return ESP_ERR_INVALID_ARG;
}
const client_info_t *info = client_registry_find_by_id(client_id);
if (info == NULL) {
return ESP_ERR_NOT_FOUND;
}
*single_out = info->tap_notify_single;
*double_tap_out = info->tap_notify_double;
*triple_out = info->tap_notify_triple;
return ESP_OK;
}
size_t client_registry_set_tap_notify_all(bool single, bool double_tap,
bool triple) {
size_t n = 0;
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active) {
continue;
}
s_clients[i].info.tap_notify_single = single;
s_clients[i].info.tap_notify_double = double_tap;
s_clients[i].info.tap_notify_triple = triple;
if (!single && !double_tap && !triple) {
clear_client_tap(&s_clients[i]);
}
n++;
}
return n;
}
esp_err_t client_registry_update_tap(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, uint32_t kind) {
if (mac == NULL || kind < 1 || kind > 3) {
return ESP_ERR_INVALID_ARG;
}
client_slot_t *slot = find_slot(mac);
if (slot == NULL) {
return ESP_ERR_NOT_FOUND;
}
if (slot->info.id != slave_id) {
return ESP_ERR_INVALID_ARG;
}
if (!tap_kind_enabled(&slot->info, kind)) {
return ESP_ERR_INVALID_STATE;
}
slot->info.tap_kind = kind;
slot->info.tap_valid = true;
slot->info.tap_updated_at = now_ms();
return ESP_OK;
}
void client_registry_expire_tap(client_info_t *info) {
if (info == NULL || !info->tap_valid) {
return;
}
if (client_registry_ms_since(info->tap_updated_at) >
CLIENT_REGISTRY_TAP_MAX_AGE_MS) {
info->tap_valid = false;
info->tap_kind = 0;
info->tap_updated_at = 0;
}
}
void client_registry_clear_tap(client_info_t *info) {
if (info == NULL) {
return;
}
info->tap_valid = false;
info->tap_kind = 0;
info->tap_updated_at = 0;
}
bool client_registry_take_tap(uint32_t client_id, uint32_t *kind_out,
uint32_t *age_ms_out) {
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active || s_clients[i].info.id != client_id) {
continue;
}
client_info_t *info = &s_clients[i].info;
client_registry_expire_tap(info);
if (!info->tap_valid) {
return false;
}
if (kind_out != NULL) {
*kind_out = info->tap_kind;
}
if (age_ms_out != NULL) {
*age_ms_out = client_registry_ms_since(info->tap_updated_at);
}
client_registry_clear_tap(info);
return true;
}
return false;
}
void client_registry_set_master_battery(const board_lipo_reading_t *reading) {
if (reading == NULL) {
return;
}
s_master_battery.reading = *reading;
s_master_battery.updated_at = now_ms();
}
bool client_registry_get_master_battery(board_lipo_reading_t *reading_out,
uint32_t *age_ms_out) {
if (reading_out == NULL) {
return false;
}
*reading_out = s_master_battery.reading;
if (age_ms_out != NULL) {
*age_ms_out = client_registry_ms_since(s_master_battery.updated_at);
}
return s_master_battery.updated_at != 0;
}
esp_err_t client_registry_update_battery(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, bool lipo1_valid,
uint32_t lipo1_mv, bool lipo2_valid,
uint32_t lipo2_mv) {
if (mac == NULL) {
return ESP_ERR_INVALID_ARG;
}
client_slot_t *slot = find_slot(mac);
if (slot == NULL) {
bool is_new = false;
esp_err_t err = client_registry_upsert(mac, slave_id, 0, true, false, &is_new);
if (err != ESP_OK) {
return err;
}
slot = find_slot(mac);
if (slot == NULL) {
return ESP_ERR_NOT_FOUND;
}
ESP_LOGI(TAG, "battery auto-registered id=%lu (report before heartbeat)",
(unsigned long)slave_id);
}
if (slot->info.id != slave_id) {
ESP_LOGW(TAG, "battery id %lu → %lu for mac %02x:…:%02x",
(unsigned long)slot->info.id, (unsigned long)slave_id, mac[0],
mac[5]);
slot->info.id = slave_id;
}
slot->info.lipo1_valid = lipo1_valid;
slot->info.lipo2_valid = lipo2_valid;
slot->info.lipo1_mv = lipo1_mv;
slot->info.lipo2_mv = lipo2_mv;
slot->info.battery_updated_at = now_ms();
return ESP_OK;
}
const client_info_t *client_registry_at(size_t index) { const client_info_t *client_registry_at(size_t index) {
size_t n = 0; size_t n = 0;
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) { for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {

View File

@ -1,6 +1,7 @@
#ifndef CLIENT_REGISTRY_H #ifndef CLIENT_REGISTRY_H
#define CLIENT_REGISTRY_H #define CLIENT_REGISTRY_H
#include "board_input.h"
#include "esp_err.h" #include "esp_err.h"
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -21,9 +22,33 @@ typedef struct {
uint32_t version; uint32_t version;
/** Accel deadzone in raw LSB per axis (master copy for ESP-NOW config). */ /** Accel deadzone in raw LSB per axis (master copy for ESP-NOW config). */
uint32_t accel_deadzone; uint32_t accel_deadzone;
/** Latest accel from slave ESP-NOW stream (master only). */
bool accel_valid;
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
uint32_t accel_updated_at;
/** Host-enabled ESP-NOW accel stream to master. */
bool accel_stream_enabled;
/** Host-enabled ESP-NOW tap notify flags. */
bool tap_notify_single;
bool tap_notify_double;
bool tap_notify_triple;
/** Latest tap from slave ESP-NOW (master only, short-lived cache). */
bool tap_valid;
uint32_t tap_kind;
uint32_t tap_updated_at;
/** Latest LiPo ADC from slave ESP-NOW battery report (~30 s). */
bool lipo1_valid;
bool lipo2_valid;
uint32_t lipo1_mv;
uint32_t lipo2_mv;
uint32_t battery_updated_at;
} client_info_t; } client_info_t;
#define CLIENT_REGISTRY_DEFAULT_ACCEL_DEADZONE 100u #define CLIENT_REGISTRY_DEFAULT_ACCEL_DEADZONE 100u
/** Tap events older than this are discarded (matches accel stream interval). */
#define CLIENT_REGISTRY_TAP_MAX_AGE_MS 16u
void client_registry_init(void); void client_registry_init(void);
@ -63,4 +88,49 @@ esp_err_t client_registry_get_accel_deadzone(uint32_t client_id,
/** Push deadzone to all active registry entries; returns count updated. */ /** Push deadzone to all active registry entries; returns count updated. */
size_t client_registry_set_accel_deadzone_all(uint32_t deadzone); size_t client_registry_set_accel_deadzone_all(uint32_t deadzone);
/** Store latest accel sample from a slave (matched by sender MAC). */
esp_err_t client_registry_update_accel(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, int16_t x, int16_t y,
int16_t z);
esp_err_t client_registry_set_accel_stream(uint32_t client_id, bool enabled);
esp_err_t client_registry_get_accel_stream(uint32_t client_id, bool *enabled_out);
size_t client_registry_set_accel_stream_all(bool enabled);
esp_err_t client_registry_set_tap_notify(uint32_t client_id, bool single,
bool double_tap, bool triple);
esp_err_t client_registry_get_tap_notify(uint32_t client_id, bool *single_out,
bool *double_tap_out,
bool *triple_out);
size_t client_registry_set_tap_notify_all(bool single, bool double_tap,
bool triple);
/** Store tap event from slave (matched by sender MAC). kind: 1=single, 2=double, 3=triple. */
esp_err_t client_registry_update_tap(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, uint32_t kind);
/** Drop cached tap if older than CLIENT_REGISTRY_TAP_MAX_AGE_MS. */
void client_registry_expire_tap(client_info_t *info);
/** Clear cached tap after UART snapshot or expiry. */
void client_registry_clear_tap(client_info_t *info);
/**
* If client has a fresh tap (age <= CLIENT_REGISTRY_TAP_MAX_AGE_MS), copy it out
* and clear the cache. Returns true when an event was returned.
*/
bool client_registry_take_tap(uint32_t client_id, uint32_t *kind_out,
uint32_t *age_ms_out);
/** Master local LiPo (client_id 0 in UART battery responses). */
void client_registry_set_master_battery(const board_lipo_reading_t *reading);
bool client_registry_get_master_battery(board_lipo_reading_t *reading_out,
uint32_t *age_ms_out);
/** Store latest battery report from a slave (matched by sender MAC). */
esp_err_t client_registry_update_battery(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, bool lipo1_valid,
uint32_t lipo1_mv, bool lipo2_valid,
uint32_t lipo2_mv);
#endif #endif

View File

@ -0,0 +1,96 @@
#include "client_registry.h"
#include "cmd_accel_stream.h"
#include "esp_log.h"
#include "esp_now_comm.h"
#include "uart_cmd.h"
static const char *TAG = "[ACCEL_STREAM]";
static void reply(bool enabled, uint32_t client_id, bool success,
uint32_t slaves_updated) {
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_ACCEL_STREAM,
alox_UartMessage_accel_stream_response_tag);
response.payload.accel_stream_response.enabled = enabled;
response.payload.accel_stream_response.client_id = client_id;
response.payload.accel_stream_response.success = success;
response.payload.accel_stream_response.slaves_updated = slaves_updated;
uart_cmd_send(&response, TAG);
}
static esp_err_t push_stream_to_slave(const client_info_t *client, bool enable) {
if (client == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = client_registry_set_accel_stream(client->id, enable);
if (err != ESP_OK) {
return err;
}
return esp_now_comm_send_accel_stream(client->mac, client->id, enable);
}
static void handle_accel_stream(const uint8_t *data, size_t len) {
alox_UartMessage uart_msg;
alox_AccelStreamRequest req = alox_AccelStreamRequest_init_zero;
if (uart_cmd_decode(data, len, &uart_msg) == ESP_OK) {
const alox_AccelStreamRequest *req_ptr = UART_CMD_REQ(
&uart_msg, alox_UartMessage_accel_stream_request_tag, accel_stream_request);
if (req_ptr != NULL) {
req = *req_ptr;
}
}
if (req.write) {
if (req.all_clients) {
size_t n = client_registry_set_accel_stream_all(req.enable);
uint32_t sent = 0;
for (size_t i = 0; i < client_registry_count(); i++) {
const client_info_t *client = client_registry_at(i);
if (client == NULL) {
continue;
}
if (esp_now_comm_send_accel_stream(client->mac, client->id,
req.enable) == ESP_OK) {
sent++;
}
}
ESP_LOGI(TAG, "accel stream %s for %u/%u slaves",
req.enable ? "on" : "off", (unsigned)sent, (unsigned)n);
reply(req.enable, 0, sent > 0, sent);
return;
}
if (req.client_id == 0) {
ESP_LOGW(TAG, "client_id required (or all_clients)");
reply(req.enable, 0, false, 0);
return;
}
const client_info_t *client = client_registry_find_by_id(req.client_id);
if (client == NULL) {
ESP_LOGW(TAG, "client id %lu not found", (unsigned long)req.client_id);
reply(req.enable, req.client_id, false, 0);
return;
}
esp_err_t err = push_stream_to_slave(client, req.enable);
reply(req.enable, req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u);
return;
}
if (req.all_clients || req.client_id == 0) {
reply(false, 0, false, 0);
return;
}
bool enabled = false;
esp_err_t err = client_registry_get_accel_stream(req.client_id, &enabled);
reply(enabled, req.client_id, err == ESP_OK, 0);
}
void cmd_accel_stream_register(void) {
uart_cmd_register(alox_MessageType_ACCEL_STREAM, handle_accel_stream);
}

View File

@ -0,0 +1,6 @@
#ifndef CMD_ACCEL_STREAM_H
#define CMD_ACCEL_STREAM_H
void cmd_accel_stream_register(void);
#endif

119
main/cmd/cmd_battery.c Normal file
View File

@ -0,0 +1,119 @@
#include "cmd_battery.h"
#include "board_input.h"
#include "client_registry.h"
#include "esp_log.h"
#include "uart_cmd.h"
static const char *TAG = "[BATTERY]";
static void fill_lipo(alox_LipoReading *dst, bool *has_dst, bool valid,
uint32_t mv) {
if (dst == NULL || has_dst == NULL) {
return;
}
*has_dst = true;
dst->valid = valid;
dst->voltage_mv = valid ? mv : 0;
}
static bool append_battery_sample(alox_BatteryStatusResponse *resp,
uint32_t client_id, bool lipo1_valid,
uint32_t lipo1_mv, bool lipo2_valid,
uint32_t lipo2_mv, uint32_t age_ms) {
if (resp->samples_count >=
sizeof(resp->samples) / sizeof(resp->samples[0])) {
return false;
}
alox_BatterySample *sample = &resp->samples[resp->samples_count++];
sample->client_id = client_id;
fill_lipo(&sample->lipo1, &sample->has_lipo1, lipo1_valid, lipo1_mv);
fill_lipo(&sample->lipo2, &sample->has_lipo2, lipo2_valid, lipo2_mv);
sample->age_ms = age_ms;
return lipo1_valid || lipo2_valid;
}
static bool append_master_cached(alox_BatteryStatusResponse *resp) {
board_lipo_reading_t reading;
uint32_t age_ms = 0;
if (!client_registry_get_master_battery(&reading, &age_ms)) {
board_input_read_lipo(&reading);
client_registry_set_master_battery(&reading);
age_ms = 0;
}
return append_battery_sample(resp, 0, reading.lipo1_valid, reading.lipo1_mv,
reading.lipo2_valid, reading.lipo2_mv, age_ms);
}
static bool append_slave_cached(alox_BatteryStatusResponse *resp,
const client_info_t *client) {
if (client == NULL) {
return false;
}
if (client->battery_updated_at == 0) {
return false;
}
return append_battery_sample(
resp, client->id, client->lipo1_valid, client->lipo1_mv,
client->lipo2_valid, client->lipo2_mv,
client_registry_ms_since(client->battery_updated_at));
}
static void handle_battery_status(const uint8_t *data, size_t len) {
alox_BatteryStatusRequest req = alox_BatteryStatusRequest_init_zero;
if (len > 0) {
alox_UartMessage uart_msg;
if (uart_cmd_decode(data, len, &uart_msg) == ESP_OK) {
const alox_BatteryStatusRequest *req_ptr = UART_CMD_REQ(
&uart_msg, alox_UartMessage_battery_status_request_tag,
battery_status_request);
if (req_ptr != NULL) {
req = *req_ptr;
}
}
}
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_BATTERY_STATUS,
alox_UartMessage_battery_status_response_tag);
alox_BatteryStatusResponse *resp =
&response.payload.battery_status_response;
resp->success = false;
resp->samples_count = 0;
bool any = false;
if (req.all_clients) {
any |= append_master_cached(resp);
for (size_t i = 0; i < client_registry_count(); i++) {
const client_info_t *client = client_registry_at(i);
if (client == NULL) {
continue;
}
any |= append_slave_cached(resp, client);
}
ESP_LOGI(TAG, "battery cache all_clients → %u samples",
(unsigned)resp->samples_count);
} else if (req.client_id == 0) {
any = append_master_cached(resp);
ESP_LOGI(TAG, "battery cache master");
} else {
const client_info_t *client = client_registry_find_by_id(req.client_id);
if (client != NULL) {
any = append_slave_cached(resp, client);
} else {
ESP_LOGW(TAG, "client %lu not in registry", (unsigned long)req.client_id);
}
}
resp->success = any;
uart_cmd_send(&response, TAG);
}
void cmd_battery_register(void) {
uart_cmd_register(alox_MessageType_BATTERY_STATUS, handle_battery_status);
}

6
main/cmd/cmd_battery.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef CMD_BATTERY_H
#define CMD_BATTERY_H
void cmd_battery_register(void);
#endif

View File

@ -0,0 +1,92 @@
#include "client_registry.h"
#include "cmd_cache_status.h"
#include "uart_cmd.h"
static const char *TAG = "[CACHE_STAT]";
static bool tap_notify_any(const client_info_t *client) {
return client != NULL &&
(client->tap_notify_single || client->tap_notify_double ||
client->tap_notify_triple);
}
static alox_TapKind tap_kind_from_registry(uint32_t kind) {
switch (kind) {
case 1:
return alox_TapKind_TAP_SINGLE;
case 2:
return alox_TapKind_TAP_DOUBLE;
case 3:
return alox_TapKind_TAP_TRIPLE;
default:
return alox_TapKind_TAP_NONE;
}
}
static void fill_cache_status(alox_CacheStatusResponse *out) {
if (out == NULL) {
return;
}
out->clients_count = 0;
size_t count = client_registry_count();
for (size_t i = 0; i < count; i++) {
const client_info_t *client = client_registry_at(i);
if (client == NULL) {
continue;
}
const bool want_accel = client->accel_stream_enabled;
const bool want_tap = tap_notify_any(client);
if (!want_accel && !want_tap) {
continue;
}
if (out->clients_count >=
sizeof(out->clients) / sizeof(out->clients[0])) {
break;
}
alox_CacheClientStatus *entry = &out->clients[out->clients_count++];
entry->client_id = client->id;
entry->has_accel = false;
entry->has_tap = false;
if (want_accel) {
entry->has_accel = true;
entry->accel.valid = client->accel_valid;
if (client->accel_valid) {
entry->accel.x = client->accel_x;
entry->accel.y = client->accel_y;
entry->accel.z = client->accel_z;
entry->accel.age_ms =
client_registry_ms_since(client->accel_updated_at);
}
}
if (want_tap) {
uint32_t kind = 0;
uint32_t age_ms = 0;
if (client_registry_take_tap(client->id, &kind, &age_ms)) {
entry->has_tap = true;
entry->tap.kind = tap_kind_from_registry(kind);
entry->tap.age_ms = age_ms;
}
}
}
}
static void handle_cache_status(const uint8_t *data, size_t len) {
(void)data;
(void)len;
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_CACHE_STATUS,
alox_UartMessage_cache_status_response_tag);
fill_cache_status(&response.payload.cache_status_response);
uart_cmd_send(&response, TAG);
}
void cmd_cache_status_register(void) {
uart_cmd_register(alox_MessageType_CACHE_STATUS, handle_cache_status);
}

View File

@ -0,0 +1,3 @@
#pragma once
void cmd_cache_status_register(void);

View File

@ -27,6 +27,10 @@ static bool encode_clients_list(pb_ostream_t *stream, const pb_field_t *field,
proto.last_success_ping = proto.last_success_ping =
client_registry_ms_since(client->last_success_ping_at); client_registry_ms_since(client->last_success_ping_at);
proto.version = client->version; proto.version = client->version;
proto.accel_stream_enabled = client->accel_stream_enabled;
proto.tap_notify_single = client->tap_notify_single;
proto.tap_notify_double = client->tap_notify_double;
proto.tap_notify_triple = client->tap_notify_triple;
proto.mac.funcs.encode = uart_cmd_encode_bytes; proto.mac.funcs.encode = uart_cmd_encode_bytes;
proto.mac.arg = &mac; proto.mac.arg = &mac;

View File

@ -48,6 +48,14 @@ static const char *message_type_name(uint16_t id) {
return "FIND_ME"; return "FIND_ME";
case alox_MessageType_RESTART: case alox_MessageType_RESTART:
return "RESTART"; return "RESTART";
case alox_MessageType_ACCEL_STREAM:
return "ACCEL_STREAM";
case alox_MessageType_BATTERY_STATUS:
return "BATTERY_STATUS";
case alox_MessageType_TAP_NOTIFY:
return "TAP_NOTIFY";
case alox_MessageType_CACHE_STATUS:
return "CACHE_STATUS";
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }

View File

@ -1,5 +1,7 @@
#include "cmd_led_ring.h" #include "cmd_led_ring.h"
#include "client_registry.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_now_comm.h"
#include "led_ring.h" #include "led_ring.h"
#include "uart_cmd.h" #include "uart_cmd.h"
@ -10,6 +12,7 @@ static const char *TAG = "[LED_RING_CMD]";
#define LED_RING_MODE_DIGIT 2 #define LED_RING_MODE_DIGIT 2
#define LED_RING_MODE_BLINK 3 #define LED_RING_MODE_BLINK 3
#define LED_RING_MODE_FIND_ME 4 #define LED_RING_MODE_FIND_ME 4
#define LED_RING_MODE_COLOR 5
static uint8_t clamp_u8(uint32_t v) { static uint8_t clamp_u8(uint32_t v) {
if (v > 255) { if (v > 255) {
@ -32,7 +35,82 @@ static uint8_t resolve_intensity(uint32_t intensity) {
return clamp_u8(intensity); return clamp_u8(intensity);
} }
static void reply(bool success, uint32_t mode, uint32_t progress, uint32_t digit) { bool cmd_led_ring_apply(const alox_LedRingProgressRequest *req) {
if (req == NULL) {
return false;
}
uint32_t mode = req->mode;
uint8_t r = clamp_u8(req->r);
uint8_t g = clamp_u8(req->g);
uint8_t b = clamp_u8(req->b);
uint8_t intensity = resolve_intensity(req->intensity);
led_command_t cmd = {0};
switch (mode) {
case LED_RING_MODE_CLEAR:
cmd.mode = LED_CMD_CLEAR;
led_ring_send_command(&cmd);
return true;
case LED_RING_MODE_COLOR:
cmd.mode = LED_CMD_SET_COLOR;
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
led_ring_send_command(&cmd);
return true;
case LED_RING_MODE_PROGRESS: {
cmd.mode = LED_CMD_PROGRESS;
cmd.progress = clamp_progress(req->progress);
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
led_ring_send_command(&cmd);
return true;
}
case LED_RING_MODE_DIGIT:
if (req->digit > 10) {
return false;
}
cmd.mode = LED_CMD_SET_DIGIT;
cmd.value = (uint8_t)req->digit;
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
led_ring_send_command(&cmd);
return true;
case LED_RING_MODE_FIND_ME:
led_ring_find_me();
return true;
case LED_RING_MODE_BLINK:
cmd.mode = LED_CMD_BLINK;
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
cmd.blink_ms = (uint16_t)(req->blink_ms > 0 ? req->blink_ms : 350);
cmd.blink_count = req->blink_count > 0 ? (uint8_t)req->blink_count : 1;
if (cmd.blink_count == 0) {
cmd.blink_count = 1;
}
led_ring_send_command(&cmd);
return true;
default:
return false;
}
}
static void reply(bool success, uint32_t mode, uint32_t progress, uint32_t digit,
uint32_t client_id, uint32_t slaves_updated) {
alox_UartMessage response; alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_LED_RING, uart_cmd_init_response(&response, alox_MessageType_LED_RING,
alox_UartMessage_led_ring_progress_response_tag); alox_UartMessage_led_ring_progress_response_tag);
@ -40,16 +118,26 @@ static void reply(bool success, uint32_t mode, uint32_t progress, uint32_t digit
response.payload.led_ring_progress_response.mode = mode; response.payload.led_ring_progress_response.mode = mode;
response.payload.led_ring_progress_response.progress = progress; response.payload.led_ring_progress_response.progress = progress;
response.payload.led_ring_progress_response.digit = digit; response.payload.led_ring_progress_response.digit = digit;
response.payload.led_ring_progress_response.client_id = client_id;
response.payload.led_ring_progress_response.slaves_updated = slaves_updated;
uart_cmd_send(&response, TAG); uart_cmd_send(&response, TAG);
} }
static esp_err_t push_led_ring_to_slave(const client_info_t *client,
const alox_LedRingProgressRequest *req) {
if (client == NULL || req == NULL) {
return ESP_ERR_INVALID_ARG;
}
return esp_now_comm_send_led_ring(client->mac, client->id, req);
}
static void handle_led_ring(const uint8_t *data, size_t len) { static void handle_led_ring(const uint8_t *data, size_t len) {
alox_UartMessage uart_msg; alox_UartMessage uart_msg;
alox_LedRingProgressRequest req = alox_LedRingProgressRequest_init_zero; alox_LedRingProgressRequest req = alox_LedRingProgressRequest_init_zero;
if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) { if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) {
ESP_LOGW(TAG, "decode failed"); ESP_LOGW(TAG, "decode failed");
reply(false, 0, 0, 0); reply(false, 0, 0, 0, 0, 0);
return; return;
} }
@ -61,84 +149,53 @@ static void handle_led_ring(const uint8_t *data, size_t len) {
} }
uint32_t mode = req.mode; uint32_t mode = req.mode;
uint8_t r = clamp_u8(req.r);
uint8_t g = clamp_u8(req.g);
uint8_t b = clamp_u8(req.b);
uint8_t intensity = resolve_intensity(req.intensity);
led_command_t cmd = {0}; if (req.all_clients) {
size_t n = client_registry_count();
switch (mode) { uint32_t sent = 0;
case LED_RING_MODE_CLEAR: for (size_t i = 0; i < n; i++) {
cmd.mode = LED_CMD_CLEAR; const client_info_t *client = client_registry_at(i);
led_ring_send_command(&cmd); if (client == NULL) {
ESP_LOGI(TAG, "clear"); continue;
reply(true, mode, 0, 0); }
return; if (push_led_ring_to_slave(client, &req) == ESP_OK) {
sent++;
case LED_RING_MODE_PROGRESS: { }
uint8_t progress = clamp_progress(req.progress); }
cmd.mode = LED_CMD_PROGRESS; bool local_ok = true;
cmd.progress = progress; if (!req.slaves_only) {
cmd.r = r; local_ok = cmd_led_ring_apply(&req);
cmd.g = g; }
cmd.b = b; ESP_LOGI(TAG, "LED ring mode %lu → %u/%u slaves%s", (unsigned long)mode,
cmd.intensity = intensity; (unsigned)sent, (unsigned)n, req.slaves_only ? "" : " + master");
led_ring_send_command(&cmd); reply(local_ok || sent > 0, mode, req.progress, req.digit, 0, sent);
ESP_LOGI(TAG, "progress %u%% rgb=%u,%u,%u", (unsigned)progress,
(unsigned)r, (unsigned)g, (unsigned)b);
reply(true, mode, progress, 0);
return; return;
} }
case LED_RING_MODE_DIGIT: { if (req.client_id == 0) {
if (req.digit > 10) { bool ok = cmd_led_ring_apply(&req);
ESP_LOGW(TAG, "digit %lu out of range", (unsigned long)req.digit); ESP_LOGI(TAG, "LED ring mode %lu on master", (unsigned long)mode);
reply(false, mode, 0, req.digit); reply(ok, mode, req.progress, req.digit, 0, 0);
return;
}
cmd.mode = LED_CMD_SET_DIGIT;
cmd.value = (uint8_t)req.digit;
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
led_ring_send_command(&cmd);
ESP_LOGI(TAG, "digit %u rgb=%u,%u,%u", (unsigned)cmd.value, (unsigned)r,
(unsigned)g, (unsigned)b);
reply(true, mode, 0, req.digit);
return; return;
} }
case LED_RING_MODE_FIND_ME: const client_info_t *client = client_registry_find_by_id(req.client_id);
led_ring_find_me(); if (client == NULL) {
ESP_LOGI(TAG, "find-me"); ESP_LOGW(TAG, "client id %lu not in registry", (unsigned long)req.client_id);
reply(true, mode, 0, 0); reply(false, mode, req.progress, req.digit, req.client_id, 0);
return;
case LED_RING_MODE_BLINK: {
cmd.mode = LED_CMD_BLINK;
cmd.r = r;
cmd.g = g;
cmd.b = b;
cmd.intensity = intensity;
cmd.blink_ms = (uint16_t)(req.blink_ms > 0 ? req.blink_ms : 350);
cmd.blink_count = req.blink_count > 0 ? (uint8_t)req.blink_count : 1;
if (cmd.blink_count == 0) {
cmd.blink_count = 1;
}
led_ring_send_command(&cmd);
ESP_LOGI(TAG, "blink x%u %u ms rgb=%u,%u,%u", (unsigned)cmd.blink_count,
(unsigned)cmd.blink_ms, (unsigned)r, (unsigned)g, (unsigned)b);
reply(true, mode, 0, 0);
return; return;
} }
default: esp_err_t err = push_led_ring_to_slave(client, &req);
ESP_LOGW(TAG, "unknown mode %lu", (unsigned long)mode); if (err == ESP_OK) {
reply(false, mode, 0, 0); ESP_LOGI(TAG, "LED ring mode %lu → slave %lu", (unsigned long)mode,
return; (unsigned long)req.client_id);
} else {
ESP_LOGW(TAG, "LED ring to slave %lu failed: %s",
(unsigned long)req.client_id, esp_err_to_name(err));
} }
reply(err == ESP_OK, mode, req.progress, req.digit, req.client_id,
err == ESP_OK ? 1 : 0);
} }
void cmd_led_ring_register(void) { void cmd_led_ring_register(void) {

View File

@ -1,6 +1,12 @@
#ifndef CMD_LED_RING_H #ifndef CMD_LED_RING_H
#define CMD_LED_RING_H #define CMD_LED_RING_H
#include <stdbool.h>
#include "uart_messages.pb.h"
/** Apply LED ring command locally (master or slave). */
bool cmd_led_ring_apply(const alox_LedRingProgressRequest *req);
void cmd_led_ring_register(void); void cmd_led_ring_register(void);
#endif #endif

110
main/cmd/cmd_tap_notify.c Normal file
View File

@ -0,0 +1,110 @@
#include "client_registry.h"
#include "cmd_tap_notify.h"
#include "esp_log.h"
#include "esp_now_comm.h"
#include "uart_cmd.h"
static const char *TAG = "[TAP_NOTIFY]";
static void reply(uint32_t client_id, bool success, uint32_t slaves_updated,
bool single, bool double_tap, bool triple) {
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_TAP_NOTIFY,
alox_UartMessage_tap_notify_response_tag);
response.payload.tap_notify_response.client_id = client_id;
response.payload.tap_notify_response.success = success;
response.payload.tap_notify_response.slaves_updated = slaves_updated;
response.payload.tap_notify_response.single = single;
response.payload.tap_notify_response.double_tap = double_tap;
response.payload.tap_notify_response.triple = triple;
uart_cmd_send(&response, TAG);
}
static esp_err_t push_tap_notify_to_slave(const client_info_t *client,
bool single, bool double_tap,
bool triple) {
if (client == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err =
client_registry_set_tap_notify(client->id, single, double_tap, triple);
if (err != ESP_OK) {
return err;
}
return esp_now_comm_send_tap_notify(client->mac, client->id, single,
double_tap, triple);
}
static void handle_tap_notify(const uint8_t *data, size_t len) {
alox_UartMessage uart_msg;
alox_TapNotifyRequest req = alox_TapNotifyRequest_init_zero;
if (uart_cmd_decode(data, len, &uart_msg) == ESP_OK) {
const alox_TapNotifyRequest *req_ptr = UART_CMD_REQ(
&uart_msg, alox_UartMessage_tap_notify_request_tag, tap_notify_request);
if (req_ptr != NULL) {
req = *req_ptr;
}
}
if (req.write) {
if (req.all_clients) {
size_t n = client_registry_set_tap_notify_all(req.single, req.double_tap,
req.triple);
uint32_t sent = 0;
for (size_t i = 0; i < client_registry_count(); i++) {
const client_info_t *client = client_registry_at(i);
if (client == NULL) {
continue;
}
if (esp_now_comm_send_tap_notify(client->mac, client->id, req.single,
req.double_tap,
req.triple) == ESP_OK) {
sent++;
}
}
ESP_LOGI(TAG, "tap notify single=%d double=%d triple=%d for %u/%u slaves",
req.single, req.double_tap, req.triple, (unsigned)sent,
(unsigned)n);
reply(0, sent > 0, sent, req.single, req.double_tap, req.triple);
return;
}
if (req.client_id == 0) {
ESP_LOGW(TAG, "client_id required (or all_clients)");
reply(0, false, 0, req.single, req.double_tap, req.triple);
return;
}
const client_info_t *client = client_registry_find_by_id(req.client_id);
if (client == NULL) {
ESP_LOGW(TAG, "client id %lu not found", (unsigned long)req.client_id);
reply(req.client_id, false, 0, req.single, req.double_tap, req.triple);
return;
}
esp_err_t err =
push_tap_notify_to_slave(client, req.single, req.double_tap, req.triple);
reply(req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u, req.single,
req.double_tap, req.triple);
return;
}
if (req.all_clients || req.client_id == 0) {
reply(0, false, 0, false, false, false);
return;
}
bool single = false;
bool double_tap = false;
bool triple = false;
esp_err_t err = client_registry_get_tap_notify(req.client_id, &single,
&double_tap, &triple);
reply(req.client_id, err == ESP_OK, 0, single, double_tap, triple);
}
void cmd_tap_notify_register(void) {
uart_cmd_register(alox_MessageType_TAP_NOTIFY, handle_tap_notify);
}

View File

@ -0,0 +1,6 @@
#ifndef CMD_TAP_NOTIFY_H
#define CMD_TAP_NOTIFY_H
void cmd_tap_notify_register(void);
#endif

View File

@ -1,6 +1,8 @@
#include "bosch456.h" #include "bosch456.h"
#include "client_registry.h" #include "client_registry.h"
#include "esp_now_comm.h" #include "esp_now_comm.h"
#include "board_input.h"
#include "cmd_led_ring.h"
#include "led_ring.h" #include "led_ring.h"
#include "ota_espnow.h" #include "ota_espnow.h"
#include "pod_reboot.h" #include "pod_reboot.h"
@ -29,6 +31,7 @@
#define ESPNOW_CLIENT_TIMEOUT_MS \ #define ESPNOW_CLIENT_TIMEOUT_MS \
(ESPNOW_HEARTBEAT_INTERVAL_MS * ESPNOW_HEARTBEAT_MISS_COUNT) (ESPNOW_HEARTBEAT_INTERVAL_MS * ESPNOW_HEARTBEAT_MISS_COUNT)
#define SLAVE_MASTER_LOST_MS (ESPNOW_HEARTBEAT_INTERVAL_MS * 5) #define SLAVE_MASTER_LOST_MS (ESPNOW_HEARTBEAT_INTERVAL_MS * 5)
#define ESPNOW_ACCEL_INTERVAL_MS 16
static const uint8_t ESPNOW_BCAST[ESP_NOW_ETH_ALEN] = {0xff, 0xff, 0xff, static const uint8_t ESPNOW_BCAST[ESP_NOW_ETH_ALEN] = {0xff, 0xff, 0xff,
0xff, 0xff, 0xff}; 0xff, 0xff, 0xff};
@ -39,12 +42,26 @@ static app_config_t s_config;
static uint8_t s_wifi_channel; static uint8_t s_wifi_channel;
static uint8_t s_own_mac[ESP_NOW_ETH_ALEN]; static uint8_t s_own_mac[ESP_NOW_ETH_ALEN];
static bool s_slave_joined; static bool s_slave_joined;
static bool s_accel_stream_enabled;
static bool s_tap_notify_single;
static bool s_tap_notify_double;
static bool s_tap_notify_triple;
static uint8_t s_master_mac[ESP_NOW_ETH_ALEN]; static uint8_t s_master_mac[ESP_NOW_ETH_ALEN];
static uint32_t s_last_discover_ms; static uint32_t s_last_discover_ms;
static SemaphoreHandle_t s_send_done; static SemaphoreHandle_t s_send_done;
static bool s_send_cb_ready; static bool s_send_cb_ready;
#define ESPNOW_BATTERY_INTERVAL_MS 30000
#define SLAVE_BATTERY_AFTER_JOIN_MS 150
typedef enum {
SLAVE_TX_SLAVE_INFO = 1,
SLAVE_TX_BATTERY,
} slave_tx_op_t;
static QueueHandle_t s_slave_tx_queue;
static uint32_t now_ms(void) { static uint32_t now_ms(void) {
return (uint32_t)(xTaskGetTickCount() * portTICK_PERIOD_MS); return (uint32_t)(xTaskGetTickCount() * portTICK_PERIOD_MS);
} }
@ -87,6 +104,7 @@ static esp_err_t ensure_broadcast_peer(void) { return ensure_peer(ESPNOW_BCAST);
static esp_err_t send_message_ex(const uint8_t *dest_mac, static esp_err_t send_message_ex(const uint8_t *dest_mac,
const alox_EspNowMessage *msg, bool wait_done); const alox_EspNowMessage *msg, bool wait_done);
static void slave_send_battery_report_to_master(void);
static void fill_presence(alox_EspNowSlavePresence *presence) { static void fill_presence(alox_EspNowSlavePresence *presence) {
presence->network = s_config.network; presence->network = s_config.network;
@ -111,6 +129,40 @@ static esp_err_t send_message(const uint8_t *dest_mac,
return send_message_ex(dest_mac, msg, false); return send_message_ex(dest_mac, msg, false);
} }
static esp_err_t send_accel_sample(const uint8_t *dest_mac, uint32_t slave_id,
int16_t x, int16_t y, int16_t z) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_ACCEL_SAMPLE;
msg.which_payload = alox_EspNowMessage_accel_sample_tag;
msg.payload.accel_sample.slave_id = slave_id;
msg.payload.accel_sample.x = x;
msg.payload.accel_sample.y = y;
msg.payload.accel_sample.z = z;
return send_message(dest_mac, &msg);
}
static esp_err_t send_tap_event(const uint8_t *dest_mac, uint32_t slave_id,
uint32_t kind) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_TAP_EVENT;
msg.which_payload = alox_EspNowMessage_tap_event_tag;
msg.payload.tap_event.slave_id = slave_id;
msg.payload.tap_event.kind = kind;
return send_message(dest_mac, &msg);
}
static esp_err_t send_tap_notify(const uint8_t *dest_mac, uint32_t client_id,
bool single, bool double_tap, bool triple) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_SET_TAP_NOTIFY;
msg.which_payload = alox_EspNowMessage_tap_notify_tag;
msg.payload.tap_notify.client_id = client_id;
msg.payload.tap_notify.single = single;
msg.payload.tap_notify.double_tap = double_tap;
msg.payload.tap_notify.triple = triple;
return send_message(dest_mac, &msg);
}
static esp_err_t send_message_ex(const uint8_t *dest_mac, static esp_err_t send_message_ex(const uint8_t *dest_mac,
const alox_EspNowMessage *msg, bool wait_done) { const alox_EspNowMessage *msg, bool wait_done) {
uint8_t buf[ESPNOW_PB_MAX_SIZE]; uint8_t buf[ESPNOW_PB_MAX_SIZE];
@ -151,6 +203,18 @@ static esp_err_t send_message_ex(const uint8_t *dest_mac,
return err; return err;
} }
static esp_err_t send_accel_stream(const uint8_t *dest_mac, uint32_t client_id,
bool enable) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM;
msg.which_payload = alox_EspNowMessage_accel_stream_tag;
msg.payload.accel_stream.enable = enable;
msg.payload.accel_stream.client_id = client_id;
return send_message(dest_mac, &msg);
}
static esp_err_t send_accel_deadzone(const uint8_t *dest_mac, uint32_t client_id, static esp_err_t send_accel_deadzone(const uint8_t *dest_mac, uint32_t client_id,
uint32_t deadzone) { uint32_t deadzone) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero; alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
@ -183,6 +247,45 @@ static esp_err_t send_find_me(const uint8_t *dest_mac, uint32_t client_id) {
return send_message(dest_mac, &msg); return send_message(dest_mac, &msg);
} }
static esp_err_t send_battery_report(const uint8_t *dest_mac,
const alox_EspNowBatteryReport *report) {
if (report == NULL) {
return ESP_ERR_INVALID_ARG;
}
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_BATTERY_REPORT;
msg.which_payload = alox_EspNowMessage_battery_report_tag;
msg.payload.battery_report = *report;
return send_message(dest_mac, &msg);
}
static esp_err_t send_led_ring(const uint8_t *dest_mac, uint32_t client_id,
const alox_LedRingProgressRequest *req) {
if (req == NULL) {
return ESP_ERR_INVALID_ARG;
}
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_LED_RING;
msg.which_payload = alox_EspNowMessage_led_ring_tag;
msg.payload.led_ring.client_id = client_id;
msg.payload.led_ring.mode = req->mode;
msg.payload.led_ring.progress = req->progress;
msg.payload.led_ring.digit = req->digit;
msg.payload.led_ring.r = req->r;
msg.payload.led_ring.g = req->g;
msg.payload.led_ring.b = req->b;
msg.payload.led_ring.intensity = req->intensity;
msg.payload.led_ring.blink_ms = req->blink_ms;
msg.payload.led_ring.blink_count = req->blink_count;
return send_message(dest_mac, &msg);
}
static esp_err_t send_restart(const uint8_t *dest_mac, uint32_t client_id) { static esp_err_t send_restart(const uint8_t *dest_mac, uint32_t client_id) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero; alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
@ -321,6 +424,26 @@ esp_err_t esp_now_comm_send_find_me(const uint8_t mac[CLIENT_MAC_LEN],
return err; return err;
} }
esp_err_t esp_now_comm_send_led_ring(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id,
const alox_LedRingProgressRequest *req) {
if (mac == NULL || !s_config.master || req == NULL) {
return ESP_ERR_INVALID_STATE;
}
char mac_str[18];
mac_to_str(mac, mac_str, sizeof(mac_str));
esp_err_t err = send_led_ring(mac, client_id, req);
if (err == ESP_OK) {
ESP_LOGI(TAG, "unicast LED_RING mode %lu to %s client_id=%lu",
(unsigned long)req->mode, mac_str, (unsigned long)client_id);
} else {
ESP_LOGW(TAG, "unicast LED_RING to %s failed: %s", mac_str,
esp_err_to_name(err));
}
return err;
}
esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN], esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t seq) { uint32_t seq) {
if (mac == NULL || !s_config.master) { if (mac == NULL || !s_config.master) {
@ -338,6 +461,48 @@ esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
return err; return err;
} }
esp_err_t esp_now_comm_send_accel_stream(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool enable) {
if (mac == NULL || !s_config.master) {
return ESP_ERR_INVALID_STATE;
}
char mac_str[18];
mac_to_str(mac, mac_str, sizeof(mac_str));
esp_err_t err = send_accel_stream(mac, client_id, enable);
if (err == ESP_OK) {
ESP_LOGI(TAG, "unicast SET_ACCEL_STREAM to %s: %s client_id=%lu", mac_str,
enable ? "on" : "off", (unsigned long)client_id);
} else {
ESP_LOGW(TAG, "unicast SET_ACCEL_STREAM to %s failed: %s", mac_str,
esp_err_to_name(err));
}
return err;
}
esp_err_t esp_now_comm_send_tap_notify(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool single,
bool double_tap, bool triple) {
if (mac == NULL || !s_config.master) {
return ESP_ERR_INVALID_STATE;
}
char mac_str[18];
mac_to_str(mac, mac_str, sizeof(mac_str));
esp_err_t err =
send_tap_notify(mac, client_id, single, double_tap, triple);
if (err == ESP_OK) {
ESP_LOGI(TAG,
"unicast SET_TAP_NOTIFY to %s: single=%d double=%d triple=%d "
"client_id=%lu",
mac_str, single, double_tap, triple, (unsigned long)client_id);
} else {
ESP_LOGW(TAG, "unicast SET_TAP_NOTIFY to %s failed: %s", mac_str,
esp_err_to_name(err));
}
return err;
}
esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN], esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, uint32_t deadzone) { uint32_t client_id, uint32_t deadzone) {
if (mac == NULL || !s_config.master) { if (mac == NULL || !s_config.master) {
@ -377,8 +542,50 @@ static void send_presence(const uint8_t *dest_mac,
static void slave_reset_join(void) { static void slave_reset_join(void) {
s_slave_joined = false; s_slave_joined = false;
s_accel_stream_enabled = false;
memset(s_master_mac, 0, sizeof(s_master_mac)); memset(s_master_mac, 0, sizeof(s_master_mac));
s_last_discover_ms = 0; s_last_discover_ms = 0;
if (s_slave_tx_queue != NULL) {
xQueueReset(s_slave_tx_queue);
}
}
static void slave_queue_tx(slave_tx_op_t op) {
if (s_slave_tx_queue == NULL) {
return;
}
if (xQueueSend(s_slave_tx_queue, &op, 0) != pdTRUE) {
ESP_LOGW(TAG, "slave tx queue full (op=%d)", (int)op);
}
}
static void slave_tx_task(void *param) {
(void)param;
slave_tx_op_t op;
ESP_LOGI(TAG, "slave tx task ready");
while (1) {
if (xQueueReceive(s_slave_tx_queue, &op, portMAX_DELAY) != pdTRUE) {
continue;
}
if (!s_slave_joined) {
continue;
}
switch (op) {
case SLAVE_TX_SLAVE_INFO:
send_presence(s_master_mac, alox_EspNowMessageType_ESPNOW_SLAVE_INFO);
break;
case SLAVE_TX_BATTERY:
vTaskDelay(pdMS_TO_TICKS(SLAVE_BATTERY_AFTER_JOIN_MS));
slave_send_battery_report_to_master();
break;
default:
break;
}
}
} }
static void handle_slave_unicast_test(const uint8_t *master_mac, static void handle_slave_unicast_test(const uint8_t *master_mac,
@ -408,6 +615,107 @@ static void handle_slave_restart(const uint8_t *master_mac,
pod_schedule_restart(); pod_schedule_restart();
} }
static void slave_send_battery_report_to_master(void) {
if (!s_slave_joined) {
return;
}
board_lipo_reading_t reading;
board_input_read_lipo(&reading);
alox_EspNowBatteryReport report = alox_EspNowBatteryReport_init_zero;
report.client_id = s_own_mac[5];
report.lipo1_valid = reading.lipo1_valid;
report.lipo2_valid = reading.lipo2_valid;
report.lipo1_mv = reading.lipo1_mv;
report.lipo2_mv = reading.lipo2_mv;
esp_err_t err = send_battery_report(s_master_mac, &report);
if (err != ESP_OK) {
ESP_LOGW(TAG, "battery report send failed id=%lu: %s",
(unsigned long)report.client_id, esp_err_to_name(err));
} else {
ESP_LOGI(TAG, "battery report sent id=%lu L1=%s %lu mV L2=%s %lu mV",
(unsigned long)report.client_id,
report.lipo1_valid ? "ok" : "n/a",
(unsigned long)report.lipo1_mv,
report.lipo2_valid ? "ok" : "n/a",
(unsigned long)report.lipo2_mv);
}
}
static void handle_slave_battery_query(const uint8_t *master_mac,
const alox_EspNowBatteryQuery *query) {
uint32_t my_id = s_own_mac[5];
if (query->client_id != 0 && query->client_id != my_id) {
return;
}
if (s_slave_joined && !mac_equal(master_mac, s_master_mac)) {
return;
}
slave_send_battery_report_to_master();
}
static void handle_master_battery_report(const uint8_t *mac,
const alox_EspNowBatteryReport *report) {
if (report == NULL || mac == NULL) {
return;
}
esp_err_t err = client_registry_update_battery(
mac, report->client_id, report->lipo1_valid, report->lipo1_mv,
report->lipo2_valid, report->lipo2_mv);
if (err == ESP_ERR_NOT_FOUND) {
ESP_LOGW(TAG, "battery report from unregistered slave id=%lu",
(unsigned long)report->client_id);
return;
}
if (err != ESP_OK) {
ESP_LOGW(TAG, "battery report id=%lu rejected: %s",
(unsigned long)report->client_id, esp_err_to_name(err));
return;
}
ESP_LOGI(TAG, "battery cached id=%lu L1=%s %lu mV L2=%s %lu mV",
(unsigned long)report->client_id,
report->lipo1_valid ? "ok" : "n/a",
(unsigned long)report->lipo1_mv, report->lipo2_valid ? "ok" : "n/a",
(unsigned long)report->lipo2_mv);
}
static void handle_slave_led_ring(const uint8_t *master_mac,
const alox_EspNowLedRing *msg) {
uint32_t my_id = s_own_mac[5];
if (msg->client_id != 0 && msg->client_id != my_id) {
return;
}
if (s_slave_joined && !mac_equal(master_mac, s_master_mac)) {
return;
}
alox_LedRingProgressRequest req = alox_LedRingProgressRequest_init_zero;
req.mode = msg->mode;
req.progress = msg->progress;
req.digit = msg->digit;
req.r = msg->r;
req.g = msg->g;
req.b = msg->b;
req.intensity = msg->intensity;
req.blink_ms = msg->blink_ms;
req.blink_count = msg->blink_count;
char mac_str[18];
mac_to_str(master_mac, mac_str, sizeof(mac_str));
ESP_LOGI(TAG, "LED_RING mode %lu from master %s (id=%lu)",
(unsigned long)req.mode, mac_str, (unsigned long)my_id);
cmd_led_ring_apply(&req);
}
static void handle_slave_find_me(const uint8_t *master_mac, static void handle_slave_find_me(const uint8_t *master_mac,
const alox_EspNowFindMe *req) { const alox_EspNowFindMe *req) {
uint32_t my_id = s_own_mac[5]; uint32_t my_id = s_own_mac[5];
@ -426,6 +734,49 @@ static void handle_slave_find_me(const uint8_t *master_mac,
led_ring_find_me(); led_ring_find_me();
} }
static void handle_slave_accel_stream(const uint8_t *master_mac,
const alox_EspNowAccelStream *cfg) {
uint32_t my_id = s_own_mac[5];
if (cfg->client_id != 0 && cfg->client_id != my_id) {
return;
}
if (s_slave_joined && !mac_equal(master_mac, s_master_mac)) {
return;
}
s_accel_stream_enabled = cfg->enable;
char mac_str[18];
mac_to_str(master_mac, mac_str, sizeof(mac_str));
ESP_LOGI(TAG, "accel stream %s from master %s (id=%lu)",
cfg->enable ? "on" : "off", mac_str, (unsigned long)my_id);
}
static void handle_slave_tap_notify(const uint8_t *master_mac,
const alox_EspNowTapNotify *cfg) {
uint32_t my_id = s_own_mac[5];
if (cfg->client_id != 0 && cfg->client_id != my_id) {
return;
}
if (s_slave_joined && !mac_equal(master_mac, s_master_mac)) {
return;
}
s_tap_notify_single = cfg->single;
s_tap_notify_double = cfg->double_tap;
s_tap_notify_triple = cfg->triple;
char mac_str[18];
mac_to_str(master_mac, mac_str, sizeof(mac_str));
ESP_LOGI(TAG,
"tap notify single=%d double=%d triple=%d from master %s (id=%lu)",
cfg->single, cfg->double_tap, cfg->triple, mac_str,
(unsigned long)my_id);
}
static void handle_slave_accel_deadzone(const uint8_t *master_mac, static void handle_slave_accel_deadzone(const uint8_t *master_mac,
const alox_EspNowAccelDeadzone *cfg) { const alox_EspNowAccelDeadzone *cfg) {
uint32_t my_id = s_own_mac[5]; uint32_t my_id = s_own_mac[5];
@ -453,6 +804,41 @@ static void handle_slave_accel_deadzone(const uint8_t *master_mac,
} }
} }
static void handle_master_accel_sample(const uint8_t mac[CLIENT_MAC_LEN],
const alox_EspNowAccelSample *sample) {
if (sample == NULL) {
return;
}
esp_err_t err = client_registry_update_accel(
mac, sample->slave_id, (int16_t)sample->x, (int16_t)sample->y,
(int16_t)sample->z);
if (err == ESP_ERR_NOT_FOUND) {
return;
}
if (err != ESP_OK) {
ESP_LOGW(TAG, "accel sample id mismatch from %02x:…:%02x", mac[0], mac[5]);
}
}
static void handle_master_tap_event(const uint8_t mac[CLIENT_MAC_LEN],
const alox_EspNowTapEvent *event) {
if (event == NULL) {
return;
}
esp_err_t err =
client_registry_update_tap(mac, event->slave_id, event->kind);
if (err == ESP_ERR_NOT_FOUND) {
return;
}
if (err != ESP_OK) {
ESP_LOGW(TAG, "tap event id=%lu kind=%lu rejected from %02x:…:%02x",
(unsigned long)event->slave_id, (unsigned long)event->kind, mac[0],
mac[5]);
}
}
static void handle_client_presence(const alox_EspNowSlavePresence *presence, static void handle_client_presence(const alox_EspNowSlavePresence *presence,
const uint8_t mac[CLIENT_MAC_LEN]) { const uint8_t mac[CLIENT_MAC_LEN]) {
if (presence->network != s_config.network) { if (presence->network != s_config.network) {
@ -513,7 +899,9 @@ static void handle_discover(const uint8_t *sender_mac,
ESP_LOGI(TAG, "joined network %u, master %s", (unsigned)discover->network, ESP_LOGI(TAG, "joined network %u, master %s", (unsigned)discover->network,
mac_str); mac_str);
send_presence(sender_mac, alox_EspNowMessageType_ESPNOW_SLAVE_INFO); /* Do not esp_now_send from recv callback — defer to slave_tx_task. */
slave_queue_tx(SLAVE_TX_SLAVE_INFO);
slave_queue_tx(SLAVE_TX_BATTERY);
} }
static void slave_check_master_timeout(void) { static void slave_check_master_timeout(void) {
@ -533,8 +921,61 @@ static void slave_check_master_timeout(void) {
} }
} }
static void slave_accel_stream_task(void *param) {
(void)param;
ESP_LOGI(TAG, "slave accel stream task (interval %u ms)",
(unsigned)ESPNOW_ACCEL_INTERVAL_MS);
while (1) {
vTaskDelay(pdMS_TO_TICKS(ESPNOW_ACCEL_INTERVAL_MS));
if (!s_slave_joined || !s_accel_stream_enabled || !bma456_is_ready()) {
continue;
}
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
if (bma456_read_accel(&x, &y, &z) != ESP_OK) {
continue;
}
(void)send_accel_sample(s_master_mac, s_own_mac[5], x, y, z);
}
}
static void on_bma456_tap(bma456_tap_kind_t kind, void *ctx) {
(void)ctx;
if (!s_slave_joined) {
return;
}
bool enabled = false;
switch (kind) {
case BMA456_TAP_SINGLE:
enabled = s_tap_notify_single;
break;
case BMA456_TAP_DOUBLE:
enabled = s_tap_notify_double;
break;
case BMA456_TAP_TRIPLE:
enabled = s_tap_notify_triple;
break;
default:
return;
}
if (!enabled) {
return;
}
(void)send_tap_event(s_master_mac, s_own_mac[5], (uint32_t)kind);
}
static void slave_heartbeat_task(void *param) { static void slave_heartbeat_task(void *param) {
(void)param; (void)param;
uint32_t last_battery_ms = 0;
ESP_LOGI(TAG, "slave heartbeat task (interval %u ms)", ESP_LOGI(TAG, "slave heartbeat task (interval %u ms)",
(unsigned)ESPNOW_HEARTBEAT_INTERVAL_MS); (unsigned)ESPNOW_HEARTBEAT_INTERVAL_MS);
@ -545,22 +986,43 @@ static void slave_heartbeat_task(void *param) {
slave_check_master_timeout(); slave_check_master_timeout();
if (!s_slave_joined) { if (!s_slave_joined) {
last_battery_ms = 0;
continue; continue;
} }
send_presence(s_master_mac, alox_EspNowMessageType_ESPNOW_HEARTBEAT); send_presence(s_master_mac, alox_EspNowMessageType_ESPNOW_HEARTBEAT);
uint32_t now = now_ms();
if (last_battery_ms == 0 ||
(now - last_battery_ms) >= ESPNOW_BATTERY_INTERVAL_MS) {
slave_send_battery_report_to_master();
last_battery_ms = now;
}
} }
} }
static void master_monitor_task(void *param) { static void master_monitor_task(void *param) {
(void)param; (void)param;
uint32_t last_local_battery_ms = 0;
ESP_LOGI(TAG, "master monitor task (timeout %u ms)", ESP_LOGI(TAG, "master monitor task (timeout %u ms)",
(unsigned)ESPNOW_CLIENT_TIMEOUT_MS); (unsigned)ESPNOW_CLIENT_TIMEOUT_MS);
board_lipo_reading_t reading;
board_input_read_lipo(&reading);
client_registry_set_master_battery(&reading);
last_local_battery_ms = now_ms();
while (1) { while (1) {
vTaskDelay(pdMS_TO_TICKS(ESPNOW_HEARTBEAT_INTERVAL_MS)); vTaskDelay(pdMS_TO_TICKS(ESPNOW_HEARTBEAT_INTERVAL_MS));
client_registry_check_timeouts(ESPNOW_CLIENT_TIMEOUT_MS); client_registry_check_timeouts(ESPNOW_CLIENT_TIMEOUT_MS);
uint32_t t = now_ms();
if (t - last_local_battery_ms >= ESPNOW_BATTERY_INTERVAL_MS) {
board_input_read_lipo(&reading);
client_registry_set_master_battery(&reading);
last_local_battery_ms = t;
}
} }
} }
@ -592,6 +1054,30 @@ static void espnow_recv_cb(const esp_now_recv_info_t *info, const uint8_t *data,
case alox_EspNowMessage_accel_deadzone_tag: case alox_EspNowMessage_accel_deadzone_tag:
handle_slave_accel_deadzone(info->src_addr, &msg.payload.accel_deadzone); handle_slave_accel_deadzone(info->src_addr, &msg.payload.accel_deadzone);
break; break;
case alox_EspNowMessage_accel_stream_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
}
handle_slave_accel_stream(info->src_addr, &msg.payload.accel_stream);
break;
case alox_EspNowMessage_tap_notify_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
}
handle_slave_tap_notify(info->src_addr, &msg.payload.tap_notify);
break;
case alox_EspNowMessage_battery_query_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
}
handle_slave_battery_query(info->src_addr, &msg.payload.battery_query);
break;
case alox_EspNowMessage_led_ring_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
}
handle_slave_led_ring(info->src_addr, &msg.payload.led_ring);
break;
case alox_EspNowMessage_find_me_tag: case alox_EspNowMessage_find_me_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) { if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break; break;
@ -639,6 +1125,29 @@ static void espnow_recv_cb(const esp_now_recv_info_t *info, const uint8_t *data,
return; return;
} }
if (msg.which_payload == alox_EspNowMessage_accel_sample_tag) {
ensure_peer(info->src_addr);
handle_master_accel_sample(info->src_addr, &msg.payload.accel_sample);
return;
}
if (msg.which_payload == alox_EspNowMessage_tap_event_tag) {
ensure_peer(info->src_addr);
handle_master_tap_event(info->src_addr, &msg.payload.tap_event);
return;
}
if (msg.which_payload == alox_EspNowMessage_battery_report_tag) {
ensure_peer(info->src_addr);
handle_master_battery_report(info->src_addr, &msg.payload.battery_report);
return;
}
if (msg.type == alox_EspNowMessageType_ESPNOW_BATTERY_REPORT &&
msg.which_payload != alox_EspNowMessage_battery_report_tag) {
ESP_LOGW(TAG, "master: BATTERY_REPORT type but which=%u", msg.which_payload);
}
const alox_EspNowSlavePresence *presence = esp_now_proto_get_presence(&msg); const alox_EspNowSlavePresence *presence = esp_now_proto_get_presence(&msg);
if (presence != NULL) { if (presence != NULL) {
/* Registry key is the ESP-NOW sender MAC, not the optional protobuf mac field. */ /* Registry key is the ESP-NOW sender MAC, not the optional protobuf mac field. */
@ -734,11 +1243,27 @@ esp_err_t esp_now_comm_init(const app_config_t *config) {
return ESP_FAIL; return ESP_FAIL;
} }
} else { } else {
s_slave_tx_queue = xQueueCreate(4, sizeof(slave_tx_op_t));
if (s_slave_tx_queue == NULL) {
ESP_LOGE(TAG, "failed to create slave tx queue");
return ESP_ERR_NO_MEM;
}
if (xTaskCreate(slave_tx_task, "espnow_stx", 4096, NULL, 5, NULL) !=
pdPASS) {
ESP_LOGE(TAG, "failed to create slave tx task");
return ESP_FAIL;
}
if (xTaskCreate(slave_heartbeat_task, "espnow_hb", 4096, NULL, 4, NULL) != if (xTaskCreate(slave_heartbeat_task, "espnow_hb", 4096, NULL, 4, NULL) !=
pdPASS) { pdPASS) {
ESP_LOGE(TAG, "failed to create heartbeat task"); ESP_LOGE(TAG, "failed to create heartbeat task");
return ESP_FAIL; return ESP_FAIL;
} }
if (xTaskCreate(slave_accel_stream_task, "espnow_accel", 4096, NULL, 5,
NULL) != pdPASS) {
ESP_LOGE(TAG, "failed to create accel stream task");
return ESP_FAIL;
}
bma456_set_tap_handler(on_bma456_tap, NULL);
} }
return ESP_OK; return ESP_OK;

View File

@ -4,9 +4,20 @@
#include "app_config.h" #include "app_config.h"
#include "client_registry.h" #include "client_registry.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_now_messages.pb.h"
#include "uart_messages.pb.h"
esp_err_t esp_now_comm_init(const app_config_t *config); esp_err_t esp_now_comm_init(const app_config_t *config);
/** Master: enable/disable accel ESP-NOW stream on one slave. */
esp_err_t esp_now_comm_send_accel_stream(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool enable);
/** Master: configure tap notify flags on one slave. */
esp_err_t esp_now_comm_send_tap_notify(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool single,
bool double_tap, bool triple);
/** Master: unicast accel deadzone to one slave (client_id is echoed for filtering). */ /** Master: unicast accel deadzone to one slave (client_id is echoed for filtering). */
esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN], esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, uint32_t deadzone); uint32_t client_id, uint32_t deadzone);
@ -19,6 +30,11 @@ esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
esp_err_t esp_now_comm_send_find_me(const uint8_t mac[CLIENT_MAC_LEN], esp_err_t esp_now_comm_send_find_me(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id); uint32_t client_id);
/** Master: LED ring command on one slave. */
esp_err_t esp_now_comm_send_led_ring(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id,
const alox_LedRingProgressRequest *req);
/** Master: request reboot on one slave. */ /** Master: request reboot on one slave. */
esp_err_t esp_now_comm_send_restart(const uint8_t mac[CLIENT_MAC_LEN], esp_err_t esp_now_comm_send_restart(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id); uint32_t client_id);

View File

@ -116,6 +116,8 @@ void vTaskLedRing(void *pvParameters) {
for (int i = 0; i < digit.count; i++) { for (int i = 0; i < digit.count; i++) {
led_strip_set_pixel(led_ring, RING_LEDS - digit.leds[i], r, g, b); led_strip_set_pixel(led_ring, RING_LEDS - digit.leds[i], r, g, b);
} }
} else if (cmd.mode == LED_CMD_SET_COLOR) {
ring_fill_color(r, g, b);
} else if (cmd.mode == LED_CMD_PROGRESS) { } else if (cmd.mode == LED_CMD_PROGRESS) {
uint32_t lit = ((uint32_t)cmd.progress * RING_LEDS + 50) / 100; uint32_t lit = ((uint32_t)cmd.progress * RING_LEDS + 50) / 100;
if (lit > RING_LEDS) { if (lit > RING_LEDS) {

View File

@ -1,6 +1,9 @@
#include "app_config.h" #include "app_config.h"
#include "cmd_handler.h" #include "cmd_handler.h"
#include "cmd_accel_deadzone.h" #include "cmd_accel_deadzone.h"
#include "cmd_accel_stream.h"
#include "cmd_tap_notify.h"
#include "cmd_cache_status.h"
#include "cmd_espnow_unicast_test.h" #include "cmd_espnow_unicast_test.h"
#include "cmd_espnow_find_me.h" #include "cmd_espnow_find_me.h"
#include "cmd_restart.h" #include "cmd_restart.h"
@ -9,6 +12,7 @@
#include "cmd_ota.h" #include "cmd_ota.h"
#include "cmd_ota_slave_progress.h" #include "cmd_ota_slave_progress.h"
#include "cmd_led_ring.h" #include "cmd_led_ring.h"
#include "cmd_battery.h"
#include "esp_now_comm.h" #include "esp_now_comm.h"
#include "powerpod.h" #include "powerpod.h"
#include "driver/gpio.h" #include "driver/gpio.h"
@ -161,6 +165,8 @@ void app_main(void) {
ESP_LOGI(TAG, "Running Partition: %s (OTA slot %d)", ESP_LOGI(TAG, "Running Partition: %s (OTA slot %d)",
app_config.running_partition, ota_slot); app_config.running_partition, ota_slot);
board_input_init();
err = esp_now_comm_init(&app_config); err = esp_now_comm_init(&app_config);
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "ESP-NOW init failed: %s", esp_err_to_name(err)); ESP_LOGE(TAG, "ESP-NOW init failed: %s", esp_err_to_name(err));
@ -168,8 +174,6 @@ void app_main(void) {
led_ring_init(); led_ring_init();
board_input_init();
if (app_config.master) { if (app_config.master) {
cmd_queue = xQueueCreate(64, sizeof(generic_msg_t)); cmd_queue = xQueueCreate(64, sizeof(generic_msg_t));
init_cmdHandler(cmd_queue); init_cmdHandler(cmd_queue);
@ -177,10 +181,14 @@ void app_main(void) {
cmd_version_register(); cmd_version_register();
cmd_client_info_register(); cmd_client_info_register();
cmd_accel_deadzone_register(); cmd_accel_deadzone_register();
cmd_accel_stream_register();
cmd_tap_notify_register();
cmd_cache_status_register();
cmd_espnow_unicast_test_register(); cmd_espnow_unicast_test_register();
cmd_espnow_find_me_register(); cmd_espnow_find_me_register();
cmd_restart_register(); cmd_restart_register();
cmd_led_ring_register(); cmd_led_ring_register();
cmd_battery_register();
cmd_ota_register(); cmd_ota_register();
cmd_ota_slave_progress_register(); cmd_ota_slave_progress_register();
} }

View File

@ -24,6 +24,27 @@ PB_BIND(alox_EspNowSlavePresence, alox_EspNowSlavePresence, AUTO)
PB_BIND(alox_EspNowAccelDeadzone, alox_EspNowAccelDeadzone, AUTO) PB_BIND(alox_EspNowAccelDeadzone, alox_EspNowAccelDeadzone, AUTO)
PB_BIND(alox_EspNowAccelStream, alox_EspNowAccelStream, AUTO)
PB_BIND(alox_EspNowAccelSample, alox_EspNowAccelSample, AUTO)
PB_BIND(alox_EspNowBatteryQuery, alox_EspNowBatteryQuery, AUTO)
PB_BIND(alox_EspNowTapNotify, alox_EspNowTapNotify, AUTO)
PB_BIND(alox_EspNowTapEvent, alox_EspNowTapEvent, AUTO)
PB_BIND(alox_EspNowBatteryReport, alox_EspNowBatteryReport, AUTO)
PB_BIND(alox_EspNowLedRing, alox_EspNowLedRing, AUTO)
PB_BIND(alox_EspNowOtaStart, alox_EspNowOtaStart, AUTO) PB_BIND(alox_EspNowOtaStart, alox_EspNowOtaStart, AUTO)

View File

@ -22,7 +22,14 @@ typedef enum _alox_EspNowMessageType {
alox_EspNowMessageType_ESPNOW_OTA_END = 8, alox_EspNowMessageType_ESPNOW_OTA_END = 8,
alox_EspNowMessageType_ESPNOW_OTA_STATUS = 9, alox_EspNowMessageType_ESPNOW_OTA_STATUS = 9,
alox_EspNowMessageType_ESPNOW_FIND_ME = 10, alox_EspNowMessageType_ESPNOW_FIND_ME = 10,
alox_EspNowMessageType_ESPNOW_RESTART = 11 alox_EspNowMessageType_ESPNOW_RESTART = 11,
alox_EspNowMessageType_ESPNOW_ACCEL_SAMPLE = 12,
alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM = 13,
alox_EspNowMessageType_ESPNOW_LED_RING = 14,
alox_EspNowMessageType_ESPNOW_BATTERY_QUERY = 15,
alox_EspNowMessageType_ESPNOW_BATTERY_REPORT = 16,
alox_EspNowMessageType_ESPNOW_SET_TAP_NOTIFY = 17,
alox_EspNowMessageType_ESPNOW_TAP_EVENT = 18
} alox_EspNowMessageType; } alox_EspNowMessageType;
/* Struct definitions */ /* Struct definitions */
@ -59,6 +66,63 @@ typedef struct _alox_EspNowAccelDeadzone {
uint32_t client_id; /* 0 = all slaves; otherwise only matching slave_id applies */ uint32_t client_id; /* 0 = all slaves; otherwise only matching slave_id applies */
} alox_EspNowAccelDeadzone; } alox_EspNowAccelDeadzone;
/* * Master → slave: enable/disable periodic accel ESP-NOW stream (~16 ms). */
typedef struct _alox_EspNowAccelStream {
bool enable;
uint32_t client_id;
} alox_EspNowAccelStream;
/* * Slave → master: latest BMA456 sample (sent ~every 16 ms). */
typedef struct _alox_EspNowAccelSample {
uint32_t slave_id;
int32_t x;
int32_t y;
int32_t z;
} alox_EspNowAccelSample;
/* * Master → slave: on-demand LiPo read (optional; slaves also push every ~30 s). */
typedef struct _alox_EspNowBatteryQuery {
uint32_t client_id;
} alox_EspNowBatteryQuery;
/* * Master → slave: which tap kinds should be reported via ESP-NOW. */
typedef struct _alox_EspNowTapNotify {
uint32_t client_id;
bool single;
bool double_tap;
bool triple;
} alox_EspNowTapNotify;
/* * Slave → master: tap detected on BMA456 (event, not periodic). */
typedef struct _alox_EspNowTapEvent {
uint32_t slave_id;
/* * 1=single, 2=double, 3=triple */
uint32_t kind;
} alox_EspNowTapEvent;
/* * Slave → master: LiPo voltages (periodic ~30 s and on query). */
typedef struct _alox_EspNowBatteryReport {
uint32_t client_id;
bool lipo1_valid;
bool lipo2_valid;
uint32_t lipo1_mv;
uint32_t lipo2_mv;
} alox_EspNowBatteryReport;
/* * Master → slave: LED ring command (same modes as UART LedRingProgressRequest). */
typedef struct _alox_EspNowLedRing {
uint32_t client_id;
uint32_t mode;
uint32_t progress;
uint32_t digit;
uint32_t r;
uint32_t g;
uint32_t b;
uint32_t intensity;
uint32_t blink_ms;
uint32_t blink_count;
} alox_EspNowLedRing;
/* Master → slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS). */ /* Master → slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS). */
typedef struct _alox_EspNowOtaStart { typedef struct _alox_EspNowOtaStart {
uint32_t total_size; uint32_t total_size;
@ -98,6 +162,13 @@ typedef struct _alox_EspNowMessage {
alox_EspNowOtaStatus ota_status; alox_EspNowOtaStatus ota_status;
alox_EspNowFindMe find_me; alox_EspNowFindMe find_me;
alox_EspNowRestart restart; alox_EspNowRestart restart;
alox_EspNowAccelSample accel_sample;
alox_EspNowAccelStream accel_stream;
alox_EspNowLedRing led_ring;
alox_EspNowBatteryQuery battery_query;
alox_EspNowBatteryReport battery_report;
alox_EspNowTapNotify tap_notify;
alox_EspNowTapEvent tap_event;
} payload; } payload;
} alox_EspNowMessage; } alox_EspNowMessage;
@ -108,8 +179,15 @@ extern "C" {
/* Helper constants for enums */ /* Helper constants for enums */
#define _alox_EspNowMessageType_MIN alox_EspNowMessageType_ESPNOW_UNKNOWN #define _alox_EspNowMessageType_MIN alox_EspNowMessageType_ESPNOW_UNKNOWN
#define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_RESTART #define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_TAP_EVENT
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_RESTART+1)) #define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_TAP_EVENT+1))
@ -131,6 +209,13 @@ extern "C" {
#define alox_EspNowDiscover_init_default {0} #define alox_EspNowDiscover_init_default {0}
#define alox_EspNowSlavePresence_init_default {0, {{NULL}, NULL}, 0, 0, 0, 0} #define alox_EspNowSlavePresence_init_default {0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_EspNowAccelDeadzone_init_default {0, 0} #define alox_EspNowAccelDeadzone_init_default {0, 0}
#define alox_EspNowAccelStream_init_default {0, 0}
#define alox_EspNowAccelSample_init_default {0, 0, 0, 0}
#define alox_EspNowBatteryQuery_init_default {0}
#define alox_EspNowTapNotify_init_default {0, 0, 0, 0}
#define alox_EspNowTapEvent_init_default {0, 0}
#define alox_EspNowBatteryReport_init_default {0, 0, 0, 0, 0}
#define alox_EspNowLedRing_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define alox_EspNowOtaStart_init_default {0} #define alox_EspNowOtaStart_init_default {0}
#define alox_EspNowOtaPayload_init_default {0, {0, {0}}} #define alox_EspNowOtaPayload_init_default {0, {0, {0}}}
#define alox_EspNowOtaEnd_init_default {0} #define alox_EspNowOtaEnd_init_default {0}
@ -142,6 +227,13 @@ extern "C" {
#define alox_EspNowDiscover_init_zero {0} #define alox_EspNowDiscover_init_zero {0}
#define alox_EspNowSlavePresence_init_zero {0, {{NULL}, NULL}, 0, 0, 0, 0} #define alox_EspNowSlavePresence_init_zero {0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_EspNowAccelDeadzone_init_zero {0, 0} #define alox_EspNowAccelDeadzone_init_zero {0, 0}
#define alox_EspNowAccelStream_init_zero {0, 0}
#define alox_EspNowAccelSample_init_zero {0, 0, 0, 0}
#define alox_EspNowBatteryQuery_init_zero {0}
#define alox_EspNowTapNotify_init_zero {0, 0, 0, 0}
#define alox_EspNowTapEvent_init_zero {0, 0}
#define alox_EspNowBatteryReport_init_zero {0, 0, 0, 0, 0}
#define alox_EspNowLedRing_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define alox_EspNowOtaStart_init_zero {0} #define alox_EspNowOtaStart_init_zero {0}
#define alox_EspNowOtaPayload_init_zero {0, {0, {0}}} #define alox_EspNowOtaPayload_init_zero {0, {0, {0}}}
#define alox_EspNowOtaEnd_init_zero {0} #define alox_EspNowOtaEnd_init_zero {0}
@ -161,6 +253,34 @@ extern "C" {
#define alox_EspNowSlavePresence_used_tag 6 #define alox_EspNowSlavePresence_used_tag 6
#define alox_EspNowAccelDeadzone_deadzone_tag 1 #define alox_EspNowAccelDeadzone_deadzone_tag 1
#define alox_EspNowAccelDeadzone_client_id_tag 2 #define alox_EspNowAccelDeadzone_client_id_tag 2
#define alox_EspNowAccelStream_enable_tag 1
#define alox_EspNowAccelStream_client_id_tag 2
#define alox_EspNowAccelSample_slave_id_tag 1
#define alox_EspNowAccelSample_x_tag 2
#define alox_EspNowAccelSample_y_tag 3
#define alox_EspNowAccelSample_z_tag 4
#define alox_EspNowBatteryQuery_client_id_tag 1
#define alox_EspNowTapNotify_client_id_tag 1
#define alox_EspNowTapNotify_single_tag 2
#define alox_EspNowTapNotify_double_tap_tag 3
#define alox_EspNowTapNotify_triple_tag 4
#define alox_EspNowTapEvent_slave_id_tag 1
#define alox_EspNowTapEvent_kind_tag 2
#define alox_EspNowBatteryReport_client_id_tag 1
#define alox_EspNowBatteryReport_lipo1_valid_tag 2
#define alox_EspNowBatteryReport_lipo2_valid_tag 3
#define alox_EspNowBatteryReport_lipo1_mv_tag 4
#define alox_EspNowBatteryReport_lipo2_mv_tag 5
#define alox_EspNowLedRing_client_id_tag 1
#define alox_EspNowLedRing_mode_tag 2
#define alox_EspNowLedRing_progress_tag 3
#define alox_EspNowLedRing_digit_tag 4
#define alox_EspNowLedRing_r_tag 5
#define alox_EspNowLedRing_g_tag 6
#define alox_EspNowLedRing_b_tag 7
#define alox_EspNowLedRing_intensity_tag 8
#define alox_EspNowLedRing_blink_ms_tag 9
#define alox_EspNowLedRing_blink_count_tag 10
#define alox_EspNowOtaStart_total_size_tag 1 #define alox_EspNowOtaStart_total_size_tag 1
#define alox_EspNowOtaPayload_seq_tag 1 #define alox_EspNowOtaPayload_seq_tag 1
#define alox_EspNowOtaPayload_data_tag 2 #define alox_EspNowOtaPayload_data_tag 2
@ -179,6 +299,13 @@ extern "C" {
#define alox_EspNowMessage_ota_status_tag 10 #define alox_EspNowMessage_ota_status_tag 10
#define alox_EspNowMessage_find_me_tag 11 #define alox_EspNowMessage_find_me_tag 11
#define alox_EspNowMessage_restart_tag 12 #define alox_EspNowMessage_restart_tag 12
#define alox_EspNowMessage_accel_sample_tag 13
#define alox_EspNowMessage_accel_stream_tag 14
#define alox_EspNowMessage_led_ring_tag 15
#define alox_EspNowMessage_battery_query_tag 16
#define alox_EspNowMessage_battery_report_tag 17
#define alox_EspNowMessage_tap_notify_tag 18
#define alox_EspNowMessage_tap_event_tag 19
/* Struct field encoding specification for nanopb */ /* Struct field encoding specification for nanopb */
#define alox_EspNowUnicastTest_FIELDLIST(X, a) \ #define alox_EspNowUnicastTest_FIELDLIST(X, a) \
@ -217,6 +344,62 @@ X(a, STATIC, SINGULAR, UINT32, client_id, 2)
#define alox_EspNowAccelDeadzone_CALLBACK NULL #define alox_EspNowAccelDeadzone_CALLBACK NULL
#define alox_EspNowAccelDeadzone_DEFAULT NULL #define alox_EspNowAccelDeadzone_DEFAULT NULL
#define alox_EspNowAccelStream_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, enable, 1) \
X(a, STATIC, SINGULAR, UINT32, client_id, 2)
#define alox_EspNowAccelStream_CALLBACK NULL
#define alox_EspNowAccelStream_DEFAULT NULL
#define alox_EspNowAccelSample_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, slave_id, 1) \
X(a, STATIC, SINGULAR, SINT32, x, 2) \
X(a, STATIC, SINGULAR, SINT32, y, 3) \
X(a, STATIC, SINGULAR, SINT32, z, 4)
#define alox_EspNowAccelSample_CALLBACK NULL
#define alox_EspNowAccelSample_DEFAULT NULL
#define alox_EspNowBatteryQuery_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1)
#define alox_EspNowBatteryQuery_CALLBACK NULL
#define alox_EspNowBatteryQuery_DEFAULT NULL
#define alox_EspNowTapNotify_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, single, 2) \
X(a, STATIC, SINGULAR, BOOL, double_tap, 3) \
X(a, STATIC, SINGULAR, BOOL, triple, 4)
#define alox_EspNowTapNotify_CALLBACK NULL
#define alox_EspNowTapNotify_DEFAULT NULL
#define alox_EspNowTapEvent_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, slave_id, 1) \
X(a, STATIC, SINGULAR, UINT32, kind, 2)
#define alox_EspNowTapEvent_CALLBACK NULL
#define alox_EspNowTapEvent_DEFAULT NULL
#define alox_EspNowBatteryReport_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, lipo1_valid, 2) \
X(a, STATIC, SINGULAR, BOOL, lipo2_valid, 3) \
X(a, STATIC, SINGULAR, UINT32, lipo1_mv, 4) \
X(a, STATIC, SINGULAR, UINT32, lipo2_mv, 5)
#define alox_EspNowBatteryReport_CALLBACK NULL
#define alox_EspNowBatteryReport_DEFAULT NULL
#define alox_EspNowLedRing_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, UINT32, mode, 2) \
X(a, STATIC, SINGULAR, UINT32, progress, 3) \
X(a, STATIC, SINGULAR, UINT32, digit, 4) \
X(a, STATIC, SINGULAR, UINT32, r, 5) \
X(a, STATIC, SINGULAR, UINT32, g, 6) \
X(a, STATIC, SINGULAR, UINT32, b, 7) \
X(a, STATIC, SINGULAR, UINT32, intensity, 8) \
X(a, STATIC, SINGULAR, UINT32, blink_ms, 9) \
X(a, STATIC, SINGULAR, UINT32, blink_count, 10)
#define alox_EspNowLedRing_CALLBACK NULL
#define alox_EspNowLedRing_DEFAULT NULL
#define alox_EspNowOtaStart_FIELDLIST(X, a) \ #define alox_EspNowOtaStart_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, total_size, 1) X(a, STATIC, SINGULAR, UINT32, total_size, 1)
#define alox_EspNowOtaStart_CALLBACK NULL #define alox_EspNowOtaStart_CALLBACK NULL
@ -252,7 +435,14 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,ota_payload,payload.ota_payload),
X(a, STATIC, ONEOF, MESSAGE, (payload,ota_end,payload.ota_end), 9) \ X(a, STATIC, ONEOF, MESSAGE, (payload,ota_end,payload.ota_end), 9) \
X(a, STATIC, ONEOF, MESSAGE, (payload,ota_status,payload.ota_status), 10) \ X(a, STATIC, ONEOF, MESSAGE, (payload,ota_status,payload.ota_status), 10) \
X(a, STATIC, ONEOF, MESSAGE, (payload,find_me,payload.find_me), 11) \ X(a, STATIC, ONEOF, MESSAGE, (payload,find_me,payload.find_me), 11) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12) X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_sample,payload.accel_sample), 13) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream,payload.accel_stream), 14) \
X(a, STATIC, ONEOF, MESSAGE, (payload,led_ring,payload.led_ring), 15) \
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_query,payload.battery_query), 16) \
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_report,payload.battery_report), 17) \
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify,payload.tap_notify), 18) \
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_event,payload.tap_event), 19)
#define alox_EspNowMessage_CALLBACK NULL #define alox_EspNowMessage_CALLBACK NULL
#define alox_EspNowMessage_DEFAULT NULL #define alox_EspNowMessage_DEFAULT NULL
#define alox_EspNowMessage_payload_discover_MSGTYPE alox_EspNowDiscover #define alox_EspNowMessage_payload_discover_MSGTYPE alox_EspNowDiscover
@ -266,6 +456,13 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12)
#define alox_EspNowMessage_payload_ota_status_MSGTYPE alox_EspNowOtaStatus #define alox_EspNowMessage_payload_ota_status_MSGTYPE alox_EspNowOtaStatus
#define alox_EspNowMessage_payload_find_me_MSGTYPE alox_EspNowFindMe #define alox_EspNowMessage_payload_find_me_MSGTYPE alox_EspNowFindMe
#define alox_EspNowMessage_payload_restart_MSGTYPE alox_EspNowRestart #define alox_EspNowMessage_payload_restart_MSGTYPE alox_EspNowRestart
#define alox_EspNowMessage_payload_accel_sample_MSGTYPE alox_EspNowAccelSample
#define alox_EspNowMessage_payload_accel_stream_MSGTYPE alox_EspNowAccelStream
#define alox_EspNowMessage_payload_led_ring_MSGTYPE alox_EspNowLedRing
#define alox_EspNowMessage_payload_battery_query_MSGTYPE alox_EspNowBatteryQuery
#define alox_EspNowMessage_payload_battery_report_MSGTYPE alox_EspNowBatteryReport
#define alox_EspNowMessage_payload_tap_notify_MSGTYPE alox_EspNowTapNotify
#define alox_EspNowMessage_payload_tap_event_MSGTYPE alox_EspNowTapEvent
extern const pb_msgdesc_t alox_EspNowUnicastTest_msg; extern const pb_msgdesc_t alox_EspNowUnicastTest_msg;
extern const pb_msgdesc_t alox_EspNowFindMe_msg; extern const pb_msgdesc_t alox_EspNowFindMe_msg;
@ -273,6 +470,13 @@ extern const pb_msgdesc_t alox_EspNowRestart_msg;
extern const pb_msgdesc_t alox_EspNowDiscover_msg; extern const pb_msgdesc_t alox_EspNowDiscover_msg;
extern const pb_msgdesc_t alox_EspNowSlavePresence_msg; extern const pb_msgdesc_t alox_EspNowSlavePresence_msg;
extern const pb_msgdesc_t alox_EspNowAccelDeadzone_msg; extern const pb_msgdesc_t alox_EspNowAccelDeadzone_msg;
extern const pb_msgdesc_t alox_EspNowAccelStream_msg;
extern const pb_msgdesc_t alox_EspNowAccelSample_msg;
extern const pb_msgdesc_t alox_EspNowBatteryQuery_msg;
extern const pb_msgdesc_t alox_EspNowTapNotify_msg;
extern const pb_msgdesc_t alox_EspNowTapEvent_msg;
extern const pb_msgdesc_t alox_EspNowBatteryReport_msg;
extern const pb_msgdesc_t alox_EspNowLedRing_msg;
extern const pb_msgdesc_t alox_EspNowOtaStart_msg; extern const pb_msgdesc_t alox_EspNowOtaStart_msg;
extern const pb_msgdesc_t alox_EspNowOtaPayload_msg; extern const pb_msgdesc_t alox_EspNowOtaPayload_msg;
extern const pb_msgdesc_t alox_EspNowOtaEnd_msg; extern const pb_msgdesc_t alox_EspNowOtaEnd_msg;
@ -286,6 +490,13 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
#define alox_EspNowDiscover_fields &alox_EspNowDiscover_msg #define alox_EspNowDiscover_fields &alox_EspNowDiscover_msg
#define alox_EspNowSlavePresence_fields &alox_EspNowSlavePresence_msg #define alox_EspNowSlavePresence_fields &alox_EspNowSlavePresence_msg
#define alox_EspNowAccelDeadzone_fields &alox_EspNowAccelDeadzone_msg #define alox_EspNowAccelDeadzone_fields &alox_EspNowAccelDeadzone_msg
#define alox_EspNowAccelStream_fields &alox_EspNowAccelStream_msg
#define alox_EspNowAccelSample_fields &alox_EspNowAccelSample_msg
#define alox_EspNowBatteryQuery_fields &alox_EspNowBatteryQuery_msg
#define alox_EspNowTapNotify_fields &alox_EspNowTapNotify_msg
#define alox_EspNowTapEvent_fields &alox_EspNowTapEvent_msg
#define alox_EspNowBatteryReport_fields &alox_EspNowBatteryReport_msg
#define alox_EspNowLedRing_fields &alox_EspNowLedRing_msg
#define alox_EspNowOtaStart_fields &alox_EspNowOtaStart_msg #define alox_EspNowOtaStart_fields &alox_EspNowOtaStart_msg
#define alox_EspNowOtaPayload_fields &alox_EspNowOtaPayload_msg #define alox_EspNowOtaPayload_fields &alox_EspNowOtaPayload_msg
#define alox_EspNowOtaEnd_fields &alox_EspNowOtaEnd_msg #define alox_EspNowOtaEnd_fields &alox_EspNowOtaEnd_msg
@ -297,13 +508,20 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
/* alox_EspNowMessage_size depends on runtime parameters */ /* alox_EspNowMessage_size depends on runtime parameters */
#define ALOX_ESP_NOW_MESSAGES_PB_H_MAX_SIZE alox_EspNowOtaPayload_size #define ALOX_ESP_NOW_MESSAGES_PB_H_MAX_SIZE alox_EspNowOtaPayload_size
#define alox_EspNowAccelDeadzone_size 12 #define alox_EspNowAccelDeadzone_size 12
#define alox_EspNowAccelSample_size 24
#define alox_EspNowAccelStream_size 8
#define alox_EspNowBatteryQuery_size 6
#define alox_EspNowBatteryReport_size 22
#define alox_EspNowDiscover_size 6 #define alox_EspNowDiscover_size 6
#define alox_EspNowFindMe_size 6 #define alox_EspNowFindMe_size 6
#define alox_EspNowLedRing_size 60
#define alox_EspNowOtaEnd_size 0 #define alox_EspNowOtaEnd_size 0
#define alox_EspNowOtaPayload_size 209 #define alox_EspNowOtaPayload_size 209
#define alox_EspNowOtaStart_size 6 #define alox_EspNowOtaStart_size 6
#define alox_EspNowOtaStatus_size 18 #define alox_EspNowOtaStatus_size 18
#define alox_EspNowRestart_size 6 #define alox_EspNowRestart_size 6
#define alox_EspNowTapEvent_size 12
#define alox_EspNowTapNotify_size 12
#define alox_EspNowUnicastTest_size 6 #define alox_EspNowUnicastTest_size 6
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -17,6 +17,13 @@ enum EspNowMessageType {
ESPNOW_OTA_STATUS = 9; ESPNOW_OTA_STATUS = 9;
ESPNOW_FIND_ME = 10; ESPNOW_FIND_ME = 10;
ESPNOW_RESTART = 11; ESPNOW_RESTART = 11;
ESPNOW_ACCEL_SAMPLE = 12;
ESPNOW_SET_ACCEL_STREAM = 13;
ESPNOW_LED_RING = 14;
ESPNOW_BATTERY_QUERY = 15;
ESPNOW_BATTERY_REPORT = 16;
ESPNOW_SET_TAP_NOTIFY = 17;
ESPNOW_TAP_EVENT = 18;
} }
message EspNowUnicastTest { message EspNowUnicastTest {
@ -52,6 +59,63 @@ message EspNowAccelDeadzone {
uint32 client_id = 2; // 0 = all slaves; otherwise only matching slave_id applies uint32 client_id = 2; // 0 = all slaves; otherwise only matching slave_id applies
} }
/** Master → slave: enable/disable periodic accel ESP-NOW stream (~16 ms). */
message EspNowAccelStream {
bool enable = 1;
uint32 client_id = 2;
}
/** Slave → master: latest BMA456 sample (sent ~every 16 ms). */
message EspNowAccelSample {
uint32 slave_id = 1;
sint32 x = 2;
sint32 y = 3;
sint32 z = 4;
}
/** Master → slave: on-demand LiPo read (optional; slaves also push every ~30 s). */
message EspNowBatteryQuery {
uint32 client_id = 1;
}
/** Master → slave: which tap kinds should be reported via ESP-NOW. */
message EspNowTapNotify {
uint32 client_id = 1;
bool single = 2;
bool double_tap = 3;
bool triple = 4;
}
/** Slave → master: tap detected on BMA456 (event, not periodic). */
message EspNowTapEvent {
uint32 slave_id = 1;
/** 1=single, 2=double, 3=triple */
uint32 kind = 2;
}
/** Slave → master: LiPo voltages (periodic ~30 s and on query). */
message EspNowBatteryReport {
uint32 client_id = 1;
bool lipo1_valid = 2;
bool lipo2_valid = 3;
uint32 lipo1_mv = 4;
uint32 lipo2_mv = 5;
}
/** Master → slave: LED ring command (same modes as UART LedRingProgressRequest). */
message EspNowLedRing {
uint32 client_id = 1;
uint32 mode = 2;
uint32 progress = 3;
uint32 digit = 4;
uint32 r = 5;
uint32 g = 6;
uint32 b = 7;
uint32 intensity = 8;
uint32 blink_ms = 9;
uint32 blink_count = 10;
}
// Master slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS). // Master slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS).
message EspNowOtaStart { message EspNowOtaStart {
uint32 total_size = 1; uint32 total_size = 1;
@ -87,5 +151,12 @@ message EspNowMessage {
EspNowOtaStatus ota_status = 10; EspNowOtaStatus ota_status = 10;
EspNowFindMe find_me = 11; EspNowFindMe find_me = 11;
EspNowRestart restart = 12; EspNowRestart restart = 12;
EspNowAccelSample accel_sample = 13;
EspNowAccelStream accel_stream = 14;
EspNowLedRing led_ring = 15;
EspNowBatteryQuery battery_query = 16;
EspNowBatteryReport battery_report = 17;
EspNowTapNotify tap_notify = 18;
EspNowTapEvent tap_event = 19;
} }
} }

View File

@ -36,6 +36,51 @@ PB_BIND(alox_AccelDeadzoneRequest, alox_AccelDeadzoneRequest, AUTO)
PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO) PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO)
PB_BIND(alox_AccelStreamRequest, alox_AccelStreamRequest, AUTO)
PB_BIND(alox_AccelStreamResponse, alox_AccelStreamResponse, AUTO)
PB_BIND(alox_BatteryStatusRequest, alox_BatteryStatusRequest, AUTO)
PB_BIND(alox_LipoReading, alox_LipoReading, AUTO)
PB_BIND(alox_BatterySample, alox_BatterySample, AUTO)
PB_BIND(alox_BatteryStatusResponse, alox_BatteryStatusResponse, 2)
PB_BIND(alox_AccelSample, alox_AccelSample, AUTO)
PB_BIND(alox_TapNotifyRequest, alox_TapNotifyRequest, AUTO)
PB_BIND(alox_TapNotifyResponse, alox_TapNotifyResponse, AUTO)
PB_BIND(alox_TapEvent, alox_TapEvent, AUTO)
PB_BIND(alox_CacheStatusRequest, alox_CacheStatusRequest, AUTO)
PB_BIND(alox_CacheClientAccel, alox_CacheClientAccel, AUTO)
PB_BIND(alox_CacheClientTap, alox_CacheClientTap, AUTO)
PB_BIND(alox_CacheClientStatus, alox_CacheClientStatus, AUTO)
PB_BIND(alox_CacheStatusResponse, alox_CacheStatusResponse, 2)
PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO) PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO)
@ -84,3 +129,5 @@ PB_BIND(alox_OtaSlaveProgressResponse, alox_OtaSlaveProgressResponse, 2)

View File

@ -27,9 +27,21 @@ typedef enum _alox_MessageType {
alox_MessageType_OTA_START_ESPNOW = 20, alox_MessageType_OTA_START_ESPNOW = 20,
alox_MessageType_OTA_SLAVE_PROGRESS = 21, alox_MessageType_OTA_SLAVE_PROGRESS = 21,
alox_MessageType_FIND_ME = 22, alox_MessageType_FIND_ME = 22,
alox_MessageType_RESTART = 23 alox_MessageType_RESTART = 23,
alox_MessageType_ACCEL_STREAM = 25,
alox_MessageType_BATTERY_STATUS = 26,
alox_MessageType_TAP_NOTIFY = 27,
/* * Combined cached accel + tap poll (one UART round-trip, ~16 ms cadence). */
alox_MessageType_CACHE_STATUS = 29
} alox_MessageType; } alox_MessageType;
typedef enum _alox_TapKind {
alox_TapKind_TAP_NONE = 0,
alox_TapKind_TAP_SINGLE = 1,
alox_TapKind_TAP_DOUBLE = 2,
alox_TapKind_TAP_TRIPLE = 3
} alox_TapKind;
/* Struct definitions */ /* Struct definitions */
typedef struct _alox_Ack { typedef struct _alox_Ack {
char dummy_field; char dummy_field;
@ -54,6 +66,12 @@ typedef struct _alox_ClientInfo {
uint32_t last_ping; uint32_t last_ping;
uint32_t last_success_ping; uint32_t last_success_ping;
uint32_t version; uint32_t version;
/* * Master: ESP-NOW accel stream enabled for this slave. */
bool accel_stream_enabled;
/* * Master: ESP-NOW tap notify flags for this slave. */
bool tap_notify_single;
bool tap_notify_double;
bool tap_notify_triple;
} alox_ClientInfo; } alox_ClientInfo;
typedef struct _alox_ClientInfoResponse { typedef struct _alox_ClientInfoResponse {
@ -88,6 +106,125 @@ typedef struct _alox_AccelDeadzoneResponse {
uint32_t slaves_updated; uint32_t slaves_updated;
} alox_AccelDeadzoneResponse; } alox_AccelDeadzoneResponse;
/* Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients). */
typedef struct _alox_AccelStreamRequest {
bool write;
bool enable;
uint32_t client_id;
bool all_clients;
} alox_AccelStreamRequest;
typedef struct _alox_AccelStreamResponse {
bool enabled;
uint32_t client_id;
bool success;
uint32_t slaves_updated;
} alox_AccelStreamResponse;
/* * Host → master: read LiPo ADC voltages (master local and/or slaves via ESP-NOW). */
typedef struct _alox_BatteryStatusRequest {
/* * 0 = master only; >0 = one slave; ignored when all_clients */
uint32_t client_id;
/* * Master (client_id 0) plus every registered slave */
bool all_clients;
} alox_BatteryStatusRequest;
typedef struct _alox_LipoReading {
bool valid;
/* * Estimated pack voltage in millivolts from ADC */
uint32_t voltage_mv;
} alox_LipoReading;
typedef struct _alox_BatterySample {
uint32_t client_id;
bool has_lipo1;
alox_LipoReading lipo1;
bool has_lipo2;
alox_LipoReading lipo2;
/* * Milliseconds since last ESP-NOW battery report from this pod. */
uint32_t age_ms;
} alox_BatterySample;
typedef struct _alox_BatteryStatusResponse {
bool success;
pb_size_t samples_count;
alox_BatterySample samples[17];
} alox_BatteryStatusResponse;
/* * Legacy host-side sample shape (dashboard helpers); use CACHE_STATUS on the wire. */
typedef struct _alox_AccelSample {
uint32_t client_id;
bool valid;
int32_t x;
int32_t y;
int32_t z;
/* * Milliseconds since last ESP-NOW sample from this slave. */
uint32_t age_ms;
} alox_AccelSample;
/* * Host → master: enable/disable tap ESP-NOW notify per slave (single/double/triple). */
typedef struct _alox_TapNotifyRequest {
bool write;
uint32_t client_id;
bool all_clients;
bool single;
bool double_tap;
bool triple;
} alox_TapNotifyRequest;
typedef struct _alox_TapNotifyResponse {
uint32_t client_id;
bool success;
uint32_t slaves_updated;
bool single;
bool double_tap;
bool triple;
} alox_TapNotifyResponse;
/* * Legacy tap event shape (dashboard helpers); use CACHE_STATUS on the wire. */
typedef struct _alox_TapEvent {
uint32_t client_id;
bool valid;
alox_TapKind kind;
uint32_t age_ms;
} alox_TapEvent;
/* * Host → master: one-shot read of subscribed cached slave data (no request body). */
typedef struct _alox_CacheStatusRequest {
char dummy_field;
} alox_CacheStatusRequest;
/* * Accel slice inside CACHE_STATUS (no client_id — use parent CacheClientStatus). */
typedef struct _alox_CacheClientAccel {
bool valid;
int32_t x;
int32_t y;
int32_t z;
uint32_t age_ms;
} alox_CacheClientAccel;
/* * Tap slice inside CACHE_STATUS; only present when a pending tap was consumed. */
typedef struct _alox_CacheClientTap {
alox_TapKind kind;
uint32_t age_ms;
} alox_CacheClientTap;
/* * One slave with accel and/or tap notify enabled; only subscribed fields are set. */
typedef struct _alox_CacheClientStatus {
uint32_t client_id;
bool has_accel;
alox_CacheClientAccel accel;
bool has_tap;
alox_CacheClientTap tap;
} alox_CacheClientStatus;
typedef struct _alox_CacheStatusResponse {
/* * Slaves with accel_stream and/or tap notify; omitted fields are not subscribed. */
pb_size_t clients_count;
alox_CacheClientStatus clients[16];
} alox_CacheStatusResponse;
typedef struct _alox_EspNowUnicastTestRequest { typedef struct _alox_EspNowUnicastTestRequest {
uint32_t client_id; uint32_t client_id;
uint32_t seq; uint32_t seq;
@ -98,8 +235,8 @@ typedef struct _alox_EspNowUnicastTestResponse {
uint32_t seq; uint32_t seq;
} alox_EspNowUnicastTestResponse; } alox_EspNowUnicastTestResponse;
/* Host → device: LED ring display (progress bar, digit, clear, blink, or find-me). /* Host → master: LED ring on master (client_id=0) and/or slaves via ESP-NOW.
mode: 0=clear, 1=progress (0100 %), 2=digit (010), 3=blink full ring, 4=find-me (R/G/B ×3 @ full brightness). */ mode: 0=clear, 1=progress (0100 %), 2=digit (010), 3=blink, 4=find-me, 5=all LEDs solid color. */
typedef struct _alox_LedRingProgressRequest { typedef struct _alox_LedRingProgressRequest {
uint32_t mode; uint32_t mode;
/* * 0100: fraction of ring LEDs to light (mode=progress) */ /* * 0100: fraction of ring LEDs to light (mode=progress) */
@ -115,6 +252,12 @@ typedef struct _alox_LedRingProgressRequest {
uint32_t blink_ms; uint32_t blink_ms;
/* * Number of pulses (mode=blink, default 1) */ /* * Number of pulses (mode=blink, default 1) */
uint32_t blink_count; uint32_t blink_count;
/* * 0 = master ring only; >0 = one slave; ignored when all_clients */
uint32_t client_id;
/* * Broadcast to all registered slaves (and optionally master unless slaves_only) */
bool all_clients;
/* * With all_clients: do not change master ring */
bool slaves_only;
} alox_LedRingProgressRequest; } alox_LedRingProgressRequest;
typedef struct _alox_LedRingProgressResponse { typedef struct _alox_LedRingProgressResponse {
@ -122,6 +265,8 @@ typedef struct _alox_LedRingProgressResponse {
uint32_t mode; uint32_t mode;
uint32_t progress; uint32_t progress;
uint32_t digit; uint32_t digit;
uint32_t client_id;
uint32_t slaves_updated;
} alox_LedRingProgressResponse; } alox_LedRingProgressResponse;
/* * Host → master: find-me on local ring (client_id=0) or ESP-NOW unicast to one slave. */ /* * Host → master: find-me on local ring (client_id=0) or ESP-NOW unicast to one slave. */
@ -218,6 +363,14 @@ typedef struct _alox_UartMessage {
alox_EspNowFindMeResponse espnow_find_me_response; alox_EspNowFindMeResponse espnow_find_me_response;
alox_RestartRequest restart_request; alox_RestartRequest restart_request;
alox_RestartResponse restart_response; alox_RestartResponse restart_response;
alox_AccelStreamRequest accel_stream_request;
alox_AccelStreamResponse accel_stream_response;
alox_BatteryStatusRequest battery_status_request;
alox_BatteryStatusResponse battery_status_response;
alox_TapNotifyRequest tap_notify_request;
alox_TapNotifyResponse tap_notify_response;
alox_CacheStatusRequest cache_status_request;
alox_CacheStatusResponse cache_status_response;
} payload; } payload;
} alox_UartMessage; } alox_UartMessage;
@ -228,8 +381,12 @@ extern "C" {
/* Helper constants for enums */ /* Helper constants for enums */
#define _alox_MessageType_MIN alox_MessageType_UNKNOWN #define _alox_MessageType_MIN alox_MessageType_UNKNOWN
#define _alox_MessageType_MAX alox_MessageType_RESTART #define _alox_MessageType_MAX alox_MessageType_CACHE_STATUS
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_RESTART+1)) #define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_CACHE_STATUS+1))
#define _alox_TapKind_MIN alox_TapKind_TAP_NONE
#define _alox_TapKind_MAX alox_TapKind_TAP_TRIPLE
#define _alox_TapKind_ARRAYSIZE ((alox_TapKind)(alox_TapKind_TAP_TRIPLE+1))
#define alox_UartMessage_type_ENUMTYPE alox_MessageType #define alox_UartMessage_type_ENUMTYPE alox_MessageType
@ -251,6 +408,23 @@ extern "C" {
#define alox_TapEvent_kind_ENUMTYPE alox_TapKind
#define alox_CacheClientTap_kind_ENUMTYPE alox_TapKind
@ -263,16 +437,31 @@ extern "C" {
#define alox_Ack_init_default {0} #define alox_Ack_init_default {0}
#define alox_EchoPayload_init_default {{{NULL}, NULL}} #define alox_EchoPayload_init_default {{{NULL}, NULL}}
#define alox_VersionResponse_init_default {0, {{NULL}, NULL}, {{NULL}, NULL}} #define alox_VersionResponse_init_default {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define alox_ClientInfo_init_default {0, 0, 0, {{NULL}, NULL}, 0, 0, 0} #define alox_ClientInfo_init_default {0, 0, 0, {{NULL}, NULL}, 0, 0, 0, 0, 0, 0, 0}
#define alox_ClientInfoResponse_init_default {{{NULL}, NULL}} #define alox_ClientInfoResponse_init_default {{{NULL}, NULL}}
#define alox_ClientInput_init_default {0, 0, 0, 0} #define alox_ClientInput_init_default {0, 0, 0, 0}
#define alox_ClientInputResponse_init_default {{{NULL}, NULL}} #define alox_ClientInputResponse_init_default {{{NULL}, NULL}}
#define alox_AccelDeadzoneRequest_init_default {0, 0, 0, 0} #define alox_AccelDeadzoneRequest_init_default {0, 0, 0, 0}
#define alox_AccelDeadzoneResponse_init_default {0, 0, 0, 0} #define alox_AccelDeadzoneResponse_init_default {0, 0, 0, 0}
#define alox_AccelStreamRequest_init_default {0, 0, 0, 0}
#define alox_AccelStreamResponse_init_default {0, 0, 0, 0}
#define alox_BatteryStatusRequest_init_default {0, 0}
#define alox_LipoReading_init_default {0, 0}
#define alox_BatterySample_init_default {0, false, alox_LipoReading_init_default, false, alox_LipoReading_init_default, 0}
#define alox_BatteryStatusResponse_init_default {0, 0, {alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default, alox_BatterySample_init_default}}
#define alox_AccelSample_init_default {0, 0, 0, 0, 0, 0}
#define alox_TapNotifyRequest_init_default {0, 0, 0, 0, 0, 0}
#define alox_TapNotifyResponse_init_default {0, 0, 0, 0, 0, 0}
#define alox_TapEvent_init_default {0, 0, _alox_TapKind_MIN, 0}
#define alox_CacheStatusRequest_init_default {0}
#define alox_CacheClientAccel_init_default {0, 0, 0, 0, 0}
#define alox_CacheClientTap_init_default {_alox_TapKind_MIN, 0}
#define alox_CacheClientStatus_init_default {0, false, alox_CacheClientAccel_init_default, false, alox_CacheClientTap_init_default}
#define alox_CacheStatusResponse_init_default {0, {alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default}}
#define alox_EspNowUnicastTestRequest_init_default {0, 0} #define alox_EspNowUnicastTestRequest_init_default {0, 0}
#define alox_EspNowUnicastTestResponse_init_default {0, 0} #define alox_EspNowUnicastTestResponse_init_default {0, 0}
#define alox_LedRingProgressRequest_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0} #define alox_LedRingProgressRequest_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define alox_LedRingProgressResponse_init_default {0, 0, 0, 0} #define alox_LedRingProgressResponse_init_default {0, 0, 0, 0, 0, 0}
#define alox_EspNowFindMeRequest_init_default {0} #define alox_EspNowFindMeRequest_init_default {0}
#define alox_EspNowFindMeResponse_init_default {0, 0} #define alox_EspNowFindMeResponse_init_default {0, 0}
#define alox_RestartRequest_init_default {0} #define alox_RestartRequest_init_default {0}
@ -288,16 +477,31 @@ extern "C" {
#define alox_Ack_init_zero {0} #define alox_Ack_init_zero {0}
#define alox_EchoPayload_init_zero {{{NULL}, NULL}} #define alox_EchoPayload_init_zero {{{NULL}, NULL}}
#define alox_VersionResponse_init_zero {0, {{NULL}, NULL}, {{NULL}, NULL}} #define alox_VersionResponse_init_zero {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define alox_ClientInfo_init_zero {0, 0, 0, {{NULL}, NULL}, 0, 0, 0} #define alox_ClientInfo_init_zero {0, 0, 0, {{NULL}, NULL}, 0, 0, 0, 0, 0, 0, 0}
#define alox_ClientInfoResponse_init_zero {{{NULL}, NULL}} #define alox_ClientInfoResponse_init_zero {{{NULL}, NULL}}
#define alox_ClientInput_init_zero {0, 0, 0, 0} #define alox_ClientInput_init_zero {0, 0, 0, 0}
#define alox_ClientInputResponse_init_zero {{{NULL}, NULL}} #define alox_ClientInputResponse_init_zero {{{NULL}, NULL}}
#define alox_AccelDeadzoneRequest_init_zero {0, 0, 0, 0} #define alox_AccelDeadzoneRequest_init_zero {0, 0, 0, 0}
#define alox_AccelDeadzoneResponse_init_zero {0, 0, 0, 0} #define alox_AccelDeadzoneResponse_init_zero {0, 0, 0, 0}
#define alox_AccelStreamRequest_init_zero {0, 0, 0, 0}
#define alox_AccelStreamResponse_init_zero {0, 0, 0, 0}
#define alox_BatteryStatusRequest_init_zero {0, 0}
#define alox_LipoReading_init_zero {0, 0}
#define alox_BatterySample_init_zero {0, false, alox_LipoReading_init_zero, false, alox_LipoReading_init_zero, 0}
#define alox_BatteryStatusResponse_init_zero {0, 0, {alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero, alox_BatterySample_init_zero}}
#define alox_AccelSample_init_zero {0, 0, 0, 0, 0, 0}
#define alox_TapNotifyRequest_init_zero {0, 0, 0, 0, 0, 0}
#define alox_TapNotifyResponse_init_zero {0, 0, 0, 0, 0, 0}
#define alox_TapEvent_init_zero {0, 0, _alox_TapKind_MIN, 0}
#define alox_CacheStatusRequest_init_zero {0}
#define alox_CacheClientAccel_init_zero {0, 0, 0, 0, 0}
#define alox_CacheClientTap_init_zero {_alox_TapKind_MIN, 0}
#define alox_CacheClientStatus_init_zero {0, false, alox_CacheClientAccel_init_zero, false, alox_CacheClientTap_init_zero}
#define alox_CacheStatusResponse_init_zero {0, {alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero}}
#define alox_EspNowUnicastTestRequest_init_zero {0, 0} #define alox_EspNowUnicastTestRequest_init_zero {0, 0}
#define alox_EspNowUnicastTestResponse_init_zero {0, 0} #define alox_EspNowUnicastTestResponse_init_zero {0, 0}
#define alox_LedRingProgressRequest_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0} #define alox_LedRingProgressRequest_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define alox_LedRingProgressResponse_init_zero {0, 0, 0, 0} #define alox_LedRingProgressResponse_init_zero {0, 0, 0, 0, 0, 0}
#define alox_EspNowFindMeRequest_init_zero {0} #define alox_EspNowFindMeRequest_init_zero {0}
#define alox_EspNowFindMeResponse_init_zero {0, 0} #define alox_EspNowFindMeResponse_init_zero {0, 0}
#define alox_RestartRequest_init_zero {0} #define alox_RestartRequest_init_zero {0}
@ -322,6 +526,10 @@ extern "C" {
#define alox_ClientInfo_last_ping_tag 5 #define alox_ClientInfo_last_ping_tag 5
#define alox_ClientInfo_last_success_ping_tag 6 #define alox_ClientInfo_last_success_ping_tag 6
#define alox_ClientInfo_version_tag 7 #define alox_ClientInfo_version_tag 7
#define alox_ClientInfo_accel_stream_enabled_tag 8
#define alox_ClientInfo_tap_notify_single_tag 9
#define alox_ClientInfo_tap_notify_double_tag 10
#define alox_ClientInfo_tap_notify_triple_tag 11
#define alox_ClientInfoResponse_clients_tag 1 #define alox_ClientInfoResponse_clients_tag 1
#define alox_ClientInput_id_tag 1 #define alox_ClientInput_id_tag 1
#define alox_ClientInput_lage_x_tag 2 #define alox_ClientInput_lage_x_tag 2
@ -336,6 +544,57 @@ extern "C" {
#define alox_AccelDeadzoneResponse_client_id_tag 2 #define alox_AccelDeadzoneResponse_client_id_tag 2
#define alox_AccelDeadzoneResponse_success_tag 3 #define alox_AccelDeadzoneResponse_success_tag 3
#define alox_AccelDeadzoneResponse_slaves_updated_tag 4 #define alox_AccelDeadzoneResponse_slaves_updated_tag 4
#define alox_AccelStreamRequest_write_tag 1
#define alox_AccelStreamRequest_enable_tag 2
#define alox_AccelStreamRequest_client_id_tag 3
#define alox_AccelStreamRequest_all_clients_tag 4
#define alox_AccelStreamResponse_enabled_tag 1
#define alox_AccelStreamResponse_client_id_tag 2
#define alox_AccelStreamResponse_success_tag 3
#define alox_AccelStreamResponse_slaves_updated_tag 4
#define alox_BatteryStatusRequest_client_id_tag 1
#define alox_BatteryStatusRequest_all_clients_tag 2
#define alox_LipoReading_valid_tag 1
#define alox_LipoReading_voltage_mv_tag 2
#define alox_BatterySample_client_id_tag 1
#define alox_BatterySample_lipo1_tag 2
#define alox_BatterySample_lipo2_tag 3
#define alox_BatterySample_age_ms_tag 4
#define alox_BatteryStatusResponse_success_tag 1
#define alox_BatteryStatusResponse_samples_tag 2
#define alox_AccelSample_client_id_tag 1
#define alox_AccelSample_valid_tag 2
#define alox_AccelSample_x_tag 3
#define alox_AccelSample_y_tag 4
#define alox_AccelSample_z_tag 5
#define alox_AccelSample_age_ms_tag 6
#define alox_TapNotifyRequest_write_tag 1
#define alox_TapNotifyRequest_client_id_tag 2
#define alox_TapNotifyRequest_all_clients_tag 3
#define alox_TapNotifyRequest_single_tag 4
#define alox_TapNotifyRequest_double_tap_tag 5
#define alox_TapNotifyRequest_triple_tag 6
#define alox_TapNotifyResponse_client_id_tag 1
#define alox_TapNotifyResponse_success_tag 2
#define alox_TapNotifyResponse_slaves_updated_tag 3
#define alox_TapNotifyResponse_single_tag 4
#define alox_TapNotifyResponse_double_tap_tag 5
#define alox_TapNotifyResponse_triple_tag 6
#define alox_TapEvent_client_id_tag 1
#define alox_TapEvent_valid_tag 2
#define alox_TapEvent_kind_tag 3
#define alox_TapEvent_age_ms_tag 4
#define alox_CacheClientAccel_valid_tag 1
#define alox_CacheClientAccel_x_tag 2
#define alox_CacheClientAccel_y_tag 3
#define alox_CacheClientAccel_z_tag 4
#define alox_CacheClientAccel_age_ms_tag 5
#define alox_CacheClientTap_kind_tag 1
#define alox_CacheClientTap_age_ms_tag 2
#define alox_CacheClientStatus_client_id_tag 1
#define alox_CacheClientStatus_accel_tag 2
#define alox_CacheClientStatus_tap_tag 3
#define alox_CacheStatusResponse_clients_tag 1
#define alox_EspNowUnicastTestRequest_client_id_tag 1 #define alox_EspNowUnicastTestRequest_client_id_tag 1
#define alox_EspNowUnicastTestRequest_seq_tag 2 #define alox_EspNowUnicastTestRequest_seq_tag 2
#define alox_EspNowUnicastTestResponse_success_tag 1 #define alox_EspNowUnicastTestResponse_success_tag 1
@ -349,10 +608,15 @@ extern "C" {
#define alox_LedRingProgressRequest_intensity_tag 7 #define alox_LedRingProgressRequest_intensity_tag 7
#define alox_LedRingProgressRequest_blink_ms_tag 8 #define alox_LedRingProgressRequest_blink_ms_tag 8
#define alox_LedRingProgressRequest_blink_count_tag 9 #define alox_LedRingProgressRequest_blink_count_tag 9
#define alox_LedRingProgressRequest_client_id_tag 10
#define alox_LedRingProgressRequest_all_clients_tag 11
#define alox_LedRingProgressRequest_slaves_only_tag 12
#define alox_LedRingProgressResponse_success_tag 1 #define alox_LedRingProgressResponse_success_tag 1
#define alox_LedRingProgressResponse_mode_tag 2 #define alox_LedRingProgressResponse_mode_tag 2
#define alox_LedRingProgressResponse_progress_tag 3 #define alox_LedRingProgressResponse_progress_tag 3
#define alox_LedRingProgressResponse_digit_tag 4 #define alox_LedRingProgressResponse_digit_tag 4
#define alox_LedRingProgressResponse_client_id_tag 5
#define alox_LedRingProgressResponse_slaves_updated_tag 6
#define alox_EspNowFindMeRequest_client_id_tag 1 #define alox_EspNowFindMeRequest_client_id_tag 1
#define alox_EspNowFindMeResponse_success_tag 1 #define alox_EspNowFindMeResponse_success_tag 1
#define alox_EspNowFindMeResponse_client_id_tag 2 #define alox_EspNowFindMeResponse_client_id_tag 2
@ -399,6 +663,14 @@ extern "C" {
#define alox_UartMessage_espnow_find_me_response_tag 20 #define alox_UartMessage_espnow_find_me_response_tag 20
#define alox_UartMessage_restart_request_tag 21 #define alox_UartMessage_restart_request_tag 21
#define alox_UartMessage_restart_response_tag 22 #define alox_UartMessage_restart_response_tag 22
#define alox_UartMessage_accel_stream_request_tag 25
#define alox_UartMessage_accel_stream_response_tag 26
#define alox_UartMessage_battery_status_request_tag 27
#define alox_UartMessage_battery_status_response_tag 28
#define alox_UartMessage_tap_notify_request_tag 29
#define alox_UartMessage_tap_notify_response_tag 30
#define alox_UartMessage_cache_status_request_tag 33
#define alox_UartMessage_cache_status_response_tag 34
/* Struct field encoding specification for nanopb */ /* Struct field encoding specification for nanopb */
#define alox_UartMessage_FIELDLIST(X, a) \ #define alox_UartMessage_FIELDLIST(X, a) \
@ -423,7 +695,15 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,led_ring_progress_response,payload.l
X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_request,payload.espnow_find_me_request), 19) \ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_request,payload.espnow_find_me_request), 19) \
X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_response,payload.espnow_find_me_response), 20) \ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_response,payload.espnow_find_me_response), 20) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart_request,payload.restart_request), 21) \ X(a, STATIC, ONEOF, MESSAGE, (payload,restart_request,payload.restart_request), 21) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_response), 22) X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_response), 22) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream_request,payload.accel_stream_request), 25) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream_response,payload.accel_stream_response), 26) \
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_status_request,payload.battery_status_request), 27) \
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_status_response,payload.battery_status_response), 28) \
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify_request,payload.tap_notify_request), 29) \
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify_response,payload.tap_notify_response), 30) \
X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_request,payload.cache_status_request), 33) \
X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_response,payload.cache_status_response), 34)
#define alox_UartMessage_CALLBACK NULL #define alox_UartMessage_CALLBACK NULL
#define alox_UartMessage_DEFAULT NULL #define alox_UartMessage_DEFAULT NULL
#define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack #define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack
@ -447,6 +727,14 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_res
#define alox_UartMessage_payload_espnow_find_me_response_MSGTYPE alox_EspNowFindMeResponse #define alox_UartMessage_payload_espnow_find_me_response_MSGTYPE alox_EspNowFindMeResponse
#define alox_UartMessage_payload_restart_request_MSGTYPE alox_RestartRequest #define alox_UartMessage_payload_restart_request_MSGTYPE alox_RestartRequest
#define alox_UartMessage_payload_restart_response_MSGTYPE alox_RestartResponse #define alox_UartMessage_payload_restart_response_MSGTYPE alox_RestartResponse
#define alox_UartMessage_payload_accel_stream_request_MSGTYPE alox_AccelStreamRequest
#define alox_UartMessage_payload_accel_stream_response_MSGTYPE alox_AccelStreamResponse
#define alox_UartMessage_payload_battery_status_request_MSGTYPE alox_BatteryStatusRequest
#define alox_UartMessage_payload_battery_status_response_MSGTYPE alox_BatteryStatusResponse
#define alox_UartMessage_payload_tap_notify_request_MSGTYPE alox_TapNotifyRequest
#define alox_UartMessage_payload_tap_notify_response_MSGTYPE alox_TapNotifyResponse
#define alox_UartMessage_payload_cache_status_request_MSGTYPE alox_CacheStatusRequest
#define alox_UartMessage_payload_cache_status_response_MSGTYPE alox_CacheStatusResponse
#define alox_Ack_FIELDLIST(X, a) \ #define alox_Ack_FIELDLIST(X, a) \
@ -472,7 +760,11 @@ X(a, STATIC, SINGULAR, BOOL, used, 3) \
X(a, CALLBACK, SINGULAR, BYTES, mac, 4) \ X(a, CALLBACK, SINGULAR, BYTES, mac, 4) \
X(a, STATIC, SINGULAR, UINT32, last_ping, 5) \ X(a, STATIC, SINGULAR, UINT32, last_ping, 5) \
X(a, STATIC, SINGULAR, UINT32, last_success_ping, 6) \ X(a, STATIC, SINGULAR, UINT32, last_success_ping, 6) \
X(a, STATIC, SINGULAR, UINT32, version, 7) X(a, STATIC, SINGULAR, UINT32, version, 7) \
X(a, STATIC, SINGULAR, BOOL, accel_stream_enabled, 8) \
X(a, STATIC, SINGULAR, BOOL, tap_notify_single, 9) \
X(a, STATIC, SINGULAR, BOOL, tap_notify_double, 10) \
X(a, STATIC, SINGULAR, BOOL, tap_notify_triple, 11)
#define alox_ClientInfo_CALLBACK pb_default_field_callback #define alox_ClientInfo_CALLBACK pb_default_field_callback
#define alox_ClientInfo_DEFAULT NULL #define alox_ClientInfo_DEFAULT NULL
@ -512,6 +804,124 @@ X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4)
#define alox_AccelDeadzoneResponse_CALLBACK NULL #define alox_AccelDeadzoneResponse_CALLBACK NULL
#define alox_AccelDeadzoneResponse_DEFAULT NULL #define alox_AccelDeadzoneResponse_DEFAULT NULL
#define alox_AccelStreamRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, write, 1) \
X(a, STATIC, SINGULAR, BOOL, enable, 2) \
X(a, STATIC, SINGULAR, UINT32, client_id, 3) \
X(a, STATIC, SINGULAR, BOOL, all_clients, 4)
#define alox_AccelStreamRequest_CALLBACK NULL
#define alox_AccelStreamRequest_DEFAULT NULL
#define alox_AccelStreamResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, enabled, 1) \
X(a, STATIC, SINGULAR, UINT32, client_id, 2) \
X(a, STATIC, SINGULAR, BOOL, success, 3) \
X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4)
#define alox_AccelStreamResponse_CALLBACK NULL
#define alox_AccelStreamResponse_DEFAULT NULL
#define alox_BatteryStatusRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, all_clients, 2)
#define alox_BatteryStatusRequest_CALLBACK NULL
#define alox_BatteryStatusRequest_DEFAULT NULL
#define alox_LipoReading_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, valid, 1) \
X(a, STATIC, SINGULAR, UINT32, voltage_mv, 2)
#define alox_LipoReading_CALLBACK NULL
#define alox_LipoReading_DEFAULT NULL
#define alox_BatterySample_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, OPTIONAL, MESSAGE, lipo1, 2) \
X(a, STATIC, OPTIONAL, MESSAGE, lipo2, 3) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 4)
#define alox_BatterySample_CALLBACK NULL
#define alox_BatterySample_DEFAULT NULL
#define alox_BatterySample_lipo1_MSGTYPE alox_LipoReading
#define alox_BatterySample_lipo2_MSGTYPE alox_LipoReading
#define alox_BatteryStatusResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, success, 1) \
X(a, STATIC, REPEATED, MESSAGE, samples, 2)
#define alox_BatteryStatusResponse_CALLBACK NULL
#define alox_BatteryStatusResponse_DEFAULT NULL
#define alox_BatteryStatusResponse_samples_MSGTYPE alox_BatterySample
#define alox_AccelSample_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, valid, 2) \
X(a, STATIC, SINGULAR, SINT32, x, 3) \
X(a, STATIC, SINGULAR, SINT32, y, 4) \
X(a, STATIC, SINGULAR, SINT32, z, 5) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 6)
#define alox_AccelSample_CALLBACK NULL
#define alox_AccelSample_DEFAULT NULL
#define alox_TapNotifyRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, write, 1) \
X(a, STATIC, SINGULAR, UINT32, client_id, 2) \
X(a, STATIC, SINGULAR, BOOL, all_clients, 3) \
X(a, STATIC, SINGULAR, BOOL, single, 4) \
X(a, STATIC, SINGULAR, BOOL, double_tap, 5) \
X(a, STATIC, SINGULAR, BOOL, triple, 6)
#define alox_TapNotifyRequest_CALLBACK NULL
#define alox_TapNotifyRequest_DEFAULT NULL
#define alox_TapNotifyResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, success, 2) \
X(a, STATIC, SINGULAR, UINT32, slaves_updated, 3) \
X(a, STATIC, SINGULAR, BOOL, single, 4) \
X(a, STATIC, SINGULAR, BOOL, double_tap, 5) \
X(a, STATIC, SINGULAR, BOOL, triple, 6)
#define alox_TapNotifyResponse_CALLBACK NULL
#define alox_TapNotifyResponse_DEFAULT NULL
#define alox_TapEvent_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, valid, 2) \
X(a, STATIC, SINGULAR, UENUM, kind, 3) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 4)
#define alox_TapEvent_CALLBACK NULL
#define alox_TapEvent_DEFAULT NULL
#define alox_CacheStatusRequest_FIELDLIST(X, a) \
#define alox_CacheStatusRequest_CALLBACK NULL
#define alox_CacheStatusRequest_DEFAULT NULL
#define alox_CacheClientAccel_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, valid, 1) \
X(a, STATIC, SINGULAR, SINT32, x, 2) \
X(a, STATIC, SINGULAR, SINT32, y, 3) \
X(a, STATIC, SINGULAR, SINT32, z, 4) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 5)
#define alox_CacheClientAccel_CALLBACK NULL
#define alox_CacheClientAccel_DEFAULT NULL
#define alox_CacheClientTap_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UENUM, kind, 1) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 2)
#define alox_CacheClientTap_CALLBACK NULL
#define alox_CacheClientTap_DEFAULT NULL
#define alox_CacheClientStatus_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, OPTIONAL, MESSAGE, accel, 2) \
X(a, STATIC, OPTIONAL, MESSAGE, tap, 3)
#define alox_CacheClientStatus_CALLBACK NULL
#define alox_CacheClientStatus_DEFAULT NULL
#define alox_CacheClientStatus_accel_MSGTYPE alox_CacheClientAccel
#define alox_CacheClientStatus_tap_MSGTYPE alox_CacheClientTap
#define alox_CacheStatusResponse_FIELDLIST(X, a) \
X(a, STATIC, REPEATED, MESSAGE, clients, 1)
#define alox_CacheStatusResponse_CALLBACK NULL
#define alox_CacheStatusResponse_DEFAULT NULL
#define alox_CacheStatusResponse_clients_MSGTYPE alox_CacheClientStatus
#define alox_EspNowUnicastTestRequest_FIELDLIST(X, a) \ #define alox_EspNowUnicastTestRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \ X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, UINT32, seq, 2) X(a, STATIC, SINGULAR, UINT32, seq, 2)
@ -533,7 +943,10 @@ X(a, STATIC, SINGULAR, UINT32, g, 5) \
X(a, STATIC, SINGULAR, UINT32, b, 6) \ X(a, STATIC, SINGULAR, UINT32, b, 6) \
X(a, STATIC, SINGULAR, UINT32, intensity, 7) \ X(a, STATIC, SINGULAR, UINT32, intensity, 7) \
X(a, STATIC, SINGULAR, UINT32, blink_ms, 8) \ X(a, STATIC, SINGULAR, UINT32, blink_ms, 8) \
X(a, STATIC, SINGULAR, UINT32, blink_count, 9) X(a, STATIC, SINGULAR, UINT32, blink_count, 9) \
X(a, STATIC, SINGULAR, UINT32, client_id, 10) \
X(a, STATIC, SINGULAR, BOOL, all_clients, 11) \
X(a, STATIC, SINGULAR, BOOL, slaves_only, 12)
#define alox_LedRingProgressRequest_CALLBACK NULL #define alox_LedRingProgressRequest_CALLBACK NULL
#define alox_LedRingProgressRequest_DEFAULT NULL #define alox_LedRingProgressRequest_DEFAULT NULL
@ -541,7 +954,9 @@ X(a, STATIC, SINGULAR, UINT32, blink_count, 9)
X(a, STATIC, SINGULAR, BOOL, success, 1) \ X(a, STATIC, SINGULAR, BOOL, success, 1) \
X(a, STATIC, SINGULAR, UINT32, mode, 2) \ X(a, STATIC, SINGULAR, UINT32, mode, 2) \
X(a, STATIC, SINGULAR, UINT32, progress, 3) \ X(a, STATIC, SINGULAR, UINT32, progress, 3) \
X(a, STATIC, SINGULAR, UINT32, digit, 4) X(a, STATIC, SINGULAR, UINT32, digit, 4) \
X(a, STATIC, SINGULAR, UINT32, client_id, 5) \
X(a, STATIC, SINGULAR, UINT32, slaves_updated, 6)
#define alox_LedRingProgressResponse_CALLBACK NULL #define alox_LedRingProgressResponse_CALLBACK NULL
#define alox_LedRingProgressResponse_DEFAULT NULL #define alox_LedRingProgressResponse_DEFAULT NULL
@ -625,6 +1040,21 @@ extern const pb_msgdesc_t alox_ClientInput_msg;
extern const pb_msgdesc_t alox_ClientInputResponse_msg; extern const pb_msgdesc_t alox_ClientInputResponse_msg;
extern const pb_msgdesc_t alox_AccelDeadzoneRequest_msg; extern const pb_msgdesc_t alox_AccelDeadzoneRequest_msg;
extern const pb_msgdesc_t alox_AccelDeadzoneResponse_msg; extern const pb_msgdesc_t alox_AccelDeadzoneResponse_msg;
extern const pb_msgdesc_t alox_AccelStreamRequest_msg;
extern const pb_msgdesc_t alox_AccelStreamResponse_msg;
extern const pb_msgdesc_t alox_BatteryStatusRequest_msg;
extern const pb_msgdesc_t alox_LipoReading_msg;
extern const pb_msgdesc_t alox_BatterySample_msg;
extern const pb_msgdesc_t alox_BatteryStatusResponse_msg;
extern const pb_msgdesc_t alox_AccelSample_msg;
extern const pb_msgdesc_t alox_TapNotifyRequest_msg;
extern const pb_msgdesc_t alox_TapNotifyResponse_msg;
extern const pb_msgdesc_t alox_TapEvent_msg;
extern const pb_msgdesc_t alox_CacheStatusRequest_msg;
extern const pb_msgdesc_t alox_CacheClientAccel_msg;
extern const pb_msgdesc_t alox_CacheClientTap_msg;
extern const pb_msgdesc_t alox_CacheClientStatus_msg;
extern const pb_msgdesc_t alox_CacheStatusResponse_msg;
extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg; extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg;
extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg; extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg;
extern const pb_msgdesc_t alox_LedRingProgressRequest_msg; extern const pb_msgdesc_t alox_LedRingProgressRequest_msg;
@ -652,6 +1082,21 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
#define alox_ClientInputResponse_fields &alox_ClientInputResponse_msg #define alox_ClientInputResponse_fields &alox_ClientInputResponse_msg
#define alox_AccelDeadzoneRequest_fields &alox_AccelDeadzoneRequest_msg #define alox_AccelDeadzoneRequest_fields &alox_AccelDeadzoneRequest_msg
#define alox_AccelDeadzoneResponse_fields &alox_AccelDeadzoneResponse_msg #define alox_AccelDeadzoneResponse_fields &alox_AccelDeadzoneResponse_msg
#define alox_AccelStreamRequest_fields &alox_AccelStreamRequest_msg
#define alox_AccelStreamResponse_fields &alox_AccelStreamResponse_msg
#define alox_BatteryStatusRequest_fields &alox_BatteryStatusRequest_msg
#define alox_LipoReading_fields &alox_LipoReading_msg
#define alox_BatterySample_fields &alox_BatterySample_msg
#define alox_BatteryStatusResponse_fields &alox_BatteryStatusResponse_msg
#define alox_AccelSample_fields &alox_AccelSample_msg
#define alox_TapNotifyRequest_fields &alox_TapNotifyRequest_msg
#define alox_TapNotifyResponse_fields &alox_TapNotifyResponse_msg
#define alox_TapEvent_fields &alox_TapEvent_msg
#define alox_CacheStatusRequest_fields &alox_CacheStatusRequest_msg
#define alox_CacheClientAccel_fields &alox_CacheClientAccel_msg
#define alox_CacheClientTap_fields &alox_CacheClientTap_msg
#define alox_CacheClientStatus_fields &alox_CacheClientStatus_msg
#define alox_CacheStatusResponse_fields &alox_CacheStatusResponse_msg
#define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg #define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg
#define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg #define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg
#define alox_LedRingProgressRequest_fields &alox_LedRingProgressRequest_msg #define alox_LedRingProgressRequest_fields &alox_LedRingProgressRequest_msg
@ -675,17 +1120,29 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
/* alox_ClientInfo_size depends on runtime parameters */ /* alox_ClientInfo_size depends on runtime parameters */
/* alox_ClientInfoResponse_size depends on runtime parameters */ /* alox_ClientInfoResponse_size depends on runtime parameters */
/* alox_ClientInputResponse_size depends on runtime parameters */ /* alox_ClientInputResponse_size depends on runtime parameters */
#define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_OtaSlaveProgressResponse_size #define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_CacheStatusResponse_size
#define alox_AccelDeadzoneRequest_size 16 #define alox_AccelDeadzoneRequest_size 16
#define alox_AccelDeadzoneResponse_size 20 #define alox_AccelDeadzoneResponse_size 20
#define alox_AccelSample_size 32
#define alox_AccelStreamRequest_size 12
#define alox_AccelStreamResponse_size 16
#define alox_Ack_size 0 #define alox_Ack_size 0
#define alox_BatterySample_size 32
#define alox_BatteryStatusRequest_size 8
#define alox_BatteryStatusResponse_size 580
#define alox_CacheClientAccel_size 26
#define alox_CacheClientStatus_size 44
#define alox_CacheClientTap_size 8
#define alox_CacheStatusRequest_size 0
#define alox_CacheStatusResponse_size 736
#define alox_ClientInput_size 22 #define alox_ClientInput_size 22
#define alox_EspNowFindMeRequest_size 6 #define alox_EspNowFindMeRequest_size 6
#define alox_EspNowFindMeResponse_size 8 #define alox_EspNowFindMeResponse_size 8
#define alox_EspNowUnicastTestRequest_size 12 #define alox_EspNowUnicastTestRequest_size 12
#define alox_EspNowUnicastTestResponse_size 8 #define alox_EspNowUnicastTestResponse_size 8
#define alox_LedRingProgressRequest_size 54 #define alox_LedRingProgressRequest_size 64
#define alox_LedRingProgressResponse_size 20 #define alox_LedRingProgressResponse_size 32
#define alox_LipoReading_size 8
#define alox_OtaEndPayload_size 0 #define alox_OtaEndPayload_size 0
#define alox_OtaPayload_size 209 #define alox_OtaPayload_size 209
#define alox_OtaSlaveProgressEntry_size 30 #define alox_OtaSlaveProgressEntry_size 30
@ -695,6 +1152,9 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
#define alox_OtaStatusPayload_size 24 #define alox_OtaStatusPayload_size 24
#define alox_RestartRequest_size 6 #define alox_RestartRequest_size 6
#define alox_RestartResponse_size 8 #define alox_RestartResponse_size 8
#define alox_TapEvent_size 16
#define alox_TapNotifyRequest_size 16
#define alox_TapNotifyResponse_size 20
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */

View File

@ -22,6 +22,13 @@ enum MessageType {
OTA_SLAVE_PROGRESS = 21; OTA_SLAVE_PROGRESS = 21;
FIND_ME = 22; FIND_ME = 22;
RESTART = 23; RESTART = 23;
reserved 24;
ACCEL_STREAM = 25;
BATTERY_STATUS = 26;
TAP_NOTIFY = 27;
reserved 28;
/** Combined cached accel + tap poll (one UART round-trip, ~16 ms cadence). */
CACHE_STATUS = 29;
} }
message UartMessage { message UartMessage {
@ -48,6 +55,14 @@ message UartMessage {
EspNowFindMeResponse espnow_find_me_response = 20; EspNowFindMeResponse espnow_find_me_response = 20;
RestartRequest restart_request = 21; RestartRequest restart_request = 21;
RestartResponse restart_response = 22; RestartResponse restart_response = 22;
AccelStreamRequest accel_stream_request = 25;
AccelStreamResponse accel_stream_response = 26;
BatteryStatusRequest battery_status_request = 27;
BatteryStatusResponse battery_status_response = 28;
TapNotifyRequest tap_notify_request = 29;
TapNotifyResponse tap_notify_response = 30;
CacheStatusRequest cache_status_request = 33;
CacheStatusResponse cache_status_response = 34;
} }
} }
@ -72,6 +87,12 @@ message ClientInfo {
uint32 last_ping = 5; uint32 last_ping = 5;
uint32 last_success_ping = 6; uint32 last_success_ping = 6;
uint32 version = 7; uint32 version = 7;
/** Master: ESP-NOW accel stream enabled for this slave. */
bool accel_stream_enabled = 8;
/** Master: ESP-NOW tap notify flags for this slave. */
bool tap_notify_single = 9;
bool tap_notify_double = 10;
bool tap_notify_triple = 11;
} }
message ClientInfoResponse { message ClientInfoResponse {
@ -106,6 +127,124 @@ message AccelDeadzoneResponse {
uint32 slaves_updated = 4; uint32 slaves_updated = 4;
} }
// Host master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
// write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients).
message AccelStreamRequest {
bool write = 1;
bool enable = 2;
uint32 client_id = 3;
bool all_clients = 4;
}
message AccelStreamResponse {
bool enabled = 1;
uint32 client_id = 2;
bool success = 3;
uint32 slaves_updated = 4;
}
/** Host → master: read LiPo ADC voltages (master local and/or slaves via ESP-NOW). */
message BatteryStatusRequest {
/** 0 = master only; >0 = one slave; ignored when all_clients */
uint32 client_id = 1;
/** Master (client_id 0) plus every registered slave */
bool all_clients = 2;
}
message LipoReading {
bool valid = 1;
/** Estimated pack voltage in millivolts from ADC */
uint32 voltage_mv = 2;
}
message BatterySample {
uint32 client_id = 1;
LipoReading lipo1 = 2;
LipoReading lipo2 = 3;
/** Milliseconds since last ESP-NOW battery report from this pod. */
uint32 age_ms = 4;
}
message BatteryStatusResponse {
bool success = 1;
repeated BatterySample samples = 2 [(nanopb).max_count = 17];
}
/** Legacy host-side sample shape (dashboard helpers); use CACHE_STATUS on the wire. */
message AccelSample {
uint32 client_id = 1;
bool valid = 2;
sint32 x = 3;
sint32 y = 4;
sint32 z = 5;
/** Milliseconds since last ESP-NOW sample from this slave. */
uint32 age_ms = 6;
}
/** Host → master: enable/disable tap ESP-NOW notify per slave (single/double/triple). */
message TapNotifyRequest {
bool write = 1;
uint32 client_id = 2;
bool all_clients = 3;
bool single = 4;
bool double_tap = 5;
bool triple = 6;
}
message TapNotifyResponse {
uint32 client_id = 1;
bool success = 2;
uint32 slaves_updated = 3;
bool single = 4;
bool double_tap = 5;
bool triple = 6;
}
enum TapKind {
TAP_NONE = 0;
TAP_SINGLE = 1;
TAP_DOUBLE = 2;
TAP_TRIPLE = 3;
}
/** Legacy tap event shape (dashboard helpers); use CACHE_STATUS on the wire. */
message TapEvent {
uint32 client_id = 1;
bool valid = 2;
TapKind kind = 3;
uint32 age_ms = 4;
}
/** Host → master: one-shot read of subscribed cached slave data (no request body). */
message CacheStatusRequest {}
/** Accel slice inside CACHE_STATUS (no client_id — use parent CacheClientStatus). */
message CacheClientAccel {
bool valid = 1;
sint32 x = 2;
sint32 y = 3;
sint32 z = 4;
uint32 age_ms = 5;
}
/** Tap slice inside CACHE_STATUS; only present when a pending tap was consumed. */
message CacheClientTap {
TapKind kind = 1;
uint32 age_ms = 2;
}
/** One slave with accel and/or tap notify enabled; only subscribed fields are set. */
message CacheClientStatus {
uint32 client_id = 1;
CacheClientAccel accel = 2;
CacheClientTap tap = 3;
}
message CacheStatusResponse {
/** Slaves with accel_stream and/or tap notify; omitted fields are not subscribed. */
repeated CacheClientStatus clients = 1 [(nanopb).max_count = 16];
}
message EspNowUnicastTestRequest { message EspNowUnicastTestRequest {
uint32 client_id = 1; uint32 client_id = 1;
uint32 seq = 2; uint32 seq = 2;
@ -116,8 +255,8 @@ message EspNowUnicastTestResponse {
uint32 seq = 2; uint32 seq = 2;
} }
// Host device: LED ring display (progress bar, digit, clear, blink, or find-me). // Host master: LED ring on master (client_id=0) and/or slaves via ESP-NOW.
// mode: 0=clear, 1=progress (0100 %), 2=digit (010), 3=blink full ring, 4=find-me (R/G/B ×3 @ full brightness). // mode: 0=clear, 1=progress (0100 %), 2=digit (010), 3=blink, 4=find-me, 5=all LEDs solid color.
message LedRingProgressRequest { message LedRingProgressRequest {
uint32 mode = 1; uint32 mode = 1;
/** 0100: fraction of ring LEDs to light (mode=progress) */ /** 0100: fraction of ring LEDs to light (mode=progress) */
@ -133,6 +272,12 @@ message LedRingProgressRequest {
uint32 blink_ms = 8; uint32 blink_ms = 8;
/** Number of pulses (mode=blink, default 1) */ /** Number of pulses (mode=blink, default 1) */
uint32 blink_count = 9; uint32 blink_count = 9;
/** 0 = master ring only; >0 = one slave; ignored when all_clients */
uint32 client_id = 10;
/** Broadcast to all registered slaves (and optionally master unless slaves_only) */
bool all_clients = 11;
/** With all_clients: do not change master ring */
bool slaves_only = 12;
} }
message LedRingProgressResponse { message LedRingProgressResponse {
@ -140,6 +285,8 @@ message LedRingProgressResponse {
uint32 mode = 2; uint32 mode = 2;
uint32 progress = 3; uint32 progress = 3;
uint32 digit = 4; uint32 digit = 4;
uint32 client_id = 5;
uint32 slaves_updated = 6;
} }
/** Host → master: find-me on local ring (client_id=0) or ESP-NOW unicast to one slave. */ /** Host → master: find-me on local ring (client_id=0) or ESP-NOW unicast to one slave. */

View File

@ -9,8 +9,12 @@
#define UART_NUM UART_NUM_1 #define UART_NUM UART_NUM_1
#define UART_BAUD_RATE 921600 #define UART_BAUD_RATE 921600
#define UART_TXD_PIN 3 // #define UART_TXD_PIN 3
#define UART_RXD_PIN 2 // #define UART_RXD_PIN 2
#define UART_TXD_PIN 2
#define UART_RXD_PIN 3
#define UART_BUF_SIZE 2048 #define UART_BUF_SIZE 2048
#define START_MARKER 0xAA #define START_MARKER 0xAA