Skip to content

Commit

Permalink
Replaced TFT_eSPI with LovyanGFX
Browse files Browse the repository at this point in the history
Replaced GFX library in preparation for rev 5. There should be no change in performance with SPI, but a huge improvement when using 8-bit in rev 5.
Touch is now also handled by Lovyan with more direct harwdare access than the Adafruit library.
  • Loading branch information
CoretechR committed Dec 23, 2024
1 parent e6a993b commit 88cfb6a
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 114 deletions.
106 changes: 67 additions & 39 deletions Platformio/hardware/ESP32/boardtest/main_boardtest.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// OMOTE firmware for ESP32
// 2023 Maximilian Kern

#include <TFT_eSPI.h> // Hardware-specific library
#include <LovyanGFX.hpp>
#include <Keypad.h> // modified for inverted logic
#include <Preferences.h>
#include "SparkFunLIS3DH.h"
Expand All @@ -12,7 +12,6 @@
#include <IRutils.h>
#include <lvgl.h>
#include "WiFi.h"
#include <Adafruit_FT6206.h>
#include "driver/ledc.h"
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h>
#include "secrets.h"
Expand All @@ -21,7 +20,7 @@

// Pin assignment -----------------------------------------------------------------------------------------------------------------------

#define LCD_DC 9 // defined in TFT_eSPI User_Setup.h
#define LCD_DC 9
#define LCD_CS 5
#define LCD_MOSI 23
#define LCD_SCK 18
Expand Down Expand Up @@ -69,12 +68,60 @@ bool wakeupByIMUEnabled = true;
LIS3DH IMU(I2C_MODE, 0x19); // Default constructor is I2C, addr 0x19.

