refactor and doc for bottom_half.cpp and others

This commit is contained in:
Mitchell Marino 2025-03-25 18:11:33 -05:00
parent af93abb8cb
commit b594d6fcb3
10 changed files with 113 additions and 66 deletions

View File

@ -4,8 +4,11 @@ static void init_i2c();
void init_drivers() { void init_drivers() {
init_i2c(); init_i2c();
// init char_lcd so we can report any issues on it. // init char_lcd so we can use it to report other initialization errors.
init_char_lcd(); init_char_lcd();
// init the bottom half so that we can get user input.
init_bottom_half();
init_sd();
} }
/// @brief Initializes I2C_NUM_0. /// @brief Initializes I2C_NUM_0.

View File

@ -1,7 +1,20 @@
#ifndef ALL_H #ifndef ALL_H
#define ALL_H #define ALL_H
// #include "driver/uart.h"
// #include "driver/i2c.h"
// #include "drivers/tft.h"
// #include "drivers/wires.h"
// #include "drivers/sd.h"
// #include "drivers/game_timer.h"
// #include "drivers/speaker.h"
// #include "drivers/char_lcd.h"
// #include "drivers/leds.h"
// #include "drivers/power.h"
#include "char_lcd.h" #include "char_lcd.h"
#include "drivers/bottom_half.h"
#include "sd.h" #include "sd.h"
void init_drivers(); void init_drivers();

View File

