Tasmota/tasmota/xdsp_04_ili9341.ino

412 lines
12 KiB
Arduino
Raw Normal View History

/*
2021-02-13 06:48:20 +00:00
xdsp_04_ILI9341.ino - Display ILI9341/2 support for Tasmota
2021-02-13 06:48:20 +00:00
Copyright (C) 2021 Gerhard Mutz and Theo Arends
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_SPI
#ifdef USE_DISPLAY
2021-03-02 18:34:18 +00:00
#ifdef USE_DISPLAY_ILI9341
#define XDSP_04 4
enum IliModes { ILIMODE_9341 = 1, ILIMODE_9342, ILIMODE_MAX };
2021-02-13 06:48:20 +00:00
#include <ILI9341_2.h>
2021-02-13 06:48:20 +00:00
extern uint8_t color_type;
ILI9341_2 *ili9341_2;
2021-02-25 09:58:33 +00:00
#if defined(USE_FT5206)
2021-02-13 06:48:20 +00:00
#include <FT5206.h>
uint8_t ili9342_ctouch_counter = 0;
2021-02-25 09:58:33 +00:00
#elif defined(USE_XPT2046)
#include <XPT2046_Touchscreen.h>
uint8_t ili9342_ctouch_counter = 0;
2021-02-13 06:48:20 +00:00
#endif // USE_FT5206
bool tft_init_done = false;
2021-04-30 14:26:41 +01:00
void Core2DisplayPower(uint8_t on);
void Core2DisplayDim(uint8_t dim);
2021-03-02 18:34:18 +00:00
2021-06-11 17:14:12 +01:00
//Settings->display_options.type = ILIMODE_9341;
2021-02-18 20:19:26 +00:00
/*********************************************************************************************/
2021-02-13 06:48:20 +00:00
void ILI9341_InitDriver()
{
2021-02-14 08:42:43 +00:00
2021-02-14 15:07:35 +00:00
// There are displays without CS
if (PinUsed(GPIO_ILI9341_CS) || PinUsed(GPIO_ILI9341_DC) &&
(TasmotaGlobal.spi_enabled || TasmotaGlobal.soft_spi_enabled)) {
2021-06-11 17:14:12 +01:00
Settings->display_model = XDSP_04;
2021-06-11 17:14:12 +01:00
if (Settings->display_width != ILI9341_TFTWIDTH) {
Settings->display_width = ILI9341_TFTWIDTH;
}
2021-06-11 17:14:12 +01:00
if (Settings->display_height != ILI9341_TFTHEIGHT) {
Settings->display_height = ILI9341_TFTHEIGHT;
}
2020-06-20 12:18:33 +01:00
2021-06-11 17:14:12 +01:00
if (!Settings->display_options.type || (Settings->display_options.type >= ILIMODE_MAX)) {
Settings->display_options.type = ILIMODE_9341;
2021-03-02 18:34:18 +00:00
}
2021-02-18 20:19:26 +00:00
2021-02-13 06:48:20 +00:00
// default colors
fg_color = ILI9341_WHITE;
bg_color = ILI9341_BLACK;
// check for special case with 2 SPI busses (ESP32 bitcoin)
if (TasmotaGlobal.soft_spi_enabled) {
// Init renderer, may use hardware spi, however we use SSPI defintion because SD card uses SPI definition (2 spi busses)
if (PinUsed(GPIO_SSPI_MOSI) && PinUsed(GPIO_SSPI_MISO) && PinUsed(GPIO_SSPI_SCLK)) {
2021-06-11 17:14:12 +01:00
ili9341_2 = new ILI9341_2(Pin(GPIO_ILI9341_CS), Pin(GPIO_SSPI_MOSI), Pin(GPIO_SSPI_MISO), Pin(GPIO_SSPI_SCLK), Pin(GPIO_OLED_RESET), Pin(GPIO_ILI9341_DC), Pin(GPIO_BACKLIGHT), 2, Settings->display_options.type & 3);
2021-02-13 06:48:20 +00:00
}
} else if (TasmotaGlobal.spi_enabled) {
if (PinUsed(GPIO_ILI9341_DC)) {
2021-06-11 17:14:12 +01:00
ili9341_2 = new ILI9341_2(Pin(GPIO_ILI9341_CS), Pin(GPIO_SPI_MOSI), Pin(GPIO_SPI_MISO), Pin(GPIO_SPI_CLK), Pin(GPIO_OLED_RESET), Pin(GPIO_ILI9341_DC), Pin(GPIO_BACKLIGHT), 1, Settings->display_options.type & 3);
2021-02-13 06:48:20 +00:00
}
}
2021-02-13 06:48:20 +00:00
if (ili9341_2 == nullptr) {
AddLog(LOG_LEVEL_INFO, PSTR("DSP: ILI934x invalid GPIOs"));
return;
}
2021-06-11 17:14:12 +01:00
ili9341_2->init(Settings->display_width, Settings->display_height);
2021-02-13 06:48:20 +00:00
renderer = ili9341_2;
2021-03-02 18:34:18 +00:00
2021-04-30 14:26:41 +01:00
#ifdef USE_M5STACK_CORE2
renderer->SetPwrCB(Core2DisplayPower);
renderer->SetDimCB(Core2DisplayDim);
#endif
2021-06-11 17:14:12 +01:00
renderer->DisplayInit(DISPLAY_INIT_MODE, Settings->display_size, Settings->display_rotate, Settings->display_font);
renderer->dim(GetDisplayDimmer16());
2021-02-13 06:48:20 +00:00
#ifdef SHOW_SPLASH
// Welcome text
renderer->setTextFont(2);
2021-02-14 08:42:43 +00:00
renderer->setTextSize(1);
2021-02-13 06:48:20 +00:00
renderer->setTextColor(ILI9341_WHITE, ILI9341_BLACK);
2021-06-11 17:14:12 +01:00
renderer->DrawStringAt(50, (Settings->display_height/2)-12, (Settings->display_options.type & 3)==ILIMODE_9341?"ILI9341 TFT!":"ILI9342 TFT!", ILI9341_WHITE, 0);
2021-02-13 06:48:20 +00:00
delay(1000);
#endif // SHOW_SPLASH
color_type = COLOR_COLOR;
#ifdef USE_DISPLAY_MODES1TO5
2021-06-11 17:14:12 +01:00
if (Settings->display_rotate) {
DisplayAllocScreenBuffer();
}
Ili9341InitMode();
#endif // USE_DISPLAY_MODES1TO5
2021-02-13 06:48:20 +00:00
#ifdef ESP32
#ifdef USE_FT5206
// start digitizer with fixed adress and pins for esp32
#undef SDA_2
#define SDA_2 21
#undef SCL_2
#define SCL_2 22
Wire1.begin(SDA_2, SCL_2, (uint32_t)400000);
2021-04-21 10:01:40 +01:00
FT5206_Touch_Init(Wire1);
2021-02-13 06:48:20 +00:00
#endif // USE_FT5206
#endif // ESP32
2020-06-22 16:36:39 +01:00
2021-02-25 09:58:33 +00:00
#ifdef USE_XPT2046
2021-04-21 10:01:40 +01:00
XPT2046_Touch_Init(Pin(GPIO_XPT2046_CS));
2021-02-25 09:58:33 +00:00
#endif
tft_init_done = true;
2021-01-23 16:10:06 +00:00
AddLog(LOG_LEVEL_INFO, PSTR("DSP: ILI9341"));
}
}
2021-02-25 09:58:33 +00:00
#if defined(USE_FT5206) || defined(USE_XPT2046)
2021-02-13 06:48:20 +00:00
#ifdef USE_TOUCH_BUTTONS
2021-04-21 10:01:40 +01:00
#ifdef USE_FT5206
void FT5206_TS_RotConvert(int16_t *x, int16_t *y) {
2021-02-13 06:48:20 +00:00
int16_t temp;
if (renderer) {
uint8_t rot = renderer->getRotation();
switch (rot) {
case 0:
break;
case 1:
temp = *y;
*y = renderer->height() - *x;
*x = temp;
break;
case 2:
*x = renderer->width() - *x;
*y = renderer->height() - *y;
break;
case 3:
temp = *y;
*y = *x;
*x = renderer->width() - temp;
break;
}
}
}
2021-04-21 10:01:40 +01:00
#endif // USE_FT5206
#ifdef USE_XPT2046
void XPT2046_TS_RotConvert(int16_t *x, int16_t *y) {
2021-02-28 09:49:49 +00:00
int16_t temp;
if (renderer) {
uint8_t rot = renderer->getRotation();
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(" TS: before convert x:%d / y:%d screen r:%d / w:%d / h:%d"), *x, *y,rot,renderer->width(),renderer->height());
temp = map(*x,XPT2046_MINX,XPT2046_MAXX, renderer->height(), 0);
*x = map(*y,XPT2046_MINY,XPT2046_MAXY, renderer->width(), 0);
*y = temp;
switch (rot) {
case 0:
break;
case 1:
temp = *y;
*y = renderer->width() - *x;
*x = temp;
break;
case 2:
*x = renderer->width() - *x;
*y = renderer->height() - *y;
break;
case 3:
temp = *y;
*y = *x;
*x = renderer->height() - temp;
break;
}
2021-02-28 17:27:32 +00:00
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(" TS: after convert x:%d / y:%d screen r:%d / w:%d / h:%d"), *x, *y,rot,renderer->width(),renderer->height());
2021-02-28 09:49:49 +00:00
}
}
#endif
2021-02-13 06:48:20 +00:00
// check digitizer hit
void ili9342_CheckTouch() {
ili9342_ctouch_counter++;
if (2 == ili9342_ctouch_counter) {
// every 100 ms should be enough
ili9342_ctouch_counter = 0;
2021-04-21 11:02:46 +01:00
#ifdef USE_FT5206
2021-04-21 10:01:40 +01:00
if (FT5206_found) {
Touch_Check(FT5206_TS_RotConvert);
}
2021-04-21 11:02:46 +01:00
#endif // USE_FT5206
#ifdef USE_XPT2046
2021-04-21 10:01:40 +01:00
if (XPT2046_found) {
Touch_Check(XPT2046_TS_RotConvert);
}
2021-04-21 11:02:46 +01:00
#endif // USE_XPT2046
}
}
2021-02-13 06:48:20 +00:00
#endif // USE_TOUCH_BUTTONS
#endif // USE_FT5206
2021-03-02 18:34:18 +00:00
#ifdef USE_DISPLAY_MODES1TO5
2021-02-13 06:48:20 +00:00
#define TFT_TOP 16
#define TFT_BOTTOM 16
#define TFT_FONT_WIDTH 6
#define TFT_FONT_HEIGTH 8 // Adafruit minimal font heigth pixels
uint16_t tft_top = TFT_TOP;
uint16_t tft_bottom = TFT_BOTTOM;
uint16_t tft_scroll = TFT_TOP;
uint16_t tft_cols = 0;
bool Ili9341Header(void) {
2021-06-11 17:14:12 +01:00
if (Settings->display_cols[0] != tft_cols) {
tft_cols = Settings->display_cols[0];
2021-02-13 06:48:20 +00:00
if (tft_cols > 17) {
tft_top = TFT_TOP;
tft_bottom = TFT_BOTTOM;
} else {
tft_top = 0;
tft_bottom = 0;
}
tft_scroll = tft_top;
renderer->setScrollMargins(tft_top, tft_bottom);
}
return (tft_cols > 17);
}
void Ili9341InitMode(void) {
2021-06-11 17:14:12 +01:00
// renderer->setRotation(Settings->display_rotate); // 0
2021-02-14 08:42:43 +00:00
#ifdef USE_DISPLAY_ILI9341
2021-03-02 18:34:18 +00:00
// renderer->invertDisplay(0);
2021-02-14 08:42:43 +00:00
#endif
renderer->fillScreen(ILI9341_BLACK);
renderer->setTextWrap(false); // Allow text to run off edges
renderer->cp437(true);
2021-06-11 17:14:12 +01:00
if (!Settings->display_mode) {
renderer->setCursor(0, 0);
renderer->setTextColor(ILI9341_WHITE, ILI9341_BLACK);
renderer->setTextSize(1);
} else {
Ili9341Header();
renderer->setCursor(0, 0);
renderer->setTextColor(ILI9341_YELLOW, ILI9341_BLACK);
renderer->setTextSize(2);
// tft->println("HEADER");
}
}
void Ili9341PrintLog(void) {
disp_refresh--;
if (!disp_refresh) {
2021-06-11 17:14:12 +01:00
disp_refresh = Settings->display_refresh;
if (Settings->display_rotate) {
if (!disp_screen_buffer_cols) { DisplayAllocScreenBuffer(); }
}
char* txt = DisplayLogBuffer('\370');
if (txt != nullptr) {
2021-06-11 17:14:12 +01:00
uint8_t size = Settings->display_size;
uint16_t theight = size * TFT_FONT_HEIGTH;
2021-02-13 06:48:20 +00:00
renderer->setTextSize(size);
renderer->setTextColor(ILI9341_CYAN, ILI9341_BLACK); // Add background color to solve flicker
2021-06-11 17:14:12 +01:00
if (!Settings->display_rotate) { // Use hardware scroll
2021-02-13 06:48:20 +00:00
renderer->setCursor(0, tft_scroll);
renderer->fillRect(0, tft_scroll, renderer->width(), theight, ILI9341_BLACK); // Erase line
renderer->print(txt);
2020-06-22 16:36:39 +01:00
tft_scroll += theight;
2021-02-13 06:48:20 +00:00
if (tft_scroll >= (renderer->height() - tft_bottom)) {
2020-06-21 11:11:40 +01:00
tft_scroll = tft_top;
}
2021-02-13 06:48:20 +00:00
renderer->scrollTo(tft_scroll);
} else {
2021-06-11 17:14:12 +01:00
uint8_t last_row = Settings->display_rows -1;
2020-06-21 11:11:40 +01:00
tft_scroll = (tft_top) ? theight : 0; // Start below header
2021-02-13 06:48:20 +00:00
renderer->setCursor(0, tft_scroll);
for (uint32_t i = 0; i < last_row; i++) {
strlcpy(disp_screen_buffer[i], disp_screen_buffer[i +1], disp_screen_buffer_cols);
// tft->fillRect(0, tft_scroll, tft->width(), theight, ILI9341_BLACK); // Erase line
2021-02-13 06:48:20 +00:00
renderer->print(disp_screen_buffer[i]);
tft_scroll += theight;
2021-02-13 06:48:20 +00:00
renderer->setCursor(0, tft_scroll);
delay(1); // Fix background runs heap usage due to long runtime of this loop (up to 1 second)
}
strlcpy(disp_screen_buffer[last_row], txt, disp_screen_buffer_cols);
DisplayFillScreen(last_row);
2021-02-13 06:48:20 +00:00
renderer->print(disp_screen_buffer[last_row]);
}
2021-01-23 16:10:06 +00:00
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION "[%s]"), txt);
}
}
}
2021-02-13 06:48:20 +00:00
void ILI9341_Refresh(void) { // Every second
2021-06-11 17:14:12 +01:00
if (Settings->display_mode) { // Mode 0 is User text
2020-06-21 11:11:40 +01:00
// 24-04-2017 13:45:43 = 19 + 1 ('\0') = 20
// 24-04-2017 13:45 = 16 + 1 ('\0') = 17
2020-06-22 16:36:39 +01:00
if (Ili9341Header()) {
2021-06-11 17:14:12 +01:00
char tftdt[Settings->display_cols[0] +1];
2020-06-21 11:11:40 +01:00
char date4[11]; // 24-04-2017
2021-06-11 17:14:12 +01:00
uint8_t time_size = (Settings->display_cols[0] >= 20) ? 9 : 6; // 13:45:43 or 13:45
char spaces[Settings->display_cols[0] - (8 + time_size)];
2020-06-21 11:11:40 +01:00
char time[time_size]; // 13:45:43
2021-06-11 17:14:12 +01:00
renderer->setTextSize(Settings->display_size);
2021-02-13 06:48:20 +00:00
renderer->setTextColor(ILI9341_YELLOW, ILI9341_RED); // Add background color to solve flicker
renderer->setCursor(0, 0);
2020-06-21 11:11:40 +01:00
snprintf_P(date4, sizeof(date4), PSTR("%02d" D_MONTH_DAY_SEPARATOR "%02d" D_YEAR_MONTH_SEPARATOR "%04d"), RtcTime.day_of_month, RtcTime.month, RtcTime.year);
memset(spaces, 0x20, sizeof(spaces));
spaces[sizeof(spaces) -1] = '\0';
snprintf_P(time, sizeof(time), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d"), RtcTime.hour, RtcTime.minute, RtcTime.second);
snprintf_P(tftdt, sizeof(tftdt), PSTR("%s%s%s"), date4, spaces, time);
2021-02-13 06:48:20 +00:00
renderer->print(tftdt);
2020-06-21 11:11:40 +01:00
} else {
2021-02-13 06:48:20 +00:00
renderer->setCursor(0, 0);
2020-06-19 17:29:15 +01:00
}
2021-06-11 17:14:12 +01:00
switch (Settings->display_mode) {
case 1: // Text
case 2: // Local
case 3: // Local
case 4: // Mqtt
case 5: // Mqtt
Ili9341PrintLog();
break;
}
}
}
#endif // USE_DISPLAY_MODES1TO5
2021-02-13 06:48:20 +00:00
/*********************************************************************************************/
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
2021-02-13 06:48:20 +00:00
bool Xdsp04(uint8_t function)
{
bool result = false;
if (FUNC_DISPLAY_INIT_DRIVER == function) {
2021-02-13 06:48:20 +00:00
ILI9341_InitDriver();
}
2021-06-11 17:14:12 +01:00
else if (tft_init_done && (XDSP_04 == Settings->display_model)) {
2021-02-13 06:48:20 +00:00
switch (function) {
case FUNC_DISPLAY_MODEL:
result = true;
break;
case FUNC_DISPLAY_TEXT_SIZE:
case FUNC_DISPLAY_FONT_SIZE:
case DISPLAY_INIT_MODE:
renderer->clearDisplay();
break;
2021-02-25 09:58:33 +00:00
#if defined(USE_FT5206) || defined(USE_XPT2046)
2021-02-13 06:48:20 +00:00
#ifdef USE_TOUCH_BUTTONS
case FUNC_DISPLAY_EVERY_50_MSECOND:
2021-02-25 09:58:33 +00:00
#if defined(USE_FT5206)
2021-02-13 06:48:20 +00:00
if (FT5206_found) {
2021-02-25 09:58:33 +00:00
#elif defined(USE_XPT2046)
if (XPT2046_found) {
#endif
2021-02-13 06:48:20 +00:00
ili9342_CheckTouch();
}
break;
#endif // USE_TOUCH_BUTTONS
#endif // USE_FT5206
#ifdef USE_DISPLAY_MODES1TO5
2021-02-13 06:48:20 +00:00
case FUNC_DISPLAY_EVERY_SECOND:
ILI9341_Refresh();
break;
#endif // USE_DISPLAY_MODES1TO5
2021-02-13 06:48:20 +00:00
}
}
return result;
}
#endif // USE_DISPLAY_ILI9341
#endif // USE_DISPLAY
#endif // USE_SPI