From 579a7559c13c757336eaea979f37ca3164ce12cf Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Fri, 28 Mar 2025 00:29:24 -0500 Subject: [PATCH] Add state tracking to bottom half --- main/drivers/bottom_half.cpp | 48 ++++++++++++++++++++++++++++++++- main/drivers/state_tracking.cpp | 34 +++++++++++++++++------ 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/main/drivers/bottom_half.cpp b/main/drivers/bottom_half.cpp index b88e3cc..f2a0b4b 100644 --- a/main/drivers/bottom_half.cpp +++ b/main/drivers/bottom_half.cpp @@ -1,5 +1,6 @@ #include "bottom_half.h" #include +#include "state_tracking.h" static const char *TAG = "bottom_half"; @@ -75,9 +76,19 @@ static void receive_keypad(void) { uint16_t just_pressed = new_keypad_state & ~keypad_state; keypad_pressed |= just_pressed; + if (is_state_tracking() && just_pressed) { + char buf[6]; + sprintf(buf, "%d", just_pressed); + event_occured("KP_PRESS", buf); + } uint16_t just_released = ~new_keypad_state & keypad_state; keypad_released |= just_released; + if (is_state_tracking() && just_released) { + char buf[6]; + sprintf(buf, "%d", just_released); + event_occured("KP_RELEASE", buf); + } keypad_state = new_keypad_state; } @@ -92,27 +103,57 @@ static void receive_button_switch(void) { // button uint8_t just_pressed = new_button_state & ~button_state; button_pressed |= just_pressed; + if (is_state_tracking() && just_pressed) { + char buf[4]; + sprintf(buf, "%d", just_pressed); + event_occured("BTN_PRESS", buf); + } uint8_t just_released = ~new_button_state & button_state; button_released |= just_released; + if (is_state_tracking() && just_released) { + char buf[4]; + sprintf(buf, "%d", just_released); + event_occured("BTN_RELEASE", buf); + } button_state = new_button_state; // switch uint8_t just_flipped_up = new_switch_state & ~switch_state; switch_flipped_up |= just_flipped_up; + if (is_state_tracking() && just_flipped_up) { + char buf[4]; + sprintf(buf, "%d", just_flipped_up); + event_occured("SW_UP", buf); + } uint8_t just_flipped_down = ~new_switch_state & switch_state; switch_flipped_down |= just_flipped_down; + if (is_state_tracking() && just_flipped_down) { + char buf[4]; + sprintf(buf, "%d", just_flipped_down); + event_occured("SW_DOWN", buf); + } switch_state = new_switch_state; // switch touch uint8_t touch_just_pressed = new_switch_touch_state & ~switch_touch_state; switch_touch_pressed |= touch_just_pressed; + if (is_state_tracking() && touch_just_pressed) { + char buf[4]; + sprintf(buf, "%d", touch_just_pressed); + event_occured("SW_TOUCH", buf); + } uint8_t touch_just_released = ~new_switch_touch_state & switch_touch_state; switch_touch_released |= touch_just_released; + if (is_state_tracking() && touch_just_released) { + char buf[4]; + sprintf(buf, "%d", touch_just_released); + event_occured("SW_UNTOUCH", buf); + } switch_touch_state = new_switch_touch_state; } @@ -125,9 +166,15 @@ static void receive_touch(void) { bool just_pressed = new_touch_state & !touch_state; touch_pressed |= just_pressed; + if (is_state_tracking() && just_pressed) { + event_occured("FINGER_TOUCHED", NULL); + } bool just_released = (!new_touch_state) & touch_state; touch_released |= just_released; + if (is_state_tracking() && just_released) { + event_occured("FINGER_UNTOUCHED", NULL); + } touch_state = new_touch_state; } @@ -307,7 +354,6 @@ uint8_t get_switch_touch_state(){ return switch_touch_state; } - bool get_touch_state(void) { return touch_state; } diff --git a/main/drivers/state_tracking.cpp b/main/drivers/state_tracking.cpp index 409a29b..272fa7f 100644 --- a/main/drivers/state_tracking.cpp +++ b/main/drivers/state_tracking.cpp @@ -13,10 +13,29 @@ static std::vector event_cbs; static bool should_close_recording_stream; static FILE* recording_stream; static uint32_t recording_start_time; +TaskHandle_t flush_file_task_handle; static bool should_close_playback_stream; static FILE* playback_stream; -static state_t state = STATE_IDLE; +static volatile state_t state = STATE_IDLE; + + +/// @brief Periodically flushes and syncs (if neccesary) the output stream. +/// @param arg unused. +static void flush_file_task(void* arg) { + while (state == STATE_RECORDING && recording_stream != nullptr) { + fflush(recording_stream); + + int fd = fileno(recording_stream); + if (fd != -1) { + fsync(fd); + } + vTaskDelay(pdMS_TO_TICKS(5000)); + } + + flush_file_task_handle = NULL; + vTaskDelete(NULL); +} void register_event_cb(const char* name, void (*replay_callback)(uint32_t, const char*)) { EventCB event = { @@ -37,13 +56,6 @@ void event_occured(const char* name, const char* arg) { arg = (arg == nullptr) ? "" : arg; uint32_t ticks = xTaskGetTickCount() - recording_start_time; fprintf(recording_stream, "%ld,%s:%s\n", ticks, name, (arg == nullptr) ? "" : arg); - - fflush(recording_stream); - - int fd = fileno(recording_stream); - if (fd != -1) { - fsync(fd); - } } bool set_recording_source(FILE* stream, bool should_close) { @@ -68,6 +80,7 @@ bool start_recording() { state = STATE_RECORDING; recording_start_time = xTaskGetTickCount(); + xTaskCreate(flush_file_task, "flush_recording", 2048, NULL, 2, &flush_file_task_handle); return true; } @@ -80,6 +93,11 @@ bool stop_recording() { fclose(recording_stream); recording_stream = nullptr; } + + if (flush_file_task_handle != nullptr) { + vTaskDelete(flush_file_task_handle); + flush_file_task_handle = NULL; + } return true; }