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>
This commit is contained in:
simon 2026-05-29 19:11:36 +02:00
parent ba20544762
commit 47c75110c9
35 changed files with 2409 additions and 300 deletions

View File

@ -25,7 +25,7 @@ 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`) | | `deadzone` | `0x06` | Get/set accelerometer deadzone LSB (`-set`, `-value`, `-client`, `-all`) |
| `accel` | `0x18` | Read current BMA456 XYZ (raw LSB, ±2g); alias `accel-read` | | `accel` | `0x18` | Cached slave accel snapshot from master (`ACCEL_SNAPSHOT`); alias `accel-read` |
| `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) |
@ -62,11 +62,91 @@ 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`.
### External API (second HTTP server)
`serve` starts a separate listener (default **`:8081`**, disable with `-api-addr ""`) for external programs. It shares the same UART connection as the dashboard.
| Endpoint | Description |
|----------|-------------|
| `GET /` or `GET /api/v1/` | JSON service info (`default_interval_ms`, min/max, `serial_port`) |
| `WebSocket /ws` | Per-connection accel receive + interval; slave ESP-NOW stream control |
Two layers:
1. **`set_stream`** — this WebSocket connection: whether to receive `accel` JSON and at what poll rate (1 ms … 10 s per client; server UART poll uses the minimum among active subscribers).
2. **`set_accel_stream`** — firmware: whether a slave sends accel to the master over ESP-NOW (16 ms on the pod).
Polling runs only when at least one connection has `receive_accel: true` **and** at least one slave streams (via `set_accel_stream` or dashboard `:8080`).
**Hello** (on connect; accel is off until `set_stream`):
```json
{"type":"hello","serial_port":"/dev/ttyUSB0","interval_ms":16,"commands":["set_stream","get_stream","set_accel_stream","get_accel_stream"]}
```
**Receive accel on this connection** (optional `interval_ms`, default from `-accel-interval`):
```json
{"type":"set_stream","enable":true,"interval_ms":32}
{"type":"get_stream"}
```
Reply:
```json
{"type":"stream_status","receive_accel":true,"interval_ms":32,"success":true}
```
**Slave ESP-NOW stream** (per `client_id`):
```json
{"type":"set_accel_stream","client_id":16,"enable":true}
{"type":"get_accel_stream","client_id":16}
```
Reply:
```json
{"type":"accel_stream_status","client_id":16,"enabled":true,"success":true}
```
**Accel** (only to connections with `receive_accel: true`, and only while slaves stream):
```json
{"type":"accel","t":1716900123456789012,"success":true,"clients":[{"client_id":16,"valid":true,"x":12,"y":-34,"z":16384,"age_ms":8}]}
```
`t` is Unix time in nanoseconds. Each `clients[]` entry is one slave's latest cached sample (raw LSB, ±2g).
Example (Python):
```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_stream", "enable": True, "interval_ms": 16}))
print(await ws.recv()) # stream_status
await ws.send(json.dumps({"type": "set_accel_stream", "client_id": 16, "enable": True}))
print(await ws.recv()) # accel_stream_status
while True:
msg = json.loads(await ws.recv())
if msg.get("type") != "accel" or not msg.get("success"):
continue
for c in msg.get("clients", []):
if c.get("valid"):
print(c["client_id"], c["x"], c["y"], c["z"])
asyncio.run(main())
```
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: The dashboard can configure nodes using the same UART commands as the CLI:
@ -78,7 +158,21 @@ The dashboard can configure nodes using the same UART commands as the CLI:
| Alle Slaves | per-slave ESP-NOW (Master bleibt unverändert; CLI `-all` setzt auch den Master) | | Alle Slaves | per-slave ESP-NOW (Master bleibt unverändert; CLI `-all` setzt auch den Master) |
| Unicast test | `unicast-test -client ID` | | 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). HTTP API (used by the web UI): `GET/POST /api/deadzone`, `GET/PUT /api/clients/{id}/accel-stream`, `POST /api/accel-stream` (legacy / `all_clients`), `POST /api/unicast-test`, `POST /api/find-me`, `POST /api/restart`, `POST /api/ota` (multipart field `firmware`, max 2 MiB).
**Accel stream per slave** (must be enabled before values appear; goTool polls only while at least one slave has stream on):
```http
GET /api/clients/16/accel-stream
→ {"enabled":false,"client_id":16,"success":true}
PUT /api/clients/16/accel-stream
Content-Type: application/json
{"enable": true}
→ {"enabled":true,"client_id":16,"success":true}
```
Enable all slaves: `POST /api/accel-stream` with `{"write":true,"enable":true,"all_clients":true}`.
| UI / API | Behaviour | | UI / API | Behaviour |
|----------|-----------| |----------|-----------|

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
}

View File

@ -67,7 +67,8 @@ 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) {
mountAccelStreamAPI(mux, link, hub, streamCtl)
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:

476
goTool/api_stream.go Normal file
View File

@ -0,0 +1,476 @@
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
)
// 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.
type AccelStreamMessage struct {
Type string `json:"type"` // "hello" | "accel"
Serial string `json:"serial_port,omitempty"`
IntervalMs int `json:"interval_ms,omitempty"`
Commands []string `json:"commands,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"`
}
type accelWSCommand struct {
Type string `json:"type"`
ClientID uint32 `json:"client_id"`
Enable *bool `json:"enable"`
IntervalMs *int `json:"interval_ms"`
}
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"`
Description string `json:"description"`
}
type wsSubscriber struct {
conn *websocket.Conn
receiveAccel bool
interval time.Duration
lastSent time.Time
}
type accelStreamHub struct {
mu sync.RWMutex
clients map[*websocket.Conn]*wsSubscriber
defaultInterval time.Duration
configChanged chan struct{}
}
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),
Commands: []string{"set_stream", "get_stream", "set_accel_stream", "get_accel_stream"},
}
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)
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) minWantedInterval() time.Duration {
h.mu.RLock()
defer h.mu.RUnlock()
var min time.Duration
for _, sub := range h.clients {
if !sub.receiveAccel {
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) 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.lastSent.IsZero() && now.Sub(sub.lastSent) < sub.interval {
continue
}
sub.lastSent = 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, 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:
if !hub.anyWantsAccel() {
continue
}
if !accelStreamPollingActive(dash, ctl) {
continue
}
now := time.Now().UnixNano()
resp, err := link.readAccelSnapshotPoll(0)
if errors.Is(err, errUARTBusy) {
hub.deliver(AccelStreamMessage{
Type: "accel",
T: now,
Success: false,
Error: "uart busy",
})
continue
}
if err != nil {
hub.deliver(AccelStreamMessage{
Type: "accel",
T: now,
Success: false,
Error: err.Error(),
})
continue
}
clients := make([]AccelClientSample, 0, len(resp.GetSamples()))
for _, s := range resp.GetSamples() {
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,
})
}
}
}
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 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 handleAccelWSCommand(conn *websocket.Conn, sub *wsSubscriber, data []byte, link *managedSerial, dash *wsHub, ctl *accelStreamCtl, 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 "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(),
})
default:
writeStreamStatus(conn, StreamStatusMessage{
Type: "stream_status",
Error: "unknown type (set_stream, get_stream, set_accel_stream, get_accel_stream)",
})
}
}
func serveExternalWS(conn *websocket.Conn, link *managedSerial, dash *wsHub, ctl *accelStreamCtl, 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, hub)
}
}
func mountExternalAPI(mux *http.ServeMux, portName string, defaultInterval time.Duration, hub *accelStreamHub, link *managedSerial, dash *wsHub, ctl *accelStreamCtl) {
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),
Description: "WebSocket: per-connection accel receive + interval; slave stream via set_accel_stream",
})
})
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, portName, hub)
})
}
func runAPIServer(portName string, link *managedSerial, addr string, defaultInterval time.Duration, dash *wsHub, ctl *accelStreamCtl, stop <-chan struct{}) *http.Server {
hub := newAccelStreamHub(defaultInterval)
go runAccelStreamer(link, hub, dash, ctl, stop)
mux := http.NewServeMux()
mountExternalAPI(mux, portName, defaultInterval, hub, link, dash, ctl)
srv := &http.Server{Addr: addr, Handler: mux}
go func() {
log.Printf("external API http://localhost%s WebSocket ws://localhost%s/ws (default accel interval %s, per-client via set_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)
}

View File

@ -40,6 +40,70 @@ func (m *managedSerial) listClientsPoll() ([]*pb.ClientInfo, error) {
return decodeClientsPayload(payload) return decodeClientsPayload(payload)
} }
func (m *managedSerial) readAccelSnapshotPoll(clientID uint32) (*pb.AccelSnapshotResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_ACCEL_SNAPSHOT,
Payload: &pb.UartMessage_AccelSnapshotRequest{
AccelSnapshotRequest: &pb.AccelSnapshotRequest{ClientId: clientID},
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_ACCEL_SNAPSHOT)}, body...)
respPayload, err := m.exchangePayloadPoll(payload, "ACCEL_SNAPSHOT")
if err != nil {
return nil, err
}
return decodeAccelSnapshotPayload(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) 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)
} }
@ -101,25 +165,43 @@ func decodeClientsPayload(payload []byte) ([]*pb.ClientInfo, error) {
return info.GetClients(), nil return info.GetClients(), nil
} }
func (s *serialPort) readAccel() (*pb.AccelReadResponse, error) { func decodeAccelSnapshotPayload(payload []byte) (*pb.AccelSnapshotResponse, error) {
payload, err := s.exchange(byte(pb.MessageType_ACCEL_READ), "ACCEL_READ") if len(payload) < 1 {
if err != nil { return nil, fmt.Errorf("empty response payload")
return nil, err
} }
var msg pb.UartMessage var msg pb.UartMessage
if err := proto.Unmarshal(payload[1:], &msg); err != nil { if err := proto.Unmarshal(payload[1:], &msg); err != nil {
return nil, fmt.Errorf("decode: %w", err) return nil, fmt.Errorf("decode: %w", err)
} }
if msg.GetType() != pb.MessageType_ACCEL_READ { if msg.GetType() != pb.MessageType_ACCEL_SNAPSHOT {
return nil, fmt.Errorf("unexpected type %v", msg.GetType()) return nil, fmt.Errorf("unexpected type %v", msg.GetType())
} }
r := msg.GetAccelReadResponse() r := msg.GetAccelSnapshotResponse()
if r == nil { if r == nil {
return nil, fmt.Errorf("missing accel_read_response") return nil, fmt.Errorf("missing accel_snapshot_response")
} }
return r, nil return r, nil
} }
func (s *serialPort) readAccelSnapshot(clientID uint32) (*pb.AccelSnapshotResponse, error) {
msg := &pb.UartMessage{
Type: pb.MessageType_ACCEL_SNAPSHOT,
Payload: &pb.UartMessage_AccelSnapshotRequest{
AccelSnapshotRequest: &pb.AccelSnapshotRequest{ClientId: clientID},
},
}
body, err := proto.Marshal(msg)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
payload := append([]byte{byte(pb.MessageType_ACCEL_SNAPSHOT)}, body...)
respPayload, err := s.exchangePayload(payload, "ACCEL_SNAPSHOT")
if err != nil {
return nil, err
}
return decodeAccelSnapshotPayload(respPayload)
}
func (s *serialPort) getVersion() (*pb.VersionResponse, error) { func (s *serialPort) getVersion() (*pb.VersionResponse, error) {
payload, err := s.exchange(byte(pb.MessageType_VERSION), "VERSION") payload, err := s.exchange(byte(pb.MessageType_VERSION), "VERSION")
if err != nil { if err != nil {
@ -136,6 +218,33 @@ 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) 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,
@ -234,6 +343,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)
} }

