From afdbbeb2336cfeffb3837d9737cefc47686f0214 Mon Sep 17 00:00:00 2001 From: Mitchell M Date: Wed, 28 Aug 2024 17:27:43 -0500 Subject: [PATCH] change set_game_time --- main/main.cpp | 2 - main/steps/step0.cpp | 93 +++++++++++++++++++++++++------------------- main/steps/step2.cpp | 6 ++- main/steps/step4.cpp | 4 +- 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 902a081..76ddc32 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -74,8 +74,6 @@ extern "C" void app_main(void) { init_bottom_half(); init_char_lcd(); - // debug_switches(); - clean_bomb(); step0(); set_game_time(initial_game_time + 1000); diff --git a/main/steps/step0.cpp b/main/steps/step0.cpp index 0217f35..9a9f33c 100644 --- a/main/steps/step0.cpp +++ b/main/steps/step0.cpp @@ -22,6 +22,8 @@ static void issue_strike(void) { strike("Strike Issued"); } /// Wait for "*9819" void step0(void) { + led_strip_set_pixel(leds, Led::speaker, 0, 0, 20); + led_strip_refresh(leds); StarCodeHandler star_codes[] = { { .code = "*9819", @@ -124,54 +126,60 @@ void step0(void) { do_star_codes(star_codes, len); } -static const char* SECONDS_OR_MINUTES[] = { - "Minutes", - "Seconds", -}; - -static const int STRING_MAX_LEN = 8; -static void set_game_time(void) { - KeypadKey key; - bool seconds = true; - - int current = initial_game_time / 1000; - char str_buf[21] = {0}; +static const int CURSOR_POS_MAP[5] = {1, 3, 4, 6, 7}; +static char str_buf[18] = {0}; +static void _update_display(uint8_t* digits, uint8_t cursor_pos) { + sprintf(str_buf, "%d:%d%d:%d%d", digits[0], digits[1], digits[2], digits[3], digits[4]); lcd_clear(&lcd); lcd_set_cursor(&lcd, 1, 1); - sprintf(str_buf, "%d", current); lcd_print(&lcd, str_buf); - lcd_set_cursor(&lcd, 1, 2); - lcd_print(&lcd, SECONDS_OR_MINUTES[seconds]); + cursor_pos = MAX(0, MIN(4, cursor_pos)); + int mapped_cursor_pos = CURSOR_POS_MAP[cursor_pos]; + lcd_set_cursor(&lcd, mapped_cursor_pos, 1); +} +static void set_game_time(void) { + uint8_t hours = (initial_game_time / (1000*60*60)) % 10; + uint8_t minutes = (initial_game_time / (1000*60)) % 60; + uint8_t seconds = (initial_game_time / (1000)) % 60; + uint8_t digits[5] = {hours, (uint8_t)(minutes / 10), (uint8_t)(minutes % 10), (uint8_t)(seconds / 10), (uint8_t)(seconds % 10)}; + uint8_t cursor_pos = 0; + lcd_cursor(&lcd); + + _update_display(digits, cursor_pos); + + KeypadKey key; + ButtonKey button; while (1) { while (get_pressed_keypad(&key)) { if (key == KeypadKey::star) { - current = 0; + digits[0] = 0; + digits[1] = 0; + digits[2] = 0; + digits[3] = 0; + digits[4] = 0; + cursor_pos = 0; } else if (key == KeypadKey::pound) { // submit - if (current == 0) { - clean_bomb(); - return; + if (digits[0] != 0 || digits[1] != 0 || digits[2] != 0 || digits[3] != 0 || digits[4] != 0) { + uint32_t new_game_time = 0; + new_game_time += digits[0] * (1000*60*60); + new_game_time += (digits[1] * 10 + digits[2]) * (1000*60); + new_game_time += (digits[3] * 10 + digits[4]) * (1000); + initial_game_time = new_game_time; } - if (seconds) { - initial_game_time = current * 1000; - } else { - initial_game_time = current * 60*1000; - } clean_bomb(); + led_strip_set_pixel(leds, Led::speaker, 0, 0, 20); + led_strip_refresh(leds); return; - } else if (key == KeypadKey::ka) { - if (seconds) { - current = current / 60; - } else { - current = current * 60; - } - seconds = !seconds; } else { - int just_pressed = 0; + int just_pressed = -1; switch (key) { + case KeypadKey::k0: + just_pressed = 0; + break; case KeypadKey::k1: just_pressed = 1; break; @@ -203,15 +211,22 @@ static void set_game_time(void) { break; } - current = (current * 10) + just_pressed; + if (just_pressed != -1) { + digits[cursor_pos] = just_pressed; + cursor_pos = MIN(4, cursor_pos+1); + } } - lcd_clear(&lcd); - lcd_set_cursor(&lcd, 1, 1); - sprintf(str_buf, "%d", current); - lcd_print(&lcd, str_buf); - lcd_set_cursor(&lcd, 1, 2); - lcd_print(&lcd, SECONDS_OR_MINUTES[seconds]); + _update_display(digits, cursor_pos); + } + + while (get_pressed_button(&button)) { + if (button == ButtonKey::b1) { + cursor_pos = MAX(0, cursor_pos-1); + } else if (button == ButtonKey::b2) { + cursor_pos = MIN(4, cursor_pos+1); + } + _update_display(digits, cursor_pos); } vTaskDelay(pdMS_TO_TICKS(10)); diff --git a/main/steps/step2.cpp b/main/steps/step2.cpp index f593cd4..2709837 100644 --- a/main/steps/step2.cpp +++ b/main/steps/step2.cpp @@ -2,6 +2,8 @@ static const char *TAG = "step2"; +static const int NUM_SOLVES = 4; + // one: 0b00000110 // seven: 0b00000111 static const uint8_t SSEG_NUMS[8] = {0b00111111, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b01111111, 0b01101111}; @@ -90,7 +92,7 @@ void step2(void) { new_puzzle(); int strike_time = xTaskGetTickCount(); bool striked = false; - while(solved_times < 4) { + while(solved_times < NUM_SOLVES) { // for every bit in the answer- set_module_sseg_raw(display_map); if (get_pressed_keypad(&key)) { @@ -100,7 +102,7 @@ void step2(void) { if (c == answer_char) { play_raw(MOUNT_POINT "/correct.pcm"); solved_times++; - if (solved_times < 3) { + if (solved_times < NUM_SOLVES) { clean_bomb(); new_puzzle(); } else { diff --git a/main/steps/step4.cpp b/main/steps/step4.cpp index 37a5cb7..1889a34 100644 --- a/main/steps/step4.cpp +++ b/main/steps/step4.cpp @@ -220,7 +220,7 @@ bool play_game(int time, int required_score) { TickType_t last_repeat = 0; while(game) { - if (get_pressed_button(&button)) { + while (get_pressed_button(&button)) { switch (button) { case ButtonKey::b1: move_left(); @@ -245,7 +245,7 @@ bool play_game(int time, int required_score) { return true; } } - if (get_released_button(&button)) { + while (get_released_button(&button)) { if (button == ButtonKey::b3) { down_held = 0; }