#pragma once #include #include #include #include "esp_err.h" #include "beat.h" #include "bridge.h" // Beat frame, broadcast. Little endian, 8 bytes. #define BEAT_FRAME_MAGIC 0x4C // 0x4C wire protocol, message type 0x25 #define BEAT_FRAME_TYPE 0x25 #define BEAT_FLAG_DOWNBEAT 0x01 #define BEAT_FLAG_BAR_SHIFT 1 // bits 1-3: bar position 0..7 (0 = unknown) typedef struct __attribute__((packed)) { uint8_t magic; uint8_t type; uint8_t flags; uint16_t beat_idx; // same for every copy of one beat uint16_t period_ms; // beat period, drivers free-run on it uint8_t fire_in_ms; // drivers fire this long after receipt } beat_frame_t; typedef struct { uint32_t beats_sent; uint32_t beat_send_fail; uint32_t bulk_sent; uint32_t bulk_retries; uint32_t bulk_failed; uint32_t sim_dropped; // frames discarded by the lossy-link test mode } espnow_stats_t; // Start Wi-Fi on the configured channel and ESP-NOW; hooks beat_set_callback. // ap_ssid non-NULL also runs an access point of that name (192.168.5.1): WPA2 with // ap_password (8-63 chars), open when it is NULL or empty. esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password); // JSON array [{"mac","ip"}] of stations on the access point (DHCP leases); "[]" when it is off. Returns its length. int espnow_tx_ap_clients(char *out, size_t n); // Before espnow_tx_init: send every frame to these bridges (see bridge.h) instead of the radio. None keeps the radio. void espnow_tx_set_bridges(const char addrs[][BRIDGE_ADDR_MAX], size_t n); // While running (host build): make these the bridges (see bridge_set_all). Returns the first error, if any. esp_err_t espnow_tx_change_bridges(const char addrs[][BRIDGE_ADDR_MAX], size_t n); // Frames received from drivers (radio or bridge). Runs in the Wi-Fi or bridge task. typedef void (*espnow_rx_cb_t)(const uint8_t *src_mac, const uint8_t *data, int len); esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb); // Queue a bulk frame (<= 240 bytes). mac NULL broadcasts. Unicast frames are // retried; they never go out within the hold-off window after a beat. esp_err_t espnow_tx_send(const uint8_t *mac, const void *data, size_t len); void espnow_tx_stats(espnow_stats_t *out); // Send one beat now (also used by the bench generator). bar 0 = unknown. void espnow_tx_beat(float bpm, bool downbeat, int bar); // Whether the mic's detected beats go to the drivers (off while audio timing is switched off) void espnow_tx_set_mic_beats(bool on); // Lossy-link test mode: discard this percentage of outgoing frames and of // received acks/echoes. include_beats = false leaves beat copies alone. void espnow_tx_set_loss(int percent, bool include_beats); int espnow_tx_loss(bool *include_beats); bool espnow_tx_drop(void);