From bf663d43236ee195e4715a6245338adaac56dff0 Mon Sep 17 00:00:00 2001 From: Sassa NF Date: Sat, 12 Oct 2024 13:04:14 +0100 Subject: [PATCH] Capture data for JSON on events --- src/main.cpp | 61 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bda4cde..f192580 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -471,34 +471,53 @@ struct frequency_scan_result { uint64_t begin; uint64_t end; + uint64_t last_epoch; + int16_t rssi; // deliberately not a float; floats can pin task to wrong core forever } frequency_scan_result; +void eventListenerForMSP(void *arg, Event &e) +{ + if (e.type == EventType::DETECTED) + { + if (e.epoch != frequency_scan_result.last_epoch || + e.detected.rssi > frequency_scan_result.rssi) + { + frequency_scan_result.last_epoch = e.epoch; + frequency_scan_result.rssi = e.detected.rssi; + } + + return; + } + + if (e.type == EventType::SCAN_TASK_COMPLETE) + { + // notify async communication that the data is ready + return; + } +} + void logToSerialTask(void *parameter) { #ifdef HELTEC JsonDocument doc; -#endif char jsonOutput[200]; +#endif + + + uint64_t last_epoch = frequency_scan_result.last_epoch; + frequency_scan_result.rssi = -999; for (;;) { - if (frequency_scan_result.begin != frequency_scan_result.end) + vTaskDelay(LOG_DATA_JSON_INTERVAL / portTICK_PERIOD_MS); + if (frequency_scan_result.begin != frequency_scan_result.end || + frequency_scan_result.last_epoch != last_epoch) { - String max_result = "-999"; - int highest_value_scanned = 999; - - for (int rssi_item = 0; rssi_item < STEPS / WINDOW_SIZE; rssi_item++) + int16_t highest_value_scanned = frequency_scan_result.rssi; + frequency_scan_result.rssi = -999; + last_epoch = frequency_scan_result.last_epoch; + if (highest_value_scanned == -999) { - if (max_x_window[rssi_item] != 0 && - max_x_window[rssi_item] < highest_value_scanned) - { - max_result = "-" + String(max_x_window[rssi_item]); - highest_value_scanned = max_x_window[rssi_item]; - } - } - if (highest_value_scanned == 999) - { - vTaskDelay(LOG_DATA_JSON_INTERVAL / portTICK_PERIOD_MS); continue; } @@ -510,13 +529,13 @@ void logToSerialTask(void *parameter) serializeJson(doc, jsonOutput); Serial.println(jsonOutput); #else - Serial.printf("{\"low_range_freq\": %ull, \"high_range_freq\": %ull, " - "\"value\": \"%s\"}\n", + Serial.printf("{\"low_range_freq\": %" PRIu64 + ", \"high_range_freq\": %" PRIu64 ", " + "\"value\": \"%" PRIi16 "\"}\n", frequency_scan_result.begin, frequency_scan_result.end, - max_result); + highest_value_scanned); #endif } - vTaskDelay(LOG_DATA_JSON_INTERVAL / portTICK_PERIOD_MS); } } @@ -713,6 +732,8 @@ void setup(void) r.addEventListener(DETECTED, drone_sound_alarm, &r); r.addEventListener(SCAN_TASK_COMPLETE, stacked); + r.addEventListener(DETECTED, eventListenerForMSP, NULL); + #ifdef UPTIME_CLOCK uptime = new UptimeClock(display, millis()); #endif