// LCD declarations
TFT_eSPI tft = TFT_eSPI();
#define screenWidth 240
#define screenHeight 320
Adafruit_FT6206 touch = Adafruit_FT6206();
TS_Point touchPoint;
TS_Point oldPoint;
class LGFX : public lgfx::LGFX_Device
{
lgfx::Panel_ILI9341 _panel_instance;
lgfx::Bus_SPI _bus_instance;
lgfx::Touch_FT5x06 _touch_instance;

public:

const char* driver_name = "ILI9341";
const char* mcu_name = "ESP32";

LGFX(void)
{
{
auto cfg = _bus_instance.config();
cfg.freq_write = 40000000;
cfg.freq_read = 16000000;
cfg.dma_channel = SPI_DMA_CH_AUTO;
cfg.pin_sclk = LCD_SCK;
cfg.pin_mosi = LCD_MOSI;
cfg.pin_dc = LCD_DC;

_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{
auto cfg = _panel_instance.config();
cfg.pin_cs = LCD_CS;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.memory_width = screenWidth;
cfg.memory_height = screenHeight; // 162 or 160 or 132
cfg.panel_width = screenWidth;
cfg.panel_height = screenHeight;
cfg.offset_rotation = 2;

_panel_instance.config(cfg);
}
{
auto cfg = _touch_instance.config();
cfg.i2c_addr = 0x38;
cfg.i2c_port = 0;
cfg.pin_sda = SDA;
cfg.pin_scl = SCL;
cfg.freq = 400000;
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
};
static LGFX tft;
int backlight_brightness = 255;

// LVGL declarations
Expand Down Expand Up @@ -162,34 +209,15 @@ void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colo
}

// Read the touchpad
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data){
// int16_t touchX, touchY;
touchPoint = touch.getPoint();
int16_t touchX = touchPoint.x;
int16_t touchY = touchPoint.y;
bool touched = false;
if ((touchX > 0) || (touchY > 0)) {
touched = true;
standbyTimer = SLEEP_TIMEOUT;
}

if( !touched ){
data->state = LV_INDEV_STATE_REL;
}
else{
data->state = LV_INDEV_STATE_PR;

// Set the coordinates
data->point.x = screenWidth - touchX;
data->point.y = screenHeight - touchY;

//Serial.print( "touchpoint: x" );
//Serial.print( touchX );
//Serial.print( " y" );
//Serial.println( touchY );
//tft.drawFastHLine(0, screenHeight - touchY, screenWidth, TFT_RED);
//tft.drawFastVLine(screenWidth - touchX, 0, screenHeight, TFT_RED);
}
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
uint16_t x, y;
if (tft.getTouch(&x, &y)) {
data->state = LV_INDEV_STATE_PR;
data->point.x = x;
data->point.y = y;
} else {
data->state = LV_INDEV_STATE_REL;
}
}

void activityDetection(){
Expand Down Expand Up @@ -453,7 +481,6 @@ void setup() {

// Setup touchscreen
Wire.begin(SDA, SCL, 400000); // Configure i2c pins and set frequency to 400kHz
bool TouchInitSuccessful = touch.begin(128); // Initialize touchscreen and set sensitivity threshold

// Setup LVGL
lv_init();
Expand Down Expand Up @@ -556,13 +583,14 @@ void setup() {


// Automated Checks



uint64_t _chipmacid = 0LL;
esp_efuse_mac_get_default((uint8_t*) (&_chipmacid));
Serial.print("ESP32 MAC: ");
Serial.println(_chipmacid);
// Check if the touchscreen is responding
boolean TouchInitSuccessful = false;
Wire.beginTransmission(0x38);
if(Wire.endTransmission() == 0) TouchInitSuccessful = true;


if(IMUInitSuccessful == 0) lv_table_set_cell_value_fmt(ChecksTable, 6, 1, LV_SYMBOL_OK);
Expand Down
37 changes: 9 additions & 28 deletions Platformio/hardware/ESP32/lvgl_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,15 @@ void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colo
}

// Read the touchpad
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data){
int16_t touchX;
int16_t touchY;
get_touchpoint(&touchX, &touchY);

bool touched = false;
if ((touchX > 0) || (touchY > 0)) {
touched = true;
setLastActivityTimestamp_HAL();
}

if( !touched ){
data->state = LV_INDEV_STATE_REL;
}
else{
data->state = LV_INDEV_STATE_PR;

// Set the coordinates
data->point.x = SCR_WIDTH - touchX;
data->point.y = SCR_HEIGHT - touchY;

//Serial.print( "touchpoint: x" );
//Serial.print( touchX );
//Serial.print( " y" );
//Serial.println( touchY );
//tft.drawFastHLine(0, SCR_HEIGHT - touchY, SCR_WIDTH, TFT_RED);
//tft.drawFastVLine(SCR_WIDTH - touchX, 0, SCR_HEIGHT, TFT_RED);
}
void my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) {
uint16_t x, y;
if (tft.getTouch(&x, &y)) {
data->state = LV_INDEV_STATE_PR;
data->point.x = x;
data->point.y = y;
} else {
data->state = LV_INDEV_STATE_REL;
}
}

static lv_disp_draw_buf_t draw_buf;
Expand Down
8 changes: 4 additions & 4 deletions Platformio/hardware/ESP32/sleep_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ void enterSleep(){
#endif

// Prepare IO states
digitalWrite(TFT_DC, LOW); // LCD control signals off
digitalWrite(TFT_CS, LOW);
digitalWrite(TFT_MOSI, LOW);
digitalWrite(TFT_SCLK, LOW);
digitalWrite(LCD_DC_GPIO, LOW); // LCD control signals off
digitalWrite(LCD_CS_GPIO, LOW);
digitalWrite(LCD_MOSI_GPIO, LOW);
digitalWrite(LCD_SCK_GPIO, LOW);
digitalWrite(LCD_EN_GPIO, HIGH); // LCD logic off
digitalWrite(LCD_BL_GPIO, HIGH); // LCD backlight off
// pinMode(CRG_STAT, INPUT); // Disable Pull-Up
Expand Down
57 changes: 41 additions & 16 deletions Platformio/hardware/ESP32/tft_hal_esp32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,48 @@ uint8_t SCL_GPIO = 22;

uint8_t LCD_BL_GPIO = 4;
uint8_t LCD_EN_GPIO = 10;
uint8_t LCD_CS_GPIO = 5;
uint8_t LCD_MOSI_GPIO = 23;
uint8_t LCD_SCK_GPIO = 18;
uint8_t LCD_DC_GPIO = 9;

TFT_eSPI tft = TFT_eSPI();
Adafruit_FT6206 touch = Adafruit_FT6206();
TS_Point touchPoint;
LGFX::LGFX(void) {
{
auto cfg = _bus_instance.config();
cfg.freq_write = SPI_FREQUENCY;
cfg.freq_read = 16000000;
cfg.dma_channel = SPI_DMA_CH_AUTO;
cfg.pin_sclk = LCD_SCK_GPIO;
cfg.pin_mosi = LCD_MOSI_GPIO;
cfg.pin_dc = LCD_DC_GPIO;
_bus_instance.config(cfg);
_panel_instance.setBus(&_bus_instance);
}
{
auto cfg = _panel_instance.config();
cfg.pin_cs = LCD_CS_GPIO;
cfg.pin_rst = -1;
cfg.pin_busy = -1;
cfg.memory_width = SCR_WIDTH;
cfg.memory_height = SCR_HEIGHT;
cfg.panel_width = SCR_WIDTH;
cfg.panel_height = SCR_HEIGHT;
cfg.offset_rotation = 2;
_panel_instance.config(cfg);
}
{
auto cfg = _touch_instance.config();
cfg.i2c_addr = 0x38;
cfg.i2c_port = 0;
cfg.pin_sda = SDA_GPIO;
cfg.pin_scl = SCL_GPIO;
cfg.freq = 400000;
_touch_instance.config(cfg);
_panel_instance.setTouch(&_touch_instance);
}
setPanel(&_panel_instance);
}
LGFX tft;
byte backlightBrightness = 255;

void init_tft(void) {
Expand Down Expand Up @@ -65,21 +103,8 @@ void init_tft(void) {
delay(5); // Wait for the LCD driver to power on
tft.init();
tft.initDMA();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
tft.setSwapBytes(true);

// SDA and SCL need to be set explicitly, because for IMU you cannot set it explicitly in the constructor.
// Configure i2c pins and set frequency to 400kHz
Wire.begin(SDA_GPIO, SCL_GPIO, 400000);
// Setup touchscreen
touch.begin(128); // Initialize touchscreen and set sensitivity threshold
}

void get_touchpoint(int16_t *touchX, int16_t *touchY) {
touchPoint = touch.getPoint();
*touchX = touchPoint.x;
*touchY = touchPoint.y;
}

void update_backligthBrighness_HAL(void) {
Expand Down
22 changes: 18 additions & 4 deletions Platformio/hardware/ESP32/tft_hal_esp32.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#pragma once

#include <TFT_eSPI.h>
#include <Adafruit_FT6206.h>
#include <LovyanGFX.hpp>

extern uint8_t LCD_BL_GPIO;
extern uint8_t LCD_EN_GPIO;
extern uint8_t LCD_CS_GPIO;
extern uint8_t LCD_MOSI_GPIO;
extern uint8_t LCD_SCK_GPIO;
extern uint8_t LCD_DC_GPIO;

// used in lvgl_hal.cpp "void my_disp_flush(..."
extern TFT_eSPI tft;
class LGFX : public lgfx::LGFX_Device{
private:
lgfx::Panel_ILI9341 _panel_instance;
lgfx::Bus_SPI _bus_instance;
lgfx::Touch_FT5x06 _touch_instance;

public:
const char* driver_name = "ILI9341";
const char* mcu_name = "ESP32";

LGFX(void);
};
extern LGFX tft;

// only called from lvgl_hal.cpp, not from the HAL
void init_tft(void);
void get_touchpoint(int16_t *touchX, int16_t *touchY);

// called from the HAL
void update_backligthBrighness_HAL(void);
Expand Down
26 changes: 3 additions & 23 deletions Platformio/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
default_envs = esp32

[env]
;-- platformio.ini custom options, reused by TFT_eSPI, SDL2 and in OMOTE code -
;-- platformio.ini custom options, reused by lovyanGFX, SDL2 and in OMOTE code -
custom_screen_width = 240
custom_screen_height = 320
lib_deps =
Expand Down Expand Up @@ -89,9 +89,7 @@ board_build.partitions = huge_app.csv
upload_speed = 1000000
lib_deps =
${env.lib_deps}
bodmer/TFT_eSPI@^2.5.43
adafruit/Adafruit BusIO@^1.15.0
adafruit/Adafruit FT6206 Library@^1.1.0
lovyan03/LovyanGFX@^1.2.0
sparkfun/SparkFun LIS3DH Arduino Library@^1.0.3
crankyoldgit/IRremoteESP8266@^2.8.6
knolleary/PubSubClient@^2.8
Expand Down Expand Up @@ -121,29 +119,11 @@ build_flags =
; static memory, will be allocated in static DRAM
-D LV_MEM_CUSTOM=0
-D LV_MEM_SIZE="(32U * 1024U)"
;-- TFT_eSPI --------------------------------------------------------------
;-- GFX -------------------------------------------------------------------
-D DISABLE_ALL_LIBRARY_WARNINGS=1
; The following lines replace the TFT_eSPI User_setup.h-file
-D USER_SETUP_LOADED=1
-D ILI9341_DRIVER=1
-D TFT_WIDTH=${env.custom_screen_width}
-D TFT_HEIGHT=${env.custom_screen_height}
;-D TFT_MISO not connected
-D TFT_MOSI=23
-D TFT_SCLK=18
-D TFT_CS=5
-D TFT_DC=9
-D TFT_RST=-1
-D SPI_FREQUENCY=40000000 ; 40MHz default, some displays might support 80MHz
; TFT_eSPI fonts are disabled by default
;-D LOAD_GLCD=1
;-D LOAD_FONT2=1
;-D LOAD_FONT4=1
;-D LOAD_FONT6=1
;-D LOAD_FONT7=1
;-D LOAD_FONT8=1
;-D LOAD_GFXFF=1
;-D SMOOTH_FONT=1
;-- for BLE Keyboard. Set the maximum number of bonded BLE peers ----------
-D CONFIG_BT_NIMBLE_MAX_BONDS=3
; has to be 4x CONFIG_BT_NIMBLE_MAX_BONDS
Expand Down

0 comments on commit 88cfb6a

Please sign in to comment.