From 4e75aeb69c56f98dfead686e5038e7d68406a19d Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Sun, 13 Jul 2025 14:37:03 -0500 Subject: [PATCH] work on header --- main/drivers/char_lcd.cpp | 2 +- main/drivers/game_info.cpp | 2 ++ main/drivers/power.cpp | 2 ++ main/drivers/star_code.cpp | 59 ++++++++++++++++++++++++++++++++------ main/drivers/star_code.h | 4 +-- main/drivers/wires.cpp | 2 +- main/steps/step0.cpp | 40 +++++++++++++------------- main/steps/step3.cpp | 2 +- main/steps/step4.cpp | 2 +- main/steps/step5.cpp | 2 +- 10 files changed, 81 insertions(+), 36 deletions(-) diff --git a/main/drivers/char_lcd.cpp b/main/drivers/char_lcd.cpp index 4a2a313..0aa9818 100644 --- a/main/drivers/char_lcd.cpp +++ b/main/drivers/char_lcd.cpp @@ -256,4 +256,4 @@ void lcd_print_header() { lcd_print_header_bat(); } -// TODO: add task to continoususly print the header. +// TODO: add task to continoususly print the battery header. diff --git a/main/drivers/game_info.cpp b/main/drivers/game_info.cpp index 8be4798..3dfa616 100644 --- a/main/drivers/game_info.cpp +++ b/main/drivers/game_info.cpp @@ -16,5 +16,7 @@ void reset_game_state() { } void lcd_print_header_step() { + if (!lcd_header_enabled()) return; + lcd_print(11, 1, game_state); } diff --git a/main/drivers/power.cpp b/main/drivers/power.cpp index ee939a4..0a4312a 100644 --- a/main/drivers/power.cpp +++ b/main/drivers/power.cpp @@ -55,6 +55,8 @@ uint16_t get_bat_voltage() { } void lcd_print_header_bat() { + if (!lcd_header_enabled()) return; + uint8_t soc = lipo.soc(); char buf[5]; if (soc < 5) { diff --git a/main/drivers/star_code.cpp b/main/drivers/star_code.cpp index 56d0001..65b7664 100644 --- a/main/drivers/star_code.cpp +++ b/main/drivers/star_code.cpp @@ -6,24 +6,42 @@ #include #include "drivers/bottom_half.h" #include "char_lcd.h" +#include "esp_timer.h" static const char* TAG = "star_code"; volatile bool handling_new_starcodes = false; static volatile bool system_initialized = false; +// TODO: use the semaphore, convert to RWLock? static volatile SemaphoreHandle_t star_codes_sem; static std::vector star_codes; static const char EMPTY_STAR_CODE_HEADER[] = " "; +esp_timer_handle_t starcode_delay_timer; +static volatile StarCodeEntry* processing_starcode = nullptr; static volatile bool doing_starcode = false; static uint16_t starcode_waiting_on_release; static char current[STARCODE_MAX_LEN + 1]; static size_t current_idx; +static void starcode_trigger_cb(void* arg) { + (void) arg; + + if (processing_starcode->triggered_sem != nullptr) + xSemaphoreGive(processing_starcode->triggered_sem); + if (processing_starcode->callback != nullptr) + (processing_starcode->callback)(); + + processing_starcode = nullptr; + // TODO: rename star code everywhere to starcode + lcd_print_header_star_code(); +} + + void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) { - if (handling_new_starcodes && (*just_pressed | (1 << KeypadKey::star))) { + if ((processing_starcode == nullptr) && handling_new_starcodes && (*just_pressed | (1 << KeypadKey::star))) { current_idx = 0; current[current_idx] = '\0'; doing_starcode = true; @@ -39,6 +57,7 @@ void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) { current[current_idx] = '\0'; } else if (key == KeypadKey::pound) { doing_starcode = false; + lcd_print_header_star_code(); if (current_idx != 0) { trigger_star_code(current); } @@ -53,6 +72,7 @@ void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) { // append the character current[current_idx++] = char_of_keypad_key(key); current[current_idx] = '\0'; + lcd_print_header_star_code(); } } } @@ -67,6 +87,16 @@ void init_star_code_system() { star_codes_sem = xSemaphoreCreateBinary(); xSemaphoreGive(star_codes_sem); + const esp_timer_create_args_t timer_args = { + .callback = &starcode_trigger_cb, + .arg = nullptr, + .dispatch_method = ESP_TIMER_TASK, + .name = "starcode_trigger", + .skip_unhandled_events = false, + }; + + esp_timer_create(&timer_args, &starcode_delay_timer); + handling_new_starcodes = true; system_initialized = true; } @@ -169,13 +199,14 @@ bool trigger_star_code(const char* code) { return false; } - // TODO: do display text - // TODO: do delay ticks + processing_starcode = &*it; - if (it->triggered_sem != nullptr) - xSemaphoreGive(it->triggered_sem); - if (it->callback != nullptr) - (it->callback)(); + // print display text + if (processing_starcode->display_text != nullptr) { + lcd_print_header_star_code(); + } + + esp_timer_start_once(starcode_delay_timer, processing_starcode->delay_us); return true; } @@ -190,9 +221,19 @@ void resume_star_code_system() { } void lcd_print_header_star_code() { - if (doing_starcode) { + if (!lcd_header_enabled()) return; + + if (processing_starcode != nullptr) { + if (processing_starcode->display_text != nullptr) { + char buf[STARCODE_MAX_LEN + 2]; + snprintf(buf, sizeof(buf), "%s", processing_starcode->display_text); + lcd_print(1, 1, processing_starcode->display_text); + } else { + lcd_print(1, 1, EMPTY_STAR_CODE_HEADER); + } + } else if (doing_starcode) { char buf[STARCODE_MAX_LEN + 2]; - sprintf(buf, "*%-8s", current); + snprintf(buf, sizeof(buf), "*%-8s", current); lcd_print(1, 1, buf); } else { lcd_print(1, 1, EMPTY_STAR_CODE_HEADER); diff --git a/main/drivers/star_code.h b/main/drivers/star_code.h index e6528c2..409ed6f 100644 --- a/main/drivers/star_code.h +++ b/main/drivers/star_code.h @@ -20,8 +20,8 @@ struct StarCodeEntry { /// /// This must be <= 9 characters. const char* display_text; - /// @brief The number of ticks to delay when the star code is entered before calling the handler. - uint32_t delay_ticks; + /// @brief The number of microseconds to delay when the star code is entered before calling the handler. + uint64_t delay_us; /// @brief The function to call when the star code is entered. /// Can be null. void (*callback)(void); diff --git a/main/drivers/wires.cpp b/main/drivers/wires.cpp index bda11a8..23e1f62 100644 --- a/main/drivers/wires.cpp +++ b/main/drivers/wires.cpp @@ -32,7 +32,7 @@ void init_wires(void) { .scl_pullup_en = GPIO_PULLUP_ENABLE, .master = { .clk_speed = 100000, - } + }, }; gpio_reset_pin(GPIO_NUM_41); diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index a99ca89..75d54fb 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -51,140 +51,140 @@ void step0() { { .code = "9819", .display_text = "Defusal Initiated", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = nullptr, .triggered_sem = nullptr, }, { .code = "59862", .display_text = "Set Up Wires", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = setup_wires, .triggered_sem = nullptr, }, { .code = "59862", .display_text = "Set Game Time", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = set_game_time, .triggered_sem = nullptr, }, { .code = "59863", .display_text = "Debug Switches", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = debug_switches, .triggered_sem = nullptr, }, { .code = "59864", .display_text = "Battery Stats", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = battery_stats, .triggered_sem = nullptr, }, { .code = "59871", .display_text = "Try Step 1", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step1, .triggered_sem = nullptr, }, { .code = "59872", .display_text = "Try Step 2", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step2, .triggered_sem = nullptr, }, { .code = "59873", .display_text = "Try Step 3", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step3, .triggered_sem = nullptr, }, { .code = "59874", .display_text = "Try Step 4", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step4, .triggered_sem = nullptr, }, { .code = "59875", .display_text = "Try Step 5", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step5, .triggered_sem = nullptr, }, { .code = "59876", .display_text = "Try Step 6", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = try_step6, .triggered_sem = nullptr, }, { .code = "59881", .display_text = "Skip To Step 1", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step1, .triggered_sem = nullptr, }, { .code = "59882", .display_text = "Skip To Step 2", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step2, .triggered_sem = nullptr, }, { .code = "59883", .display_text = "Skip To Step 3", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step3, .triggered_sem = nullptr, }, { .code = "59884", .display_text = "Skip To Step 4", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step4, .triggered_sem = nullptr, }, { .code = "59885", .display_text = "Skip To Step 5", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step5, .triggered_sem = nullptr, }, { .code = "59886", .display_text = "Skip To Step 6", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = skip_to_step6, .triggered_sem = nullptr, }, { .code = "1111", .display_text = "Issue Strike", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = issue_strike, .triggered_sem = nullptr, }, { .code = "1112", .display_text = "????", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = flashbang, .triggered_sem = nullptr, }, { .code = "1113", .display_text = "replay_last", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = replay_last, .triggered_sem = nullptr, }, diff --git a/main/steps/step3.cpp b/main/steps/step3.cpp index 03ceeb3..696dc16 100644 --- a/main/steps/step3.cpp +++ b/main/steps/step3.cpp @@ -82,7 +82,7 @@ void step3(void) { StarCodeEntry start_code = { .code = "1642", .display_text = "Starting...", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = nullptr, .triggered_sem = continue_sem, }; diff --git a/main/steps/step4.cpp b/main/steps/step4.cpp index 68178ec..21452fe 100644 --- a/main/steps/step4.cpp +++ b/main/steps/step4.cpp @@ -389,7 +389,7 @@ void step4() { StarCodeEntry start_code = { .code = "3850", .display_text = "Starting...", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = nullptr, .triggered_sem = continue_sem, }; diff --git a/main/steps/step5.cpp b/main/steps/step5.cpp index 9ce7e68..873e7ed 100644 --- a/main/steps/step5.cpp +++ b/main/steps/step5.cpp @@ -187,7 +187,7 @@ void step5(void) { StarCodeEntry start_code = { .code = "2648", .display_text = "Starting...", - .delay_ticks = pdMS_TO_TICKS(2000), + .delay_us = 2'000'000, .callback = nullptr, .triggered_sem = continue_sem, };