From 72ff92b4441d98c260333f9c99cd4fd9030513f4 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Thu, 5 Jun 2025 16:48:03 -0500 Subject: [PATCH] update steps 3 & 5 --- main/drivers/all.h | 14 +- main/drivers/leds.h | 1 + main/drivers/star_code.cpp | 43 +++++- main/drivers/star_code.h | 29 +++- main/helper.h | 8 -- main/steps/step0.cpp | 273 +++++++++++++++++++------------------ main/steps/step0.h | 7 +- main/steps/step1.h | 9 +- main/steps/step2.h | 6 +- main/steps/step3.cpp | 33 +++-- main/steps/step3.h | 7 +- main/steps/step4.h | 7 +- main/steps/step5.cpp | 20 ++- main/steps/step5.h | 6 +- main/steps/step6.h | 6 +- 15 files changed, 255 insertions(+), 214 deletions(-) diff --git a/main/drivers/all.h b/main/drivers/all.h index 863a28e..5db7c9a 100644 --- a/main/drivers/all.h +++ b/main/drivers/all.h @@ -1,14 +1,18 @@ #ifndef ALL_H #define ALL_H -#include "char_lcd.h" #include "bottom_half.h" +#include "char_lcd.h" +#include "game_timer.h" +#include "leds.h" +#include "power.h" #include "sd.h" #include "speaker.h" -#include "game_timer.h" -#include "drivers/tft.h" -#include "drivers/leds.h" -#include "drivers/power.h" +#include "sseg.h" +#include "star_code.h" +#include "state_tracking.h" +#include "tft.h" +#include "wires.h" void init_drivers(); diff --git a/main/drivers/leds.h b/main/drivers/leds.h index b0c6868..4f62cfe 100644 --- a/main/drivers/leds.h +++ b/main/drivers/leds.h @@ -26,6 +26,7 @@ enum LEDColor: uint32_t { LED_COLOR_WHITE_STRONG = 0xFF'FF'FF, }; +// TODO: sepperate the indicator leds from the shape display. enum IndicatorLED { LED_SHAPE1 = 0u, LED_SHAPE2 = 1u, diff --git a/main/drivers/star_code.cpp b/main/drivers/star_code.cpp index 7ddfba1..82fb197 100644 --- a/main/drivers/star_code.cpp +++ b/main/drivers/star_code.cpp @@ -4,6 +4,9 @@ #include #include #include +#include + +static const char* TAG = "star_code"; static std::vector star_codes; @@ -40,6 +43,7 @@ bool add_star_code(StarCodeEntry code) { if (it != star_codes.end()) { // existing star code found! + ESP_LOGW(TAG, "Failed to add star code %s", code.code); return false; } @@ -47,12 +51,23 @@ bool add_star_code(StarCodeEntry code) { return true; } -bool rm_star_code(char* code){ +bool add_star_codes(const StarCodeEntry* codes, size_t len) { + bool success = true; + for (int i = 0; i < len; i++) { + if (!add_star_code(codes[i])) { + success = false; + } + } + return success; +} + +bool rm_star_code(const char* code){ auto it = std::find_if(star_codes.begin(), star_codes.end(), [&](const StarCodeEntry& star_code) { return strcmp(code, star_code.code) == 0; }); if (it == star_codes.end()) { + ESP_LOGW(TAG, "Failed to remove star code %s", code); return false; } @@ -60,7 +75,31 @@ bool rm_star_code(char* code){ return true; } -bool trigger_star_code(char* code) { +bool rm_star_codes(const StarCodeEntry* codes, size_t len) { + bool success = true; + for (int i = 0; i < len; i++) { + if (!rm_star_code(codes[i].code)) { + success = false; + } + } + return success; +} + +bool rm_star_codes_str(const char** codes, size_t len) { + bool success = true; + for (int i = 0; i < len; i++) { + if (!rm_star_code(codes[i])) { + success = false; + } + } + return success; +} + +void clear_star_codes() { + star_codes.clear(); +} + +bool trigger_star_code(const char* code) { auto it = std::find_if(star_codes.begin(), star_codes.end(), [&](const StarCodeEntry& other) { return check_code_match(code, other.code); }); diff --git a/main/drivers/star_code.h b/main/drivers/star_code.h index e4d09e3..285550e 100644 --- a/main/drivers/star_code.h +++ b/main/drivers/star_code.h @@ -22,18 +22,39 @@ struct StarCodeEntry { }; /// @brief Adds a star code to be handled. -/// @param code the star code to add (without the *) +/// @param code the star code to add /// @return true iff the star code was added bool add_star_code(StarCodeEntry code); +/// @brief Adds a list of star codes to be handled. +/// @param codes the list of star codes to add +/// @param len the length of the list +/// @return true iff all the star codes were added +bool add_star_codes(const StarCodeEntry* codes, size_t len); + /// @brief removes a star code to stop handling it. -/// @param code the star code to remove (without the *) +/// @param code the star code to remove /// @return true iff the star code was removed -bool rm_star_code(char* code); +bool rm_star_code(const char* code); + +/// @brief removes all given star codes to stop handling them. +/// @param codes the list of star codes to remove +/// @param len the length of the list +/// @return true iff all star codes were removed +bool rm_star_codes(const StarCodeEntry* codes, size_t len); + +/// @brief removes all given star codes to stop handling them. +/// @param codes the list of star codes to remove +/// @param len the length of the list +/// @return true iff all star codes were removed +bool rm_star_codes_str(const char** codes, size_t len); + +/// @brief clears all star codes. +void clear_star_codes(); /// @brief Triggers the given starcode. /// @param code the star code to trigger (without the *) /// @return true iff a star code was triggered -bool trigger_star_code(char* code); +bool trigger_star_code(const char* code); #endif /* STAR_CODE_H */ \ No newline at end of file diff --git a/main/helper.h b/main/helper.h index ec27789..fccf4f5 100644 --- a/main/helper.h +++ b/main/helper.h @@ -10,19 +10,11 @@ #include "drivers/speaker.h" #include "drivers/tft.h" -struct StarCodeHandler { - const char* code; - const char* display_text; - bool should_exit; - void (*callback)(void); -}; - // TODO: add something for RNG. /// Clears most persistant bomb state void clean_bomb(void); void poster_child_task(void* arg); -void do_star_codes(StarCodeHandler* star_codes, int star_codes_len); void display_game_results(); #endif /* HELPER_H */ diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index 415fa4d..b8b90b7 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -6,13 +6,15 @@ static const char* TAG = "step0"; extern uint32_t initial_game_time; extern uint32_t skip_to_step; +static SemaphoreHandle_t continue_sem; + static void set_game_time(); -static void skip_to_step1() { skip_to_step = 1; } -static void skip_to_step2() { skip_to_step = 2; } -static void skip_to_step3() { skip_to_step = 3; } -static void skip_to_step4() { skip_to_step = 4; } -static void skip_to_step5() { skip_to_step = 5; } -static void skip_to_step6() { skip_to_step = 6; } +static void skip_to_step1() { skip_to_step = 1; xSemaphoreGive(continue_sem, portMAX_DELAY); } +static void skip_to_step2() { skip_to_step = 2; xSemaphoreGive(continue_sem, portMAX_DELAY); } +static void skip_to_step3() { skip_to_step = 3; xSemaphoreGive(continue_sem, portMAX_DELAY); } +static void skip_to_step4() { skip_to_step = 4; xSemaphoreGive(continue_sem, portMAX_DELAY); } +static void skip_to_step5() { skip_to_step = 5; xSemaphoreGive(continue_sem, portMAX_DELAY); } +static void skip_to_step6() { skip_to_step = 6; xSemaphoreGive(continue_sem, portMAX_DELAY); } static void try_step1() { clean_bomb(); step1(); } static void try_step2() { clean_bomb(); step2(); } static void try_step3() { clean_bomb(); step3(); } @@ -45,138 +47,145 @@ static void replay_last() { start_playback(); } -/// Wait for "*9819" +static const StarCodeEntry star_codes[] = { + { + .code = "9819", + .display_text = "Defusal Initiated", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = nullptr, + }, + { + .code = "*9861", + .display_text = "Set Up Wires", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = setup_wires, + }, + { + .code = "59862", + .display_text = "Set Game Time", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = set_game_time, + }, + { + .code = "59863", + .display_text = "Debug Switches", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = debug_switches, + }, + { + .code = "59864", + .display_text = "Battery Stats", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = battery_stats, + }, + { + .code = "59871", + .display_text = "Try Step 1", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step1, + }, + { + .code = "59872", + .display_text = "Try Step 2", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step2, + }, + { + .code = "59873", + .display_text = "Try Step 3", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step3, + }, + { + .code = "59874", + .display_text = "Try Step 4", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step4, + }, + { + .code = "59875", + .display_text = "Try Step 5", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step5, + }, + { + .code = "59876", + .display_text = "Try Step 6", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = try_step6, + }, + { + .code = "59881", + .display_text = "Skip To Step 1", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step1, + }, + { + .code = "59882", + .display_text = "Skip To Step 2", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step2, + }, + { + .code = "59883", + .display_text = "Skip To Step 3", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step3, + }, + { + .code = "59884", + .display_text = "Skip To Step 4", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step4, + }, + { + .code = "59885", + .display_text = "Skip To Step 5", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step5, + }, + { + .code = "59886", + .display_text = "Skip To Step 6", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = skip_to_step6, + }, + { + .code = "1111", + .display_text = "Issue Strike", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = issue_strike, + }, + { + .code = "1112", + .display_text = "????", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = flashbang, + }, + { + .code = "1113", + .display_text = "replay_last", + .delay_ticks = pdMS_TO_TICKS(2000), + .callback = replay_last, + }, +}; +const static size_t len = sizeof(star_codes)/sizeof(star_codes[0]); + void step0() { + uint8_t _byte; + continue_sem = xSemaphoreCreateBinary(); + if (continue_sem == nullptr) { + ESP_FAIL("could not create semaphore"); + } + led_set(IndicatorLED::LED_SPEAKER, LEDColor::LED_COLOR_BLUE); leds_flush(); - StarCodeHandler star_codes[] = { - { - .code = "*9819", - .display_text = "Defusal Initiated", - .should_exit = true, - .callback = nullptr, - }, - { - .code = "*59861", - .display_text = "Set Up Wires", - .should_exit = false, - .callback = setup_wires, - }, - { - .code = "*59862", - .display_text = "Set Game Time", - .should_exit = false, - .callback = set_game_time, - }, - { - .code = "*59863", - .display_text = "Debug Switches", - .should_exit = false, - .callback = debug_switches, - }, - { - .code = "*59864", - .display_text = "Battery Stats", - .should_exit = false, - .callback = battery_stats, - }, - { - .code = "*59871", - .display_text = "Try Step 1", - .should_exit = false, - .callback = try_step1, - }, - { - .code = "*59872", - .display_text = "Try Step 2", - .should_exit = false, - .callback = try_step2, - }, - { - .code = "*59873", - .display_text = "Try Step 3", - .should_exit = false, - .callback = try_step3, - }, - { - .code = "*59874", - .display_text = "Try Step 4", - .should_exit = false, - .callback = try_step4, - }, - { - .code = "*59875", - .display_text = "Try Step 5", - .should_exit = false, - .callback = try_step5, - }, - { - .code = "*59876", - .display_text = "Try Step 6", - .should_exit = false, - .callback = try_step6, - }, - { - .code = "*59881", - .display_text = "Skip To Step 1", - .should_exit = true, - .callback = skip_to_step1, - }, - { - .code = "*59882", - .display_text = "Skip To Step 2", - .should_exit = true, - .callback = skip_to_step2, - }, - { - .code = "*59883", - .display_text = "Skip To Step 3", - .should_exit = true, - .callback = skip_to_step3, - }, - { - .code = "*59884", - .display_text = "Skip To Step 4", - .should_exit = true, - .callback = skip_to_step4, - }, - { - .code = "*59885", - .display_text = "Skip To Step 5", - .should_exit = true, - .callback = skip_to_step5, - }, - { - .code = "*59886", - .display_text = "Skip To Step 6", - .should_exit = true, - .callback = skip_to_step6, - }, - { - .code = "*1111", - .display_text = "Issue Strike", - .should_exit = false, - .callback = issue_strike, - }, - { - .code = "*1112", - .display_text = "????", - .should_exit = false, - .callback = flashbang, - }, - { - .code = "*1113", - .display_text = "replay_last", - .should_exit = false, - .callback = replay_last, - }, - }; - int len = sizeof(star_codes)/sizeof(StarCodeHandler); - do_star_codes(star_codes, len); + add_star_codes(star_codes, len); + xSemaphoreTake(continue_sem, portMAX_DELAY); + rm_star_codes(star_codes, len); } - static const int CURSOR_POS_MAP[5] = {1, 3, 4, 6, 7}; static char str_buf[18] = {0}; static void _update_display(uint8_t* digits, uint8_t cursor_pos) { diff --git a/main/steps/step0.h b/main/steps/step0.h index 66aa431..65591f1 100644 --- a/main/steps/step0.h +++ b/main/steps/step0.h @@ -1,12 +1,7 @@ #ifndef STEP_0_H #define STEP_0_H -#include "../drivers/bottom_half.h" -#include "../drivers/char_lcd.h" -#include "../drivers/wires.h" -#include "../drivers/power.h" -#include "setup_wires.h" -#include "../helper.h" +#include "../drivers/all.h" #include "step1.h" #include "step2.h" diff --git a/main/steps/step1.h b/main/steps/step1.h index 07e827c..81363eb 100644 --- a/main/steps/step1.h +++ b/main/steps/step1.h @@ -2,13 +2,8 @@ #define STEP_1_H #include -#include "../drivers/bottom_half.h" -#include "../drivers/tft.h" -#include "../drivers/leds.h" -#include "../drivers/wires.h" -#include "../drivers/speaker.h" -#include "../drivers/game_timer.h" -#include "../drivers/char_lcd.h" +#include "../drivers/all.h" +#include "../helper.h" void step1(void); diff --git a/main/steps/step2.h b/main/steps/step2.h index 23c630c..54cadca 100644 --- a/main/steps/step2.h +++ b/main/steps/step2.h @@ -1,11 +1,7 @@ #ifndef STEP_2_H #define STEP_2_H -#include "../drivers/bottom_half.h" -#include "../drivers/wires.h" -#include "../drivers/game_timer.h" -#include "../drivers/leds.h" -#include "../drivers/speaker.h" +#include "../drivers/all.h" #include "../helper.h" #include #include diff --git a/main/steps/step3.cpp b/main/steps/step3.cpp index 49f0e78..bdc6c83 100644 --- a/main/steps/step3.cpp +++ b/main/steps/step3.cpp @@ -21,6 +21,8 @@ static const char* TONE_FILES[] = { MOUNT_POINT "/high-6.wav", }; +static const size_t LCD_STRING_SOMETHING = 0; +static const size_t LCD_STRING_NOTHING = 1; static const char* LCD_STRINGS[] = { "something", "nothing", @@ -49,13 +51,20 @@ static std::uniform_int_distribution<> lcd_rand_char_dist(0, sizeof(lcd_random_c static std::uniform_int_distribution<> has_coconut_dist(0, 2); static std::uniform_int_distribution<> coconut_position_dist(0, 13); +const static uint32_t NEOPIXEL_COLOR_IDX_RED = 0; +const static uint32_t NEOPIXEL_COLOR_IDX_YELLOW = 1; +const static uint32_t NEOPIXEL_COLOR_IDX_GREEN = 2; +const static uint32_t NEOPIXEL_COLOR_IDX_BLUE = 3; +const static uint32_t NEOPIXEL_COLOR_IDX_PINK = 4; +const static uint32_t NEOPIXEL_COLOR_IDX_WHITE = 5; +const static uint32_t NEOPIXEL_COLOR_IDX_OFF = 6; static uint32_t NEOPIXEL_COLORS[7] = { LEDColor::LED_COLOR_RED, - LEDColor::LED_COLOR_ORANGE, LEDColor::LED_COLOR_YELLOW, LEDColor::LED_COLOR_GREEN, LEDColor::LED_COLOR_BLUE, LEDColor::LED_COLOR_PINK, + LEDColor::LED_COLOR_WHITE, LEDColor::LED_COLOR_OFF, }; @@ -82,7 +91,7 @@ void step3(void) { play_clip_wav(MOUNT_POINT "/ready.wav", true, false, 3, 0); // The high pitched tones need to be scaled down by 3 more - play_clip_wav(TONE_FILES[tone], false, false, 2 + (tone/3) * 3, 0); + play_clip_wav(TONE_FILES[tone], false, false, 1 + (tone/3) * 4, 0); bool correct = false; switch (tone % 3) { @@ -221,16 +230,16 @@ static bool one_second() { int red_led_count = 0; int blue_led_count = 0; for (int i = 0; i < LED_COUNT; i++) { - if (indicator_led_idxs[i] == 0) { + if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_RED) { red_led_count++; - } else if (indicator_led_idxs[i] == 4) { + } else if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_BLUE) { blue_led_count++; } } uint8_t correct_switches = four_bit_flag( - speaker_color == 0 || speaker_color == 1 || speaker_color == 2, - lcd_string_idx == 0 || lcd_string_idx == 1, + speaker_color == NEOPIXEL_COLOR_IDX_RED || speaker_color == NEOPIXEL_COLOR_IDX_YELLOW || speaker_color == NEOPIXEL_COLOR_IDX_PINK, + lcd_string_idx == LCD_STRING_SOMETHING || lcd_string_idx == LCD_STRING_NOTHING, was_high, !was_high ); @@ -280,9 +289,9 @@ static bool three_second() { int red_led_count = 0; int blue_led_count = 0; for (int i = 0; i < LED_COUNT; i++) { - if (indicator_led_idxs[i] == 0) { + if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_RED) { red_led_count++; - } else if (indicator_led_idxs[i] == 4) { + } else if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_BLUE) { blue_led_count++; } } @@ -345,7 +354,7 @@ static bool six_second() { bool was_high = (tone / 3) == 1; - bool second_switch_correct_state = (indicator_led_idxs[IndicatorLED::LED_S2] == 0) || (indicator_led_idxs[IndicatorLED::LED_S2] == 6); + bool second_switch_correct_state = (indicator_led_idxs[IndicatorLED::LED_S2] == NEOPIXEL_COLOR_IDX_RED) || (indicator_led_idxs[IndicatorLED::LED_S2] == NEOPIXEL_COLOR_IDX_OFF); second_switch_correct_state = second_switch_correct_state || was_high; rng_leds(); @@ -354,16 +363,16 @@ static bool six_second() { int green_led_count = 0; int blue_led_count = 0; for (int i = 0; i < LED_COUNT; i++) { - if (indicator_led_idxs[i] == 4) { + if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_BLUE) { blue_led_count++; - } else if (indicator_led_idxs[i] == 3) { + } else if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_GREEN) { green_led_count++; } } int pink_led_on_bottom_count = 0; for (int i = IndicatorLED::LED_RFID; i < LED_COUNT; i++) { - if (indicator_led_idxs[i] == 5) { + if (indicator_led_idxs[i] == NEOPIXEL_COLOR_IDX_PINK) { pink_led_on_bottom_count++; } } diff --git a/main/steps/step3.h b/main/steps/step3.h index cdb5b60..06b0f52 100644 --- a/main/steps/step3.h +++ b/main/steps/step3.h @@ -2,12 +2,7 @@ #define STEP_3_H #include -#include "../drivers/bottom_half.h" -#include "../drivers/wires.h" -#include "../drivers/speaker.h" -#include "../drivers/leds.h" -#include "../drivers/char_lcd.h" -#include "../drivers/game_timer.h" +#include "../drivers/all.h" #include "../helper.h" void step3(void); diff --git a/main/steps/step4.h b/main/steps/step4.h index ba711b1..165da7d 100644 --- a/main/steps/step4.h +++ b/main/steps/step4.h @@ -2,12 +2,7 @@ #define STEP_4_H #include -#include "../drivers/tft.h" -#include "../drivers/speaker.h" -#include "../drivers/bottom_half.h" -#include "../drivers/game_timer.h" -#include "../drivers/wires.h" -#include "../drivers/char_lcd.h" +#include "../drivers/all.h" #include "../helper.h" #define TETRIS_USE_FLASH_IMG diff --git a/main/steps/step5.cpp b/main/steps/step5.cpp index 611f790..9c05175 100644 --- a/main/steps/step5.cpp +++ b/main/steps/step5.cpp @@ -179,16 +179,13 @@ bool submit_6(bool* buttons_cycling, bool button_turned_on, int led_off) { } void step5(void) { - StarCodeHandler star_codes[] = { - { - .code = "*2648", - .display_text = "Starting...", - .should_exit = true, - .callback = nullptr, - }, + StarCodeEntry star_code = { + .code = "*2648", + .display_text = "Starting...", + .should_exit = true, + .callback = nullptr, }; - int len = sizeof(star_codes)/sizeof(StarCodeHandler); - do_star_codes(star_codes, len); + add_star_code(star_code); std::vector all_leds; @@ -226,7 +223,6 @@ void step5(void) { bool solved_correctly = false; int puzzle = puzzle_dist(gen); - // int puzzle = 2; switch (puzzle) { case 0: { @@ -900,13 +896,13 @@ void step5(void) { stop_module_timer(); if (solved_correctly) { solved_puzzles++; - play_clip_wav(MOUNT_POINT "/correct.wav", true, false, 3, 0); + play_clip_wav(MOUNT_POINT "/partdone.wav", true, false, 0, 0); vTaskDelay(pdMS_TO_TICKS(500)); solved_correctly = false; } else { vTaskDelay(pdMS_TO_TICKS(3000)); } - clear_all_pressed_released(); + play_clip_wav(MOUNT_POINT "/stepdone.wav", true, false, 1, 0); clean_bomb(); } } diff --git a/main/steps/step5.h b/main/steps/step5.h index 10b1c06..ef15a20 100644 --- a/main/steps/step5.h +++ b/main/steps/step5.h @@ -1,11 +1,7 @@ #ifndef STEP_5_H #define STEP_5_H -#include "../drivers/bottom_half.h" -#include "../drivers/game_timer.h" -#include "../drivers/char_lcd.h" -#include "../drivers/leds.h" -#include "../drivers/wires.h" +#include "../drivers/all.h" #include "../helper.h" #include #include diff --git a/main/steps/step6.h b/main/steps/step6.h index e6eeee9..7e8711b 100644 --- a/main/steps/step6.h +++ b/main/steps/step6.h @@ -2,10 +2,8 @@ #define STEP_6_H #include "wires_puzzle.h" -#include "drivers/wires.h" -#include "drivers/bottom_half.h" -#include "drivers/sd.h" -#include "drivers/speaker.h" +#include "../drivers/all.h" +#include "../helper.h" void step6(void);