diff --git a/main/drivers/all.cpp b/main/drivers/all.cpp index d214208..df11ed4 100644 --- a/main/drivers/all.cpp +++ b/main/drivers/all.cpp @@ -4,8 +4,11 @@ static void init_i2c(); void init_drivers() { 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 the bottom half so that we can get user input. + init_bottom_half(); + init_sd(); } /// @brief Initializes I2C_NUM_0. diff --git a/main/drivers/all.h b/main/drivers/all.h index 4844fe0..7ed3382 100644 --- a/main/drivers/all.h +++ b/main/drivers/all.h @@ -1,7 +1,20 @@ #ifndef 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 "drivers/bottom_half.h" #include "sd.h" void init_drivers(); diff --git a/main/drivers/bottom_half.cpp b/main/drivers/bottom_half.cpp index dfa0652..66b8fc3 100644 --- a/main/drivers/bottom_half.cpp +++ b/main/drivers/bottom_half.cpp @@ -173,6 +173,7 @@ static void receive_keypad(void); static void receive_button(void); static void receive_touch(void); +// TODO: add intrupt on bottom half delta pin // static void IRAM_ATTR gpio_isr_handler(void* arg) // { // // TODO: do somthing @@ -180,8 +181,6 @@ static void receive_touch(void); // } void init_bottom_half() { - - gpio_config_t delta_pin_conf = {}; // delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL; delta_pin_conf.mode = GPIO_MODE_INPUT; diff --git a/main/drivers/bottom_half.h b/main/drivers/bottom_half.h index a63210b..5a21ae4 100644 --- a/main/drivers/bottom_half.h +++ b/main/drivers/bottom_half.h @@ -13,6 +13,7 @@ #define DELTA_BIT_BUTTON 1 #define DELTA_BIT_TOUCH 2 +/// @brief An enum for the possible keypad buttons. typedef enum { k1 = 0, k4 = 1, @@ -32,13 +33,19 @@ typedef enum { kd = 15, } KeypadKey; +/// @brief An enum for the possible buttons. typedef enum { b1 = 0, b2 = 1, b3 = 2, b4 = 3, + green = 0, + red = 1, + yellow = 2, + blue = 3, } ButtonKey; +/// @brief An enum for the possible switches. typedef enum { s1 = 0, s2 = 1, @@ -46,25 +53,66 @@ typedef enum { s4 = 3, } SwitchKey; -void clear_all_pressed_released(void); - -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); - +/// @brief Initializes communication with the 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 */ \ No newline at end of file diff --git a/main/drivers/char_lcd.cpp b/main/drivers/char_lcd.cpp index 3ffdbf6..cf8e3b5 100644 --- a/main/drivers/char_lcd.cpp +++ b/main/drivers/char_lcd.cpp @@ -7,33 +7,30 @@ i2c_lcd_pcf8574_handle_t lcd; static const char *TAG = "char_lcd"; -/// Initializes the 2004 Character LCD -void init_char_lcd(void) { +void init_char_lcd() { + ESP_LOGI(TAG, "Initializing LCD"); + lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM); lcd_begin(&lcd, LCD_COLS, LCD_ROWS); lcd_set_backlight(&lcd, 255); - ESP_LOGI(TAG, "LCD initialized"); + ESP_LOGI(TAG, "LCD initialized!"); } -/// Clear the LCD void lcd_clear() { lcd_clear(&lcd); } -/// Move cursor to home position void lcd_cursor_home() { lcd_home(&lcd); } -/// Set cursor position void lcd_set_cursor_pos(uint8_t col, uint8_t row) { lcd_set_cursor(&lcd, col, row); } -/// Turn the display on/off void lcd_set_display(bool display) { if (display) { 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) { if (cursor) { 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) { if (blink) { lcd_blink(&lcd); @@ -60,25 +55,20 @@ void lcd_set_cursor_blink(bool blink) { } } -/// Scroll the display left void lcd_scroll_display_left() { lcd_scroll_display_left(&lcd); } -/// Scroll the display right void lcd_scroll_display_right() { lcd_scroll_display_right(&lcd); } -/// Set the text to flows automatically left to right void lcd_left_to_right() { lcd_left_to_right(&lcd); } -/// Set the text to flows automatically right to left void lcd_right_to_left() { lcd_right_to_left(&lcd); } -// Turn on/off autoscroll void lcd_set_autoscroll(bool autoscroll) { if (autoscroll) { lcd_autoscroll(&lcd); @@ -87,27 +77,22 @@ void lcd_set_autoscroll(bool autoscroll) { } } -// Set backlight brightness void lcd_set_backlight(uint8_t brightness) { lcd_set_backlight(&lcd, brightness); } -// Create a custom character void lcd_create_char(uint8_t location, uint8_t charmap[]) { lcd_create_char(&lcd, location, charmap); } -// Write a character to the LCD void lcd_write(uint8_t value) { lcd_write(&lcd, value); } -// Print a string to the LCD void lcd_print(const char* 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) { lcd_set_cursor_pos(col, row); lcd_print(&lcd, str); diff --git a/main/drivers/char_lcd.h b/main/drivers/char_lcd.h index 59bb74f..73d562b 100644 --- a/main/drivers/char_lcd.h +++ b/main/drivers/char_lcd.h @@ -10,7 +10,7 @@ #define LCD_ROWS 4 /// Initializes the 2004 Character LCD -void init_lcd(void); +void init_lcd(); /// Clear the LCD void lcd_clear(); diff --git a/main/drivers/sd.cpp b/main/drivers/sd.cpp index 0033e44..d11b4d7 100644 --- a/main/drivers/sd.cpp +++ b/main/drivers/sd.cpp @@ -1,4 +1,6 @@ #include "sd.h" +#include "char_lcd.h" +#include "bottom_half.h" sdmmc_card_t *card; @@ -21,8 +23,6 @@ bool init_sd() { ESP_LOGI(TAG, "Using SDMMC peripheral"); 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 = { .clk = SD_PIN_CLK, .cmd = SD_PIN_CMD, @@ -40,24 +40,28 @@ bool init_sd() { .flags = 0 // .flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP }; - ESP_LOGI(TAG, "Mounting filesystem"); ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card); - if (ret != ESP_OK) { - if (ret == ESP_FAIL) { - 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 { - ESP_LOGE(TAG, "Failed to initialize the card (%s). " - "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret)); - } + if (ret == ESP_OK) { + ESP_LOGI(TAG, "Filesystem mounted"); + } 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, 3, "Press Red to format"); + + // TODO: impl buttons + return false; } - ESP_LOGI(TAG, "Filesystem mounted"); // Card has been initialized, print its properties sdmmc_card_print_info(stdout, card); + + ESP_LOGI(TAG, "SD card initialized!"); return true; } diff --git a/main/drivers/sd.h b/main/drivers/sd.h index c051159..24ceb3d 100644 --- a/main/drivers/sd.h +++ b/main/drivers/sd.h @@ -19,6 +19,10 @@ extern sdmmc_card_t *card; #define SD_PIN_D2 GPIO_NUM_39 #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(); void deinit_sd(); diff --git a/main/helper.cpp b/main/helper.cpp index 1f9a209..7136791 100644 --- a/main/helper.cpp +++ b/main/helper.cpp @@ -1,5 +1,6 @@ #include "helper.h" +// TODO: move to driver void clean_bomb(void) { // clear pending inputs clear_all_pressed_released(); diff --git a/main/main.cpp b/main/main.cpp index a392ebb..b5d7969 100755 --- a/main/main.cpp +++ b/main/main.cpp @@ -4,17 +4,7 @@ #include "freertos/task.h" #include "esp_rom_gpio.h" -#include "driver/uart.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 "drivers/all.h" #include "helper.h" @@ -33,7 +23,7 @@ uint32_t skip_to_step = 0; extern "C" void app_main(void) { printf("app_main\n"); - init_sd(); + init_drivers(); init_tft(); init_speaker(); init_sseg();