change set_game_time

This commit is contained in:
Mitchell Marino 2024-08-28 17:27:43 -05:00
parent 856f557af6
commit afdbbeb233
4 changed files with 60 additions and 45 deletions

View File

@ -74,8 +74,6 @@ extern "C" void app_main(void) {
init_bottom_half(); init_bottom_half();
init_char_lcd(); init_char_lcd();
// debug_switches();
clean_bomb(); clean_bomb();
step0(); step0();
set_game_time(initial_game_time + 1000); set_game_time(initial_game_time + 1000);

View File

@ -22,6 +22,8 @@ static void issue_strike(void) { strike("Strike Issued"); }
/// Wait for "*9819" /// Wait for "*9819"
void step0(void) { void step0(void) {
led_strip_set_pixel(leds, Led::speaker, 0, 0, 20);
led_strip_refresh(leds);
StarCodeHandler star_codes[] = { StarCodeHandler star_codes[] = {
{ {
.code = "*9819", .code = "*9819",
@ -124,54 +126,60 @@ void step0(void) {
do_star_codes(star_codes, len); 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_clear(&lcd);
lcd_set_cursor(&lcd, 1, 1); lcd_set_cursor(&lcd, 1, 1);
sprintf(str_buf, "%d", current);
lcd_print(&lcd, str_buf); lcd_print(&lcd, str_buf);
lcd_set_cursor(&lcd, 1, 2); cursor_pos = MAX(0, MIN(4, cursor_pos));
lcd_print(&lcd, SECONDS_OR_MINUTES[seconds]); 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 (1) {
while (get_pressed_keypad(&key)) { while (get_pressed_keypad(&key)) {
if (key == KeypadKey::star) { 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) { } else if (key == KeypadKey::pound) {
// submit // submit
if (current == 0) { if (digits[0] != 0 || digits[1] != 0 || digits[2] != 0 || digits[3] != 0 || digits[4] != 0) {
clean_bomb(); uint32_t new_game_time = 0;
return; 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(); clean_bomb();
led_strip_set_pixel(leds, Led::speaker, 0, 0, 20);
led_strip_refresh(leds);
return; return;
} else if (key == KeypadKey::ka) {
if (seconds) {
current = current / 60;
} else {
current = current * 60;
}
seconds = !seconds;
} else { } else {
int just_pressed = 0; int just_pressed = -1;
switch (key) { switch (key) {
case KeypadKey::k0:
just_pressed = 0;
break;
case KeypadKey::k1: case KeypadKey::k1:
just_pressed = 1; just_pressed = 1;
break; break;
@ -203,15 +211,22 @@ static void set_game_time(void) {
break; 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); _update_display(digits, cursor_pos);
lcd_set_cursor(&lcd, 1, 1); }
sprintf(str_buf, "%d", current);
lcd_print(&lcd, str_buf); while (get_pressed_button(&button)) {
lcd_set_cursor(&lcd, 1, 2); if (button == ButtonKey::b1) {
lcd_print(&lcd, SECONDS_OR_MINUTES[seconds]); 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)); vTaskDelay(pdMS_TO_TICKS(10));

View File

@ -2,6 +2,8 @@
static const char *TAG = "step2"; static const char *TAG = "step2";
static const int NUM_SOLVES = 4;
// one: 0b00000110 // one: 0b00000110
// seven: 0b00000111 // seven: 0b00000111
static const uint8_t SSEG_NUMS[8] = {0b00111111, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b01111111, 0b01101111}; static const uint8_t SSEG_NUMS[8] = {0b00111111, 0b01011011, 0b01001111, 0b01100110, 0b01101101, 0b01111101, 0b01111111, 0b01101111};
@ -90,7 +92,7 @@ void step2(void) {
new_puzzle(); new_puzzle();
int strike_time = xTaskGetTickCount(); int strike_time = xTaskGetTickCount();
bool striked = false; bool striked = false;
while(solved_times < 4) { while(solved_times < NUM_SOLVES) {
// for every bit in the answer- // for every bit in the answer-
set_module_sseg_raw(display_map); set_module_sseg_raw(display_map);
if (get_pressed_keypad(&key)) { if (get_pressed_keypad(&key)) {
@ -100,7 +102,7 @@ void step2(void) {
if (c == answer_char) { if (c == answer_char) {
play_raw(MOUNT_POINT "/correct.pcm"); play_raw(MOUNT_POINT "/correct.pcm");
solved_times++; solved_times++;
if (solved_times < 3) { if (solved_times < NUM_SOLVES) {
clean_bomb(); clean_bomb();
new_puzzle(); new_puzzle();
} else { } else {

View File

@ -220,7 +220,7 @@ bool play_game(int time, int required_score) {
TickType_t last_repeat = 0; TickType_t last_repeat = 0;
while(game) { while(game) {
if (get_pressed_button(&button)) { while (get_pressed_button(&button)) {
switch (button) { switch (button) {
case ButtonKey::b1: case ButtonKey::b1:
move_left(); move_left();
@ -245,7 +245,7 @@ bool play_game(int time, int required_score) {
return true; return true;
} }
} }
if (get_released_button(&button)) { while (get_released_button(&button)) {
if (button == ButtonKey::b3) { if (button == ButtonKey::b3) {
down_held = 0; down_held = 0;
} }