View File

@ -5,13 +5,26 @@ import (
) )
func runAccel(sp *serialPort) error { func runAccel(sp *serialPort) error {
r, err := sp.readAccel() return runAccelSnapshot(sp, 0)
}
func runAccelSnapshot(sp *serialPort, clientID uint32) error {
r, err := sp.readAccelSnapshot(clientID)
if err != nil { if err != nil {
return err return err
} }
if !r.GetSuccess() { samples := r.GetSamples()
return fmt.Errorf("accel read failed (sensor not ready?)") if len(samples) == 0 {
} fmt.Println("no accel samples (no slaves or no ESP-NOW stream yet)")
fmt.Printf("accel: x=%d y=%d z=%d (raw LSB, ±2g)\n", r.GetX(), r.GetY(), r.GetZ()) return nil
}
for _, s := range samples {
if !s.GetValid() {
fmt.Printf("client %d: no sample yet\n", s.GetClientId())
continue
}
fmt.Printf("client %d: x=%d y=%d z=%d (age %d ms, raw LSB ±2g)\n",
s.GetClientId(), s.GetX(), s.GetY(), s.GetZ(), s.GetAgeMs())
}
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,20 @@ func runServe(portName string, baud int, args []string) error {
defer link.Close() defer link.Close()
hub := newWSHub() hub := newWSHub()
streamCtl := newAccelStreamCtl()
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, *interval, stop)
go runAccelDashboardPoller(link, hub, *accelInterval, stop)
var apiSrv *http.Server
if *apiAddr != "" {
apiSrv = runAPIServer(portName, link, *apiAddr, *accelInterval, hub, streamCtl, stop)
defer shutdownAPIServer(apiSrv)
}
mux := http.NewServeMux() mux := http.NewServeMux()
mountServeAPI(mux, link, hub) mountServeAPI(mux, link, hub, streamCtl)
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 +74,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, accel %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)
} }

