work on header

This commit is contained in:
Mitchell Marino 2025-07-13 14:37:03 -05:00
parent 4a47558ef6
commit 4e75aeb69c
10 changed files with 81 additions and 36 deletions

View File

@ -256,4 +256,4 @@ void lcd_print_header() {
lcd_print_header_bat();
}
// TODO: add task to continoususly print the header.
// TODO: add task to continoususly print the battery header.

View File

@ -16,5 +16,7 @@ void reset_game_state() {
}
void lcd_print_header_step() {
if (!lcd_header_enabled()) return;
lcd_print(11, 1, game_state);
}

View File

@ -55,6 +55,8 @@ uint16_t get_bat_voltage() {
}
void lcd_print_header_bat() {
if (!lcd_header_enabled()) return;
uint8_t soc = lipo.soc();
char buf[5];
if (soc < 5) {

View File

@ -6,24 +6,42 @@
#include <esp_log.h>
#include "drivers/bottom_half.h"
#include "char_lcd.h"
#include "esp_timer.h"
static const char* TAG = "star_code";
volatile bool handling_new_starcodes = false;
static volatile bool system_initialized = false;
// TODO: use the semaphore, convert to RWLock?
static volatile SemaphoreHandle_t star_codes_sem;
static std::vector<StarCodeEntry> star_codes;
static const char EMPTY_STAR_CODE_HEADER[] = " ";
esp_timer_handle_t starcode_delay_timer;
static volatile StarCodeEntry* processing_starcode = nullptr;
static volatile bool doing_starcode = false;
static uint16_t starcode_waiting_on_release;
static char current[STARCODE_MAX_LEN + 1];
static size_t current_idx;
static void starcode_trigger_cb(void* arg) {
(void) arg;
if (processing_starcode->triggered_sem != nullptr)
xSemaphoreGive(processing_starcode->triggered_sem);
if (processing_starcode->callback != nullptr)
(processing_starcode->callback)();
processing_starcode = nullptr;
// TODO: rename star code everywhere to starcode
lcd_print_header_star_code();
}
void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) {
if (handling_new_starcodes && (*just_pressed | (1 << KeypadKey::star))) {
if ((processing_starcode == nullptr) && handling_new_starcodes && (*just_pressed | (1 << KeypadKey::star))) {
current_idx = 0;
current[current_idx] = '\0';
doing_starcode = true;
@ -39,6 +57,7 @@ void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) {
current[current_idx] = '\0';
} else if (key == KeypadKey::pound) {
doing_starcode = false;
lcd_print_header_star_code();
if (current_idx != 0) {
trigger_star_code(current);
}
@ -53,6 +72,7 @@ void star_code_handle_keypad(uint16_t* just_pressed, uint16_t* just_released) {
// append the character
current[current_idx++] = char_of_keypad_key(key);
current[current_idx] = '\0';
lcd_print_header_star_code();
}
}
}
@ -67,6 +87,16 @@ void init_star_code_system() {
star_codes_sem = xSemaphoreCreateBinary();
xSemaphoreGive(star_codes_sem);
const esp_timer_create_args_t timer_args = {
.callback = &starcode_trigger_cb,
.arg = nullptr,
.dispatch_method = ESP_TIMER_TASK,
.name = "starcode_trigger",
.skip_unhandled_events = false,
};
esp_timer_create(&timer_args, &starcode_delay_timer);
handling_new_starcodes = true;
system_initialized = true;
}
@ -169,13 +199,14 @@ bool trigger_star_code(const char* code) {
return false;
}
// TODO: do display text
// TODO: do delay ticks
processing_starcode = &*it;
if (it->triggered_sem != nullptr)
xSemaphoreGive(it->triggered_sem);
if (it->callback != nullptr)
(it->callback)();
// print display text
if (processing_starcode->display_text != nullptr) {
lcd_print_header_star_code();
}
esp_timer_start_once(starcode_delay_timer, processing_starcode->delay_us);
return true;
}
@ -190,9 +221,19 @@ void resume_star_code_system() {
}
void lcd_print_header_star_code() {
if (doing_starcode) {
if (!lcd_header_enabled()) return;
if (processing_starcode != nullptr) {
if (processing_starcode->display_text != nullptr) {
char buf[STARCODE_MAX_LEN + 2];
snprintf(buf, sizeof(buf), "%s", processing_starcode->display_text);
lcd_print(1, 1, processing_starcode->display_text);
} else {
lcd_print(1, 1, EMPTY_STAR_CODE_HEADER);
}
} else if (doing_starcode) {
char buf[STARCODE_MAX_LEN + 2];
sprintf(buf, "*%-8s", current);
snprintf(buf, sizeof(buf), "*%-8s", current);
lcd_print(1, 1, buf);
} else {
lcd_print(1, 1, EMPTY_STAR_CODE_HEADER);

View File

@ -20,8 +20,8 @@ struct StarCodeEntry {
///
/// This must be <= 9 characters.
const char* display_text;
/// @brief The number of ticks to delay when the star code is entered before calling the handler.
uint32_t delay_ticks;
/// @brief The number of microseconds to delay when the star code is entered before calling the handler.
uint64_t delay_us;
/// @brief The function to call when the star code is entered.
/// Can be null.
void (*callback)(void);

View File

@ -32,7 +32,7 @@ void init_wires(void) {
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 100000,
}
},
};
gpio_reset_pin(GPIO_NUM_41);

View File

@ -51,140 +51,140 @@ void step0() {
{
.code = "9819",
.display_text = "Defusal Initiated",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = nullptr,
.triggered_sem = nullptr,
},
{
.code = "59862",
.display_text = "Set Up Wires",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = setup_wires,
.triggered_sem = nullptr,
},
{
.code = "59862",
.display_text = "Set Game Time",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = set_game_time,
.triggered_sem = nullptr,
},
{
.code = "59863",
.display_text = "Debug Switches",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = debug_switches,
.triggered_sem = nullptr,
},
{
.code = "59864",
.display_text = "Battery Stats",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = battery_stats,
.triggered_sem = nullptr,
},
{
.code = "59871",
.display_text = "Try Step 1",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step1,
.triggered_sem = nullptr,
},
{
.code = "59872",
.display_text = "Try Step 2",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step2,
.triggered_sem = nullptr,
},
{
.code = "59873",
.display_text = "Try Step 3",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step3,
.triggered_sem = nullptr,
},
{
.code = "59874",
.display_text = "Try Step 4",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step4,
.triggered_sem = nullptr,
},
{
.code = "59875",
.display_text = "Try Step 5",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step5,
.triggered_sem = nullptr,
},
{
.code = "59876",
.display_text = "Try Step 6",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = try_step6,
.triggered_sem = nullptr,
},
{
.code = "59881",
.display_text = "Skip To Step 1",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step1,
.triggered_sem = nullptr,
},
{
.code = "59882",
.display_text = "Skip To Step 2",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step2,
.triggered_sem = nullptr,
},
{
.code = "59883",
.display_text = "Skip To Step 3",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step3,
.triggered_sem = nullptr,
},
{
.code = "59884",
.display_text = "Skip To Step 4",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step4,
.triggered_sem = nullptr,
},
{
.code = "59885",
.display_text = "Skip To Step 5",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step5,
.triggered_sem = nullptr,
},
{
.code = "59886",
.display_text = "Skip To Step 6",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = skip_to_step6,
.triggered_sem = nullptr,
},
{
.code = "1111",
.display_text = "Issue Strike",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = issue_strike,
.triggered_sem = nullptr,
},
{
.code = "1112",
.display_text = "????",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = flashbang,
.triggered_sem = nullptr,
},
{
.code = "1113",
.display_text = "replay_last",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = replay_last,
.triggered_sem = nullptr,
},

View File

@ -82,7 +82,7 @@ void step3(void) {
StarCodeEntry start_code = {
.code = "1642",
.display_text = "Starting...",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = nullptr,
.triggered_sem = continue_sem,
};

View File

@ -389,7 +389,7 @@ void step4() {
StarCodeEntry start_code = {
.code = "3850",
.display_text = "Starting...",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = nullptr,
.triggered_sem = continue_sem,
};

View File

@ -187,7 +187,7 @@ void step5(void) {
StarCodeEntry start_code = {
.code = "2648",
.display_text = "Starting...",
.delay_ticks = pdMS_TO_TICKS(2000),
.delay_us = 2'000'000,
.callback = nullptr,
.triggered_sem = continue_sem,
};