diff --git a/main/drivers/game_timer.cpp b/main/drivers/game_timer.cpp index 2d55cbb..ac4f107 100644 --- a/main/drivers/game_timer.cpp +++ b/main/drivers/game_timer.cpp @@ -13,6 +13,25 @@ static uint32_t module_time_left; static void game_timer_task(void *arg); static uint32_t sat_sub(uint32_t x, uint32_t y); +static void write_game_time(uint32_t millis) { + if (millis > 60'000) { + int disp_time = (millis / 60'000) * 100; + disp_time = disp_time + ((millis % 60'000) / 1'000); + set_game_sseg_num(disp_time, 2); + } else { + set_game_sseg_num(millis / 100, 1); + } +} +static void write_module_time(uint32_t millis) { + if (millis > 60'000) { + int disp_time = (millis / 60'000) * 100; + disp_time = disp_time + ((millis % 60'000) / 1'000); + set_module_sseg_num(disp_time, 2); + } else { + set_module_sseg_num(millis / 100, 1); + } +} + void init_game_module_timer(void) { xTaskCreate(game_timer_task, "game_module_timer", 4096, NULL, 10, NULL); } @@ -35,6 +54,7 @@ void stop_module_timer(void) { void set_game_time(uint32_t new_time) { game_time_left = new_time; + write_game_time(game_time_left); } uint32_t get_game_time() { @@ -43,6 +63,7 @@ uint32_t get_game_time() { void set_module_time(uint32_t new_time) { module_time_left = new_time; + write_module_time(module_time_left); } uint32_t get_module_time() { @@ -58,23 +79,11 @@ static void game_timer_task(void *arg) vTaskDelayUntil( &lastWakeTime, pdMS_TO_TICKS(frequency)); if (is_game_playing) { game_time_left = sat_sub(game_time_left, frequency); - if (game_time_left > 60'000) { - int disp_time = (game_time_left / 60'000) * 100; - disp_time = disp_time + ((game_time_left % 60'000) / 1'000); - set_game_sseg_num(disp_time, 2); - } else { - set_game_sseg_num(game_time_left / 100, 1); - } + write_game_time(game_time_left); } if (is_module_playing) { module_time_left = sat_sub(module_time_left, frequency); - if (module_time_left > 60'000) { - int disp_time = (module_time_left / 60'000) * 100; - disp_time = disp_time + ((module_time_left % 60'000) / 1'000); - set_module_sseg_num(disp_time, 2); - } else { - set_module_sseg_num(module_time_left / 100, 1); - } + write_module_time(module_time_left); } } diff --git a/main/steps/step1.cpp b/main/steps/step1.cpp index aed1310..1116c0d 100644 --- a/main/steps/step1.cpp +++ b/main/steps/step1.cpp @@ -1,12 +1,12 @@ #include "step1.h" -static const char *STEP1_TAG = "step1"; +static const char *TAG = "step1"; static char* COLOR_NAMES[] = { "green", "red", "yellow", - "purple" + "blue" }; static char* NUM_NAMES[] = { @@ -20,7 +20,7 @@ static uint8_t NEOPIXEL_COLORS[4][3] = { {0, 20, 0}, {20, 0, 0}, {20, 20, 0}, - {15, 0, 20}, + {0, 0, 20}, }; static std::random_device my_rd; @@ -36,7 +36,7 @@ static lv_style_t style_screen; static lv_style_t green_text; static lv_style_t red_text; static lv_style_t yellow_text; -static lv_style_t purple_text; +static lv_style_t blue_text; static int switch_leds[] = {0, 0, 0, 0}; @@ -44,46 +44,53 @@ static lv_style_t* color_styles[] = { &green_text, &red_text, &yellow_text, - &purple_text + &blue_text }; +/// 0 => part a +/// 1 => part b +/// 2 => part c +static int part = 0; + + static void init_step(void) { - scr = lv_disp_get_scr_act(NULL); + if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { + scr = lv_disp_get_scr_act(NULL); - // Set the background color of the display to black. - lv_style_init(&style_screen); - lv_style_set_bg_color(&style_screen, lv_color_black()); - lv_style_set_text_font(&style_screen, &lv_font_montserrat_32); - lv_obj_add_style(lv_scr_act(), &style_screen, LV_STATE_DEFAULT); + // Set the background color of the display to black. + lv_style_init(&style_screen); + lv_style_set_bg_color(&style_screen, lv_color_black()); + lv_style_set_text_font(&style_screen, &lv_font_montserrat_32); + lv_obj_add_style(lv_scr_act(), &style_screen, LV_STATE_DEFAULT); - // rgb565 - lv_color_t green; - green.full = 0x27e0; - lv_color_t red; - red.full = 0xf800; - lv_color_t yellow; - yellow.full = 0xffa9; - lv_color_t purple; - purple.full = 0x881f; + // rgb565 + lv_color_t green = { .full = 0x0640 }; + lv_color_t red = { .full = 0xf800 }; + lv_color_t yellow = { .full = 0xffad }; + lv_color_t blue = { .full = 0x045f }; - lv_style_init(&green_text); - lv_style_set_text_color(&green_text, green); + lv_style_init(&green_text); + lv_style_set_text_color(&green_text, green); - lv_style_init(&red_text); - lv_style_set_text_color(&red_text, red); + lv_style_init(&red_text); + lv_style_set_text_color(&red_text, red); - lv_style_init(&yellow_text); - lv_style_set_text_color(&yellow_text, yellow); + lv_style_init(&yellow_text); + lv_style_set_text_color(&yellow_text, yellow); - lv_style_init(&purple_text); - lv_style_set_text_color(&purple_text, purple); + lv_style_init(&blue_text); + lv_style_set_text_color(&blue_text, blue); - text = lv_label_create(scr); + text = lv_label_create(scr); + xSemaphoreGive(xGuiSemaphore); + } } static void clean_up_step(void) { - lv_obj_clean(text); - lv_obj_clean(scr); + if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { + lv_obj_clean(scr); + xSemaphoreGive(xGuiSemaphore); + } } static void generate_switch_leds(void) { @@ -103,7 +110,7 @@ static void generate_switch_leds(void) { switch_leds[3] = colors[0]; - ESP_LOGI(STEP1_TAG, "%d, %d, %d, %d", switch_leds[0], switch_leds[1], switch_leds[2], switch_leds[3]); + 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]]; @@ -176,66 +183,43 @@ static int generate_part_c(void) { return text_color + 4; } -static bool part_a(void) { - stop_module_timer(); - set_module_time(30*1000); - - lcd_clear(&lcd); - lcd_set_cursor(&lcd, 1, 1); - lcd_print(&lcd, "COLOR"); - ESP_ERROR_CHECK(led_strip_set_pixel(leds, Led::char_lcd, 20, 0, 20)); - ESP_ERROR_CHECK(led_strip_refresh(leds)); - - int times = 0; - int correct = generate_part_a(); - - ButtonKey button; - SwitchKey switch_; - - while (times < 15) { - while (get_pressed_button(&button)) { - start_module_timer(); - if ((int)(button) == correct) { - times++; - if (times >= 15) break; - correct = generate_part_a(); - } else { - strike("Incorrect color action"); - } - } - while (get_flipped_switch(&switch_)) { - strike("Incorrect color action"); - } - if (get_module_time() <= 0) { - strike("Out of time"); - return false; - } - - vTaskDelay(pdMS_TO_TICKS(10)); +static int generate_part(void) { + switch (part) { + case 0: + return generate_part_a(); + case 1: + return generate_part_b(); + case 2: + return generate_part_c(); } - - - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_label_set_text(text, ""); - xSemaphoreGive(xGuiSemaphore); - } - vTaskDelay(pdMS_TO_TICKS(80)); - play_raw(MOUNT_POINT "/correct.pcm"); - return true; + ESP_LOGW(TAG, "`part` (%d) was not to in range 0..=2!", part); + return -1; } -static bool part_b(void) { +static bool play_part(uint32_t time) { stop_module_timer(); - set_module_time(25*1000); + set_module_time(time); lcd_clear(&lcd); lcd_set_cursor(&lcd, 1, 1); - lcd_print(&lcd, "NUMBER"); - ESP_ERROR_CHECK(led_strip_set_pixel(leds, Led::char_lcd, 0, 0, 30)); - ESP_ERROR_CHECK(led_strip_refresh(leds)); + switch (part) { + case 0: + lcd_print(&lcd, "COLOR"); + led_strip_set_pixel(leds, Led::char_lcd, 20, 0, 20); + break; + case 1: + lcd_print(&lcd, "NUMBER"); + led_strip_set_pixel(leds, Led::char_lcd, 0, 0, 30); + break; + case 2: + lcd_print(&lcd, "SWITCH"); + led_strip_set_pixel(leds, Led::char_lcd, 20, 20, 0); + break; + } + led_strip_refresh(leds); int times = 0; - int correct = generate_part_b(); + int correct = generate_part(); ButtonKey button; SwitchKey switch_; @@ -246,54 +230,7 @@ static bool part_b(void) { if ((int)(button) == correct) { times++; if (times >= 15) break; - correct = generate_part_b(); - } else { - strike("Incorrect color action"); - } - } - while (get_flipped_switch(&switch_)) { - strike("Incorrect color action"); - } - if (get_module_time() <= 0) { - strike("Out of time"); - return false; - } - - vTaskDelay(pdMS_TO_TICKS(10)); - } - - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_label_set_text(text, ""); - xSemaphoreGive(xGuiSemaphore); - } - vTaskDelay(pdMS_TO_TICKS(80)); - play_raw(MOUNT_POINT "/correct.pcm"); - return true; -} - -static bool part_c(void) { - stop_module_timer(); - set_module_time(20*1000); - - lcd_clear(&lcd); - lcd_set_cursor(&lcd, 1, 1); - lcd_print(&lcd, "SWITCH"); - ESP_ERROR_CHECK(led_strip_set_pixel(leds, Led::char_lcd, 20, 20, 0)); - ESP_ERROR_CHECK(led_strip_refresh(leds)); - - int times = 0; - int correct = generate_part_c(); - - ButtonKey button; - SwitchKey switch_; - - while (times < 15) { - while (get_pressed_button(&button)) { - start_module_timer(); - if ((int)(button) == correct) { - times++; - if (times >= 15) break; - correct = generate_part_c(); + correct = generate_part(); } else { strike("Incorrect color action"); } @@ -303,7 +240,7 @@ static bool part_c(void) { if (switch_leds[(int)(switch_)] + 4 == correct) { times++; if (times >= 15) break; - correct = generate_part_c(); + correct = generate_part(); } else { strike("Incorrect color action"); } @@ -329,9 +266,11 @@ void step1(void) { while (get_flipped_switch(nullptr)); init_step(); - while (!part_a()); - while (!part_b()); - while (!part_c()); + while (!play_part(30*1000)); + part = 1; + while (!play_part(25*1000)); + part = 2; + while (!play_part(22*1000)); clean_up_step(); // TODO: flash lights diff --git a/sdkconfig b/sdkconfig index bd1c0f0..0017745 100644 --- a/sdkconfig +++ b/sdkconfig @@ -1,6 +1,6 @@ # # Automatically generated file. DO NOT EDIT. -# Espressif IoT Development Framework (ESP-IDF) 5.2.2 Project Configuration +# Espressif IoT Development Framework (ESP-IDF) 5.2.1 Project Configuration # CONFIG_SOC_MPU_MIN_REGION_SIZE=0x20000000 CONFIG_SOC_MPU_REGIONS_MAX_NUM=8 @@ -331,7 +331,6 @@ CONFIG_SOC_WIFI_WAPI_SUPPORT=y CONFIG_SOC_WIFI_CSI_SUPPORT=y CONFIG_SOC_WIFI_MESH_SUPPORT=y CONFIG_SOC_WIFI_SUPPORT_VARIABLE_BEACON_WINDOW=y -CONFIG_SOC_WIFI_PHY_NEEDS_USB_WORKAROUND=y CONFIG_SOC_BLE_SUPPORTED=y CONFIG_SOC_BLE_MESH_SUPPORTED=y CONFIG_SOC_BLE_50_SUPPORTED=y @@ -436,7 +435,6 @@ CONFIG_ESP_ROM_HAS_RETARGETABLE_LOCKING=y CONFIG_ESP_ROM_USB_OTG_NUM=3 CONFIG_ESP_ROM_USB_SERIAL_DEVICE_NUM=4 CONFIG_ESP_ROM_HAS_ERASE_0_REGION_BUG=y -CONFIG_ESP_ROM_HAS_ENCRYPTED_WRITES_USING_LEGACY_DRV=y CONFIG_ESP_ROM_GET_CLK_FREQ=y CONFIG_ESP_ROM_HAS_HAL_WDT=y CONFIG_ESP_ROM_NEEDS_SWSETUP_WORKAROUND=y @@ -559,7 +557,6 @@ CONFIG_APPTRACE_LOCK_ENABLE=y # Bluetooth # # CONFIG_BT_ENABLED is not set -CONFIG_BT_ALARM_MAX_NUM=50 # end of Bluetooth # @@ -747,10 +744,7 @@ CONFIG_ESP_EVENT_POST_FROM_IRAM_ISR=y # # GDB Stub # -CONFIG_ESP_GDBSTUB_ENABLED=y # CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME is not set -CONFIG_ESP_GDBSTUB_SUPPORT_TASKS=y -CONFIG_ESP_GDBSTUB_MAX_TASKS=32 # end of GDB Stub # @@ -915,7 +909,6 @@ CONFIG_ESP_PHY_RF_CAL_PARTIAL=y # CONFIG_ESP_PHY_RF_CAL_NONE is not set # CONFIG_ESP_PHY_RF_CAL_FULL is not set CONFIG_ESP_PHY_CALIBRATION_MODE=0 -# CONFIG_ESP_PHY_PLL_TRACK_DEBUG is not set # end of PHY # @@ -1213,7 +1206,6 @@ CONFIG_FREERTOS_QUEUE_REGISTRY_SIZE=0 CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1 # CONFIG_FREERTOS_USE_TRACE_FACILITY is not set # CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is not set -# CONFIG_FREERTOS_USE_APPLICATION_TASK_TAG is not set # end of Kernel # @@ -1501,9 +1493,7 @@ CONFIG_MBEDTLS_CMAC_C=y CONFIG_MBEDTLS_HARDWARE_AES=y CONFIG_MBEDTLS_AES_USE_INTERRUPT=y CONFIG_MBEDTLS_AES_INTERRUPT_LEVEL=0 -# CONFIG_MBEDTLS_GCM_SUPPORT_NON_AES_CIPHER is not set CONFIG_MBEDTLS_HARDWARE_MPI=y -# CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI is not set CONFIG_MBEDTLS_MPI_USE_INTERRUPT=y CONFIG_MBEDTLS_MPI_INTERRUPT_LEVEL=0 CONFIG_MBEDTLS_HARDWARE_SHA=y @@ -1590,7 +1580,7 @@ CONFIG_MBEDTLS_ECP_FIXED_POINT_OPTIM=y # CONFIG_MBEDTLS_CHACHA20_C is not set # CONFIG_MBEDTLS_HKDF_C is not set # CONFIG_MBEDTLS_THREADING_C is not set -CONFIG_MBEDTLS_ERROR_STRINGS=y +# CONFIG_MBEDTLS_LARGE_KEY_SOFTWARE_MPI is not set # end of mbedTLS # @@ -2235,8 +2225,6 @@ CONFIG_ESP32_APPTRACE_LOCK_ENABLE=y # CONFIG_EVENT_LOOP_PROFILING is not set CONFIG_POST_EVENTS_FROM_ISR=y CONFIG_POST_EVENTS_FROM_IRAM_ISR=y -CONFIG_GDBSTUB_SUPPORT_TASKS=y -CONFIG_GDBSTUB_MAX_TASKS=32 # CONFIG_OTA_ALLOW_HTTP is not set # CONFIG_ESP_SYSTEM_PD_FLASH is not set CONFIG_ESP32S3_DEEP_SLEEP_WAKEUP_DELAY=2000