View File

@ -32,6 +32,12 @@ 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"`
} }
type DashboardState struct { type DashboardState struct {
@ -56,6 +62,8 @@ func newWSHub() *wsHub {
func (h *wsHub) setState(st DashboardState) { func (h *wsHub) setState(st DashboardState) {
h.mu.Lock() h.mu.Lock()
prev := h.state.Clients
st.Clients = preserveClientAccel(st.Clients, prev)
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 +97,136 @@ 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) []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.AccelStream {
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 anyClientAccelStream(clients []ClientView) bool {
for _, c := range clients {
if c.AccelStream {
return true
}
}
return false
}
// 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)
}
// mergeAccel updates cached accel on clients and pushes state to dashboard WebSockets.
func (h *wsHub) mergeAccel(samples []*pb.AccelSample) {
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) 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 +244,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) DashboardState {
st := DashboardState{ st := DashboardState{
UpdatedAt: time.Now().Format(time.RFC3339), UpdatedAt: time.Now().Format(time.RFC3339),
SerialPort: portName, SerialPort: portName,
@ -152,15 +290,63 @@ 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 {
cv.Deadzone = dz
} }
st.Clients = append(st.Clients, cv) st.Clients = append(st.Clients, cv)
} }
if anyClientAccelStream(st.Clients) {
for i := range st.Clients {
if !st.Clients[i].AccelStream {
continue
}
if dz, err := readDeadzonePoll(link, st.Clients[i].ID); err == nil {
st.Clients[i].Deadzone = dz
}
}
if snap, err := link.readAccelSnapshotPoll(0); err == nil {
st.Clients = applyAccelSamples(st.Clients, snap.GetSamples())
}
} else {
for i, c := range clients {
if dz, err := readDeadzonePoll(link, c.GetId()); err == nil {
st.Clients[i].Deadzone = dz
}
}
}
if streamCtl != nil {
streamCtl.SyncFromClients(st.Clients)
}
return st return st
} }
func runAccelDashboardPoller(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 || !hub.anyAccelStreamEnabled() {
continue
}
snap, err := link.readAccelSnapshotPoll(0)
if err != nil {
continue
}
hub.mergeAccel(snap.GetSamples())
}
}
}
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,14 +394,15 @@ 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, interval time.Duration, stop <-chan struct{}) {
// streamCtl kept for external API; dashboard uses hub.state AccelStream 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)
if st.UARTConnected && st.SerialOK { if st.UARTConnected && st.SerialOK {
lastGood = st lastGood = st
} }

View File

@ -16,7 +16,7 @@ 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, " accel read current accelerometer XYZ (raw LSB)\n") fmt.Fprintf(os.Stderr, " accel read cached slave accel snapshot from master\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")

