Link protocol v2 receiver, ESP-NOW leg, Patrol gauge outputs, gear pulse
- protocol header v2 copied byte-identical from the 5B repo - uart_receiver: processFrame() shared by the UART framing task and ESP-NOW, version check, dedupe on packet_id+timestamp, per-transport liveness, CONFIG cache with sequence number - espnow_receiver: STA mode, channel hopping while silent, locks on frames - gauge_outputs: tach on IO7 (LEDC ch2, 14-bit), coolant PWM on IO8 (LEDC ch4, 8-bit), config cached in NVS, overrides applied but not saved - main: gear label from lever + engaged/target (pulses while shifting, red on mismatch), icons driven by A's alert bits, gauge update at 5 Hz, link stats - wiki: temp_stage.cfd (user's old CANNY program) decoded as prior art; controller-link, link-redundancy, patrol-gauge-outputs, open-questions updated with the bench-verified v2 results and calibration procedure Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+208
-178
@@ -1,8 +1,5 @@
|
||||
/**
|
||||
* UART Receiver Module Implementation
|
||||
*
|
||||
* Receives automotive data packets from Controller A via UART1.
|
||||
* Uses state machine for robust packet parsing and framing.
|
||||
* Link Receiver Module Implementation (Controller B)
|
||||
*/
|
||||
|
||||
#include "modules/uart_receiver.h"
|
||||
@@ -13,65 +10,159 @@
|
||||
|
||||
namespace UARTReceiver {
|
||||
|
||||
// UART Configuration
|
||||
// Note: UART_RX_PIN, UART_TX_PIN, and UART_BAUD_RATE are defined in platformio.ini
|
||||
#define UART_SERIAL_PORT Serial1 // Hardware UART1
|
||||
#define UART_RX_BUFFER_SIZE 256 // Receive buffer size
|
||||
#define UART_SERIAL_PORT Serial1
|
||||
#define UART_RX_BUFFER_SIZE 256
|
||||
|
||||
// Packet framing state machine states
|
||||
// UART framing state machine
|
||||
typedef enum {
|
||||
STATE_WAIT_START, // Waiting for start byte (0xAA)
|
||||
STATE_READ_VERSION, // Reading protocol version
|
||||
STATE_READ_LENGTH, // Reading data length
|
||||
STATE_READ_DATA, // Reading data payload
|
||||
STATE_READ_CHECKSUM, // Reading checksum byte
|
||||
STATE_READ_END // Reading end byte (0x55)
|
||||
STATE_WAIT_START,
|
||||
STATE_READ_VERSION,
|
||||
STATE_READ_TYPE,
|
||||
STATE_READ_LENGTH,
|
||||
STATE_READ_PAYLOAD,
|
||||
STATE_READ_CHECKSUM,
|
||||
STATE_READ_END
|
||||
} rx_state_t;
|
||||
|
||||
// Private state variables
|
||||
static automotive_data_t g_latest_data = {0};
|
||||
static link_config_t g_config;
|
||||
static uint32_t g_config_seq = 0;
|
||||
static uart_rx_stats_t g_rx_stats = {0};
|
||||
static SemaphoreHandle_t g_data_mutex = NULL;
|
||||
static TaskHandle_t g_uart_task_handle = NULL;
|
||||
static bool g_initialized = false;
|
||||
|
||||
// Dedupe key of the last applied DATA frame
|
||||
static bool g_have_last = false;
|
||||
static uint8_t g_last_id = 0;
|
||||
static uint32_t g_last_ts = 0;
|
||||
|
||||
static const char* sourceName(link_source_t s) {
|
||||
switch (s) {
|
||||
case LINK_SOURCE_UART: return "uart";
|
||||
case LINK_SOURCE_ESPNOW: return "espnow";
|
||||
default: return "none";
|
||||
}
|
||||
}
|
||||
|
||||
static void bump(uint32_t* counter) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(10)) == pdTRUE) {
|
||||
(*counter)++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
void processFrame(const uint8_t* frame, size_t len, link_source_t source) {
|
||||
uint8_t type = 0, plen = 0;
|
||||
const uint8_t* payload = NULL;
|
||||
frame_status_t st = frame_parse(frame, len, &type, &payload, &plen);
|
||||
|
||||
if (st != FRAME_OK) {
|
||||
switch (st) {
|
||||
case FRAME_ERR_VERSION:
|
||||
bump(&g_rx_stats.version_errors);
|
||||
if (g_rx_stats.version_errors <= 3 || g_rx_stats.version_errors % 50 == 0) {
|
||||
Serial.printf("[Link] Version mismatch on %s: got v%u, this firmware speaks v%u (%lu so far)\n",
|
||||
sourceName(source), frame[1], PACKET_VERSION,
|
||||
(unsigned long)g_rx_stats.version_errors);
|
||||
}
|
||||
break;
|
||||
case FRAME_ERR_CHECKSUM:
|
||||
bump(&g_rx_stats.checksum_errors);
|
||||
Serial.printf("[Link] Checksum error on %s\n", sourceName(source));
|
||||
break;
|
||||
default:
|
||||
bump(&g_rx_stats.framing_errors);
|
||||
Serial.printf("[Link] Framing error %d on %s (len %u)\n", (int)st, sourceName(source), (unsigned)len);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t now = millis();
|
||||
|
||||
if (type == PACKET_TYPE_CONFIG) {
|
||||
link_config_t cfg;
|
||||
memcpy(&cfg, payload, sizeof(cfg));
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(50)) == pdTRUE) {
|
||||
bool changed = (g_config_seq == 0) || (cfg.cfg_version != g_config.cfg_version);
|
||||
if (changed) {
|
||||
g_config = cfg;
|
||||
g_config_seq++;
|
||||
}
|
||||
g_rx_stats.config_received++;
|
||||
if (source == LINK_SOURCE_UART) g_rx_stats.last_uart_time = now;
|
||||
else if (source == LINK_SOURCE_ESPNOW) g_rx_stats.last_espnow_time = now;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
if (changed) {
|
||||
Serial.printf("[Link] CONFIG v%u received on %s\n", cfg.cfg_version, sourceName(source));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// DATA
|
||||
automotive_data_t d;
|
||||
memcpy(&d, payload, sizeof(d));
|
||||
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(50)) != pdTRUE) return;
|
||||
|
||||
if (source == LINK_SOURCE_UART) { g_rx_stats.packets_uart++; g_rx_stats.last_uart_time = now; }
|
||||
else if (source == LINK_SOURCE_ESPNOW) { g_rx_stats.packets_espnow++; g_rx_stats.last_espnow_time = now; }
|
||||
|
||||
bool dup = g_have_last && d.packet_id == g_last_id && d.timestamp_ms == g_last_ts;
|
||||
if (dup) {
|
||||
g_rx_stats.duplicates++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
g_have_last = true;
|
||||
g_last_id = d.packet_id;
|
||||
g_last_ts = d.timestamp_ms;
|
||||
g_latest_data = d;
|
||||
g_rx_stats.packets_received++;
|
||||
g_rx_stats.last_packet_time = now;
|
||||
g_rx_stats.last_source = source;
|
||||
uint32_t n = g_rx_stats.packets_received;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
|
||||
DebugMonitor::setVisible(d.debug_enabled != 0);
|
||||
|
||||
if (n % 25 == 0) {
|
||||
char gear[8], gearbox_buf[16], coolant_buf[16], voltage_buf[16];
|
||||
gear_compose(d.gear_lever, gear_engaged(d.gear_nibbles), gear, sizeof(gear));
|
||||
Serial.printf("[Link] RX #%u via %s | Gear:%s (t%u/e%u) | RPM:%u | GB:%s | Cool:%s | Batt:%s | Alerts:0x%02X | Valid:0x%02X\n",
|
||||
d.packet_id, sourceName(source), gear,
|
||||
gear_target(d.gear_nibbles), gear_engaged(d.gear_nibbles),
|
||||
d.engine_rpm,
|
||||
format_temperature(d.gearbox_oil_temp, gearbox_buf),
|
||||
format_temperature(d.coolant_temp, coolant_buf),
|
||||
format_voltage(d.battery_voltage, voltage_buf),
|
||||
d.alerts, d.valid_flags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UART reception task - runs on Core 0
|
||||
*
|
||||
* Implements state machine for packet parsing:
|
||||
* 1. Wait for start byte (0xAA)
|
||||
* 2. Read version and length
|
||||
* 3. Read data payload
|
||||
* 4. Read checksum
|
||||
* 5. Read end byte (0x55)
|
||||
* 6. Validate packet
|
||||
* 7. Update shared data structure
|
||||
* UART1 byte-stream framing task (Core 0)
|
||||
*/
|
||||
static void uartRxTask(void* parameter) {
|
||||
uint8_t rx_buffer[sizeof(uart_packet_t)];
|
||||
uint8_t rx_buffer[PACKET_MAX_SIZE];
|
||||
rx_state_t state = STATE_WAIT_START;
|
||||
size_t bytes_read = 0;
|
||||
uint8_t expected_length = 0;
|
||||
|
||||
Serial.println("[UARTReceiver] Task started on Core 0");
|
||||
|
||||
// Initialize UART1 hardware
|
||||
UART_SERIAL_PORT.setRxBufferSize(UART_RX_BUFFER_SIZE);
|
||||
UART_SERIAL_PORT.begin(UART_BAUD_RATE, SERIAL_8N1, UART_RX_PIN, UART_TX_PIN);
|
||||
|
||||
Serial.printf("[UARTReceiver] UART1 initialized: %d baud, RX=GPIO%d, TX=GPIO%d\n",
|
||||
UART_BAUD_RATE, UART_RX_PIN, UART_TX_PIN);
|
||||
Serial.printf("[UARTReceiver] Waiting for packets from Controller A...\n");
|
||||
Serial.printf("[UARTReceiver] UART1 initialized: %d baud, RX=GPIO%d, protocol v%u\n",
|
||||
UART_BAUD_RATE, UART_RX_PIN, PACKET_VERSION);
|
||||
|
||||
while (true) {
|
||||
// Check if data available
|
||||
if (UART_SERIAL_PORT.available()) {
|
||||
while (UART_SERIAL_PORT.available()) {
|
||||
uint8_t byte = UART_SERIAL_PORT.read();
|
||||
|
||||
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
|
||||
g_rx_stats.bytes_received++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
g_rx_stats.bytes_received++; // statistic only; a torn read is harmless
|
||||
|
||||
switch (state) {
|
||||
case STATE_WAIT_START:
|
||||
@@ -80,15 +171,16 @@ static void uartRxTask(void* parameter) {
|
||||
bytes_read = 1;
|
||||
state = STATE_READ_VERSION;
|
||||
} else {
|
||||
// Ignore byte, still waiting for start
|
||||
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
|
||||
g_rx_stats.framing_errors++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
bump(&g_rx_stats.framing_errors);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_READ_VERSION:
|
||||
rx_buffer[bytes_read++] = byte;
|
||||
state = STATE_READ_TYPE;
|
||||
break;
|
||||
|
||||
case STATE_READ_TYPE:
|
||||
rx_buffer[bytes_read++] = byte;
|
||||
state = STATE_READ_LENGTH;
|
||||
break;
|
||||
@@ -96,29 +188,20 @@ static void uartRxTask(void* parameter) {
|
||||
case STATE_READ_LENGTH:
|
||||
rx_buffer[bytes_read++] = byte;
|
||||
expected_length = byte;
|
||||
|
||||
// Validate length is reasonable
|
||||
if (expected_length > sizeof(automotive_data_t)) {
|
||||
// Invalid length, reset to wait for start
|
||||
Serial.printf("[UARTReceiver] ERROR: Invalid length %d (max: %d)\n",
|
||||
expected_length, sizeof(automotive_data_t));
|
||||
if (expected_length > PACKET_MAX_PAYLOAD) {
|
||||
bump(&g_rx_stats.framing_errors);
|
||||
state = STATE_WAIT_START;
|
||||
bytes_read = 0;
|
||||
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
|
||||
g_rx_stats.framing_errors++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
} else if (expected_length == 0) {
|
||||
state = STATE_READ_CHECKSUM;
|
||||
} else {
|
||||
state = STATE_READ_DATA;
|
||||
state = STATE_READ_PAYLOAD;
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_READ_DATA:
|
||||
case STATE_READ_PAYLOAD:
|
||||
rx_buffer[bytes_read++] = byte;
|
||||
|
||||
// Check if we've read all data bytes
|
||||
// Packet structure: start(1) + version(1) + length(1) + data(expected_length)
|
||||
if (bytes_read >= (3 + expected_length)) {
|
||||
if (bytes_read >= (size_t)(PACKET_HEADER_SIZE + expected_length)) {
|
||||
state = STATE_READ_CHECKSUM;
|
||||
}
|
||||
break;
|
||||
@@ -130,63 +213,13 @@ static void uartRxTask(void* parameter) {
|
||||
|
||||
case STATE_READ_END:
|
||||
rx_buffer[bytes_read++] = byte;
|
||||
|
||||
if (byte == PACKET_END_BYTE) {
|
||||
// Complete packet received - validate it
|
||||
uart_packet_t* packet = (uart_packet_t*)rx_buffer;
|
||||
|
||||
if (validate_packet(packet)) {
|
||||
// Valid packet - update latest data
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
memcpy(&g_latest_data, &packet->data, sizeof(automotive_data_t));
|
||||
g_rx_stats.packets_received++;
|
||||
g_rx_stats.last_packet_time = millis();
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
|
||||
// Update debug monitor visibility based on received flag
|
||||
DebugMonitor::setVisible(packet->data.debug_enabled != 0);
|
||||
|
||||
// Debug output every 20 packets
|
||||
if (g_rx_stats.packets_received % 20 == 0) {
|
||||
char gearbox_buf[16], engine_buf[16], coolant_buf[16], voltage_buf[16];
|
||||
Serial.printf("[UARTReceiver] RX #%d | Gear:%s | GB:%s | Eng:%s | Cool:%s | Batt:%s | Valid:0x%02X | Debug:%s\n",
|
||||
packet->data.packet_id,
|
||||
gear_to_string((gear_position_t)packet->data.gear_position),
|
||||
format_temperature(packet->data.gearbox_oil_temp, gearbox_buf),
|
||||
format_temperature(packet->data.engine_oil_temp, engine_buf),
|
||||
format_temperature(packet->data.coolant_temp, coolant_buf),
|
||||
format_voltage(packet->data.battery_voltage, voltage_buf),
|
||||
packet->data.valid_flags,
|
||||
packet->data.debug_enabled ? "ON" : "OFF");
|
||||
}
|
||||
} else {
|
||||
// Invalid checksum
|
||||
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
|
||||
g_rx_stats.checksum_errors++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
Serial.printf("[UARTReceiver] Checksum error (packet #%d)\n",
|
||||
((uart_packet_t*)rx_buffer)->data.packet_id);
|
||||
}
|
||||
} else {
|
||||
// Invalid end byte
|
||||
if (xSemaphoreTake(g_data_mutex, 0) == pdTRUE) {
|
||||
g_rx_stats.framing_errors++;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
Serial.printf("[UARTReceiver] Framing error: End byte 0x%02X (expected 0x55)\n", byte);
|
||||
}
|
||||
|
||||
// Reset to wait for next packet
|
||||
processFrame(rx_buffer, bytes_read, LINK_SOURCE_UART);
|
||||
state = STATE_WAIT_START;
|
||||
bytes_read = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Small delay to prevent CPU hogging
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
vTaskDelay(pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,26 +229,18 @@ bool init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
Serial.println("[UARTReceiver] Initializing UART receiver...");
|
||||
Serial.println("[UARTReceiver] Initializing link receiver...");
|
||||
|
||||
link_config_defaults(&g_config);
|
||||
|
||||
// Create mutex for thread-safe data access
|
||||
g_data_mutex = xSemaphoreCreateMutex();
|
||||
if (g_data_mutex == NULL) {
|
||||
Serial.println("[UARTReceiver] ERROR: Failed to create mutex");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create UART reception task on Core 0
|
||||
BaseType_t result = xTaskCreatePinnedToCore(
|
||||
uartRxTask,
|
||||
"UART_RX_Task",
|
||||
4096, // Stack size
|
||||
NULL, // Parameters
|
||||
1, // Priority (lower than LVGL task)
|
||||
&g_uart_task_handle,
|
||||
0 // Core 0
|
||||
);
|
||||
|
||||
uartRxTask, "UART_RX_Task", 4096, NULL, 1, &g_uart_task_handle, 0);
|
||||
if (result != pdPASS) {
|
||||
Serial.println("[UARTReceiver] ERROR: Failed to create task");
|
||||
return false;
|
||||
@@ -223,91 +248,96 @@ bool init() {
|
||||
|
||||
g_initialized = true;
|
||||
Serial.println("[UARTReceiver] Initialization complete");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
automotive_data_t getLatestData() {
|
||||
automotive_data_t data;
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
if (g_data_mutex != NULL) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
memcpy(&data, &g_latest_data, sizeof(automotive_data_t));
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
data = g_latest_data;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
uart_rx_stats_t getStats() {
|
||||
uart_rx_stats_t stats;
|
||||
memset(&stats, 0, sizeof(stats));
|
||||
|
||||
if (g_data_mutex != NULL) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
memcpy(&stats, &g_rx_stats, sizeof(uart_rx_stats_t));
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
stats = g_rx_stats;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
bool isConnected(uint32_t timeout_ms) {
|
||||
if (!g_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!g_initialized) return false;
|
||||
bool connected = false;
|
||||
|
||||
if (g_data_mutex != NULL) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
if (g_rx_stats.packets_received > 0) {
|
||||
uint32_t time_since_last = millis() - g_rx_stats.last_packet_time;
|
||||
connected = (time_since_last < timeout_ms);
|
||||
}
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
if (g_rx_stats.packets_received > 0) {
|
||||
connected = (millis() - g_rx_stats.last_packet_time) < timeout_ms;
|
||||
}
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
|
||||
return connected;
|
||||
}
|
||||
|
||||
void resetStats() {
|
||||
if (g_data_mutex != NULL) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
g_rx_stats.packets_received = 0;
|
||||
g_rx_stats.checksum_errors = 0;
|
||||
g_rx_stats.framing_errors = 0;
|
||||
g_rx_stats.timeout_errors = 0;
|
||||
g_rx_stats.bytes_received = 0;
|
||||
// Keep last_packet_time
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
bool isSourceAlive(link_source_t source, uint32_t timeout_ms) {
|
||||
if (!g_initialized) return false;
|
||||
bool alive = false;
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
uint32_t t = (source == LINK_SOURCE_UART) ? g_rx_stats.last_uart_time
|
||||
: (source == LINK_SOURCE_ESPNOW) ? g_rx_stats.last_espnow_time : 0;
|
||||
alive = (t != 0) && ((millis() - t) < timeout_ms);
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
return alive;
|
||||
}
|
||||
|
||||
link_config_t getConfig() {
|
||||
link_config_t c;
|
||||
link_config_defaults(&c);
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
c = g_config;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
uint32_t getConfigSeq() {
|
||||
uint32_t s = 0;
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
s = g_config_seq;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void resetStats() {
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
uint32_t keep_last = g_rx_stats.last_packet_time;
|
||||
uint32_t keep_uart = g_rx_stats.last_uart_time;
|
||||
uint32_t keep_espnow = g_rx_stats.last_espnow_time;
|
||||
link_source_t keep_src = g_rx_stats.last_source;
|
||||
memset(&g_rx_stats, 0, sizeof(g_rx_stats));
|
||||
g_rx_stats.last_packet_time = keep_last;
|
||||
g_rx_stats.last_uart_time = keep_uart;
|
||||
g_rx_stats.last_espnow_time = keep_espnow;
|
||||
g_rx_stats.last_source = keep_src;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
Serial.println("[UARTReceiver] Statistics reset");
|
||||
}
|
||||
|
||||
uint32_t getTimeSinceLastPacket() {
|
||||
if (!g_initialized) {
|
||||
return UINT32_MAX;
|
||||
if (!g_initialized) return UINT32_MAX;
|
||||
uint32_t t = UINT32_MAX;
|
||||
if (g_data_mutex && xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
if (g_rx_stats.packets_received > 0) t = millis() - g_rx_stats.last_packet_time;
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
|
||||
uint32_t time_since = UINT32_MAX;
|
||||
|
||||
if (g_data_mutex != NULL) {
|
||||
if (xSemaphoreTake(g_data_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
if (g_rx_stats.packets_received > 0) {
|
||||
time_since = millis() - g_rx_stats.last_packet_time;
|
||||
}
|
||||
xSemaphoreGive(g_data_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
return time_since;
|
||||
return t;
|
||||
}
|
||||
|
||||
} // namespace UARTReceiver
|
||||
|
||||
Reference in New Issue
Block a user