powerpods/main/cmd/cmd_cache_status.c
simon f512936d97 Add CACHE_STATUS UART poll and dashboard live stream.
Combine cached accel and tap in one low-overhead master command for ~16 ms
host polling. The dashboard uses a single live-stream toggle plus per-slave
accel-stream controls; fix live_stream state so polling is not cleared every
slow client refresh.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-29 20:57:54 +02:00

84 lines
2.3 KiB
C

#include "client_registry.h"
#include "cmd_cache_status.h"
#include "uart_cmd.h"
static const char *TAG = "[CACHE_STAT]";
static bool tap_notify_any(const client_info_t *client) {
return client != NULL &&
(client->tap_notify_single || client->tap_notify_double ||
client->tap_notify_triple);
}
static alox_TapKind tap_kind_from_registry(uint32_t kind) {
switch (kind) {
case 1:
return alox_TapKind_TAP_SINGLE;
case 2:
return alox_TapKind_TAP_DOUBLE;
case 3:
return alox_TapKind_TAP_TRIPLE;
default:
return alox_TapKind_TAP_NONE;
}
}
static void fill_cache_status(alox_CacheStatusResponse *out) {
if (out == NULL) {
return;
}
out->accel_count = 0;
out->taps_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 (client->accel_stream_enabled &&
out->accel_count < sizeof(out->accel) / sizeof(out->accel[0])) {
alox_AccelSample *sample = &out->accel[out->accel_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);
}
}
if (tap_notify_any(client) &&
out->taps_count < sizeof(out->taps) / sizeof(out->taps[0])) {
uint32_t kind = 0;
uint32_t age_ms = 0;
if (!client_registry_take_tap(client->id, &kind, &age_ms)) {
continue;
}
alox_TapEvent *event = &out->taps[out->taps_count++];
event->client_id = client->id;
event->valid = true;
event->kind = tap_kind_from_registry(kind);
event->age_ms = age_ms;
}
}
}
static void handle_cache_status(const uint8_t *data, size_t len) {
(void)data;
(void)len;
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_CACHE_STATUS,
alox_UartMessage_cache_status_response_tag);
fill_cache_status(&response.payload.cache_status_response);
uart_cmd_send(&response, TAG);
}
void cmd_cache_status_register(void) {
uart_cmd_register(alox_MessageType_CACHE_STATUS, handle_cache_status);
}