File diff suppressed because it is too large Load Diff

View File

@ -55,11 +55,12 @@
.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); }
.pp-table { .pp-table {
--bs-table-color: var(--pp-text); --bs-table-color: var(--pp-text);
@ -265,7 +266,9 @@
<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">
Accel-Stream pro Slave per „Stream an“ aktivieren (~16&nbsp;ms ESP-NOW). Ohne Aktivierung keine Werte.
</p>
<div class="card-body p-0 pt-2"> <div class="card-body p-0 pt-2">
<div class="table-responsive"> <div class="table-responsive">
<table class="table pp-table table-hover"> <table class="table pp-table table-hover">
@ -276,12 +279,14 @@
<th>Ver</th> <th>Ver</th>
<th>Status</th> <th>Status</th>
<th>Deadzone</th> <th>Deadzone</th>
<th>Accel (LSB)</th>
<th>Stream</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="8" 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 +299,20 @@
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>
<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'"></button>
</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"
@ -496,6 +515,22 @@
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(':');
}, },
formatAccel(c) {
if (!c?.accel_stream) return '—';
if (!c?.accel_valid) return '…';
return `${c.accel_x} / ${c.accel_y} / ${c.accel_z}`;
},
accelTitle(c) {
if (!c?.accel_stream) return 'Accel-Stream nicht aktiviert';
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 '';
},
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 +767,44 @@
async setMasterDeadzone() { async setMasterDeadzone() {
await this.setDeadzone(0, this.masterDz); await this.setDeadzone(0, this.masterDz);
}, },
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: 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;
}
},
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);

View File

@ -18,7 +18,8 @@ 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_read.c" "cmd/cmd_accel_snapshot.c"
"cmd/cmd_accel_stream.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"

View File

@ -115,6 +115,7 @@ 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_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` |
@ -217,7 +218,7 @@ 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 |
| 24 | `ACCEL_READ` | Implemented (`cmd/cmd_accel_read.c`) — on-demand BMA456 XYZ (raw LSB) | | 24 | `ACCEL_SNAPSHOT` | Implemented (`cmd/cmd_accel_snapshot.c`) — cached slave accel from ESP-NOW stream |
Regenerate C code: Regenerate C code:
@ -311,20 +312,20 @@ 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).
### ACCEL_READ command ### ACCEL_SNAPSHOT command
Read the **current** BMA456 accelerometer sample on this node (master or slave with sensor). Values are raw LSB in the configured **±2g** range; they are **not** filtered by the software deadzone (unlike periodic `ACC X=…` logs in `bosch456.c`). Read **cached** accelerometer samples on the **master** (one entry per registered slave). Slaves send `ESPNOW_ACCEL_SAMPLE` to the master every **16 ms** (`esp_now_comm.c`); the master stores the latest value per client in `client_registry.c`.
**Request:** framed `18` (`0x18`) only, or `18` + empty `accel_read_request`. **Request:** framed `18` (`0x18`) + optional `accel_snapshot_request` (`client_id`: `0` = all slaves, `>0` = one id).
**Response:** `accel_read_response`: **Response:** `accel_snapshot_response.samples[]`:
| Field | Meaning | | Field | Meaning |
|-------|---------| |-------|---------|
| `success` | `true` if BMA456 is ready and I2C read succeeded | | `client_id` | Slave id (registry) |
| `x`, `y`, `z` | Raw accel LSB (`sint32`; meaningful only when `success`) | | `valid` | At least one ESP-NOW sample received since boot |
| `x`, `y`, `z` | Raw BMA456 LSB (±2g) |
If the sensor was not probed at boot (`bma456_is_ready()` false), `success` is `false` and axes are zero. | `age_ms` | Ms since last sample from that slave |
Host: Host:
@ -332,7 +333,7 @@ Host:
go run . -port /dev/ttyUSB0 accel go run . -port /dev/ttyUSB0 accel
``` ```
Implementation: `bma456_read_accel()` in `bosch456.c` (mutex with the 10 Hz poll task), handler in `cmd/cmd_accel_read.c`. External API (`serve -api-addr :8081`) polls this command every 16 ms and streams JSON over WebSocket.
### ESPNOW_UNICAST_TEST command ### ESPNOW_UNICAST_TEST command
@ -479,7 +480,7 @@ Target: ESP32-S3. Close serial monitor on the UART adapter port before running `
| `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, on-demand read, tap INT, deadzone filter | | `bosch456.c/h` | BMA456H I2C driver, accel poll, on-demand read, tap INT, deadzone filter |
| `cmd/cmd_accel_read.c` | UART `ACCEL_READ` — current accel XYZ | | `cmd/cmd_accel_snapshot.c` | UART `ACCEL_SNAPSHOT` — cached slave accel |
| `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

