From ca6f03c42b157ed3d29ef74c2b5f059f543804d1 Mon Sep 17 00:00:00 2001 From: drake Date: Sun, 11 Aug 2024 16:25:39 -0500 Subject: [PATCH] step 3 polishing --- main/steps/step3.cpp | 543 ++++++++++++++++++------------------------- main/steps/step3.h | 1 + 2 files changed, 222 insertions(+), 322 deletions(-) diff --git a/main/steps/step3.cpp b/main/steps/step3.cpp index 6d501fd..da1e916 100644 --- a/main/steps/step3.cpp +++ b/main/steps/step3.cpp @@ -8,65 +8,77 @@ static std::uniform_int_distribution<> puzzle_dist(0, 7); static std::uniform_int_distribution<> led_picker_dist(0, 20); static std::uniform_int_distribution<> led_color_dist(0, 5); -static const int INDICATOR_RED[6] = {30, 0, 0, 25, 15, 10}; -static const int INDICATOR_GREEN[6] = {0, 0, 30, 5, 0, 10}; -static const int INDICATOR_BLUE[6] = {0, 30, 0, 0, 15, 10}; +static const int INDICATOR_RED[6] = {20, 0, 0, 10, 10, 5}; +static const int INDICATOR_GREEN[6] = {0, 0, 10, 5, 0, 5}; +static const int INDICATOR_BLUE[6] = {0, 10, 0, 0, 5, 5}; + +void set_unique_leds(std::vector& input_options, const int num, const int r, const int g, const int b) { + for (int i = 0; i < num; i++) { + std::uniform_int_distribution<> led_option_dist(0, input_options.size() - 1); + int led = led_option_dist(gen); + ESP_ERROR_CHECK(led_strip_set_pixel(leds, input_options[led], r, g, b)); + input_options.erase(input_options.begin() + led); + } +} + +void set_unique_leds_random_color(std::vector& input_options, const int num, const int* r, const int* g, const int* b) { + for (int i = 0; i < num; i++) { + std::uniform_int_distribution<> led_option_dist(0, input_options.size() - 1); + int led = led_option_dist(gen); + + std::uniform_int_distribution<> led_color_dist(0, sizeof(r) - 1); + int color = led_color_dist(gen); + ESP_ERROR_CHECK(led_strip_set_pixel(leds, input_options[led], r[color], g[color], b[color])); + input_options.erase(input_options.begin() + led); + } +} + +std::vector unique_values(std::vector& input_options, int num) { + std::vector output_vec; + for (int i = 0; i < num; i++) { + std::uniform_int_distribution<> option_num_dist(0, input_options.size() - 1); + int option_num = option_num_dist(gen); + output_vec.push_back(input_options[option_num]); + input_options.erase(input_options.begin() + option_num); + } + return output_vec; +} void step3(void) { + std::vector all_leds; + + for (int i = 0; i < 21; i++) { + all_leds.push_back(i); + } + + int solved_puzzles = 0; while (solved_puzzles < 3) { lcd_set_cursor(&lcd, 1, 1); int puzzle = puzzle_dist(gen); // int puzzle = 8; + + bool solved_correctly = false; switch (puzzle) { case 0: { lcd_print(&lcd, "Clear"); set_module_time(15000); start_module_timer(); - std::uniform_int_distribution<> green_indicators_dist(2, 15); + std::vector indicator_options = all_leds; - // create all green indicators + // set green + std::uniform_int_distribution<> green_indicators_dist(1, 15); uint8_t green_indicators = green_indicators_dist(gen); - std::set indicators; + set_unique_leds(indicator_options, green_indicators, 0, 10, 0); - // ESP_LOGI(TAG, "green indicators: %i", green_indicators); - - // ***CHANGE TO PUZZLE 5 METHOD*** - while (indicators.size() < green_indicators) { - int led = led_picker_dist(gen); - indicators.insert(led); - // ESP_LOGI(TAG, "added green led at %i", led); - } - - for (std::set::iterator it = indicators.begin(); it != indicators.end(); it++) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, 0, 10, 0)); - } - - // add non-green indicators + // set non-green const int NON_GREEN_INDICATOR_RED[3] = {30, 0, 15}; + const int NON_GREEN_INDICATOR_GREEN[3] = {0, 0, 0}; const int NON_GREEN_INDICATOR_BLUE[3] = {0, 30, 15}; - std::uniform_int_distribution<> non_green_indicator_color_dist(0, 2); - - std::uniform_int_distribution<> remaining_indicators_dist(0, 20 - green_indicators); - int non_green_indicators = remaining_indicators_dist(gen); - std::set remaining_indicators; - - // ESP_LOGI(TAG, "non-green indicators: %i", non_green_indicators); - - while (remaining_indicators.size() < non_green_indicators) { - int led_attempt = led_picker_dist(gen); - if (indicators.find(led_attempt) == indicators.end()) { - remaining_indicators.insert(led_attempt); - // ESP_LOGI(TAG, "added non-green led at %i", led_attempt); - } - } - - for (std::set::iterator it = remaining_indicators.begin(); it != remaining_indicators.end(); it++) { - int color = non_green_indicator_color_dist(gen); - ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, NON_GREEN_INDICATOR_RED[color], 0, NON_GREEN_INDICATOR_BLUE[color])); - } + std::uniform_int_distribution<> non_green_indicators_dist(0, (20 - green_indicators)); + set_unique_leds_random_color(indicator_options, non_green_indicators_dist(gen), NON_GREEN_INDICATOR_RED, NON_GREEN_INDICATOR_GREEN, NON_GREEN_INDICATOR_BLUE); ESP_ERROR_CHECK(led_strip_refresh(leds)); @@ -94,23 +106,18 @@ void step3(void) { } case 1: { lcd_print(&lcd, "Blank"); - set_module_time(20000); + set_module_time(30000); start_module_timer(); std::uniform_int_distribution<> on_indicators_dist(16, 21); - uint8_t indicators_num = on_indicators_dist(gen); - std::set indicators; - while (indicators.size() < indicators_num) { - indicators.insert(led_picker_dist(gen)); - } - - for (std::set::iterator it = indicators.begin(); it != indicators.end(); it++) { - int color = led_color_dist(gen); - ESP_ERROR_CHECK(led_strip_set_pixel(leds, *it, INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color])); - } - ESP_ERROR_CHECK(led_strip_refresh(leds)); + std::vector indicator_options = all_leds; + const int INDICATOR_RED[6] = {20, 0, 0, 10, 10, 5}; + const int INDICATOR_GREEN[6] = {0, 0, 10, 5, 0, 5}; + const int INDICATOR_BLUE[6] = {0, 10, 0, 0, 5, 5}; + + set_unique_leds_random_color(indicator_options, indicators_num, INDICATOR_RED, INDICATOR_BLUE, INDICATOR_GREEN); // ESP_LOGI(TAG, "puzzle 1 LEDs set (%i leds)", indicators_num); if (indicators_num < 18) { @@ -139,6 +146,7 @@ void step3(void) { if (starting_switch_state == get_switch_state()) { if (pressed_keys == "ADCB") { solved_puzzles++; + solved_correctly = true; // ESP_LOGI(TAG, "puzzle 1 solved (keypad)!"); } } else { @@ -155,12 +163,11 @@ void step3(void) { break; } case 2: { - set_module_time(20000); + set_module_time(30000); start_module_timer(); std::uniform_int_distribution<> lit_led_dist(0, 1); bool rfid_lit = lit_led_dist(gen); - // ESP_LOGI(TAG, "rfid lit: %i", rfid_lit); bool lcd_lit = lit_led_dist(gen); bool speaker_lit = lit_led_dist(gen); bool keypad_lit = lit_led_dist(gen); @@ -269,6 +276,7 @@ void step3(void) { if (rfid_correct && lcd_correct && speaker_correct && keypad_correct && tft_correct) { solved_puzzles++; + solved_correctly = true; } else { strike("Final state was not correct! (step 3, puzzle 2)"); } @@ -281,7 +289,7 @@ void step3(void) { } case 3: { lcd_print(&lcd, "Nothing"); - set_module_time(10000); + set_module_time(20000); start_module_timer(); const int COLOR_RED[4] = {0, 20, 10, 0}; @@ -324,6 +332,7 @@ void step3(void) { strike("Wrong button pressed! (step 3, puzzle 3)"); } else { solved_puzzles++; + solved_correctly = true; } break; } @@ -342,7 +351,7 @@ void step3(void) { } case 4: { lcd_print(&lcd, "Blink"); - set_module_time(25000); + set_module_time(30000); start_module_timer(); // buttons @@ -453,6 +462,7 @@ void step3(void) { if (correct) { solved_puzzles++; + solved_correctly = true; } else { strike("Wrong switch state! (step 3, puzzle 4)"); } @@ -469,53 +479,26 @@ void step3(void) { } case 5: { lcd_print(&lcd, "Ummm"); - set_module_time(20000); + set_module_time(30000); start_module_timer(); std::uniform_int_distribution<> indicator_number_dist(0, 5); - uint8_t green_indicators = indicator_number_dist(gen); - uint8_t red_indicators = indicator_number_dist(gen); - uint8_t yellow_indicators = indicator_number_dist(gen); - uint8_t blue_indicators = indicator_number_dist(gen); - // ESP_LOGI(TAG, "Green: %i, Red: %i, Yellow: %i, Blue: %i", green_indicators, red_indicators, yellow_indicators, blue_indicators); + const int INDICATOR_RED[4] = {0, 20, 10, 10}; + const int INDICATOR_GREEN[4] = {10, 0, 5, 5}; + const int INDICATOR_BLUE[4] = {0, 0, 0, 10}; - std::set indicators; + // green, red, yellow, blue + std::array indicator_numbers = {indicator_number_dist(gen), indicator_number_dist(gen), indicator_number_dist(gen), indicator_number_dist(gen)}; + std::vector indicator_options = all_leds; - while (indicators.size() < green_indicators) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 10, 0)); - } - } - - while (indicators.size() < (green_indicators + red_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 15, 0, 0)); - } - } - - while (indicators.size() < (green_indicators + red_indicators + yellow_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 10, 5, 0)); - } - } - - while (indicators.size() < (green_indicators + red_indicators + yellow_indicators + blue_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 0, 10)); - } + for (int i = 0; i < 4; i++) { + set_unique_leds(indicator_options, indicator_numbers[i], INDICATOR_RED[i], INDICATOR_GREEN[i], INDICATOR_BLUE[i]); } ESP_ERROR_CHECK(led_strip_refresh(leds)); - uint8_t green_pressed = 0; - uint8_t red_pressed = 0; - uint8_t yellow_pressed = 0; - uint8_t blue_pressed = 0; + std::array buttons_pressed = {0, 0, 0, 0}; ButtonKey button; @@ -523,50 +506,37 @@ void step3(void) { if (get_pressed_button(&button)) { uint8_t button_state = get_button_state(); - if ((button_state & 0b1) == 0b1) { - green_pressed++; - if (green_pressed > green_indicators) { - strike("Green button pressed too many times! (step 3, puzzle 5)"); - break; - } - } - if ((button_state & 0b10) == 0b10) { - red_pressed++; - if (red_pressed > red_indicators) { - strike("Red button pressed too many times! (step 3, puzzle 5)"); - break; - } - } - if ((button_state & 0b100) == 0b100) { - yellow_pressed++; - if (yellow_pressed > yellow_indicators) { - strike("Yellow button pressed too many times! (step 3, puzzle 5)"); - break; - } - } - if ((button_state & 0b1000) == 0b1000) { - blue_pressed++; - if (blue_pressed > blue_indicators) { - strike("Blue button pressed too many times! (step 3, puzzle 5)"); + for (int i = 0; i < 4; i++) { + if (((button_state >> i) & 0b1) == 0b1) { + buttons_pressed[i]++; + if (buttons_pressed[i] > indicator_numbers[i]) { + strike("Button pressed too many times! (step 3, puzzle 5)"); + break; + } } } } if (get_module_time() <= 0) { // check for correct button presses - if (green_pressed == green_indicators && red_pressed == red_indicators && yellow_pressed == yellow_indicators && blue_pressed == blue_indicators) { - uint8_t switch_state = get_switch_state(); - - // ESP_LOGI(TAG, "%i, %i, %i, %i", (~green_indicators & 0b1), ((~red_indicators & 0b1) << 1), ((~yellow_indicators & 0b1) << 2), ((~blue_indicators & 0b1) << 3)); - // ESP_LOGI(TAG, "correct: %i, inputted: %i", ((~green_indicators & 0b1) | (~red_indicators & 0b1) << 1) | (~yellow_indicators & 0b1) << 2) | (~blue_indicators & 0b1) << 3)), switch_state); - // check for correct switch states - if (((~green_indicators & 0b1) + ((~red_indicators & 0b1) << 1) + ((~yellow_indicators & 0b1) << 2) + ((~blue_indicators & 0b1) << 3)) == switch_state) { - solved_puzzles++; - } else { - strike("Wrong switch state! (step 3, puzzle 5)"); + for (int i = 0; i < 4; i++) { + if (buttons_pressed == indicator_numbers) { + strike("Wrong button state! (step 3, puzzle 5)"); + break; } - } else { - strike("Wrong button state! (step 3, puzzle 5)"); } + + uint8_t switch_state = get_switch_state(); + + // check for correct switch states + for (int i = 0; i < 4; i++) { + if (((switch_state >> i) & 0b1) == (indicator_numbers[i] & 0b1)) { + strike("Wrong switch state! (step 3, puzzle 5)"); + break; + } + } + + solved_puzzles++; + solved_correctly = true; break; } vTaskDelay(pdMS_TO_TICKS(10)); @@ -578,63 +548,49 @@ void step3(void) { } case 6: { lcd_print(&lcd, "Plank"); - set_module_time(15000); + set_module_time(40000); start_module_timer(); + std::uniform_int_distribution<> led_color_dist(0, 5); + std::uniform_int_distribution<> led_off_dist(-1, 3); + // red, purple, blue, white, green, yellow const uint8_t COLORS_RED[6] = {20, 10, 0, 5, 0, 10}; const uint8_t COLORS_GREEN[6] = {0, 0, 0, 5, 10, 5}; const uint8_t COLORS_BLUE[6] = {0, 10, 10, 5, 0, 0}; - int button_colors[4] = {led_color_dist(gen), led_color_dist(gen), led_color_dist(gen), led_color_dist(gen)}; + int button_colors[4]; + bool buttons_cycling[4]; + int led_off = led_off_dist(gen); for (int i = 0; i < 4; i++) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, (20 - i), COLORS_RED[button_colors[i]], COLORS_GREEN[button_colors[i]], COLORS_BLUE[button_colors[i]])); + if (led_off != 0) { + button_colors[i] = led_color_dist(gen); + ESP_ERROR_CHECK(led_strip_set_pixel(leds, (20 - i), COLORS_RED[button_colors[i]], COLORS_GREEN[button_colors[i]], COLORS_BLUE[button_colors[i]])); + buttons_cycling[i] = true; + } else { + button_colors[i] = -1; + buttons_cycling[i] = false; + } } ESP_ERROR_CHECK(led_strip_refresh(leds)); - bool buttons_cycling[4] = {true, true, true, true}; + const uint8_t CORRECT_COLORS[4] = {4, 0, 5, 2}; TickType_t lastCycleTime = xTaskGetTickCount(); ButtonKey button; while (1) { if (get_pressed_button(&button)) { uint8_t button_state = get_button_state(); - bool correct = true; - if (button_state & 0b1) { - if (button_colors[0] != 4) { - correct = false; + for (int i = 0; i < 4; i++) { + if (button_colors[i] != CORRECT_COLORS[i]) { + strike("Paused the button at the wrong time! (step 3, puzzle 6)"); + break; } else { - buttons_cycling[0] = false; + buttons_cycling[i] = false; } } - if (button_state & 0b10) { - if (button_colors[1] != 0) { - correct = false; - } else { - buttons_cycling[1] = false; - } - } - if (button_state & 0b100) { - if (button_colors[2] != 5) { - correct = false; - } else { - buttons_cycling[2] = false; - } - } - if (button_state & 0b1000) { - if (button_colors[3] != 2) { - correct = false; - } else { - buttons_cycling[3] = false; - } - } - - if (!correct) { - strike("Paused buttons at the wrong time! (step 3, puzzle 6)"); - break; - } } if ((xTaskGetTickCount() - lastCycleTime) >= pdMS_TO_TICKS(500)) { ESP_LOGI(TAG, "Cycling LEDs"); @@ -652,43 +608,30 @@ void step3(void) { lastCycleTime = xTaskGetTickCount(); } if (get_module_time() <= 0) { - bool failed = false; for (int i = 0; i < sizeof(buttons_cycling); i++) { if (buttons_cycling[i] == true) { - failed = true; + strike("Ran out of time! (step 3, puzzle 6)"); + break; } } - if (failed) { - strike("Ran out of time! (step 3, puzzle 6)"); - } else { - solved_puzzles++; - } + solved_puzzles++; + solved_correctly = true; break; } - - vTaskDelay(pdMS_TO_TICKS(10)); } - - // *** ADD PART 2 *** - - break; } case 7: { lcd_print(&lcd, "What"); - set_module_time(20000); + set_module_time(40000); start_module_timer(); std::uniform_int_distribution<> math_number_dist(1, 9); - std::uniform_int_distribution<> math_operation_dist(0, 3); std::vector math_numbers; std::vector math_operations; - // ESP_LOGI(TAG, "math_numbers: %f, %f, %f, %f", math_numbers[0], math_numbers[1], math_numbers[2], math_numbers[3]); - // ESP_LOGI(TAG, "math_operations: %i, %i, %i", math_operations[0], math_operations[1], math_operations[2]); - std::map operation_map = { {0, '+'}, {1, '-'}, @@ -699,9 +642,10 @@ void step3(void) { int expression_answer = -1; std::string display_expression; + std::vector possible_math_operations = {0, 1, 2, 3}; while (expression_answer < 0) { math_numbers = {static_cast(math_number_dist(gen)), static_cast(math_number_dist(gen)), static_cast(math_number_dist(gen)), static_cast(math_number_dist(gen))}; - math_operations = {math_operation_dist(gen), math_operation_dist(gen), math_operation_dist(gen)}; + math_operations = unique_values(possible_math_operations, 3); display_expression = std::to_string(static_cast(math_numbers[0])); for (int i = 0; i < 3; i++) { @@ -709,6 +653,7 @@ void step3(void) { display_expression += std::to_string(static_cast(math_numbers[i + 1])); } + // Solve for (int j = 0; j < 3; j++) { bool found = false; for (int i = 0; i < math_operations.size(); i++) { @@ -728,9 +673,7 @@ void step3(void) { break; } } - if (found) { - continue; - } + if (found) continue; for (int i = 0; i < math_operations.size(); i++) { if (math_operations[i] == 0) { // ESP_LOGI(TAG, "i = %i, condensing %f + %f to %f", i, math_numbers[i], math_numbers[i + 1], (math_numbers[i] + math_numbers[i+1])); @@ -759,50 +702,37 @@ void step3(void) { // ESP_LOGI(TAG, "Display expression: %s", display_expression.c_str()); // ESP_LOGI(TAG, "Solved expression answer: %i", static_cast(expression_answer)); - // set LEDs - std::uniform_int_distribution<> indicator_number_dist(0, 5); - uint8_t green_indicators = indicator_number_dist(gen); - uint8_t red_indicators = indicator_number_dist(gen); - uint8_t blue_indicators = indicator_number_dist(gen); + // blue, red, green, yellow + const int INDICATOR_RED[4] = {0, 20, 0, 10}; + const int INDICATOR_GREEN[4] = {0, 0, 10, 5}; + const int INDICATOR_BLUE[4] = {10, 0, 0, 0}; - while (((expression_answer + (blue_indicators * 3) - red_indicators) * (green_indicators ^ 2)) < 0) { - green_indicators = indicator_number_dist(gen); - red_indicators = indicator_number_dist(gen); - blue_indicators = indicator_number_dist(gen); + std::uniform_int_distribution<> add_sub_indicator_dist(1, 6); + std::uniform_int_distribution<> mult_div_indicator_dist(1, 3); + + int modifier_indicators[4] = {add_sub_indicator_dist(gen), add_sub_indicator_dist(gen), mult_div_indicator_dist(gen), mult_div_indicator_dist(gen)}; + + while ((((expression_answer + (modifier_indicators[0] * 3) - modifier_indicators[1]) * (3 ^ modifier_indicators[2])) / (2 ^ modifier_indicators[3])) < 0) { + modifier_indicators[0] = add_sub_indicator_dist(gen); + modifier_indicators[1] = add_sub_indicator_dist(gen); + modifier_indicators[2] = mult_div_indicator_dist(gen); + modifier_indicators[3] = mult_div_indicator_dist(gen); } + expression_answer += modifier_indicators[0] * 3; + expression_answer -= modifier_indicators[1]; + expression_answer *= 3 ^ modifier_indicators[2]; + expression_answer /= 2 ^ modifier_indicators[3]; - // ESP_LOGI(TAG, "Green: %i, Red: %i, Blue: %i", green_indicators, red_indicators, blue_indicators); - - std::set indicators; - - while (indicators.size() < green_indicators) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 10, 0)); - } - } - - while (indicators.size() < (green_indicators + red_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 15, 0, 0)); - } - } - - while (indicators.size() < (green_indicators + red_indicators + blue_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 0, 10)); - } + std::vector led_options = all_leds; + for (int i = 0; i < 4; i++) { + set_unique_leds(led_options, modifier_indicators[i], INDICATOR_RED[i], INDICATOR_GREEN[i], INDICATOR_BLUE[i]); } ESP_ERROR_CHECK(led_strip_refresh(leds)); - // *** ALSO IMPLEMENT LAST STEPS *** - std::string answer_string = std::to_string(expression_answer); std::string entered_string; @@ -821,6 +751,7 @@ void step3(void) { strike("Entered answer is not correct! (step 3, puzzle 7)"); } else { solved_puzzles++; + solved_correctly = true; } break; } else { @@ -848,138 +779,106 @@ void step3(void) { } case 8: { lcd_print(&lcd, "Plink"); - set_module_time(10000); + set_module_time(15000); start_module_timer(); std::uniform_int_distribution<> indicator_number_dist(0, 4); - uint8_t green_indicators = indicator_number_dist(gen); - uint8_t red_indicators = indicator_number_dist(gen); - uint8_t yellow_indicators = indicator_number_dist(gen); - uint8_t blue_indicators = indicator_number_dist(gen); - uint8_t purple_indicators = indicator_number_dist(gen); // ESP_LOGI(TAG, "Green: %i, Red: %i, Yellow: %i, Blue: %i", green_indicators, red_indicators, yellow_indicators, blue_indicators); - std::set indicators; + // green, red, yellow, blue, purple + const int INDICATOR_RED[5] = {0, 20, 10, 0, 10}; + const int INDICATOR_GREEN[5] = {10, 0, 5, 0, 0}; + const int INDICATOR_BLUE[5] = {0, 0, 0, 10, 5}; - while (indicators.size() < green_indicators) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 10, 0)); + int solved_times = 0; + bool failed = false; + while (solved_times < 3 && !failed) { + int indicator_numbers[5] = {indicator_number_dist(gen), indicator_number_dist(gen), indicator_number_dist(gen), indicator_number_dist(gen), indicator_number_dist(gen)}; + + std::vector led_options = all_leds; + for (int i = 0; i < 5; i++) { + set_unique_leds(led_options, indicator_numbers[i], INDICATOR_RED[i], INDICATOR_GREEN[i], INDICATOR_BLUE[i]); } - } - while (indicators.size() < (green_indicators + red_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 15, 0, 0)); - } - } - while (indicators.size() < (green_indicators + red_indicators + yellow_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 10, 5, 0)); - } - } + ESP_ERROR_CHECK(led_strip_refresh(leds)); - while (indicators.size() < (green_indicators + red_indicators + yellow_indicators + blue_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 0, 0, 10)); - } - } + std::uniform_int_distribution<> answer_color_dist(0, 4); - while (indicators.size() < (green_indicators + red_indicators + yellow_indicators + blue_indicators + purple_indicators)) { - uint8_t led = led_picker_dist(gen); - if (indicators.insert(led).second) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, led, 10, 0, 5)); - } - } + std::map color_name_map = { + {0, "Green"}, + {1, "Red"}, + {2, "Yellow"}, + {3, "Blue"}, + {4, "Purple"}, + }; - ESP_ERROR_CHECK(led_strip_refresh(leds)); + int answer_color = answer_color_dist(gen); - std::uniform_int_distribution<> answer_color_dist(0, 4); + std::string color_string = color_name_map[answer_color]; + std::string answer_num = std::to_string(indicator_numbers[answer_color]); - int answer_color = answer_color_dist(gen); - std::string color_string; - std::string answer_num; + // ESP_LOGI(TAG, "color string: %s", color_string.c_str()); - switch (answer_color) { - case 0: { - answer_num = std::to_string(green_indicators); - color_string = "Green"; - break; - } - case 1: { - answer_num = std::to_string(red_indicators); - color_string = "Red"; - break; - } - case 2: { - answer_num = std::to_string(yellow_indicators); - color_string = "Yellow"; - break; - } - case 3: { - answer_num = std::to_string(blue_indicators); - color_string = "Blue"; - break; - } - case 4: { - answer_num = std::to_string(purple_indicators); - color_string = "Purple"; - break; - } - } - // ESP_LOGI(TAG, "color string: %s", color_string.c_str()); + lcd_set_cursor(&lcd, 1, 2); + lcd_print(&lcd, color_string.c_str()); - lcd_set_cursor(&lcd, 1, 2); - lcd_print(&lcd, color_string.c_str()); + std::string entered_string; - std::string entered_string; - - KeypadKey key; - while (1) { - if (get_pressed_keypad(&key)) { - if (key == KeypadKey::star) { - // clear - entered_string = ""; - } else if (key == KeypadKey::pound) { - // submit - if (entered_string != answer_num) { - strike("Entered answer is not correct! (step 3, puzzle 7)"); + KeypadKey key; + while (1) { + if (get_pressed_keypad(&key)) { + if (key == KeypadKey::star) { + // clear + entered_string = ""; + } else if (key == KeypadKey::pound) { + // submit + if (entered_string != answer_num) { + strike("Entered answer is not correct! (step 3, puzzle 7)"); + } else { + solved_times++; + } + break; } else { - solved_puzzles++; + entered_string += char_of_keypad_key(key); } + + lcd_clear(&lcd); + lcd_set_cursor(&lcd, 1, 1); + lcd_print(&lcd, "Plink"); + lcd_set_cursor(&lcd, 1, 2); + lcd_print(&lcd, color_string.c_str()); + lcd_set_cursor(&lcd, 1, 3); + lcd_print(&lcd, entered_string.c_str()); + } + if (get_module_time() <= 0) { + strike("Ran out of time! (step 3, puzzle 7)"); break; - } else { - entered_string += char_of_keypad_key(key); } - lcd_clear(&lcd); - lcd_set_cursor(&lcd, 1, 1); - lcd_print(&lcd, "Plink"); - lcd_set_cursor(&lcd, 1, 2); - lcd_print(&lcd, color_string.c_str()); - lcd_set_cursor(&lcd, 1, 3); - lcd_print(&lcd, entered_string.c_str()); - } - if (get_module_time() <= 0) { - strike("Ran out of time! (step 3, puzzle 7)"); - break; + + vTaskDelay(pdMS_TO_TICKS(10)); } + break; - vTaskDelay(pdMS_TO_TICKS(10)); } - - break; + if (!failed) { + solved_puzzles++; + solved_correctly = true; + } } } - vTaskDelay(pdMS_TO_TICKS(1000)); + if (solved_correctly) { + play_raw(MOUNT_POINT "/correct.pcm"); + vTaskDelay(pdMS_TO_TICKS(1000)); + solved_correctly = false; + } else { + vTaskDelay(pdMS_TO_TICKS(3000)); + } clean_bomb(); } } diff --git a/main/steps/step3.h b/main/steps/step3.h index de80bb7..3d5e900 100644 --- a/main/steps/step3.h +++ b/main/steps/step3.h @@ -13,6 +13,7 @@ #include #include #include +#include static const char *STEP3_TAG = "step3";