2018-08-28 17:13:14 +01:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xdsp_01_lcd.ino - Display LCD support for Tasmota
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends and Adafruit
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_DISPLAY
|
|
|
|
#ifdef USE_DISPLAY_LCD
|
|
|
|
|
|
|
|
#define XDSP_01 1
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_03 3 // See I2CDEVICES.md
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
#define LCD_ADDRESS1 0x27 // LCD I2C address option 1
|
|
|
|
#define LCD_ADDRESS2 0x3F // LCD I2C address option 2
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <LiquidCrystal_I2C.h>
|
|
|
|
|
|
|
|
LiquidCrystal_I2C *lcd;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void LcdInitMode(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
|
|
|
lcd->init();
|
|
|
|
lcd->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LcdInit(uint8_t mode)
|
|
|
|
{
|
|
|
|
switch(mode) {
|
|
|
|
case DISPLAY_INIT_MODE:
|
|
|
|
LcdInitMode();
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
DisplayClearScreenBuffer();
|
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
break;
|
|
|
|
case DISPLAY_INIT_PARTIAL:
|
|
|
|
case DISPLAY_INIT_FULL:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:26:04 +00:00
|
|
|
void LcdInitDriver(void) {
|
2024-10-30 21:23:13 +00:00
|
|
|
if (!TasmotaGlobal.i2c_enabled[0]) { return; }
|
2021-03-17 15:26:04 +00:00
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
if (!Settings->display_model) {
|
2019-11-06 16:48:38 +00:00
|
|
|
if (I2cSetDevice(LCD_ADDRESS1)) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->display_address[0] = LCD_ADDRESS1;
|
|
|
|
Settings->display_model = XDSP_01;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
2019-11-06 16:48:38 +00:00
|
|
|
else if (I2cSetDevice(LCD_ADDRESS2)) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->display_address[0] = LCD_ADDRESS2;
|
|
|
|
Settings->display_model = XDSP_01;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
if (XDSP_01 == Settings->display_model) {
|
|
|
|
I2cSetActiveFound(Settings->display_address[0], "LCD");
|
2019-11-06 16:48:38 +00:00
|
|
|
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->display_width = Settings->display_cols[0];
|
|
|
|
Settings->display_height = Settings->display_rows;
|
|
|
|
lcd = new LiquidCrystal_I2C(Settings->display_address[0], Settings->display_cols[0], Settings->display_rows);
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
DisplayAllocScreenBuffer();
|
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
|
|
|
|
LcdInitMode();
|
2021-04-30 16:46:01 +01:00
|
|
|
|
|
|
|
AddLog(LOG_LEVEL_INFO, PSTR("DSP: LCD"));
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void LcdDrawStringAt(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2020-01-01 14:13:58 +00:00
|
|
|
if (dsp_flag) { // Supply Line and Column starting with Line 1 and Column 1
|
|
|
|
dsp_x--;
|
|
|
|
dsp_y--;
|
|
|
|
}
|
2018-08-28 17:13:14 +01:00
|
|
|
lcd->setCursor(dsp_x, dsp_y);
|
|
|
|
lcd->print(dsp_str);
|
|
|
|
}
|
|
|
|
|
2020-08-17 13:49:24 +01:00
|
|
|
void LcdDisplayOnOff()
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2020-08-17 13:49:24 +01:00
|
|
|
if (disp_power) {
|
2018-08-28 17:13:14 +01:00
|
|
|
lcd->backlight();
|
|
|
|
} else {
|
|
|
|
lcd->noBacklight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void LcdCenter(uint8_t row, char* txt)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2021-06-11 17:14:12 +01:00
|
|
|
char line[Settings->display_cols[0] +2];
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2019-02-11 15:53:46 +00:00
|
|
|
int len = strlen(txt);
|
|
|
|
int offset = 0;
|
2021-06-11 17:14:12 +01:00
|
|
|
if (len >= Settings->display_cols[0]) {
|
|
|
|
len = Settings->display_cols[0];
|
2019-02-11 15:53:46 +00:00
|
|
|
} else {
|
2021-06-11 17:14:12 +01:00
|
|
|
offset = (Settings->display_cols[0] - len) / 2;
|
2019-02-11 15:53:46 +00:00
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
memset(line, 0x20, Settings->display_cols[0]);
|
|
|
|
line[Settings->display_cols[0]] = 0;
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
2019-02-11 15:53:46 +00:00
|
|
|
line[offset +i] = txt[i];
|
|
|
|
}
|
2018-08-28 17:13:14 +01:00
|
|
|
lcd->setCursor(0, row);
|
|
|
|
lcd->print(line);
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool LcdPrintLog(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
disp_refresh--;
|
|
|
|
if (!disp_refresh) {
|
2021-06-11 17:14:12 +01:00
|
|
|
disp_refresh = Settings->display_refresh;
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
char* txt = DisplayLogBuffer('\337');
|
2019-03-26 17:26:50 +00:00
|
|
|
if (txt != nullptr) {
|
2021-06-11 17:14:12 +01:00
|
|
|
uint8_t last_row = Settings->display_rows -1;
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < last_row; i++) {
|
2018-08-31 11:17:09 +01:00
|
|
|
strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
|
|
|
|
lcd->setCursor(0, i); // Col 0, Row i
|
|
|
|
lcd->print(disp_screen_buffer[i +1]);
|
|
|
|
}
|
|
|
|
strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
|
|
|
|
DisplayFillScreen(last_row);
|
|
|
|
lcd->setCursor(0, last_row);
|
|
|
|
lcd->print(disp_screen_buffer[last_row]);
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2018-08-31 11:17:09 +01:00
|
|
|
result = true;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-31 11:17:09 +01:00
|
|
|
return result;
|
2018-08-28 17:13:14 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void LcdTime(void)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2021-06-11 17:14:12 +01:00
|
|
|
char line[Settings->display_cols[0] +1];
|
2018-08-28 17:13:14 +01:00
|
|
|
|
|
|
|
snprintf_P(line, sizeof(line), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
|
|
|
|
LcdCenter(0, line);
|
|
|
|
snprintf_P(line, sizeof(line), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year);
|
|
|
|
LcdCenter(1, line);
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void LcdRefresh(void) // Every second
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2021-06-11 17:14:12 +01:00
|
|
|
if (Settings->display_mode) { // Mode 0 is User text
|
|
|
|
switch (Settings->display_mode) {
|
2018-08-28 17:13:14 +01:00
|
|
|
case 1: // Time
|
|
|
|
LcdTime();
|
|
|
|
break;
|
|
|
|
case 2: // Local
|
|
|
|
case 4: // Mqtt
|
|
|
|
LcdPrintLog();
|
|
|
|
break;
|
|
|
|
case 3: // Local
|
|
|
|
case 5: { // Mqtt
|
2018-08-31 11:17:09 +01:00
|
|
|
if (!LcdPrintLog()) { LcdTime(); }
|
2018-08-28 17:13:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xdsp01(uint32_t function)
|
2018-08-28 17:13:14 +01:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_03)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-08-28 17:13:14 +01:00
|
|
|
|
2019-11-04 09:38:05 +00:00
|
|
|
if (FUNC_DISPLAY_INIT_DRIVER == function) {
|
|
|
|
LcdInitDriver();
|
|
|
|
}
|
2021-06-11 17:14:12 +01:00
|
|
|
else if (XDSP_01 == Settings->display_model) {
|
2019-11-04 09:38:05 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_DISPLAY_MODEL:
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_INIT:
|
|
|
|
LcdInit(dsp_init);
|
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_POWER:
|
2020-08-17 13:49:24 +01:00
|
|
|
LcdDisplayOnOff();
|
2019-11-04 09:38:05 +00:00
|
|
|
break;
|
|
|
|
case FUNC_DISPLAY_CLEAR:
|
|
|
|
lcd->clear();
|
|
|
|
break;
|
2018-08-28 17:13:14 +01:00
|
|
|
// case FUNC_DISPLAY_DRAW_HLINE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_DRAW_VLINE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_DRAW_CIRCLE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_FILL_CIRCLE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_DRAW_RECTANGLE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_FILL_RECTANGLE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_DRAW_FRAME:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_TEXT_SIZE:
|
|
|
|
// break;
|
|
|
|
// case FUNC_DISPLAY_FONT_SIZE:
|
|
|
|
// break;
|
2019-11-04 09:38:05 +00:00
|
|
|
case FUNC_DISPLAY_DRAW_STRING:
|
|
|
|
LcdDrawStringAt();
|
|
|
|
break;
|
2018-08-28 17:13:14 +01:00
|
|
|
// case FUNC_DISPLAY_ROTATION:
|
|
|
|
// break;
|
|
|
|
#ifdef USE_DISPLAY_MODES1TO5
|
2019-11-04 09:38:05 +00:00
|
|
|
case FUNC_DISPLAY_EVERY_SECOND:
|
|
|
|
LcdRefresh();
|
|
|
|
break;
|
2018-08-28 17:13:14 +01:00
|
|
|
#endif // USE_DISPLAY_MODES1TO5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_DISPLAY_LCD
|
|
|
|
#endif // USE_DISPLAY
|
|
|
|
#endif // USE_I2C
|