From 43b01f0b2b9a7480c64800b9f68b759d2d349200 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Wed, 26 Mar 2025 18:39:35 -0500 Subject: [PATCH] Clean Up calls to the leds driver --- main/drivers/leds.cpp | 7 --- main/steps/step0.cpp | 4 +- main/steps/step1.cpp | 19 +++--- main/steps/step2.cpp | 14 +++-- main/steps/step3.cpp | 19 +++--- main/steps/step5.cpp | 136 ++++++++++++++++++++++++------------------ 6 files changed, 108 insertions(+), 91 deletions(-) diff --git a/main/drivers/leds.cpp b/main/drivers/leds.cpp index a345196..cd8954c 100644 --- a/main/drivers/leds.cpp +++ b/main/drivers/leds.cpp @@ -43,10 +43,3 @@ void leds_clear() { led_strip_clear(leds); } - -void example_leds() { - for (int i = 0; i < LED_COUNT; i++) { - ESP_ERROR_CHECK(led_strip_set_pixel(leds, i, i, LED_COUNT-i, 0)); - } - ESP_ERROR_CHECK(led_strip_refresh(leds)); -} \ No newline at end of file diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index ed8e259..f345425 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -36,7 +36,7 @@ static void battery_stats() { /// Wait for "*9819" void step0() { - led_set(IndicatorLED::LED_SPEAKER, LED_COLOR_BLUE); + led_set(IndicatorLED::LED_SPEAKER, LEDColor::LED_COLOR_BLUE); leds_flush(); StarCodeHandler star_codes[] = { @@ -203,7 +203,7 @@ static void set_game_time() { } clean_bomb(); - led_set(IndicatorLED::LED_SPEAKER, 0, 0, 20); + led_set(IndicatorLED::LED_SPEAKER, LEDColor::LED_COLOR_BLUE); leds_flush(); return; } else { diff --git a/main/steps/step1.cpp b/main/steps/step1.cpp index e7421d6..caf0ed7 100644 --- a/main/steps/step1.cpp +++ b/main/steps/step1.cpp @@ -17,11 +17,11 @@ static const char* NUM_NAMES[] = { "four" }; -static uint8_t NEOPIXEL_COLORS[4][3] = { - {0, 20, 0}, - {20, 0, 0}, - {20, 20, 0}, - {0, 0, 20}, +static uint32_t NEOPIXEL_COLORS[4] = { + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_BLUE, }; static std::random_device my_rd; @@ -114,8 +114,7 @@ static void generate_switch_leds(void) { switch_leds[3] = colors[0]; for (int i = 0; i < 4; i++) { - uint8_t* rgb = NEOPIXEL_COLORS[switch_leds[i]]; - led_set(IndicatorLED::LED_S1 - i, rgb[0], rgb[1], rgb[2]); + led_set(IndicatorLED::LED_S1 - i, NEOPIXEL_COLORS[switch_leds[i]]); } leds_flush(); } @@ -208,15 +207,15 @@ static bool play_part(uint32_t time) { switch (part) { case 0: lcd_print("COLOR"); - led_set(IndicatorLED::LED_LCD, 20, 0, 20); + led_set(IndicatorLED::LED_LCD, LEDColor::LED_COLOR_PINK); break; case 1: lcd_print("NUMBER"); - led_set(IndicatorLED::LED_LCD, 0, 0, 30); + led_set(IndicatorLED::LED_LCD, LEDColor::LED_COLOR_BLUE); break; case 2: lcd_print("SWITCH"); - led_set(IndicatorLED::LED_LCD, 20, 20, 0); + led_set(IndicatorLED::LED_LCD, LEDColor::LED_COLOR_YELLOW); break; } leds_flush(); diff --git a/main/steps/step2.cpp b/main/steps/step2.cpp index 5dc1eac..e88b0f5 100644 --- a/main/steps/step2.cpp +++ b/main/steps/step2.cpp @@ -16,9 +16,13 @@ static const uint8_t SSEG_MAPS[5][4] = { {0b01000111, 0b00011001, 0b01111000, 0b00111110} }; -static const int INDICATOR_RED[5] = {15, 0, 0, 10, 5}; -static const int INDICATOR_GREEN[5] = {0, 0, 10, 5, 7}; -static const int INDICATOR_BLUE[5] = {0, 10, 0, 0, 5}; +static const uint32_t INDICATOR_COLORS[5] = { + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_WHITE, +}; // random number generators static std::random_device rd; @@ -48,7 +52,7 @@ std::map number_map = { static void new_puzzle(void) { // scramble lights for (int i = 0; i < 5; i++) { - led_set(IndicatorLED::LED_SPEAKER, INDICATOR_RED[map_dist(gen)], INDICATOR_GREEN[map_dist(gen)], INDICATOR_BLUE[map_dist(gen)]); + led_set(IndicatorLED::LED_SPEAKER, INDICATOR_COLORS[map_dist(gen)]); leds_flush(); uint8_t random_segments[4] = {0, 0, 0, 0}; @@ -72,7 +76,7 @@ static void new_puzzle(void) { } // ESP_LOGI(TAG, "Chosen Map: %i", chosen_map); - led_set(IndicatorLED::LED_SPEAKER, INDICATOR_RED[chosen_map], INDICATOR_GREEN[chosen_map], INDICATOR_BLUE[chosen_map]); + led_set(IndicatorLED::LED_SPEAKER, INDICATOR_COLORS[chosen_map]); leds_flush(); for (int i = 0; i < 8; i++) { diff --git a/main/steps/step3.cpp b/main/steps/step3.cpp index 876c027..b8f5565 100644 --- a/main/steps/step3.cpp +++ b/main/steps/step3.cpp @@ -49,14 +49,14 @@ 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); -static uint8_t NEOPIXEL_COLORS[7][3] = { - {20, 0, 0}, // red - {20, 10, 0}, // orange - {20, 20, 0}, // yellow - {0, 20, 0}, // green - {0, 0, 20}, // blue - {20, 0, 20}, // purple - {0, 0, 0}, // off +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_OFF, }; static bool one_second(); @@ -139,8 +139,7 @@ static void rng_leds() { static void write_leds() { // update all the leds for (int i = 0; i < LED_COUNT; i++) { - auto colors = NEOPIXEL_COLORS[indicator_led_idxs[i]]; - led_set(i, colors[0], colors[1], colors[2]); + led_set(i, NEOPIXEL_COLORS[indicator_led_idxs[i]]); } leds_flush(); } diff --git a/main/steps/step5.cpp b/main/steps/step5.cpp index 36d0cdc..653e2e9 100644 --- a/main/steps/step5.cpp +++ b/main/steps/step5.cpp @@ -21,23 +21,23 @@ 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); -void set_unique_leds(std::vector& input_options, const int num, const int r, const int g, const int b) { +void set_unique_leds(std::vector& input_options, const int num, const uint32_t color) { 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); - led_set(input_options[led], r, g, b); + led_set(input_options[led], color); 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) { +void set_unique_leds_random_color(std::vector& input_options, const int num, const uint32_t* COLORS, size_t COLORS_len) { 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); + std::uniform_int_distribution<> led_color_dist(0, COLORS_len - 1); int color = led_color_dist(gen); - led_set(input_options[led], r[color], g[color], b[color]); + led_set(input_options[led], COLORS[color]); input_options.erase(input_options.begin() + led); } } @@ -196,9 +196,14 @@ void step5(void) { all_leds.push_back(i); } - const int INDICATOR_RED[6] = {20, 0, 0, 10, 10, 5}; - const int INDICATOR_GREEN[6] = {0, 0, 10, 5, 0, 7}; - const int INDICATOR_BLUE[6] = {0, 10, 0, 0, 5, 5}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_PINK, + LEDColor::LED_COLOR_WHITE, + }; static std::uniform_int_distribution<> led_number_dist(0, 21); int last_cycle_tick = xTaskGetTickCount(); @@ -207,7 +212,7 @@ void step5(void) { if ((xTaskGetTickCount() - last_cycle_tick) > pdMS_TO_TICKS(100)) { clean_bomb(); std::vector led_options = all_leds; - set_unique_leds_random_color(led_options, led_number_dist(gen), INDICATOR_RED, INDICATOR_GREEN, INDICATOR_BLUE); + set_unique_leds_random_color(led_options, led_number_dist(gen), COLORS, sizeof(COLORS)/sizeof(COLORS[0])); leds_flush(); last_cycle_tick = xTaskGetTickCount(); scrambled_times++; @@ -234,14 +239,16 @@ void step5(void) { // set green std::uniform_int_distribution<> green_indicators_dist(1, 15); uint8_t green_indicators = green_indicators_dist(gen); - set_unique_leds(indicator_options, green_indicators, 0, 10, 0); + set_unique_leds(indicator_options, green_indicators, LEDColor::LED_COLOR_GREEN); // set non-green - const int NON_GREEN_INDICATOR_RED[3] = {20, 0, 10}; - const int NON_GREEN_INDICATOR_GREEN[3] = {0, 0, 0}; - const int NON_GREEN_INDICATOR_BLUE[3] = {0, 10, 5}; + const uint32_t NON_GREEN_COLORS[] = { + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_PINK, + }; 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); + set_unique_leds_random_color(indicator_options, non_green_indicators_dist(gen), NON_GREEN_COLORS, sizeof(NON_GREEN_COLORS)/sizeof(NON_GREEN_COLORS[0])); leds_flush(); @@ -266,11 +273,8 @@ void step5(void) { uint8_t indicators_num = on_indicators_dist(gen); 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, 7}; - 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); + set_unique_leds_random_color(indicator_options, indicators_num, COLORS, sizeof(COLORS)/sizeof(COLORS[0])); leds_flush(); uint8_t starting_switch_state = get_switch_state(); @@ -315,7 +319,7 @@ void step5(void) { for (int i = 0; i < 5; i++) { if (lit_leds[i]) { int color = led_color_dist(gen); - led_set(idx_to_led_map[i], INDICATOR_RED[color], INDICATOR_GREEN[color], INDICATOR_BLUE[color]); + led_set(idx_to_led_map[i], COLORS[color]); } } leds_flush(); @@ -379,9 +383,12 @@ void step5(void) { set_module_time(TIME_NOTHING); start_module_timer(); - const int COLOR_RED[5] = {0, 20, 10, 0}; - const int COLOR_GREEN[5] = {20, 0, 10, 0}; - const int COLOR_BLUE[5] = {0, 0, 0, 20}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_BLUE, + }; static std::uniform_int_distribution<> color_dist(0, 3); @@ -389,9 +396,9 @@ void step5(void) { int speaker_color = color_dist(gen); int s3_color = color_dist(gen); - led_set(6, COLOR_RED[tft_color], COLOR_GREEN[tft_color], COLOR_BLUE[tft_color]); - led_set(9, COLOR_RED[speaker_color], COLOR_GREEN[speaker_color], COLOR_BLUE[speaker_color]); - led_set(14, COLOR_RED[s3_color], COLOR_GREEN[s3_color], COLOR_BLUE[s3_color]); + led_set(IndicatorLED::LED_TFT, COLORS[tft_color]); + led_set(IndicatorLED::LED_SPEAKER, COLORS[speaker_color]); + led_set(IndicatorLED::LED_S3, COLORS[s3_color]); leds_flush(); int buttons_pressed = 0; @@ -436,27 +443,33 @@ void step5(void) { start_module_timer(); // buttons - const int BUTTON_COLOR_RED[4] = {0, 20, 10, 0}; - const int BUTTON_COLOR_GREEN[4] = {10, 0, 5, 0}; - const int BUTTON_COLOR_BLUE[4] = {0, 0, 0, 10}; + const uint32_t BUTTON_COLORS[] = { + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_BLUE, + }; static std::uniform_int_distribution<> button_color_dist(0, 3); int button_colors[4] = {button_color_dist(gen), button_color_dist(gen), button_color_dist(gen), button_color_dist(gen)}; for (int i = 0; i < 4; i++) { - led_set(IndicatorLED::LED_B1 - i, BUTTON_COLOR_RED[button_colors[i]], BUTTON_COLOR_GREEN[button_colors[i]], BUTTON_COLOR_BLUE[button_colors[i]]); + led_set(IndicatorLED::LED_B1 - i, BUTTON_COLORS[button_colors[i]]); } // switches - const int SWITCH_COLOR_RED[3] = {20, 0, 10}; - const int SWITCH_COLOR_GREEN[3] = {0, 10, 5}; + const uint32_t SWITCH_COLOR[] = { + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_YELLOW, + }; static std::uniform_int_distribution<> switch_color_dist(0, 2); int switch_colors[4] = {switch_color_dist(gen), switch_color_dist(gen), switch_color_dist(gen), switch_color_dist(gen)}; for (int i = 0; i < 4; i++) { - led_set(IndicatorLED::LED_S1 - i, SWITCH_COLOR_RED[switch_colors[i]], SWITCH_COLOR_GREEN[switch_colors[i]], 0); + led_set(IndicatorLED::LED_S1 - i, SWITCH_COLOR[switch_colors[i]]); } leds_flush(); @@ -475,9 +488,8 @@ void step5(void) { if (button_colors[i] > 3) { button_colors[i] = 0; } - led_set(IndicatorLED::LED_B1 - i, BUTTON_COLOR_RED[button_colors[i]], BUTTON_COLOR_GREEN[button_colors[i]], BUTTON_COLOR_BLUE[button_colors[i]]); + led_set(IndicatorLED::LED_B1 - i, BUTTON_COLORS[button_colors[i]]); } - } leds_flush(); } @@ -496,16 +508,19 @@ void step5(void) { std::uniform_int_distribution<> indicator_number_dist(0, 5); - const int INDICATOR_RED[4] = {0, 20, 10, 0}; - const int INDICATOR_GREEN[4] = {10, 0, 5, 0}; - const int INDICATOR_BLUE[4] = {0, 0, 0, 10}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_BLUE, + }; // 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; for (int i = 0; i < 4; i++) { - set_unique_leds(indicator_options, indicator_numbers[i], INDICATOR_RED[i], INDICATOR_GREEN[i], INDICATOR_BLUE[i]); + set_unique_leds(indicator_options, indicator_numbers[i], COLORS[i]); } leds_flush(); @@ -550,10 +565,14 @@ void step5(void) { 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, 7, 10, 5}; - const uint8_t COLORS_BLUE[6] = {0, 10, 10, 5, 0, 0}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_PINK, + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_WHITE, + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_YELLOW, + }; int button_colors[4]; bool buttons_cycling[4]; @@ -562,7 +581,7 @@ void step5(void) { for (int i = 0; i < 4; i++) { if (led_off != i) { button_colors[i] = led_color_dist(gen); - led_set(IndicatorLED::LED_B1 - i, COLORS_RED[button_colors[i]], COLORS_GREEN[button_colors[i]], COLORS_BLUE[button_colors[i]]); + led_set(IndicatorLED::LED_B1 - i, COLORS[button_colors[i]]); buttons_cycling[i] = true; } else { button_colors[i] = -1; @@ -593,7 +612,7 @@ void step5(void) { break; } else if (led_turn_on_dist(gen) == 0) { button_turned_on = true; - led_set(IndicatorLED::LED_B1 - i, COLORS_RED[CORRECT_COLORS[i]], COLORS_GREEN[CORRECT_COLORS[i]], COLORS_BLUE[CORRECT_COLORS[i]]); + led_set(IndicatorLED::LED_B1 - i, COLORS[CORRECT_COLORS[i]]); leds_flush(); } } else if (button_colors[i] != CORRECT_COLORS[i]) { @@ -616,7 +635,7 @@ void step5(void) { if (button_colors[i] > 5) { button_colors[i] = 0; } - led_set(IndicatorLED::LED_B1 - i, COLORS_RED[button_colors[i]], COLORS_GREEN[button_colors[i]], COLORS_BLUE[button_colors[i]]); + led_set(IndicatorLED::LED_B1 - i, COLORS[button_colors[i]]); } } leds_flush(); @@ -708,11 +727,12 @@ void step5(void) { lcd_print(1, 2, display_expression.c_str()); // set LEDs - - // 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}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_YELLOW, + }; std::uniform_int_distribution<> add_sub_indicator_dist(1, 6); std::uniform_int_distribution<> mult_div_indicator_dist(1, 3); @@ -734,7 +754,7 @@ void step5(void) { 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]); + set_unique_leds(led_options, modifier_indicators[i], COLORS[i]); } leds_flush(); @@ -787,10 +807,13 @@ void step5(void) { // ESP_LOGI(TAG, "Green: %i, Red: %i, Yellow: %i, Blue: %i", green_indicators, red_indicators, yellow_indicators, blue_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}; + const uint32_t COLORS[] = { + LEDColor::LED_COLOR_GREEN, + LEDColor::LED_COLOR_RED, + LEDColor::LED_COLOR_YELLOW, + LEDColor::LED_COLOR_BLUE, + LEDColor::LED_COLOR_PINK, + }; int solved_times = 0; bool failed = false; @@ -799,10 +822,9 @@ void step5(void) { 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]); + set_unique_leds(led_options, indicator_numbers[i], COLORS[i]); } - leds_flush(); std::uniform_int_distribution<> answer_color_dist(0, 4);