@ -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"

View File

@ -241,6 +241,85 @@ 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;
}
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;
}
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

@ -21,6 +21,14 @@ 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;
} client_info_t; } client_info_t;
#define CLIENT_REGISTRY_DEFAULT_ACCEL_DEADZONE 100u #define CLIENT_REGISTRY_DEFAULT_ACCEL_DEADZONE 100u
@ -63,4 +71,13 @@ 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);
#endif #endif

View File

@ -1,34 +0,0 @@
#include "bosch456.h"
#include "cmd_accel_read.h"
#include "uart_cmd.h"
static const char *TAG = "[ACCEL_READ]";
static void reply(bool success, int16_t x, int16_t y, int16_t z) {
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_ACCEL_READ,
alox_UartMessage_accel_read_response_tag);
response.payload.accel_read_response.success = success;
response.payload.accel_read_response.x = x;
response.payload.accel_read_response.y = y;
response.payload.accel_read_response.z = z;
uart_cmd_send(&response, TAG);
}
static void handle_accel_read(const uint8_t *data, size_t len) {
(void)data;
(void)len;
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
if (bma456_read_accel(&x, &y, &z) == ESP_OK) {
reply(true, x, y, z);
return;
}
reply(false, 0, 0, 0);
}
void cmd_accel_read_register(void) {
uart_cmd_register(alox_MessageType_ACCEL_READ, handle_accel_read);
}

View File

@ -1,6 +0,0 @@
#ifndef CMD_ACCEL_READ_H
#define CMD_ACCEL_READ_H
void cmd_accel_read_register(void);
#endif

View File

@ -0,0 +1,68 @@
#include "client_registry.h"
#include "cmd_accel_snapshot.h"
#include "uart_cmd.h"
static const char *TAG = "[ACCEL_SNAP]";
static void fill_accel_snapshot(alox_AccelSnapshotResponse *out,
uint32_t filter_client_id) {
if (out == NULL) {
return;
}
out->samples_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;
}
if (filter_client_id != 0 && client->id != filter_client_id) {
continue;
}
if (!client->accel_stream_enabled) {
continue;
}
if (out->samples_count >=
sizeof(out->samples) / sizeof(out->samples[0])) {
break;
}
alox_AccelSample *sample = &out->samples[out->samples_count++];
sample->client_id = client->id;
sample->valid = client->accel_valid;
sample->x = client->accel_x;
sample->y = client->accel_y;
sample->z = client->accel_z;
if (client->accel_valid) {
sample->age_ms = client_registry_ms_since(client->accel_updated_at);
}
}
}
static void handle_accel_snapshot(const uint8_t *data, size_t len) {
uint32_t filter_client_id = 0;
if (len > 0) {
alox_UartMessage req;
if (uart_cmd_decode(data, len, &req) == ESP_OK) {
alox_AccelSnapshotRequest *snap_req = UART_CMD_REQ(
&req, alox_UartMessage_accel_snapshot_request_tag, accel_snapshot_request);
if (snap_req != NULL) {
filter_client_id = snap_req->client_id;
}
}
}
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_ACCEL_SNAPSHOT,
alox_UartMessage_accel_snapshot_response_tag);
fill_accel_snapshot(&response.payload.accel_snapshot_response, filter_client_id);
uart_cmd_send(&response, TAG);
}
void cmd_accel_snapshot_register(void) {
uart_cmd_register(alox_MessageType_ACCEL_SNAPSHOT, handle_accel_snapshot);
}

View File

@ -0,0 +1,6 @@
#ifndef CMD_ACCEL_SNAPSHOT_H
#define CMD_ACCEL_SNAPSHOT_H
void cmd_accel_snapshot_register(void);
#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

View File

@ -27,6 +27,7 @@ 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.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,8 +48,10 @@ 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_READ: case alox_MessageType_ACCEL_SNAPSHOT:
return "ACCEL_READ"; return "ACCEL_SNAPSHOT";
case alox_MessageType_ACCEL_STREAM:
return "ACCEL_STREAM";
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }

View File