@ -173,6 +173,7 @@ static void receive_keypad(void);
static void receive_button(void); static void receive_button(void);
static void receive_touch(void); static void receive_touch(void);
// TODO: add intrupt on bottom half delta pin
// static void IRAM_ATTR gpio_isr_handler(void* arg) // static void IRAM_ATTR gpio_isr_handler(void* arg)
// { // {
// // TODO: do somthing // // TODO: do somthing
@ -180,8 +181,6 @@ static void receive_touch(void);
// } // }
void init_bottom_half() { void init_bottom_half() {
gpio_config_t delta_pin_conf = {}; gpio_config_t delta_pin_conf = {};
// delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL; // delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL;
delta_pin_conf.mode = GPIO_MODE_INPUT; delta_pin_conf.mode = GPIO_MODE_INPUT;

View File

@ -13,6 +13,7 @@
#define DELTA_BIT_BUTTON 1 #define DELTA_BIT_BUTTON 1
#define DELTA_BIT_TOUCH 2 #define DELTA_BIT_TOUCH 2
/// @brief An enum for the possible keypad buttons.
typedef enum { typedef enum {
k1 = 0, k1 = 0,
k4 = 1, k4 = 1,
@ -32,13 +33,19 @@ typedef enum {
kd = 15, kd = 15,
} KeypadKey; } KeypadKey;
/// @brief An enum for the possible buttons.
typedef enum { typedef enum {
b1 = 0, b1 = 0,
b2 = 1, b2 = 1,
b3 = 2, b3 = 2,
b4 = 3, b4 = 3,
green = 0,
red = 1,
yellow = 2,
blue = 3,
} ButtonKey; } ButtonKey;
/// @brief An enum for the possible switches.
typedef enum { typedef enum {
s1 = 0, s1 = 0,
s2 = 1, s2 = 1,
@ -46,25 +53,66 @@ typedef enum {
s4 = 3, s4 = 3,
} SwitchKey; } SwitchKey;
void clear_all_pressed_released(void); /// @brief Initializes communication with the bottom half.
bool get_pressed_keypad(KeypadKey* kp);
bool get_released_keypad(KeypadKey* kp);
char char_of_keypad_key(KeypadKey kp);
bool get_pressed_button(ButtonKey* button);
bool get_released_button(ButtonKey* button);
uint8_t get_button_state();
bool get_flipped_up_switch(SwitchKey* switch_);
bool get_flipped_down_switch(SwitchKey* switch_);
bool get_flipped_switch(SwitchKey* switch_);
uint8_t get_switch_state();
bool get_touch_state(void);
bool get_touch_pressed(void);
bool get_touch_released(void);
void init_bottom_half(); void init_bottom_half();
/// Clears all pending pressed/released switches/buttons/keys/touch sensors.
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);
/// @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);
/// @brief Converts a `KeypadKey` to a char
/// @param kp The value to convert
/// @return The char representing the key
char char_of_keypad_key(KeypadKey kp);
// TODO: add a get_keypad state?
/// @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);
/// @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);
/// @brief Gets the raw state of the buttons b1-b4 as bitflags.
/// B1 is MSB and B4 is LSB.
/// @return Bitflags for b1-b4.
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_);
/// @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_);
/// @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_);
/// @brief Gets the raw state of the switches s1-s4 as bitflags.
/// S1 is MSB and S4 is LSB.
/// @return Bitflags for s1-s4.
uint8_t get_switch_state();
/// @brief Gets the state of the fingerprint sensor
/// @return true if the fingerprint sensor is touched
bool get_touch_state();
/// @brief Gets whether or not the touch sensor was just pressed
/// @return true if the touch sensor was just pressed
bool get_touch_pressed();
/// @brief Gets whether or not the touch sensor was just released
/// @return true if the touch sensor was just released
bool get_touch_released();
// TODO: add touch sensor for switch
#endif /* BOTTOM_HALF_HPP */ #endif /* BOTTOM_HALF_HPP */

View File

@ -7,33 +7,30 @@ i2c_lcd_pcf8574_handle_t lcd;
static const char *TAG = "char_lcd"; static const char *TAG = "char_lcd";
/// Initializes the 2004 Character LCD void init_char_lcd() {
void init_char_lcd(void) { ESP_LOGI(TAG, "Initializing LCD");
lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM); lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
lcd_begin(&lcd, LCD_COLS, LCD_ROWS); lcd_begin(&lcd, LCD_COLS, LCD_ROWS);
lcd_set_backlight(&lcd, 255); lcd_set_backlight(&lcd, 255);
ESP_LOGI(TAG, "LCD initialized"); ESP_LOGI(TAG, "LCD initialized!");
} }
/// Clear the LCD
void lcd_clear() { void lcd_clear() {
lcd_clear(&lcd); lcd_clear(&lcd);
} }
/// Move cursor to home position
void lcd_cursor_home() { void lcd_cursor_home() {
lcd_home(&lcd); lcd_home(&lcd);
} }
/// Set cursor position
void lcd_set_cursor_pos(uint8_t col, uint8_t row) { void lcd_set_cursor_pos(uint8_t col, uint8_t row) {
lcd_set_cursor(&lcd, col, row); lcd_set_cursor(&lcd, col, row);
} }
/// Turn the display on/off
void lcd_set_display(bool display) { void lcd_set_display(bool display) {
if (display) { if (display) {
lcd_display(&lcd); lcd_display(&lcd);
@ -42,7 +39,6 @@ void lcd_set_display(bool display) {
} }
} }
/// Turn the cursor's visibility on/off
void lcd_set_cursor_vis(bool cursor) { void lcd_set_cursor_vis(bool cursor) {
if (cursor) { if (cursor) {
lcd_cursor(&lcd); lcd_cursor(&lcd);
@ -51,7 +47,6 @@ void lcd_set_cursor_vis(bool cursor) {
} }
} }
/// Turn blinking cursor on/off
void lcd_set_cursor_blink(bool blink) { void lcd_set_cursor_blink(bool blink) {
if (blink) { if (blink) {
lcd_blink(&lcd); lcd_blink(&lcd);
@ -60,25 +55,20 @@ void lcd_set_cursor_blink(bool blink) {
} }
} }
/// Scroll the display left
void lcd_scroll_display_left() { void lcd_scroll_display_left() {
lcd_scroll_display_left(&lcd); lcd_scroll_display_left(&lcd);
} }
/// Scroll the display right
void lcd_scroll_display_right() { void lcd_scroll_display_right() {
lcd_scroll_display_right(&lcd); lcd_scroll_display_right(&lcd);
} }
/// Set the text to flows automatically left to right
void lcd_left_to_right() { void lcd_left_to_right() {
lcd_left_to_right(&lcd); lcd_left_to_right(&lcd);
} }
/// Set the text to flows automatically right to left
void lcd_right_to_left() { void lcd_right_to_left() {
lcd_right_to_left(&lcd); lcd_right_to_left(&lcd);
} }
// Turn on/off autoscroll
void lcd_set_autoscroll(bool autoscroll) { void lcd_set_autoscroll(bool autoscroll) {
if (autoscroll) { if (autoscroll) {
lcd_autoscroll(&lcd); lcd_autoscroll(&lcd);
@ -87,27 +77,22 @@ void lcd_set_autoscroll(bool autoscroll) {
} }
} }
// Set backlight brightness
void lcd_set_backlight(uint8_t brightness) { void lcd_set_backlight(uint8_t brightness) {
lcd_set_backlight(&lcd, brightness); lcd_set_backlight(&lcd, brightness);
} }
// Create a custom character
void lcd_create_char(uint8_t location, uint8_t charmap[]) { void lcd_create_char(uint8_t location, uint8_t charmap[]) {
lcd_create_char(&lcd, location, charmap); lcd_create_char(&lcd, location, charmap);
} }
// Write a character to the LCD
void lcd_write(uint8_t value) { void lcd_write(uint8_t value) {
lcd_write(&lcd, value); lcd_write(&lcd, value);
} }
// Print a string to the LCD
void lcd_print(const char* str) { void lcd_print(const char* str) {
lcd_print(&lcd, str); lcd_print(&lcd, str);
} }
// Print a string to the LCD at a given pos
void lcd_print(uint8_t col, uint8_t row, const char* str) { void lcd_print(uint8_t col, uint8_t row, const char* str) {
lcd_set_cursor_pos(col, row); lcd_set_cursor_pos(col, row);
lcd_print(&lcd, str); lcd_print(&lcd, str);

View File

@ -10,7 +10,7 @@
#define LCD_ROWS 4 #define LCD_ROWS 4
/// Initializes the 2004 Character LCD /// Initializes the 2004 Character LCD
void init_lcd(void); void init_lcd();
/// Clear the LCD /// Clear the LCD
void lcd_clear(); void lcd_clear();

View File

@ -1,4 +1,6 @@
#include "sd.h" #include "sd.h"
#include "char_lcd.h"
#include "bottom_half.h"
sdmmc_card_t *card; sdmmc_card_t *card;
@ -21,8 +23,6 @@ bool init_sd() {
ESP_LOGI(TAG, "Using SDMMC peripheral"); ESP_LOGI(TAG, "Using SDMMC peripheral");
sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_host_t host = SDMMC_HOST_DEFAULT();
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdmmc_slot_config_t slot_config = { sdmmc_slot_config_t slot_config = {
.clk = SD_PIN_CLK, .clk = SD_PIN_CLK,
.cmd = SD_PIN_CMD, .cmd = SD_PIN_CMD,
@ -40,24 +40,28 @@ bool init_sd() {
.flags = 0 .flags = 0
// .flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP // .flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP
}; };
ESP_LOGI(TAG, "Mounting filesystem"); ESP_LOGI(TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card); ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) { if (ret == ESP_OK) {
if (ret == ESP_FAIL) { ESP_LOGI(TAG, "Filesystem mounted");
ESP_LOGE(TAG, "Failed to mount filesystem. "
"If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
} else { } else {
ESP_LOGE(TAG, "Failed to initialize the card (%s). " ESP_LOGE(TAG, "Failed to mount sd card: %s.", esp_err_to_name(ret));
"Make sure SD card lines have pull-up resistors in place.", 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, 3, "Press Red to format");
// TODO: impl buttons
return false; return false;
} }
ESP_LOGI(TAG, "Filesystem mounted");
// Card has been initialized, print its properties // Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card); sdmmc_card_print_info(stdout, card);
ESP_LOGI(TAG, "SD card initialized!");
return true; return true;
} }

View File

@ -19,6 +19,10 @@ extern sdmmc_card_t *card;
#define SD_PIN_D2 GPIO_NUM_39 #define SD_PIN_D2 GPIO_NUM_39
#define SD_PIN_D3 GPIO_NUM_38 #define SD_PIN_D3 GPIO_NUM_38
/// @brief Initializes the SD card
///
/// This requires the char_lcd to have been initialized.
/// @return
bool init_sd(); bool init_sd();
void deinit_sd(); void deinit_sd();

View File

@ -1,5 +1,6 @@
#include "helper.h" #include "helper.h"
// TODO: move to driver
void clean_bomb(void) { void clean_bomb(void) {
// clear pending inputs // clear pending inputs
clear_all_pressed_released(); clear_all_pressed_released();

View File

@ -4,17 +4,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_rom_gpio.h" #include "esp_rom_gpio.h"
#include "driver/uart.h" #include "drivers/all.h"
#include "driver/i2c.h"
#include "drivers/tft.h"
#include "drivers/wires.h"
#include "drivers/bottom_half.h"
#include "drivers/sd.h"
#include "drivers/game_timer.h"
#include "drivers/speaker.h"
#include "drivers/char_lcd.h"
#include "drivers/leds.h"
#include "drivers/power.h"
#include "helper.h" #include "helper.h"
@ -33,7 +23,7 @@ uint32_t skip_to_step = 0;
extern "C" void app_main(void) { extern "C" void app_main(void) {
printf("app_main\n"); printf("app_main\n");
init_sd(); init_drivers();
init_tft(); init_tft();
init_speaker(); init_speaker();
init_sseg(); init_sseg();