Refactor char_lcd functions

This commit is contained in:
2025-03-25 17:16:54 -05:00
parent 7e00a6d950
commit af93abb8cb
20 changed files with 260 additions and 108 deletions
+1
View File
@@ -6,6 +6,7 @@ set(SOURCES
"bottom_half.cpp"
"char_lcd.cpp"
"game_timer.cpp"
"i2c_lcd_pcf8574.c"
"leds.cpp"
"power.cpp"
"sd.cpp"
+36
View File
@@ -0,0 +1,36 @@
#include "all.h"
static void init_i2c();
void init_drivers() {
init_i2c();
// init char_lcd so we can report any issues on it.
init_char_lcd();
}
/// @brief Initializes I2C_NUM_0.
///
/// This is hooked up the to:
/// - The bottom half
/// - The char lcd
/// - The power board
/// - The MPU6050
/// - The PERH port
/// - The Capacitive Touch Panel
static void init_i2c() {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_5,
.scl_io_num = GPIO_NUM_6,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
// .sda_pullup_en = GPIO_PULLUP_ENABLE,
// .scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 100*1000,
}
};
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, conf.mode, 0, 0, 0));
}
+9
View File
@@ -0,0 +1,9 @@
#ifndef ALL_H
#define ALL_H
#include "char_lcd.h"
#include "sd.h"
void init_drivers();
#endif /* ALL_H */
+1 -13
View File
@@ -180,19 +180,7 @@ static void receive_touch(void);
// }
void init_bottom_half() {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_5,
.scl_io_num = GPIO_NUM_6,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = 100000,
}
};
ESP_ERROR_CHECK(i2c_param_config(BOTTOM_I2C_NUM, &conf));
ESP_ERROR_CHECK(i2c_driver_install(BOTTOM_I2C_NUM, conf.mode, 0, 0, 0));
gpio_config_t delta_pin_conf = {};
// delta_pin_conf.intr_type = GPIO_INTR_LOW_LEVEL;
+100
View File
@@ -1,9 +1,13 @@
#include "char_lcd.h"
#include "./i2c_lcd_pcf8574.h"
#include <esp_log.h>
i2c_lcd_pcf8574_handle_t lcd;
static const char *TAG = "char_lcd";
/// Initializes the 2004 Character LCD
void init_char_lcd(void) {
lcd_init(&lcd, LCD_ADDR, CHAR_LCD_I2C_NUM);
lcd_begin(&lcd, LCD_COLS, LCD_ROWS);
@@ -12,3 +16,99 @@ void init_char_lcd(void) {
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);
} else {
lcd_no_display(&lcd);
}
}
/// Turn the cursor's visibility on/off
void lcd_set_cursor_vis(bool cursor) {
if (cursor) {
lcd_cursor(&lcd);
} else {
lcd_no_cursor(&lcd);
}
}
/// Turn blinking cursor on/off
void lcd_set_cursor_blink(bool blink) {
if (blink) {
lcd_blink(&lcd);
} else {
lcd_no_blink(&lcd);
}
}
/// 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);
} else {
lcd_no_autoscroll(&lcd);
}
}
// 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);
}
+48 -4
View File
@@ -1,8 +1,7 @@
#ifndef CHAR_LCD_H
#define CHAR_LCD_H
#include "./i2c_lcd_pcf8574.h"
#include <esp_log.h>
#include <cstdint>
#define CHAR_LCD_I2C_NUM I2C_NUM_0
@@ -10,8 +9,53 @@
#define LCD_COLS 20
#define LCD_ROWS 4
extern i2c_lcd_pcf8574_handle_t lcd;
/// Initializes the 2004 Character LCD
void init_lcd(void);
void init_char_lcd(void);
/// Clear the LCD
void lcd_clear();
/// Move cursor to home position
void lcd_cursor_home();
/// Set cursor position
void lcd_set_cursor_pos(uint8_t col, uint8_t row);
/// Turn the display on/off
void lcd_set_display(bool display);
/// Turn the cursor's visibility on/off
void lcd_set_cursor_vis(bool cursor);
/// Turn blinking cursor on/off
void lcd_set_cursor_blink(bool blink);
/// Scroll the display left
void lcd_scroll_display_left();
/// Scroll the display right
void lcd_scroll_display_right();
/// Set the text to flows automatically left to right
void lcd_left_to_right();
/// Set the text to flows automatically right to left
void lcd_right_to_left();
// Turn on/off autoscroll
void lcd_set_autoscroll(bool autoscroll);
// Set backlight brightness
void lcd_set_backlight(uint8_t brightness);
// Create a custom character
void lcd_create_char(uint8_t location, uint8_t charmap[]);
// Write a character to the LCD
void lcd_write(uint8_t value);
// Print a string to the LCD
void lcd_print(const char* str);
// Print a string to the LCD at a given pos
void lcd_print(uint8_t col, uint8_t row, const char* str);
#endif /* CHAR_LCD_H */
+2 -4
View File
@@ -1,6 +1,6 @@
/// \file i2c_lcd_pcf8574.c
/// \brief Liquid Crystal display driver with PCF8574 adapter for esp-idf
///
///
/// \author Femi Olugbon, https://iamflinks.github.io
/// \copyright Copyright (c) 2024 by Femi Olugbon
///
@@ -13,12 +13,10 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define TAG "I2C_LCD_PCF8574"
#define I2C_MASTER_TIMEOUT_MS 1000
// private functions
static void lcd_send(i2c_lcd_pcf8574_handle_t* lcd, uint8_t value, bool is_data);
static void lcd_write_nibble(i2c_lcd_pcf8574_handle_t* lcd, uint8_t half_byte, bool is_data, i2c_cmd_handle_t cmd);
@@ -38,7 +36,7 @@ void lcd_init(i2c_lcd_pcf8574_handle_t* lcd, uint8_t i2c_addr, i2c_port_t i2c_po
lcd->data_mask[2] = 0x40;
lcd->data_mask[3] = 0x80;
lcd->backlight_mask = 0x08;
} // lcd_begin()
}
void lcd_begin(i2c_lcd_pcf8574_handle_t* lcd, uint8_t cols, uint8_t rows) {
+5 -9
View File
@@ -9,28 +9,24 @@ void bat_monitor_task(void* arg) {
uint16_t voltage = lipo.voltage();
sprintf(str_buf, "%d.%03dV", voltage / 1000, voltage % 1000);
lcd_clear(&lcd);
lcd_set_cursor(&lcd, 1, 0);
lcd_print(&lcd, str_buf);
lcd_clear();
lcd_print(1, 0, str_buf);
int16_t current = lipo.current(current_measure::AVG);
sprintf(str_buf, "%dmA", current);
lcd_set_cursor(&lcd, 1, 1);
lcd_print(&lcd, str_buf);
lcd_print(1, 1, str_buf);
int16_t total_cap = lipo.capacity(capacity_measure::FULL);
sprintf(str_buf, "%dmAh", total_cap);
lcd_set_cursor(&lcd, 1, 2);
lcd_print(&lcd, str_buf);
lcd_print(1, 2, str_buf);
int16_t soc = lipo.soc(soc_measure::FILTERED);
sprintf(str_buf, "%d%%", soc);
lcd_set_cursor(&lcd, 1, 3);
lcd_print(&lcd, str_buf);
lcd_print(1, 3, str_buf);
vTaskDelay(pdMS_TO_TICKS(250));
+4 -3
View File
@@ -5,7 +5,7 @@ sdmmc_card_t *card;
static const char* mount_point = MOUNT_POINT;
static const char* TAG = "sd";
void init_sd() {
bool init_sd() {
ESP_LOGI(TAG, "Initializing SD card");
esp_err_t ret;
@@ -52,15 +52,16 @@ void init_sd() {
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));
}
return;
return false;
}
ESP_LOGI(TAG, "Filesystem mounted");
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
return true;
}
void deinit_sd() {
esp_vfs_fat_sdcard_unmount(mount_point, card);
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_vfs_fat_sdcard_unmount(mount_point, card));
ESP_LOGI(TAG, "Card unmounted");
}
+1 -1
View File
@@ -19,7 +19,7 @@ extern sdmmc_card_t *card;
#define SD_PIN_D2 GPIO_NUM_39
#define SD_PIN_D3 GPIO_NUM_38
void init_sd();
bool init_sd();
void deinit_sd();
#endif /* SD_H */
+1 -2
View File
@@ -81,8 +81,7 @@ void clear_wires_pressed_released_cut(void) {
void strike(const char* reason) {
ESP_LOGW("strike!", "%s", reason);
lcd_set_cursor(&lcd, 0, 3);
lcd_print(&lcd, reason);
lcd_print(0, 3, reason);
time_penalty(STRIKE_TIME_PENALTY);
total_strikes += 1;
uint8_t reg = 6;