@ -29,6 +29,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,6 +40,7 @@ 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 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;
@ -111,6 +113,18 @@ 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_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 +165,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;
@ -338,6 +364,25 @@ 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_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,6 +422,7 @@ 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;
} }
@ -426,6 +472,25 @@ 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_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 +518,23 @@ 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_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) {
@ -533,6 +615,30 @@ 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 slave_heartbeat_task(void *param) { static void slave_heartbeat_task(void *param) {
(void)param; (void)param;
@ -592,6 +698,12 @@ 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_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 +751,12 @@ 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;
}
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. */
@ -739,6 +857,11 @@ esp_err_t esp_now_comm_init(const app_config_t *config) {
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;
}
} }
return ESP_OK; return ESP_OK;

View File

@ -7,6 +7,10 @@
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: 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);

View File

@ -1,7 +1,8 @@
#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_read.h" #include "cmd_accel_snapshot.h"
#include "cmd_accel_stream.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"
@ -178,7 +179,8 @@ 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_read_register(); cmd_accel_snapshot_register();
cmd_accel_stream_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();

View File

@ -24,6 +24,12 @@ 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_EspNowOtaStart, alox_EspNowOtaStart, AUTO) PB_BIND(alox_EspNowOtaStart, alox_EspNowOtaStart, AUTO)

View File

