260 lines
6.1 KiB
C++
260 lines
6.1 KiB
C++
#include "char_lcd.h"
|
|
|
|
#include "./i2c_lcd_pcf8574.h"
|
|
#include <esp_log.h>
|
|
#include "state_tracking.h"
|
|
#include <cstring>
|
|
#include "power.h"
|
|
|
|
i2c_lcd_pcf8574_handle_t lcd;
|
|
|
|
static volatile bool header_enabled;
|
|
|
|
static const char *TAG = "char_lcd";
|
|
static const char* EMPTY_ROW = " ";
|
|
static const char* EMPTY_ROW3 = " ";
|
|
|
|
static char buf[65];
|
|
|
|
static bool replay_handler(const char* event, char* arg) {
|
|
if (strcmp(event, "LCD_CLEAR") == 0) {
|
|
lcd_clear();
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_CURSOR") == 0) {
|
|
char* col_str = strtok(arg, ",");
|
|
char* row_str = strtok(NULL, ",");
|
|
uint32_t col = atoi(col_str);
|
|
uint32_t row = atoi(row_str);
|
|
lcd_set_cursor_pos(col, row);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_SET_DISPLAY") == 0) {
|
|
lcd_set_display(strcmp(arg, "true") == 0);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_CURSOR_VIS") == 0) {
|
|
lcd_set_cursor_vis(strcmp(arg, "true") == 0);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_CURSOR_BLINK") == 0) {
|
|
lcd_set_cursor_blink(strcmp(arg, "true") == 0);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_SCROLL_DISPLAY_LEFT") == 0) {
|
|
lcd_scroll_display_left();
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_SCROLL_DISPLAY_RIGHT") == 0) {
|
|
lcd_scroll_display_right();
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_LEFT_TO_RIGHT") == 0) {
|
|
lcd_left_to_right();
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_RIGHT_TO_LEFT") == 0) {
|
|
lcd_right_to_left();
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_AUTOSCROLL") == 0) {
|
|
lcd_set_autoscroll(strcmp(arg, "true") == 0);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_BACKLIGHT") == 0) {
|
|
lcd_set_backlight(strcmp(arg, "true") == 0);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_CREATE_CHAR") == 0) {
|
|
char* location_str = strtok(arg, ",");
|
|
uint8_t location = atoi(location_str);
|
|
|
|
uint8_t charmap[8];
|
|
for (int i = 0; i < 8; i++) {
|
|
char* str = strtok(NULL, ",");
|
|
charmap[i] = atoi(str);
|
|
}
|
|
|
|
lcd_create_char(location, charmap);
|
|
return true;
|
|
}
|
|
if (strcmp(event, "LCD_PRINT") == 0) {
|
|
// TODO: handle \r and \n
|
|
lcd_print(&lcd, arg);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void init_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);
|
|
|
|
register_replay_fn(replay_handler);
|
|
|
|
ESP_LOGI(TAG, "LCD initialized!");
|
|
}
|
|
|
|
void lcd_clear() {
|
|
if (!header_enabled) {
|
|
lcd_clear(&lcd);
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_CLEAR", NULL);
|
|
}
|
|
} else {
|
|
lcd_print(1, 2, EMPTY_ROW3);
|
|
}
|
|
}
|
|
|
|
void lcd_cursor_home() {
|
|
lcd_set_cursor_pos(0, 0);
|
|
}
|
|
|
|
void lcd_set_cursor_pos(uint8_t col, uint8_t row) {
|
|
lcd_set_cursor(&lcd, col, row);
|
|
|
|
if (is_state_tracking()) {
|
|
sprintf(buf, "%d,%d", col, row);
|
|
event_occured("LCD_CURSOR", buf);
|
|
}
|
|
}
|
|
|
|
void lcd_set_display(bool display) {
|
|
if (display) {
|
|
lcd_display(&lcd);
|
|
} else {
|
|
lcd_no_display(&lcd);
|
|
}
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_SET_DISPLAY", display ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
void lcd_set_cursor_vis(bool cursor) {
|
|
if (cursor) {
|
|
lcd_cursor(&lcd);
|
|
} else {
|
|
lcd_no_cursor(&lcd);
|
|
}
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_CURSOR_VIS", cursor ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
void lcd_set_cursor_blink(bool blink) {
|
|
if (blink) {
|
|
lcd_blink(&lcd);
|
|
} else {
|
|
lcd_no_blink(&lcd);
|
|
}
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_CURSOR_BLINK", blink ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
void lcd_scroll_display_left() {
|
|
lcd_scroll_display_left(&lcd);
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_SCROLL_DISPLAY_LEFT", NULL);
|
|
}
|
|
}
|
|
void lcd_scroll_display_right() {
|
|
lcd_scroll_display_right(&lcd);
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_SCROLL_DISPLAY_RIGHT", NULL);
|
|
}
|
|
}
|
|
|
|
void lcd_left_to_right() {
|
|
lcd_left_to_right(&lcd);
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_LEFT_TO_RIGHT", NULL);
|
|
}
|
|
}
|
|
void lcd_right_to_left() {
|
|
lcd_right_to_left(&lcd);
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_RIGHT_TO_LEFT", NULL);
|
|
}
|
|
}
|
|
|
|
void lcd_set_autoscroll(bool autoscroll) {
|
|
if (autoscroll) {
|
|
lcd_autoscroll(&lcd);
|
|
} else {
|
|
lcd_no_autoscroll(&lcd);
|
|
}
|
|
|
|
if (is_state_tracking()) {
|
|
event_occured("LCD_AUTOSCROLL", autoscroll ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
void lcd_set_backlight(bool backlight) {
|
|
lcd_set_backlight(&lcd, backlight);
|
|
|
|
if (is_state_tracking()) {
|
|
sprintf(buf, "%d", backlight);
|
|
event_occured("LCD_BACKLIGHT", backlight ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
void lcd_create_char(uint8_t location, uint8_t* charmap) {
|
|
lcd_create_char(&lcd, location, charmap);
|
|
|
|
if (is_state_tracking()) {
|
|
snprintf(buf, 65,
|
|
"%d,%d,%d,%d,%d,%d,%d,%d,%d", location,
|
|
charmap[0], charmap[1], charmap[2], charmap[3], charmap[4], charmap[5], charmap[6], charmap[7]
|
|
);
|
|
event_occured("LCD_CREATE_CHAR", buf);
|
|
}
|
|
}
|
|
|
|
// TODO: switch to row, col
|
|
void lcd_print(uint8_t col, uint8_t row, const char* str) {
|
|
lcd_set_cursor_pos(col, row);
|
|
lcd_print(&lcd, str);
|
|
|
|
if (is_state_tracking()) {
|
|
// TODO: handle \r and \n
|
|
event_occured("LCD_PRINT", str);
|
|
}
|
|
}
|
|
|
|
void set_lcd_header_enabled(bool enable) {
|
|
if (!header_enabled && enable) {
|
|
// enabling header
|
|
lcd_print_header();
|
|
} else if (header_enabled && !enable) {
|
|
// disabling header
|
|
lcd_print(1, 1, EMPTY_ROW);
|
|
}
|
|
header_enabled = enable;
|
|
}
|
|
|
|
bool lcd_header_enabled() {
|
|
return header_enabled;
|
|
}
|
|
|
|
void lcd_print_header() {
|
|
lcd_print_header_star_code();
|
|
lcd_print_header_step();
|
|
lcd_print_header_bat();
|
|
}
|
|
|
|
// TODO: add task to continoususly print the battery header.
|