add error checking and initial backlight state to char lcd

This commit is contained in:
2026-09-29 23:54:27 -05:00
parent 8d635d41c6
commit ef79b2a835
6 changed files with 135 additions and 98 deletions
+22 -12
View File
@@ -5,6 +5,7 @@
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <esp_log.h>
#include <esp_err.h>
// mutex is for all these vars
SemaphoreHandle_t lcd_mutex;
@@ -21,6 +22,12 @@ static bool is_header_enabled = false;
static const char *TAG = "char_lcd";
static const char* EMPTY_ROW = " ";
static void log_lcd_error(const char* operation, esp_err_t err) {
if (err != ESP_OK) {
ESP_LOGE(TAG, "LCD %s failed: %s", operation, esp_err_to_name(err));
}
}
// TODO: move this to power.cpp
// static void monitor_battery_task(void* _arg) {
// (void) _arg;
@@ -91,14 +98,17 @@ static const char* EMPTY_ROW = " ";
// return true;
// }
void init_lcd() {
void init_lcd(bool initial_backlight) {
ESP_LOGI(TAG, "Initializing LCD...");
lcd_mutex = xSemaphoreCreateRecursiveMutex();
assert(lcd_mutex != NULL);
lcd = LCD2004I2C();
lcd.init(LCD_ADDR);
esp_err_t err = lcd.init(LCD_ADDR, initial_backlight);
if (err != ESP_OK) {
ESP_LOGE(TAG, "LCD initialization failed: %s", esp_err_to_name(err));
}
// register_replay_fn(replay_handler);
@@ -111,7 +121,7 @@ void LCDController::clear() {
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
if (!is_header_enabled) {
lcd.clear();
log_lcd_error("clear", lcd.clear());
// if (is_state_tracking()) {
// event_occured("LCD_CLEAR", NULL);
@@ -134,7 +144,7 @@ bool LCDController::get_backlight() {
void LCDController::set_backlight(bool backlight) {
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
lcd.set_backlight(backlight);
log_lcd_error("set backlight", lcd.set_backlight(backlight));
xSemaphoreGiveRecursive(lcd_mutex);
// if (is_state_tracking()) {
@@ -145,7 +155,7 @@ void LCDController::set_backlight(bool backlight) {
void LCDController::set_display_show(bool show_display) {
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
lcd.show_hide(show_display);
log_lcd_error("set display", lcd.show_hide(show_display));
xSemaphoreGiveRecursive(lcd_mutex);
}
@@ -159,10 +169,10 @@ bool LCDController::get_display_show() {
/// Changes the cursor display mode to the given mode.
static void change_cursor_display(CursorMode cursor_display_mode) {
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
lcd.show_blink_cursor(
log_lcd_error("set cursor display", lcd.show_blink_cursor(
cursor_display_mode != CursorMode::Hide,
cursor_display_mode == CursorMode::Blink
);
));
xSemaphoreGiveRecursive(lcd_mutex);
}
@@ -178,7 +188,7 @@ void LCDController::set_resting_cursor_pos(uint8_t row, uint8_t col) {
(resting_cursor_row != row || resting_cursor_col != col) &&
cursor_resting_mode != CursorMode::Hide
) {
lcd.move_cursor(row, col);
log_lcd_error("move cursor", lcd.move_cursor(row, col));
}
resting_cursor_row = row;
@@ -243,7 +253,7 @@ void LCDController::create_custom_char(uint8_t location, const uint8_t charmap[]
if (location == 8) location = 0;
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
lcd.create_custom_char(location, charmap);
log_lcd_error("create custom character", lcd.create_custom_char(location, charmap));
xSemaphoreGiveRecursive(lcd_mutex);
// if (is_state_tracking()) {
@@ -260,12 +270,12 @@ void LCDController::create_custom_char(uint8_t location, const uint8_t charmap[]
/// Do not print across lines, as that leads to goofy behavior.
void LCDController::print(uint8_t row, uint8_t col, const char* str) {
xSemaphoreTakeRecursive(lcd_mutex, portMAX_DELAY);
lcd.move_cursor(row, col);
log_lcd_error("move cursor", lcd.move_cursor(row, col));
change_cursor_display(cursor_print_mode);
lcd.write_str(str);
log_lcd_error("write string", lcd.write_str(str));
if (cursor_resting_mode != CursorMode::Hide) {
lcd.move_cursor(resting_cursor_row, resting_cursor_col);
log_lcd_error("restore cursor", lcd.move_cursor(resting_cursor_row, resting_cursor_col));
}
if (cursor_resting_mode != cursor_print_mode) {
change_cursor_display(cursor_resting_mode);