@ -22,7 +22,9 @@ 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; } alox_EspNowMessageType;
/* Struct definitions */ /* Struct definitions */
@ -59,6 +61,20 @@ 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: 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 +114,8 @@ 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;
} payload; } payload;
} alox_EspNowMessage; } alox_EspNowMessage;
@ -108,8 +126,10 @@ 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_SET_ACCEL_STREAM
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_RESTART+1)) #define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM+1))
@ -131,6 +151,8 @@ 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_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 +164,8 @@ 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_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 +185,12 @@ 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_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 +209,8 @@ 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
/* 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 +249,20 @@ 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_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 +298,9 @@ 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)
#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 +314,8 @@ 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
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 +323,8 @@ 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_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 +338,8 @@ 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_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,6 +351,8 @@ 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_EspNowDiscover_size 6 #define alox_EspNowDiscover_size 6
#define alox_EspNowFindMe_size 6 #define alox_EspNowFindMe_size 6
#define alox_EspNowOtaEnd_size 0 #define alox_EspNowOtaEnd_size 0

View File

@ -17,6 +17,8 @@ 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;
} }
message EspNowUnicastTest { message EspNowUnicastTest {
@ -52,6 +54,20 @@ 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: 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 +103,7 @@ 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;
} }
} }

View File

@ -36,10 +36,19 @@ PB_BIND(alox_AccelDeadzoneRequest, alox_AccelDeadzoneRequest, AUTO)
PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO) PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO)
PB_BIND(alox_AccelReadRequest, alox_AccelReadRequest, AUTO) PB_BIND(alox_AccelStreamRequest, alox_AccelStreamRequest, AUTO)
PB_BIND(alox_AccelReadResponse, alox_AccelReadResponse, AUTO) PB_BIND(alox_AccelStreamResponse, alox_AccelStreamResponse, AUTO)
PB_BIND(alox_AccelSnapshotRequest, alox_AccelSnapshotRequest, AUTO)
PB_BIND(alox_AccelSample, alox_AccelSample, AUTO)
PB_BIND(alox_AccelSnapshotResponse, alox_AccelSnapshotResponse, 2)
PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO) PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO)

View File

@ -28,7 +28,8 @@ typedef enum _alox_MessageType {
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_READ = 24 alox_MessageType_ACCEL_SNAPSHOT = 24,
alox_MessageType_ACCEL_STREAM = 25
} alox_MessageType; } alox_MessageType;
/* Struct definitions */ /* Struct definitions */
@ -55,6 +56,8 @@ 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;
} alox_ClientInfo; } alox_ClientInfo;
typedef struct _alox_ClientInfoResponse { typedef struct _alox_ClientInfoResponse {
@ -89,17 +92,42 @@ typedef struct _alox_AccelDeadzoneResponse {
uint32_t slaves_updated; uint32_t slaves_updated;
} alox_AccelDeadzoneResponse; } alox_AccelDeadzoneResponse;
/* Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range). */ /* Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
typedef struct _alox_AccelReadRequest { write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients). */
char dummy_field; typedef struct _alox_AccelStreamRequest {
} alox_AccelReadRequest; bool write;
bool enable;
uint32_t client_id;
bool all_clients;
} alox_AccelStreamRequest;
typedef struct _alox_AccelReadResponse { typedef struct _alox_AccelStreamResponse {
bool enabled;
uint32_t client_id;
bool success; bool success;
uint32_t slaves_updated;
} alox_AccelStreamResponse;
/* Host → master: read cached accel samples from slaves (only while stream enabled).
client_id 0 = all registered slaves; otherwise one slave. */
typedef struct _alox_AccelSnapshotRequest {
uint32_t client_id;
} alox_AccelSnapshotRequest;
typedef struct _alox_AccelSample {
uint32_t client_id;
bool valid;
int32_t x; int32_t x;
int32_t y; int32_t y;
int32_t z; int32_t z;
} alox_AccelReadResponse; /* * Milliseconds since last ESP-NOW sample from this slave. */
uint32_t age_ms;
} alox_AccelSample;
typedef struct _alox_AccelSnapshotResponse {
pb_size_t samples_count;
alox_AccelSample samples[16];
} alox_AccelSnapshotResponse;
typedef struct _alox_EspNowUnicastTestRequest { typedef struct _alox_EspNowUnicastTestRequest {
uint32_t client_id; uint32_t client_id;
@ -231,8 +259,10 @@ 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_AccelReadRequest accel_read_request; alox_AccelSnapshotRequest accel_snapshot_request;
alox_AccelReadResponse accel_read_response; alox_AccelSnapshotResponse accel_snapshot_response;
alox_AccelStreamRequest accel_stream_request;
alox_AccelStreamResponse accel_stream_response;
} payload; } payload;
} alox_UartMessage; } alox_UartMessage;
@ -243,8 +273,8 @@ 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_ACCEL_READ #define _alox_MessageType_MAX alox_MessageType_ACCEL_STREAM
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ACCEL_READ+1)) #define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ACCEL_STREAM+1))
#define alox_UartMessage_type_ENUMTYPE alox_MessageType #define alox_UartMessage_type_ENUMTYPE alox_MessageType
@ -271,6 +301,9 @@ extern "C" {
@ -280,14 +313,17 @@ 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}
#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_AccelReadRequest_init_default {0} #define alox_AccelStreamRequest_init_default {0, 0, 0, 0}
#define alox_AccelReadResponse_init_default {0, 0, 0, 0} #define alox_AccelStreamResponse_init_default {0, 0, 0, 0}
#define alox_AccelSnapshotRequest_init_default {0}
#define alox_AccelSample_init_default {0, 0, 0, 0, 0, 0}
#define alox_AccelSnapshotResponse_init_default {0, {alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_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}
@ -307,14 +343,17 @@ 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}
#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_AccelReadRequest_init_zero {0} #define alox_AccelStreamRequest_init_zero {0, 0, 0, 0}
#define alox_AccelReadResponse_init_zero {0, 0, 0, 0} #define alox_AccelStreamResponse_init_zero {0, 0, 0, 0}
#define alox_AccelSnapshotRequest_init_zero {0}
#define alox_AccelSample_init_zero {0, 0, 0, 0, 0, 0}
#define alox_AccelSnapshotResponse_init_zero {0, {alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_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}
@ -343,6 +382,7 @@ 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_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
@ -357,10 +397,22 @@ 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_AccelReadResponse_success_tag 1 #define alox_AccelStreamRequest_write_tag 1
#define alox_AccelReadResponse_x_tag 2 #define alox_AccelStreamRequest_enable_tag 2
#define alox_AccelReadResponse_y_tag 3 #define alox_AccelStreamRequest_client_id_tag 3
#define alox_AccelReadResponse_z_tag 4 #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_AccelSnapshotRequest_client_id_tag 1
#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_AccelSnapshotResponse_samples_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
@ -424,8 +476,10 @@ 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_read_request_tag 23 #define alox_UartMessage_accel_snapshot_request_tag 23
#define alox_UartMessage_accel_read_response_tag 24 #define alox_UartMessage_accel_snapshot_response_tag 24
#define alox_UartMessage_accel_stream_request_tag 25
#define alox_UartMessage_accel_stream_response_tag 26
/* Struct field encoding specification for nanopb */ /* Struct field encoding specification for nanopb */
#define alox_UartMessage_FIELDLIST(X, a) \ #define alox_UartMessage_FIELDLIST(X, a) \
@ -451,8 +505,10 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_request,payload.espno
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_read_request,payload.accel_read_request), 23) \ X(a, STATIC, ONEOF, MESSAGE, (payload,accel_snapshot_request,payload.accel_snapshot_request), 23) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_response,payload.accel_read_response), 24) X(a, STATIC, ONEOF, MESSAGE, (payload,accel_snapshot_response,payload.accel_snapshot_response), 24) \
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)
#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
@ -476,8 +532,10 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_response,payload.accel_re
#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_read_request_MSGTYPE alox_AccelReadRequest #define alox_UartMessage_payload_accel_snapshot_request_MSGTYPE alox_AccelSnapshotRequest
#define alox_UartMessage_payload_accel_read_response_MSGTYPE alox_AccelReadResponse #define alox_UartMessage_payload_accel_snapshot_response_MSGTYPE alox_AccelSnapshotResponse
#define alox_UartMessage_payload_accel_stream_request_MSGTYPE alox_AccelStreamRequest
#define alox_UartMessage_payload_accel_stream_response_MSGTYPE alox_AccelStreamResponse
#define alox_Ack_FIELDLIST(X, a) \ #define alox_Ack_FIELDLIST(X, a) \
@ -503,7 +561,8 @@ 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)
#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
@ -543,18 +602,42 @@ 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_AccelReadRequest_FIELDLIST(X, a) \ #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_AccelReadRequest_CALLBACK NULL #define alox_AccelStreamResponse_FIELDLIST(X, a) \
#define alox_AccelReadRequest_DEFAULT NULL 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_AccelReadResponse_FIELDLIST(X, a) \ #define alox_AccelSnapshotRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, success, 1) \ X(a, STATIC, SINGULAR, UINT32, client_id, 1)
X(a, STATIC, SINGULAR, SINT32, x, 2) \ #define alox_AccelSnapshotRequest_CALLBACK NULL
X(a, STATIC, SINGULAR, SINT32, y, 3) \ #define alox_AccelSnapshotRequest_DEFAULT NULL
X(a, STATIC, SINGULAR, SINT32, z, 4)
#define alox_AccelReadResponse_CALLBACK NULL #define alox_AccelSample_FIELDLIST(X, a) \
#define alox_AccelReadResponse_DEFAULT NULL 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_AccelSnapshotResponse_FIELDLIST(X, a) \
X(a, STATIC, REPEATED, MESSAGE, samples, 1)
#define alox_AccelSnapshotResponse_CALLBACK NULL
#define alox_AccelSnapshotResponse_DEFAULT NULL
#define alox_AccelSnapshotResponse_samples_MSGTYPE alox_AccelSample
#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) \
@ -669,8 +752,11 @@ 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_AccelReadRequest_msg; extern const pb_msgdesc_t alox_AccelStreamRequest_msg;
extern const pb_msgdesc_t alox_AccelReadResponse_msg; extern const pb_msgdesc_t alox_AccelStreamResponse_msg;
extern const pb_msgdesc_t alox_AccelSnapshotRequest_msg;
extern const pb_msgdesc_t alox_AccelSample_msg;
extern const pb_msgdesc_t alox_AccelSnapshotResponse_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;
@ -698,8 +784,11 @@ 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_AccelReadRequest_fields &alox_AccelReadRequest_msg #define alox_AccelStreamRequest_fields &alox_AccelStreamRequest_msg
#define alox_AccelReadResponse_fields &alox_AccelReadResponse_msg #define alox_AccelStreamResponse_fields &alox_AccelStreamResponse_msg
#define alox_AccelSnapshotRequest_fields &alox_AccelSnapshotRequest_msg
#define alox_AccelSample_fields &alox_AccelSample_msg
#define alox_AccelSnapshotResponse_fields &alox_AccelSnapshotResponse_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
@ -723,11 +812,14 @@ 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_AccelSnapshotResponse_size
#define alox_AccelDeadzoneRequest_size 16 #define alox_AccelDeadzoneRequest_size 16
#define alox_AccelDeadzoneResponse_size 20 #define alox_AccelDeadzoneResponse_size 20
#define alox_AccelReadRequest_size 0 #define alox_AccelSample_size 32
#define alox_AccelReadResponse_size 20 #define alox_AccelSnapshotRequest_size 6
#define alox_AccelSnapshotResponse_size 544
#define alox_AccelStreamRequest_size 12
#define alox_AccelStreamResponse_size 16
#define alox_Ack_size 0 #define alox_Ack_size 0
#define alox_ClientInput_size 22 #define alox_ClientInput_size 22
#define alox_EspNowFindMeRequest_size 6 #define alox_EspNowFindMeRequest_size 6

