Compare commits

...

2 Commits

Author SHA1 Message Date
42aa5fa315 small tweaks to drivers, starting speaker refactor 2025-03-25 21:42:10 -05:00
ab7148be42 rename functions 2025-03-25 21:20:19 -05:00
15 changed files with 193 additions and 169 deletions

View File

@ -1,5 +1,6 @@
#include "all.h"
static const char *TAG = "driver_all";
static void init_i2c();
void init_drivers() {
@ -9,6 +10,8 @@ void init_drivers() {
// init the bottom half so that we can get user input.
init_bottom_half();
init_sd();
init_speaker();
}
/// @brief Initializes I2C_NUM_0.
@ -21,6 +24,8 @@ void init_drivers() {
/// - The PERH port
/// - The Capacitive Touch Panel
static void init_i2c() {
ESP_LOGI(TAG, "Initializing i2c...");
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_5,
@ -37,4 +42,6 @@ static void init_i2c() {
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0));
ESP_LOGI(TAG, "i2c initialized!");
}

View File

@ -36,6 +36,8 @@ static void receive_touch();
// }
void init_bottom_half() {
ESP_LOGI(TAG, "Initializing bottom half...");
ESP_ERROR_CHECK(gpio_set_direction(BOTTOM_PIN_INTERUPT, GPIO_MODE_INPUT));
ESP_ERROR_CHECK(gpio_set_pull_mode(BOTTOM_PIN_INTERUPT, GPIO_PULLUP_ONLY));
@ -52,6 +54,8 @@ void init_bottom_half() {
receive_touch();
xTaskCreate(poll_bottom_task, "poll_bottom", 4096, NULL, 10, NULL);
ESP_LOGI(TAG, "Bottom half initialized!");
}
static uint8_t receive_delta(void) {
@ -173,10 +177,10 @@ static bool _take_key(KeypadKey* kp, uint16_t* keypad_bitfield) {
return false;
}
bool get_pressed_keypad(KeypadKey* kp) {
bool get_keypad_pressed(KeypadKey* kp) {
return _take_key(kp, &keypad_pressed);
}
bool get_released_keypad(KeypadKey* kp) {
bool get_keypad_released(KeypadKey* kp) {
return _take_key(kp, &keypad_released);
}
@ -254,23 +258,23 @@ static bool _take_switch(SwitchKey* switch_, uint8_t* switch_bitfield) {
return false;
}
bool get_pressed_button(ButtonKey* button) {
bool get_button_pressed(ButtonKey* button) {
return _take_button(button, &button_pressed);
}
bool get_released_button(ButtonKey* button) {
bool get_button_released(ButtonKey* button) {
return _take_button(button, &button_released);
}
uint8_t get_button_state() {
return button_state;
}
bool get_flipped_up_switch(SwitchKey* switch_) {
bool get_switch_flipped_up(SwitchKey* switch_) {
return _take_switch(switch_, &switch_flipped_up);
}
bool get_flipped_down_switch(SwitchKey* switch_) {
bool get_switch_flipped_down(SwitchKey* switch_) {
return _take_switch(switch_, &switch_flipped_down);
}
bool get_flipped_switch(SwitchKey* switch_) {
bool get_switch_flipped(SwitchKey* switch_) {
for (int i = 0; i < 4; i++) {
int bit_selector = (1 << i);
if (

View File

@ -62,11 +62,11 @@ void clear_all_pressed_released();
/// @brief Gets the key that was just pressed (if any)
/// @param kp an OUT variable for the key that was pressed (if any)
/// @return true if there was a key that was just pressed
bool get_pressed_keypad(KeypadKey* kp);
bool get_keypad_pressed(KeypadKey* kp);
/// @brief Gets the key that was just released (if any)
/// @param kp an OUT variable for the key that was released (if any)
/// @return true if there was a key that was just released
bool get_released_keypad(KeypadKey* kp);
bool get_keypad_released(KeypadKey* kp);
/// @brief Converts a `KeypadKey` to a char
/// @param kp The value to convert
/// @return The char representing the key
@ -74,15 +74,14 @@ char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state?
// TODO: rename these to get_button_pressed.
/// @brief Gets the button that was just pressed (if any)
/// @param button an OUT variable for the button that was pressed (if any)
/// @return true if there was a button that was just pressed
bool get_pressed_button(ButtonKey* button);
bool get_button_pressed(ButtonKey* button);
/// @brief Gets the button that was just released (if any)
/// @param button an OUT variable for the button that was released (if any)
/// @return true if there was a button that was just released
bool get_released_button(ButtonKey* button);
bool get_button_released(ButtonKey* button);
/// @brief Gets the raw state of the buttons b1-b4 as bitflags.
/// B1 is MSB and B4 is LSB.
/// @return Bitflags for b1-b4.
@ -91,15 +90,15 @@ uint8_t get_button_state();
/// @brief Gets the switch that was just flipped up (if any)
/// @param switch_ an OUT variable for the switch that was flipped up (if any)
/// @return true if there was a switch that was just flipped up
bool get_flipped_up_switch(SwitchKey* switch_);
bool get_switch_flipped_up(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped down (if any)
/// @param switch_ an OUT variable for the switch that was flipped down (if any)
/// @return true if there was a switch that was just flipped down
bool get_flipped_down_switch(SwitchKey* switch_);
bool get_switch_flipped_down(SwitchKey* switch_);
/// @brief Gets the switch that was just flipped (if any)
/// @param switch_ an OUT variable for the switch that was flipped (if any)
/// @return true if there was a switch that was just flipped
bool get_flipped_switch(SwitchKey* switch_);
bool get_switch_flipped(SwitchKey* switch_);
/// @brief Gets the raw state of the switches s1-s4 as bitflags.
/// S1 is MSB and S4 is LSB.
/// @return Bitflags for s1-s4.

View File

@ -8,7 +8,7 @@ i2c_lcd_pcf8574_handle_t lcd;
static const char *TAG = "char_lcd";
void init_lcd() {
ESP_LOGI(TAG, "Initializing LCD");
ESP_LOGI(TAG, "Initializing LCD...");
lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
lcd_begin(&lcd, LCD_COLS, LCD_ROWS);

View File

@ -8,7 +8,7 @@ static const char* mount_point = MOUNT_POINT;
static const char* TAG = "sd";
bool init_sd() {
ESP_LOGI(TAG, "Initializing SD card");
ESP_LOGI(TAG, "Initializing SD card...");
esp_err_t ret;
@ -40,6 +40,8 @@ bool init_sd() {
.flags = 0
// .flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP
};
try_mount:
ESP_LOGI(TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
@ -48,15 +50,28 @@ bool init_sd() {
} else {
ESP_LOGE(TAG, "Failed to mount sd card: %s.", esp_err_to_name(ret));
lcd_print(0, 0, "SD Mount Failed!");
lcd_print(0, 1, esp_err_to_name(ret));
lcd_print(0, 2, "Press Green to retry");
lcd_print(0, 0, "SD: ");
lcd_print(4, 0, esp_err_to_name(ret));
lcd_print(0, 1, "Press Green to retry");
lcd_print(0, 2, "Press Yellow to skip");
lcd_print(0, 3, "Press Red to format");
// TODO: impl buttons
ButtonKey button;
while (!( get_button_pressed(&button) && (button == ButtonKey::button_green || button == ButtonKey::button_red || button == ButtonKey::button_yellow) )) vTaskDelay(pdMS_TO_TICKS(10));
lcd_clear();
if (button == ButtonKey::button_green) {
goto try_mount;
}
if (button == ButtonKey::button_yellow) {
return false;
}
if (button == ButtonKey::button_red) {
mount_config.format_if_mount_failed = true;
goto try_mount;
}
}
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);

View File

@ -2,12 +2,11 @@
i2s_chan_handle_t tx_chan;
static const char *TAG = "speaker_driver";
static const char *TAG = "speaker";
esp_err_t play_raw_stop_queue(const char *fp, QueueHandle_t stop_handle) {
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
{
if (fh == NULL) {
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
@ -61,7 +60,6 @@ esp_err_t play_raw_stop_queue(const char *fp, QueueHandle_t stop_handle) {
esp_err_t play_raw(const char *fp) {
FILE *fh = fopen(fp, "rb");
if (fh == NULL)
{
ESP_LOGE(TAG, "Failed to open file");
return ESP_ERR_INVALID_ARG;
}
@ -109,6 +107,8 @@ esp_err_t play_raw(const char *fp) {
}
void init_speaker(void) {
ESP_LOGI(TAG, "Initializing speaker...");
i2s_chan_config_t tx_chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&tx_chan_cfg, &tx_chan, NULL));
@ -129,6 +129,8 @@ void init_speaker(void) {
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &tx_std_cfg));
ESP_LOGI(TAG, "Speaker initialized!");
}
void play_example() {

View File

@ -30,7 +30,7 @@ void do_star_codes(StarCodeHandler* star_codes, int star_codes_len) {
char current[STRING_MAX_LEN+1] = {0};
while (1) {
while (get_pressed_keypad(&key)) {
while (get_keypad_pressed(&key)) {
if (key == KeypadKey::star) {
current[0] = '*';
for (int i = 1; i < STRING_MAX_LEN; i++) {

View File

@ -25,13 +25,10 @@ extern "C" void app_main(void) {
init_drivers();
init_tft();
init_speaker();
init_sseg();
init_game_module_timer();
init_leds();
init_wires();
init_bottom_half();
init_power_board();
clean_bomb();

View File

@ -42,7 +42,7 @@ void setup_wires(void) {
ButtonKey button;
while (1) {
while (get_pressed_keypad(&key)) {
while (get_keypad_pressed(&key)) {
if (key == KeypadKey::k1) {
generate_new_wires(wires);
save_wires_to_sd_card(wires);
@ -53,7 +53,7 @@ void setup_wires(void) {
}
}
while (get_pressed_button(&button)) {
while (get_button_pressed(&button)) {
if (button == ButtonKey::b1) {
// left
editing_idx = editing_idx - 1;

View File

@ -26,7 +26,7 @@ static void battery_stats(void) {
xReturned = xTaskCreate(bat_monitor_task, "bat_monitor", 4096, NULL, 5, &xHandle);
KeypadKey k;
while (!get_pressed_keypad(&k) || k != KeypadKey::pound) vTaskDelay(pdMS_TO_TICKS(10));
while (!get_keypad_pressed(&k) || k != KeypadKey::pound) vTaskDelay(pdMS_TO_TICKS(10));
if (xReturned == pdPASS) {
vTaskDelete(xHandle);
@ -176,7 +176,7 @@ static void set_game_time(void) {
KeypadKey key;
ButtonKey button;
while (1) {
while (get_pressed_keypad(&key)) {
while (get_keypad_pressed(&key)) {
if (key == KeypadKey::star) {
digits[0] = 0;
digits[1] = 0;
@ -244,7 +244,7 @@ static void set_game_time(void) {
_update_display(digits, cursor_pos);
}
while (get_pressed_button(&button)) {
while (get_button_pressed(&button)) {
if (button == ButtonKey::b1) {
cursor_pos = MAX(0, cursor_pos-1);
} else if (button == ButtonKey::b2) {
@ -279,22 +279,22 @@ static void debug_switches(void) {
ButtonKey button;
SwitchKey switch_;
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
sprintf(buf, "Button Pressed: %d ", button);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
}
if (get_released_button(&button)) {
if (get_button_released(&button)) {
sprintf(buf, "Button Released: %d", button);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
}
if (get_flipped_down_switch(&switch_)) {
if (get_switch_flipped_down(&switch_)) {
sprintf(buf, "Switch Down: %d ", switch_);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
}
if (get_flipped_up_switch(&switch_)) {
if (get_switch_flipped_up(&switch_)) {
sprintf(buf, "Switch Up: %d ", switch_);
lcd_print(0, 3, buf);
ESP_LOGI(TAG, "%s", buf);
@ -338,7 +338,7 @@ static void debug_switches(void) {
}
if (get_pressed_keypad(&key) && key == KeypadKey::pound) {
if (get_keypad_pressed(&key) && key == KeypadKey::pound) {
return;
}

View File

@ -229,7 +229,7 @@ static bool play_part(uint32_t time) {
SwitchKey switch_;
while (times < 15) {
while (get_pressed_button(&button)) {
while (get_button_pressed(&button)) {
start_module_timer();
if ((int)(button) == correct) {
times++;
@ -240,7 +240,7 @@ static bool play_part(uint32_t time) {
strike("Incorrect action");
}
}
while (get_flipped_switch(&switch_)) {
while (get_switch_flipped(&switch_)) {
start_module_timer();
if (switch_leds[(int)(switch_)] + 4 == correct) {
times++;
@ -269,7 +269,7 @@ static bool play_part(uint32_t time) {
}
void step1(void) {
while (get_flipped_switch(nullptr));
while (get_switch_flipped(nullptr));
init_step();
while (!play_part(40*1000));

View File

@ -95,7 +95,7 @@ void step2(void) {
while(solved_times < NUM_SOLVES) {
// for every bit in the answer-
set_module_sseg_raw(display_map);
if (get_pressed_keypad(&key)) {
if (get_keypad_pressed(&key)) {
lcd_clear();
char c = char_of_keypad_key(key);
// ESP_LOGI(TAG, "Pressed: %c", c);

View File

@ -78,7 +78,7 @@ void step3(void) {
while (times < TIMES_TO_COMPLETE) {
tone = tone_dist(gen);
// tone = 2;
while (get_pressed_button(nullptr)) vTaskDelay(pdMS_TO_TICKS(10));
while (get_button_pressed(nullptr)) vTaskDelay(pdMS_TO_TICKS(10));
play_raw(MOUNT_POINT "/que.pcm");
play_raw(TONE_FILES[tone]);
@ -193,7 +193,7 @@ static void debug_actual_values(uint8_t buttons, uint8_t switch_) {
static void wait_for_timer(void) {
KeypadKey key;
while (get_module_time() > 0) {
if (get_pressed_keypad(&key) && key == KeypadKey::kd) {
if (get_keypad_pressed(&key) && key == KeypadKey::kd) {
set_module_time(0);
return;
}

View File

@ -206,7 +206,7 @@ bool play_game(int time, int required_score) {
show_board();
ButtonKey button;
while (get_pressed_button(&button));
while (get_button_pressed(&button));
// SwitchKey switch_;
const TickType_t first_repeat_time = pdMS_TO_TICKS(700);
@ -215,7 +215,7 @@ bool play_game(int time, int required_score) {
TickType_t last_repeat = 0;
while(game) {
while (get_pressed_button(&button)) {
while (get_button_pressed(&button)) {
switch (button) {
case ButtonKey::b1:
move_left();
@ -240,7 +240,7 @@ bool play_game(int time, int required_score) {
return true;
}
}
while (get_released_button(&button)) {
while (get_button_released(&button)) {
if (button == ButtonKey::b3) {
down_held = 0;
}
@ -269,7 +269,7 @@ bool play_game(int time, int required_score) {
strike("Out of time");
return false;
}
// if (get_flipped_switch(&switch_)) {
// if (get_switch_flipped(&switch_)) {
// printf("%d\n", piece);
// for (int i = 0; i < sizeof(piece_nodes)/sizeof(piece_nodes[0]); i++) {
// int* p = piece_nodes[i];
@ -295,9 +295,9 @@ static void complete(void) {
xSemaphoreGive(xGuiSemaphore);
}
vTaskDelay(pdMS_TO_TICKS(500));
while (get_pressed_button(nullptr));
while (get_button_pressed(nullptr));
while (1) {
if (get_pressed_button(nullptr)) break;
if (get_button_pressed(nullptr)) break;
vTaskDelay(pdMS_TO_TICKS(10));
}
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {
@ -313,9 +313,9 @@ static void fail(void) {
xSemaphoreGive(xGuiSemaphore);
}
vTaskDelay(pdMS_TO_TICKS(500));
while (get_pressed_button(nullptr));
while (get_button_pressed(nullptr));
while (1) {
if (get_pressed_button(nullptr)) break;
if (get_button_pressed(nullptr)) break;
vTaskDelay(pdMS_TO_TICKS(10));
}
if (xSemaphoreTake(xGuiSemaphore, portMAX_DELAY) == pdTRUE) {

View File

@ -248,7 +248,7 @@ void step5(void) {
// wait for submit
KeypadKey key;
while (1) {
if (get_module_time() <= 0 || (get_pressed_keypad(&key) && (char_of_keypad_key(key) == '#'))) {
if (get_module_time() <= 0 || (get_keypad_pressed(&key) && (char_of_keypad_key(key) == '#'))) {
solved_correctly = submit_0(green_indicators);
break;
}
@ -278,7 +278,7 @@ void step5(void) {
KeypadKey key;
while (1) {
if (get_pressed_keypad(&key)) {
if (get_keypad_pressed(&key)) {
char keypad_char = char_of_keypad_key(key);
if (keypad_char == '#') {
solved_correctly = submit_1(indicators_num, pressed_keys, starting_switch_state);
@ -329,7 +329,7 @@ void step5(void) {
ButtonKey button;
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
uint8_t button_state = get_button_state();
if ((button_state & 0b1) == 0b1) {
@ -355,7 +355,7 @@ void step5(void) {
break;
}
}
if (get_pressed_keypad(&key)) {
if (get_keypad_pressed(&key)) {
char keypad_char = char_of_keypad_key(key);
if (keypad_char == '#') {
solved_correctly = submit_2(lit_leds, green_button_pressed, blue_button_pressed, fingerprint_sensor_pressed, keypad_string);
@ -398,7 +398,7 @@ void step5(void) {
ButtonKey button;
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
buttons_pressed++;
uint8_t button_state = get_button_state();
@ -466,7 +466,7 @@ void step5(void) {
uint8_t starting_switch_state = get_switch_state();
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
uint8_t button_state = get_button_state();
for (int i = 0; i < 4; i++) {
@ -481,7 +481,7 @@ void step5(void) {
}
ESP_ERROR_CHECK(led_strip_refresh(leds));
}
if (get_module_time() <= 0 || (get_pressed_keypad(&key) && (char_of_keypad_key(key) == '#'))) {
if (get_module_time() <= 0 || (get_keypad_pressed(&key) && (char_of_keypad_key(key) == '#'))) {
solved_correctly = submit_4(button_colors, switch_colors, starting_switch_state);
break;
}
@ -514,7 +514,7 @@ void step5(void) {
KeypadKey key;
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
uint8_t button_state = get_button_state();
bool failed = false;
@ -532,7 +532,7 @@ void step5(void) {
break;
}
}
if (get_module_time() <= 0 || (get_pressed_keypad(&key) && (char_of_keypad_key(key) == '#'))) {
if (get_module_time() <= 0 || (get_keypad_pressed(&key) && (char_of_keypad_key(key) == '#'))) {
ESP_LOGI(TAG, "Keypad Char: %c, time: %lu", char_of_keypad_key(key), get_module_time());
solved_correctly = submit_5(indicator_numbers, buttons_pressed);
break;
@ -580,7 +580,7 @@ void step5(void) {
std::uniform_int_distribution<> led_turn_on_dist(0, 3);
while (1) {
if (get_pressed_button(&button)) {
if (get_button_pressed(&button)) {
uint8_t button_state = get_button_state();
bool failed = false;
@ -623,7 +623,7 @@ void step5(void) {
lastCycleTime = xTaskGetTickCount();
}
if (get_module_time() <= 0 || (get_pressed_keypad(&key) && (char_of_keypad_key(key) == '#'))) {
if (get_module_time() <= 0 || (get_keypad_pressed(&key) && (char_of_keypad_key(key) == '#'))) {
solved_correctly = submit_6(buttons_cycling, button_turned_on, led_off);
break;
}
@ -747,7 +747,7 @@ void step5(void) {
KeypadKey key;
while (1) {
if (get_pressed_keypad(&key)) {
if (get_keypad_pressed(&key)) {
if (key == KeypadKey::star) {
// clear
entered_string = "";
@ -828,7 +828,7 @@ void step5(void) {
KeypadKey key;
while (1) {
if (get_pressed_keypad(&key)) {
if (get_keypad_pressed(&key)) {
bool failed = false;
if (key == KeypadKey::star) {