From 99a5402e76b783a25f2bdf1d2f360d82b40e8b11 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Mon, 12 Aug 2024 22:40:37 -0500 Subject: [PATCH] tweaks to all steps --- main/drivers/speaker.cpp | 8 ++++-- main/drivers/speaker.h | 2 ++ main/steps/step0.cpp | 7 +++++ main/steps/step1.cpp | 57 ++++++++++++++++++++++++---------------- main/steps/step2.cpp | 3 ++- main/steps/step3.cpp | 17 +++++++----- main/steps/step4.cpp | 50 ++++++++++++++++++++--------------- main/steps/step5.cpp | 2 +- sdkconfig | 2 +- 9 files changed, 93 insertions(+), 55 deletions(-) diff --git a/main/drivers/speaker.cpp b/main/drivers/speaker.cpp index 43d2342..d1807d4 100644 --- a/main/drivers/speaker.cpp +++ b/main/drivers/speaker.cpp @@ -1,6 +1,6 @@ #include "speaker.h" -static i2s_chan_handle_t tx_chan; +i2s_chan_handle_t tx_chan; static const char *TAG = "speaker_driver"; @@ -18,7 +18,11 @@ esp_err_t play_raw(const char *fp) { size_t bytes_read = 0; size_t bytes_written = 0; - ESP_ERROR_CHECK(i2s_channel_enable(tx_chan)); + esp_err_t channel_enable_result = i2s_channel_enable(tx_chan); + ESP_ERROR_CHECK_WITHOUT_ABORT(channel_enable_result); + if (channel_enable_result != ESP_OK) { + return channel_enable_result; + } bytes_read = fread(read_buf, sizeof(uint8_t), AUDIO_BUFFER, fh); for (int i = 0; i < bytes_read; i++) { diff --git a/main/drivers/speaker.h b/main/drivers/speaker.h index 47decb1..0231b25 100644 --- a/main/drivers/speaker.h +++ b/main/drivers/speaker.h @@ -17,6 +17,8 @@ #define SAMPLE_RATE 44100 #define AUDIO_BUFFER 2048 +extern i2s_chan_handle_t tx_chan; + esp_err_t play_raw(const char *fp); void init_speaker(void); void play_example(); diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index c6da0ec..0217f35 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -18,6 +18,7 @@ static void try_step3(void) { clean_bomb(); step3(); } static void try_step4(void) { clean_bomb(); step4(); } static void try_step5(void) { clean_bomb(); step5(); } static void try_step6(void) { clean_bomb(); step6(); } +static void issue_strike(void) { strike("Strike Issued"); } /// Wait for "*9819" void step0(void) { @@ -112,6 +113,12 @@ void step0(void) { .should_exit = true, .callback = skip_to_step6, }, + { + .code = "*1111", + .display_text = "Issue Strike", + .should_exit = false, + .callback = issue_strike, + }, }; int len = sizeof(star_codes)/sizeof(StarCodeHandler); do_star_codes(star_codes, len); diff --git a/main/steps/step1.cpp b/main/steps/step1.cpp index 6734491..5acfaa0 100644 --- a/main/steps/step1.cpp +++ b/main/steps/step1.cpp @@ -38,6 +38,8 @@ static lv_style_t red_text; static lv_style_t yellow_text; static lv_style_t blue_text; +static lv_style_t* last_style = nullptr; + static int switch_leds[] = {0, 0, 0, 0}; static lv_style_t* color_styles[] = { @@ -110,8 +112,6 @@ static void generate_switch_leds(void) { switch_leds[3] = colors[0]; - ESP_LOGI(TAG, "%d, %d, %d, %d", switch_leds[0], switch_leds[1], switch_leds[2], switch_leds[3]); - for (int i = 0; i < 4; i++) { uint8_t* rgb = NEOPIXEL_COLORS[switch_leds[i]]; led_strip_set_pixel(leds, Led::switch1 - i, rgb[0], rgb[1], rgb[2]); @@ -119,19 +119,33 @@ static void generate_switch_leds(void) { led_strip_refresh(leds); } +static void set_text_and_color(char* text_str, int color) { + if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { + ESP_LOGI(TAG, "set_text_and_color: setting text: %s", text_str); + lv_label_set_text(text, text_str); + ESP_LOGI(TAG, "set_text_and_color: set text"); + lv_obj_center(text); + ESP_LOGI(TAG, "set_text_and_color: setting %d", color); + if (last_style != nullptr) { + ESP_LOGI(TAG, "set_text_and_color: removing %d", color); + lv_obj_remove_style(text, last_style, LV_STATE_DEFAULT); + } + ESP_LOGI(TAG, "set_text_and_color: adding %d", color); + lv_obj_add_style(text, color_styles[color], LV_STATE_DEFAULT); + last_style = color_styles[color]; + ESP_LOGI(TAG, "set_text_and_color: style"); + xSemaphoreGive(xGuiSemaphore); + ESP_LOGI(TAG, "set_text_and_color: give"); + } +} + static int generate_part_a(void) { int text_color = zero_to_three(my_gen); int text_idx = zero_to_three(my_gen); char* text_string = COLOR_NAMES[text_idx]; - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_label_set_text(text, text_string); - lv_obj_center(text); - lv_obj_add_style(text, color_styles[text_color], LV_STATE_DEFAULT); - xSemaphoreGive(xGuiSemaphore); - } - + set_text_and_color(text_string, text_color); generate_switch_leds(); // the correct answer is the text color @@ -149,13 +163,7 @@ static int generate_part_b(void) { char* text_string = NUM_NAMES[text_number]; - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_label_set_text(text, text_string); - lv_obj_center(text); - lv_obj_add_style(text, color_styles[text_color], LV_STATE_DEFAULT); - xSemaphoreGive(xGuiSemaphore); - } - + set_text_and_color(text_string, text_color); generate_switch_leds(); // the correct answer is the number @@ -171,12 +179,7 @@ static int generate_part_c(void) { int text_color = zero_to_three(my_gen); char* text_string = "switch"; - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_label_set_text(text, text_string); - lv_obj_center(text); - lv_obj_add_style(text, color_styles[text_color], LV_STATE_DEFAULT); - xSemaphoreGive(xGuiSemaphore); - } + set_text_and_color(text_string, text_color); generate_switch_leds(); // the correct answer is the switch @@ -196,6 +199,13 @@ static int generate_part(void) { return -1; } +static void update_lcd_count(int times) { + char buf[6] = {0}; + sprintf(buf, "%d/15", times); + lcd_set_cursor(&lcd, 14, 1); + lcd_print(&lcd, buf); +} + static bool play_part(uint32_t time) { stop_module_timer(); set_module_time(time); @@ -220,6 +230,7 @@ static bool play_part(uint32_t time) { int times = 0; int correct = generate_part(); + update_lcd_count(times); ButtonKey button; SwitchKey switch_; @@ -231,6 +242,7 @@ static bool play_part(uint32_t time) { times++; if (times >= 15) break; correct = generate_part(); + update_lcd_count(times); } else { strike("Incorrect action"); } @@ -241,6 +253,7 @@ static bool play_part(uint32_t time) { times++; if (times >= 15) break; correct = generate_part(); + update_lcd_count(times); } else { strike("Incorrect action"); } diff --git a/main/steps/step2.cpp b/main/steps/step2.cpp index 62c10b5..b87e8ac 100644 --- a/main/steps/step2.cpp +++ b/main/steps/step2.cpp @@ -193,6 +193,7 @@ bool play_game(int time, int required_score) { clear_board(); generate_block(); + show_board(); ButtonKey button; while (get_pressed_button(&button)); @@ -376,7 +377,7 @@ static void generate_block(void) { piece = new_piece; piece_rotation = 0; - piece_location[0] = 20; + piece_location[0] = 18; piece_location[1] = 4; get_node_locations(); diff --git a/main/steps/step3.cpp b/main/steps/step3.cpp index 89f3f41..f413a40 100644 --- a/main/steps/step3.cpp +++ b/main/steps/step3.cpp @@ -41,13 +41,16 @@ std::vector unique_values(std::vector& input_options, int num) { } void step3(void) { - set_module_time(20000); - start_module_timer(); - - // wait - while (get_module_time() > 0) { - vTaskDelay(10); - } + StarCodeHandler star_codes[] = { + { + .code = "*2648", + .display_text = "Starting...", + .should_exit = true, + .callback = nullptr, + }, + }; + int len = sizeof(star_codes)/sizeof(StarCodeHandler); + do_star_codes(star_codes, len); std::vector all_leds; diff --git a/main/steps/step4.cpp b/main/steps/step4.cpp index 37924df..96e31ae 100644 --- a/main/steps/step4.cpp +++ b/main/steps/step4.cpp @@ -2,7 +2,7 @@ #define ONE_SECOND_TIME 90'000 #define THREE_SECOND_TIME 90'000 -#define SIX_SECOND_TIME 90'000 +#define SIX_SECOND_TIME 75'000 static const char *TAG = "step4"; @@ -23,7 +23,7 @@ static const char* LCD_STRINGS[] = { "nothing", "", "a word", - "not something", + "somethink", "what?", "LCD", "display", @@ -61,17 +61,23 @@ static bool three_second(); static bool six_second(); void step4(void) { - // start counting down module timer immediatly - set_module_time(30'000); - start_module_timer(); - - while (get_module_time() > 0) vTaskDelay(pdMS_TO_TICKS(100)); + StarCodeHandler star_codes[] = { + { + .code = "*1642", + .display_text = "Starting...", + .should_exit = true, + .callback = nullptr, + }, + }; + int len = sizeof(star_codes)/sizeof(StarCodeHandler); + do_star_codes(star_codes, len); while (times < 4) { tone = tone_dist(gen); // tone = 2; while (get_pressed_button(nullptr)) vTaskDelay(pdMS_TO_TICKS(10)); + play_raw(MOUNT_POINT "/que.pcm"); play_raw(TONE_FILES[tone]); bool correct = false; @@ -90,6 +96,8 @@ void step4(void) { times++; clean_bomb(); play_raw(MOUNT_POINT "/correct.pcm"); + } else { + vTaskDelay(pdMS_TO_TICKS(1500)); } vTaskDelay(pdMS_TO_TICKS(3000)); } @@ -179,6 +187,17 @@ static void debug_actual_values(uint8_t buttons, uint8_t switch_) { ESP_LOGI(TAG, ""); } +static void wait_for_timer(void) { + KeypadKey key; + while (get_module_time() > 0) { + if (get_pressed_keypad(&key) && key == KeypadKey::kd) { + set_module_time(0); + return; + } + vTaskDelay(pdMS_TO_TICKS(100)); + } +} + static bool one_second() { clean_bomb(); set_module_time(ONE_SECOND_TIME); @@ -220,10 +239,7 @@ static bool one_second() { debug_correct_values(correct_buttons, correct_button_mask, correct_switches); - // wait for timer - while (get_module_time() > 0) { - vTaskDelay(pdMS_TO_TICKS(100)); - } + wait_for_timer(); debug_actual_values(get_button_state(), get_switch_state()); @@ -254,8 +270,6 @@ static bool three_second() { bool was_high = (tone / 3) == 1; - - rng_leds(); write_leds(); int red_led_count = 0; @@ -289,10 +303,7 @@ static bool three_second() { debug_correct_values(correct_buttons, correct_button_mask, correct_switches); - // wait for timer - while (get_module_time() > 0) { - vTaskDelay(pdMS_TO_TICKS(100)); - } + wait_for_timer(); debug_actual_values(get_button_state(), get_switch_state()); @@ -370,10 +381,7 @@ static bool six_second() { debug_correct_values(correct_buttons, correct_button_mask, correct_switches); - // wait for timer - while (get_module_time() > 0) { - vTaskDelay(pdMS_TO_TICKS(100)); - } + wait_for_timer(); debug_actual_values(get_button_state(), get_switch_state()); diff --git a/main/steps/step5.cpp b/main/steps/step5.cpp index a6892e9..2ba0d23 100644 --- a/main/steps/step5.cpp +++ b/main/steps/step5.cpp @@ -90,7 +90,7 @@ void step5(void) { new_puzzle(); int strike_time = xTaskGetTickCount(); bool striked = false; - while(solved_times < 3) { + while(solved_times < 4) { // for every bit in the answer- set_module_sseg_raw(display_map); if (get_pressed_keypad(&key)) { diff --git a/sdkconfig b/sdkconfig index 0017745..d4ddecc 100644 --- a/sdkconfig +++ b/sdkconfig @@ -1212,7 +1212,7 @@ CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1 # Port # CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y -# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set +CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y # CONFIG_FREERTOS_TASK_PRE_DELETION_HOOK is not set # CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set