diff --git a/Platformio/hardware/ESP32/boardtest/main_boardtest.cpp b/Platformio/hardware/ESP32/boardtest/main_boardtest.cpp index 7a1f3405..9c6ac947 100644 --- a/Platformio/hardware/ESP32/boardtest/main_boardtest.cpp +++ b/Platformio/hardware/ESP32/boardtest/main_boardtest.cpp @@ -1,7 +1,7 @@ // OMOTE firmware for ESP32 // 2023 Maximilian Kern -#include // Hardware-specific library +#include #include // modified for inverted logic #include #include "SparkFunLIS3DH.h" @@ -12,7 +12,6 @@ #include #include #include "WiFi.h" -#include #include "driver/ledc.h" #include #include "secrets.h" @@ -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 @@ -69,12 +68,57 @@ 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: + + 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; lv_obj_t* objBattPercentage; @@ -158,34 +202,15 @@ static void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t *p } // Read the touchpad -static void my_touchpad_read(lv_indev_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_RELEASED; - } - else{ - data->state = LV_INDEV_STATE_PRESSED; - - // 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_PRESSED; + data->point.x = x; + data->point.y = y; + } else { + data->state = LV_INDEV_STATE_RELEASED; + } } void activityDetection(){ @@ -453,7 +478,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(); @@ -562,13 +586,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); diff --git a/Platformio/hardware/ESP32/lvgl_hal_esp32.cpp b/Platformio/hardware/ESP32/lvgl_hal_esp32.cpp index ae3f49b2..83080d5c 100644 --- a/Platformio/hardware/ESP32/lvgl_hal_esp32.cpp +++ b/Platformio/hardware/ESP32/lvgl_hal_esp32.cpp @@ -26,34 +26,15 @@ static void my_disp_flush( lv_display_t *disp, const lv_area_t *area, uint8_t *p } // Read the touchpad -static void my_touchpad_read(lv_indev_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_RELEASED; - } - else{ - data->state = LV_INDEV_STATE_PRESSED; - - // 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_PRESSED; + data->point.x = x; + data->point.y = y; + } else { + data->state = LV_INDEV_STATE_RELEASED; + } } /*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/ diff --git a/Platformio/hardware/ESP32/sleep_hal_esp32.cpp b/Platformio/hardware/ESP32/sleep_hal_esp32.cpp index 0b69e9d9..e953dde8 100644 --- a/Platformio/hardware/ESP32/sleep_hal_esp32.cpp +++ b/Platformio/hardware/ESP32/sleep_hal_esp32.cpp @@ -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 diff --git a/Platformio/hardware/ESP32/tft_hal_esp32.cpp b/Platformio/hardware/ESP32/tft_hal_esp32.cpp index 6e44e76b..e275f4f4 100644 --- a/Platformio/hardware/ESP32/tft_hal_esp32.cpp +++ b/Platformio/hardware/ESP32/tft_hal_esp32.cpp @@ -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) { @@ -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) { diff --git a/Platformio/hardware/ESP32/tft_hal_esp32.h b/Platformio/hardware/ESP32/tft_hal_esp32.h index 3bff1fb2..e2bdeaac 100644 --- a/Platformio/hardware/ESP32/tft_hal_esp32.h +++ b/Platformio/hardware/ESP32/tft_hal_esp32.h @@ -1,17 +1,28 @@ #pragma once -#include -#include +#include 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: + 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); diff --git a/Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp b/Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp index fab2f786..01550b11 100644 --- a/Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp +++ b/Platformio/hardware/windows_linux/heapUsage_hal_windows_linux.cpp @@ -7,21 +7,32 @@ #if defined(WIN32) // https://www.daniweb.com/programming/software-development/threads/135188/calculate-the-amount-of-heap-memory // returns used heap size in bytes or negative if heap is corrupted. +// It seems the while loop sometimes never ends. Reason unknown. +// If this happens, stop after 200 iterations and return the fake numer 800000, as we are doing in Linux. +// Update: and even with this fallback sometimes the function does not return. Weird. So simply return 800000. long HeapUsed() { + return 800000; + _HEAPINFO info = { 0, 0, 0 }; long used = 0; int rc; - - while ((rc=_heapwalk(&info)) == _HEAPOK) + + int loopCounter = 0; + while (((rc=_heapwalk(&info)) == _HEAPOK) && (loopCounter < 200)) { if (info._useflag == _USEDENTRY) used += info._size; + loopCounter++; } if (rc != _HEAPEND && rc != _HEAPEMPTY) used = (used?-used:-1); - return used; + if (loopCounter <= 200) { + return used; + } else { + return 800000; + } } #elif defined(__linux__) || defined(__APPLE__) diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 2ef77d26..ba3f49cd 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -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 = @@ -91,9 +91,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 @@ -121,29 +119,8 @@ build_flags = ; static memory, will be allocated in static DRAM -D LV_USE_STDLIB_MALLOC=0 -D LV_MEM_SIZE="(44U * 1024U)" - ;-- TFT_eSPI -------------------------------------------------------------- - -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 + ;-- GFX ------------------------------------------------------------------- -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 diff --git a/Platformio/src/applicationInternal/hardware/arduinoLayer.cpp b/Platformio/src/applicationInternal/hardware/arduinoLayer.cpp index d7bf9c1c..edf74294 100644 --- a/Platformio/src/applicationInternal/hardware/arduinoLayer.cpp +++ b/Platformio/src/applicationInternal/hardware/arduinoLayer.cpp @@ -4,7 +4,6 @@ #include #include #include -//#include "sdl/sdl.h" long long current_timestamp() { struct timeval te; @@ -15,8 +14,6 @@ long long current_timestamp() { } void delay(uint32_t ms) { - // we don't use this blackbox - // SDL_Delay(ms); unsigned long startTimer = millis(); while ((millis() - startTimer) < ms) { }