View File

@ -22,7 +22,8 @@ enum MessageType {
OTA_SLAVE_PROGRESS = 21; OTA_SLAVE_PROGRESS = 21;
FIND_ME = 22; FIND_ME = 22;
RESTART = 23; RESTART = 23;
ACCEL_READ = 24; ACCEL_SNAPSHOT = 24;
ACCEL_STREAM = 25;
} }
message UartMessage { message UartMessage {
@ -49,8 +50,10 @@ 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;
AccelReadRequest accel_read_request = 23; AccelSnapshotRequest accel_snapshot_request = 23;
AccelReadResponse accel_read_response = 24; AccelSnapshotResponse accel_snapshot_response = 24;
AccelStreamRequest accel_stream_request = 25;
AccelStreamResponse accel_stream_response = 26;
} }
} }
@ -75,6 +78,8 @@ 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;
} }
message ClientInfoResponse { message ClientInfoResponse {
@ -109,14 +114,40 @@ message AccelDeadzoneResponse {
uint32 slaves_updated = 4; uint32 slaves_updated = 4;
} }
// Host device: read current BMA456 accelerometer sample (raw LSB, ±2g range). // Host master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
message AccelReadRequest {} // 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 AccelReadResponse { message AccelStreamResponse {
bool success = 1; bool enabled = 1;
sint32 x = 2; uint32 client_id = 2;
sint32 y = 3; bool success = 3;
sint32 z = 4; uint32 slaves_updated = 4;
}
// Host master: read cached accel samples from slaves (only while stream enabled).
// client_id 0 = all registered slaves; otherwise one slave.
message AccelSnapshotRequest {
uint32 client_id = 1;
}
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;
}
message AccelSnapshotResponse {
repeated AccelSample samples = 1 [(nanopb).max_count = 16];
} }
message EspNowUnicastTestRequest { message EspNowUnicastTestRequest {

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