diff --git a/main/drivers/speaker.cpp b/main/drivers/speaker.cpp index 1c4244c..7bfab53 100644 --- a/main/drivers/speaker.cpp +++ b/main/drivers/speaker.cpp @@ -221,7 +221,7 @@ void init_speaker(void) { // start task xTaskCreate(speaker_task, "play_audio", 4096, NULL, 5, NULL); - play_clip_wav(MOUNT_POINT "/startup.wav", true, false, 3, 0); + play_clip_wav(MOUNT_POINT "/startup.wav", true, false, 4, 0); ESP_LOGI(TAG, "Speaker initialized!"); } diff --git a/main/drivers/wires.cpp b/main/drivers/wires.cpp index 98811bf..bda11a8 100644 --- a/main/drivers/wires.cpp +++ b/main/drivers/wires.cpp @@ -1,10 +1,14 @@ #include "wires.h" +extern uint32_t current_step; + uint32_t total_strikes; +uint32_t step_strikes[N_STEPS] = {0}; +uint32_t step_finish_times[N_STEPS] = {0}; static const char *TAG = "wires"; -static const uint32_t STRIKE_TIME_PENALTY = 30'000; +const uint32_t STRIKE_TIME_PENALTY = 30'000; static bool button_state; static bool button_pressed; @@ -83,7 +87,10 @@ void strike(const char* reason) { ESP_LOGW("strike!", "%s", reason); lcd_print(0, 3, reason); time_penalty(STRIKE_TIME_PENALTY); - total_strikes += 1; + if (current_step > 0 && current_step <= N_STEPS) { + total_strikes += 1; + step_strikes[current_step - 1] += 1; + } uint8_t reg = 6; ESP_ERROR_CHECK_WITHOUT_ABORT(i2c_master_write_to_device(WIRES_I2C_NUM, WIRES_I2C_ADDR, ®, 1, (100 / portTICK_PERIOD_MS))); } diff --git a/main/drivers/wires.h b/main/drivers/wires.h index 77aa1ab..6446344 100644 --- a/main/drivers/wires.h +++ b/main/drivers/wires.h @@ -7,6 +7,7 @@ #include #include "drivers/char_lcd.h" #include "drivers/game_timer.h" +#include "main.h" #define WIRES_PIN_DELTA GPIO_NUM_2 #define WIRES_I2C_NUM I2C_NUM_1 @@ -15,6 +16,10 @@ #define DELTA_BIT_WIRES 0 #define DELTA_BIT_BUTTON 1 +extern const uint32_t STRIKE_TIME_PENALTY; +extern uint32_t step_strikes[N_STEPS]; +extern uint32_t step_finish_times[N_STEPS]; + extern uint32_t total_strikes; void init_wires(void); diff --git a/main/helper.cpp b/main/helper.cpp index 318c0d5..9c935ff 100644 --- a/main/helper.cpp +++ b/main/helper.cpp @@ -89,40 +89,106 @@ void do_star_codes(StarCodeHandler* star_codes, int star_codes_len) { } } -static lv_style_t game_results_style; -static lv_obj_t* game_results_label; +static lv_obj_t* old_scr; +static lv_obj_t* scr; + +static lv_style_t game_results_style; +static lv_obj_t* overall_results_label; +static lv_obj_t* breakdown_results_label; +static lv_obj_t* strike_numbers_label; +static lv_obj_t* step_times_label; + +static void write_time(char* buf, int32_t game_time) { + char minus[2] = "\0"; + if (game_time < 0) { + minus[0] = '-'; + game_time = -game_time; + } -static char str_buf[10] = {0}; -static char* write_time(uint32_t game_time) { uint8_t hours = (game_time / (1000*60*60)) % 10; uint8_t minutes = (game_time / (1000*60)) % 60; uint8_t seconds = (game_time / (1000)) % 60; - sprintf(str_buf, "%d:%02d:%02d", hours, minutes, seconds); - return str_buf; + sprintf(buf, "%s%d:%02d:%02d", minus, hours, minutes, seconds); } extern uint32_t initial_game_time; void display_game_results(void) { - if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) { - lv_style_init(&game_results_style); - lv_style_set_text_color(&game_results_style, lv_color_white()); - lv_style_set_bg_color(&game_results_style, lv_color_black()); - lv_style_set_bg_opa(&game_results_style, LV_OPA_100); - lv_style_set_text_align(&game_results_style, LV_TEXT_ALIGN_CENTER); + while (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdFALSE) vTaskDelay(pdMS_TO_TICKS(10)); + scr = lv_obj_create(NULL); + lv_obj_set_style_bg_color(scr, lv_color_black(), LV_STATE_DEFAULT); + + lv_style_init(&game_results_style); + lv_style_set_text_color(&game_results_style, lv_color_white()); + // lv_style_set_bg_color(&game_results_style, lv_color_black()); + lv_style_set_text_align(&game_results_style, LV_TEXT_ALIGN_LEFT); - game_results_label = lv_label_create(lv_scr_act()); - lv_obj_align(game_results_label, LV_ALIGN_CENTER, 0, 0); - lv_obj_add_style(game_results_label, &game_results_style, LV_STATE_DEFAULT); + overall_results_label = lv_label_create(scr); + lv_obj_align(overall_results_label, LV_ALIGN_TOP_LEFT, 20, 20); + lv_obj_set_size(overall_results_label, 460, 140); + lv_obj_add_style(overall_results_label, &game_results_style, LV_STATE_DEFAULT); - char buf[100] = {0}; - int32_t time_s = get_game_time(); - int32_t time_spent = initial_game_time - time_s; - char* time = write_time(time_spent); - sprintf(buf, "Finished!\nTotal Time (w/ penalties): %s\nTotal Strikes: %ld", time, total_strikes); + breakdown_results_label = lv_label_create(scr); + lv_obj_align(breakdown_results_label, LV_ALIGN_BOTTOM_LEFT, 20, 0); + lv_obj_set_size(breakdown_results_label, 460, 140); + lv_obj_add_style(breakdown_results_label, &game_results_style, LV_STATE_DEFAULT); - lv_label_set_text(game_results_label, buf); + strike_numbers_label = lv_label_create(scr); + lv_obj_align(strike_numbers_label, LV_ALIGN_BOTTOM_LEFT, 90, 0); + lv_obj_set_size(strike_numbers_label, 60, 140); + lv_obj_add_style(strike_numbers_label, &game_results_style, LV_STATE_DEFAULT); + lv_obj_set_style_text_align(strike_numbers_label, LV_TEXT_ALIGN_RIGHT, LV_STATE_DEFAULT); - xSemaphoreGive(xGuiSemaphore); - } + step_times_label = lv_label_create(scr); + lv_obj_align(step_times_label, LV_ALIGN_BOTTOM_LEFT, 140, 0); + lv_obj_set_size(step_times_label, 80, 140); + lv_obj_add_style(step_times_label, &game_results_style, LV_STATE_DEFAULT); + lv_obj_set_style_text_align(step_times_label, LV_TEXT_ALIGN_RIGHT, LV_STATE_DEFAULT); + + char buf[1024] = {0}; + + char time_buf_1[32] = {0}; + char time_buf_2[32] = {0}; + char time_buf_3[32] = {0}; + char time_buf_4[32] = {0}; + char time_buf_5[32] = {0}; + char time_buf_6[32] = {0}; + + int32_t time_left = get_game_time(); + int32_t time_spent = initial_game_time - time_left; + int32_t true_time_spent = time_spent - (total_strikes * STRIKE_TIME_PENALTY); + write_time(time_buf_1, time_left); + write_time(time_buf_2, time_spent); + write_time(time_buf_3, true_time_spent); + + sprintf(buf, + "Finished!\n\tTotal Strikes: %ld\n\tTime Left (w/ penalties): %s\n\tTime Spent (w/ penalties): %s\n\tTrue Time Spent: %s\n\n", + total_strikes, time_buf_1, time_buf_2, time_buf_3 + ); + lv_label_set_text(overall_results_label, buf); + + lv_label_set_text(breakdown_results_label, "Step Breakdown:\n\tStep 1: \n\tStep 2:\n\tStep 3:\n\tStep 4:\n\tStep 5:\n\tStep 6:"); + + sprintf(buf, + "\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes\n%ld strikes", + step_strikes[0], step_strikes[1], step_strikes[2], step_strikes[3], step_strikes[4], step_strikes[5] + ); + lv_label_set_text(strike_numbers_label, buf); + + write_time(time_buf_1, initial_game_time - step_finish_times[0]); + write_time(time_buf_2, step_finish_times[0] - step_finish_times[1]); + write_time(time_buf_3, step_finish_times[1] - step_finish_times[2]); + write_time(time_buf_4, step_finish_times[2] - step_finish_times[3]); + write_time(time_buf_5, step_finish_times[3] - step_finish_times[4]); + write_time(time_buf_6, step_finish_times[4] - step_finish_times[5]); + sprintf(buf, + "\n%s\n%s\n%s\n%s\n%s\n%s", + time_buf_1, time_buf_2, time_buf_3, time_buf_4, time_buf_5, time_buf_6 + ); + lv_label_set_text(step_times_label, buf); + + old_scr = lv_scr_act(); + lv_scr_load(scr); + + xSemaphoreGive(xGuiSemaphore); } diff --git a/main/main.cpp b/main/main.cpp index 26d762e..781395f 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,3 +1,5 @@ +#include "main.h" + #include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" @@ -17,8 +19,10 @@ #include "steps/step6.h" static const char *TAG = "main"; -uint32_t initial_game_time = 90*60*1000; +uint32_t initial_game_time = 90*60*1000 + 1000; uint32_t skip_to_step = 0; +uint32_t current_step = 0; +extern uint32_t total_strikes; extern "C" void app_main(void) { printf("app_main\n"); @@ -31,20 +35,34 @@ extern "C" void app_main(void) { clean_bomb(); step0(); - set_game_time(initial_game_time + 1000); + set_game_time(initial_game_time); start_game_timer(); + total_strikes = 0; clean_bomb(); + current_step = 1; if (skip_to_step <= 1) step1(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); + current_step = 2; if (skip_to_step <= 2) step2(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); + current_step = 3; if (skip_to_step <= 3) step3(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); + current_step = 4; if (skip_to_step <= 4) step4(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); + current_step = 5; if (skip_to_step <= 5) step5(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); + current_step = 6; if (skip_to_step <= 6) step6(); + start_game_timer(); + step_finish_times[current_step-1] = get_game_time(); clean_bomb(); stop_game_timer(); @@ -53,4 +71,3 @@ extern "C" void app_main(void) { display_game_results(); } - diff --git a/main/main.h b/main/main.h new file mode 100644 index 0000000..2ff58c6 --- /dev/null +++ b/main/main.h @@ -0,0 +1,8 @@ +#ifndef MAIN_H +#define MAIN_H + +#include + +constexpr size_t N_STEPS = 6; + +#endif /* MAIN_H */ \ No newline at end of file