Tasmota/tasmota/xdsp_12_ST7789.ino

224 lines
5.5 KiB
Arduino
Raw Normal View History

2020-08-05 19:21:21 +01:00
/*
xdsp_12_ST7789.ino - Display ST7789 support for Tasmota
Copyright (C) 2020 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/>.
*/
2020-09-03 11:26:03 +01:00
//#ifdef USE_SPI
2020-08-05 19:21:21 +01:00
#ifdef USE_SPI
#ifdef USE_DISPLAY
#ifdef USE_DISPLAY_ST7789
#define XDSP_12 12
#define XI2C_38 38 // See I2CDEVICES.md
#undef COLORED
#define COLORED 1
#undef UNCOLORED
#define UNCOLORED 0
// touch panel controller
#undef FT5206_address
#define FT5206_address 0x38
// using font 8 is opional (num=3)
// very badly readable, but may be useful for graphs
#undef USE_TINY_FONT
#define USE_TINY_FONT
#include <Arduino_ST7789.h>
#include <FT5206.h>
// currently fixed
#define BACKPLANE_PIN 2
extern uint8_t *buffer;
extern uint8_t color_type;
Arduino_ST7789 *st7789;
#ifdef USE_FT5206
uint8_t st7789_ctouch_counter = 0;
#endif // USE_FT5206
/*********************************************************************************************/
void ST7789_InitDriver()
{
if (!Settings.display_model) {
Settings.display_model = XDSP_12;
}
if (XDSP_12 == Settings.display_model) {
if (Settings.display_width != ST7789_TFTWIDTH) {
Settings.display_width = ST7789_TFTWIDTH;
}
if (Settings.display_height != ST7789_TFTHEIGHT) {
Settings.display_height = ST7789_TFTHEIGHT;
}
// disable screen buffer
buffer=NULL;
// default colors
fg_color = ST7789_WHITE;
bg_color = ST7789_BLACK;
2020-08-09 10:08:35 +01:00
int8_t bppin=BACKPLANE_PIN;
2020-08-05 19:21:21 +01:00
if (PinUsed(GPIO_BACKLIGHT)) {
bppin=Pin(GPIO_BACKLIGHT);
}
2020-08-09 10:08:35 +01:00
int8_t reset = -1;
2020-08-05 19:21:21 +01:00
if (PinUsed(GPIO_OLED_RESET)) {
reset=Pin(GPIO_OLED_RESET);
}
2020-08-09 10:08:35 +01:00
int8_t cs = -1;
2020-08-05 19:21:21 +01:00
if (PinUsed(GPIO_SSPI_CS)) {
cs=Pin(GPIO_SSPI_CS);
} else if (PinUsed(GPIO_SPI_CS)) {
cs=Pin(GPIO_SPI_CS);
}
#ifdef ESP32
#undef HW_SPI_MOSI
#define HW_SPI_MOSI 23
#undef HW_SPI_CLK
#define HW_SPI_CLK 18
#else
#undef HW_SPI_MOSI
#define HW_SPI_MOSI 13
#undef HW_SPI_CLK
#define HW_SPI_CLK 14
#endif
// init renderer, may use hardware spi
if (PinUsed(GPIO_SPI_CS) && (Pin(GPIO_SPI_MOSI)==HW_SPI_MOSI) && (Pin(GPIO_SPI_CLK)==HW_SPI_CLK) && PinUsed(GPIO_SPI_DC)) {
2020-08-09 10:08:35 +01:00
st7789 = new Arduino_ST7789(Pin(GPIO_SPI_DC), reset, cs, bppin);
2020-08-05 19:21:21 +01:00
} else {
if ((PinUsed(GPIO_SSPI_CS) || PinUsed(GPIO_OLED_RESET)) && PinUsed(GPIO_SSPI_MOSI) && PinUsed(GPIO_SSPI_SCLK) && PinUsed(GPIO_SSPI_DC)) {
st7789 = new Arduino_ST7789(Pin(GPIO_SSPI_DC), reset, Pin(GPIO_SSPI_MOSI), Pin(GPIO_SSPI_SCLK), cs, bppin);
} else {
return;
}
}
st7789->init(Settings.display_width,Settings.display_height);
renderer = st7789;
renderer->DisplayInit(DISPLAY_INIT_MODE,Settings.display_size,Settings.display_rotate,Settings.display_font);
#ifdef SHOW_SPLASH
// Welcome text
renderer->setTextFont(2);
renderer->setTextColor(ST7789_WHITE,ST7789_BLACK);
renderer->DrawStringAt(30, 100, "ST7789 TFT!", ST7789_WHITE,0);
delay(1000);
#endif
color_type = COLOR_COLOR;
#ifdef ESP32
#ifdef USE_FT5206
// start digitizer with fixed adress and pins for esp32
#define SDA_2 23
#define SCL_2 32
2020-08-06 07:53:09 +01:00
Wire1.begin(SDA_2, SCL_2, 400000);
2020-09-03 11:26:03 +01:00
Touch_Init(Wire1);
2020-08-05 19:21:21 +01:00
#endif // USE_FT5206
#endif // ESP32
}
}
#ifdef ESP32
#ifdef USE_FT5206
#ifdef USE_TOUCH_BUTTONS
2020-09-03 11:26:03 +01:00
void ST7789_RotConvert(int16_t *x, int16_t *y) {
int16_t temp;
if (renderer) {
uint8_t rot=renderer->getRotation();
switch (rot) {
2020-08-29 10:30:23 +01:00
case 0:
2020-09-03 11:26:03 +01:00
break;
2020-08-29 10:30:23 +01:00
case 1:
2020-09-03 11:26:03 +01:00
temp=*y;
*y=renderer->height()-*x;
*x=temp;
break;
2020-08-29 10:30:23 +01:00
case 2:
2020-09-03 11:26:03 +01:00
*x=renderer->width()-*x;
*y=renderer->height()-*y;
break;
case 3:
temp=*y;
*y=*x;
*x=renderer->width()-temp;
break;
2020-08-29 10:30:23 +01:00
}
}
}
2020-08-05 19:21:21 +01:00
// check digitizer hit
2020-09-03 11:26:03 +01:00
void ST7789_CheckTouch() {
2020-08-05 19:21:21 +01:00
st7789_ctouch_counter++;
2020-09-03 11:26:03 +01:00
if (2 == st7789_ctouch_counter) {
// every 100 ms should be enough
st7789_ctouch_counter = 0;
Touch_Check(ST7789_RotConvert);
2020-08-05 19:21:21 +01:00
}
}
#endif // USE_TOUCH_BUTTONS
#endif // USE_FT5206
#endif // ESP32
/*********************************************************************************************/
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdsp12(uint8_t function)
{
bool result = false;
//AddLog_P2(LOG_LEVEL_INFO, PSTR("touch %d - %d"), FT5206_found, function);
if (FUNC_DISPLAY_INIT_DRIVER == function) {
ST7789_InitDriver();
}
else if (XDSP_12 == Settings.display_model) {
switch (function) {
case FUNC_DISPLAY_MODEL:
result = true;
break;
case FUNC_DISPLAY_EVERY_50_MSECOND:
#ifdef USE_FT5206
#ifdef USE_TOUCH_BUTTONS
if (FT5206_found) {
2020-09-03 11:26:03 +01:00
ST7789_CheckTouch();
2020-08-05 19:21:21 +01:00
}
#endif
#endif // USE_FT5206
break;
}
}
return result;
}
#endif // USE_DISPLAY_ST7789
#endif // USE_DISPLAY
#endif // USE_SPI