diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 4166ff8a..48b2a5b2 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -11,37 +11,24 @@ #include "lv_misc/lv_fs.h" #include "lv_fs_if.h" -// Select Display Driver -#if defined(USE_FSMC) -#include "fsmc_ili9341.h" -#else -#include "tft_espi_drv.h" -#endif - -// Select Touch Driver -//#include "indev/XPT2046_alt_drv.h" -#include "indev/XPT2046.h" - -//#include "lv_zifont.h" +// Device Drivers +#include "drv/hasp_drv_display.h" +#include "drv/hasp_drv_touch.h" #include "hasp_debug.h" #include "hasp_config.h" -#include "hasp_dispatch.h" #include "hasp_gui.h" #include "hasp_oobe.h" -#include "hasp.h" -//#include "lv_ex_conf.h" +#include "hasp/hasp_dispatch.h" +#include "hasp/hasp.h" + //#include "tpcal.h" #include "Ticker.h" #if HASP_USE_PNGDECODE > 0 -#include "png_decoder.h" -#endif - -#ifndef TOUCH_DRIVER -#define TOUCH_DRIVER -1 // No Touch + #include "png_decoder.h" #endif #define BACKLIGHT_CHANNEL 15 // pwm channel 0-15 @@ -53,10 +40,10 @@ File pFileOut; #define LVGL_TICK_PERIOD 20 #ifndef TFT_BCKL -#define TFT_BCKL -1 // No Backlight Control + #define TFT_BCKL -1 // No Backlight Control #endif #ifndef TFT_ROTATION -#define TFT_ROTATION 0 + #define TFT_ROTATION 0 #endif static void IRAM_ATTR lv_tick_handler(void); @@ -67,11 +54,9 @@ static int8_t guiDimLevel = -1; static int8_t guiBacklightPin = TFT_BCKL; static uint16_t guiSleepTime1 = 60; // 1 second resolution static uint16_t guiSleepTime2 = 120; // 1 second resolution -static uint8_t guiSleeping = HASP_SLEEP_OFF; static uint8_t guiTickPeriod = 20; static uint8_t guiRotation = TFT_ROTATION; -// static TFT_eSPI tft; // = TFT_eSPI(); /* TFT instance */ -static uint16_t calData[5] = {0, 65535, 0, 65535, 0}; +static uint16_t calData[5] = {0, 65535, 0, 65535, 0}; #if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266) static Ticker tick; /* timer for interrupt handler */ @@ -79,67 +64,16 @@ static Ticker tick; /* timer for interrupt handler */ static Ticker tick(lv_tick_handler, LVGL_TICK_PERIOD); // guiTickPeriod); #endif -/* **************************** SLEEP & WAKEUP ************************************** */ -bool IRAM_ATTR guiCheckSleep() +/* **************************** FLUSH DATA TO TFT ******************************* */ + +/* Defines the actual driver callback to use */ +static void gui_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) { - uint32_t idle = lv_disp_get_inactive_time(NULL); - - if(idle >= (guiSleepTime1 + guiSleepTime2) * 1000U) { - if(guiSleeping != HASP_SLEEP_LONG) { - dispatch_output_idle_state(HASP_SLEEP_LONG); - guiSleeping = HASP_SLEEP_LONG; - } - } else if(idle >= guiSleepTime1 * 1000U) { - if(guiSleeping != HASP_SLEEP_SHORT) { - dispatch_output_idle_state(HASP_SLEEP_SHORT); - guiSleeping = HASP_SLEEP_SHORT; - } - } else { - if(guiSleeping != HASP_SLEEP_OFF) { - dispatch_output_idle_state(HASP_SLEEP_OFF); - guiSleeping = HASP_SLEEP_OFF; - } - } - - return (guiSleeping != HASP_SLEEP_OFF); -} - -/** - * WakeUp the display using a command instead of touch - */ -void guiWakeUp() -{ - lv_disp_trig_activity(NULL); -} - -/* **************************** SCREENSHOTS ************************************** */ - -/* After flusing to the file stream or web client, we also send the buffer to the tft */ -static void IRAM_ATTR printscreen_flush_cb(lv_disp_drv_t * disp, const lv_area_t * area, lv_color_t * color_p) -{ -#if 0 - size_t len = (area->x2 - area->x1 + 1) * (area->y2 - area->y1 + 1); /* Number of pixels */ - - /* Update TFT */ - tft.startWrite(); /* Start new TFT transaction */ - tft.setWindow(area->x1, area->y1, area->x2, area->y2); /* set the working window */ - tft.pushPixels((uint16_t *)color_p, len); /* Write words at once */ - tft.endWrite(); /* terminate TFT transaction */ - - /* Send Screenshot data */ - // if(guiSnapshot != 0) { - // gui_take_screenshot(disp, area, color_p); - //} -#endif - #if defined(USE_FSMC) fsmc_ili9341_flush(disp, area, color_p); #else tft_espi_flush(disp, area, color_p); #endif - - /* Tell lvgl that flushing is done */ - // lv_disp_flush_ready(disp); ===> moved into the drivers } /* **************************** GUI TICKER ************************************** */ @@ -205,7 +139,7 @@ static void ICACHE_RAM_ATTR lv_tick_handler(void) // return true; // } -// bool my_touchpad_read_raw(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) +// bool gui_touchpad_read_raw(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) // { // #ifdef TOUCH_CS // uint16_t touchX, touchY; @@ -227,7 +161,7 @@ static void ICACHE_RAM_ATTR lv_tick_handler(void) // // } // // } -// if(guiSleeping > 0) guiCheckSleep(); // update Idle +// if(sleep_state > 0) hasp_update_sleep_state(); // update Idle // /*Save the state and save the pressed coordinate*/ // // lv_disp_t * disp = lv_disp_get_default(); @@ -242,8 +176,8 @@ static void ICACHE_RAM_ATTR lv_tick_handler(void) // return false; /*Return `false` because we are not buffering and no more data to read*/ // } -#if TOUCH_DRIVER == 0xADC // Analog Digital Touch Conroller -#include "Touchscreen.h" // For Uno Shield or ADC based resistive touchscreens +#if TOUCH_DRIVER == 0xADC // Analog Digital Touch Conroller + #include "Touchscreen.h" // For Uno Shield or ADC based resistive touchscreens boolean Touch_getXY(uint16_t * x, uint16_t * y, boolean showTouch) { @@ -268,8 +202,8 @@ boolean Touch_getXY(uint16_t * x, uint16_t * y, boolean showTouch) digitalWrite(aYP, HIGH); // because TFT control pins digitalWrite(aXM, HIGH); // adjust pressure sensitivity - note works 'backwards' -#define MINPRESSURE 200 -#define MAXPRESSURE 1000 + #define MINPRESSURE 200 + #define MAXPRESSURE 1000 bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE); if(pressed) { @@ -297,143 +231,12 @@ boolean Touch_getXY(uint16_t * x, uint16_t * y, boolean showTouch) } #endif -#if TOUCH_DRIVER == 911 - -#include -#include "Goodix.h" -#define INT_PIN (TOUCH_IRQ) -#define RST_PIN (TOUCH_RST) // -1 if pin is connected to VCC else set pin number - -static Goodix touch = Goodix(); -static int8_t GT911_num_touches; -static GTPoint * GT911_points; - -void handleTouch(int8_t contacts, GTPoint * points) -{ - GT911_num_touches = contacts; - GT911_points = points; - - Log.verbose(TAG_GUI, "Contacts: %d", contacts); - for(int i = 0; i < contacts; i++) { - Log.verbose(TAG_GUI, "C%d: #%d %d,%d s:%d", i, points[i].trackId, points[i].x, points[i].y, points[i].area); - yield(); - } -} - -bool IRAM_ATTR GT911_getXY(uint16_t * touchX, uint16_t * touchY, bool debug) -{ - static GTPoint points[5]; - int16_t contacts = touch.readInput((uint8_t *)&points); - if(contacts <= 0) return false; - - if(debug) { - Serial.print(contacts); - Serial.print(" : "); - Serial.print(points[0].x); - Serial.print(" x "); - Serial.println(points[0].y); - } - - *touchX = points[0].x; - *touchY = points[0].y; - return true; - - // ALTERNATE REGISTER READ METHOD - // static uint8_t touchBuffer[6]; - - // uint16_t first = 0x814E; // 8150 - // uint16_t last = 0x8153; - // uint16_t len = first - last + 1; - // uint8_t res = touch.read(first, touchBuffer, len); - - // if(res != GOODIX_OK || touchBuffer[0] - 128 == 0) return false; - - // *touchX = touchBuffer[2] + touchBuffer[3] * 256; - // *touchY = touchBuffer[4] + touchBuffer[5] * 256; - - // if (debug) { - // Serial.print(touchBuffer[0] - 128); - // Serial.print(" : "); - // Serial.print(*touchX); - // Serial.print(" x "); - // Serial.println(*touchY); - // } - // return true; -} - -void touchStart() -{ - if(touch.begin(INT_PIN, RST_PIN) != true) { - Serial.println("! Module reset failed"); - } else { - Serial.println("Module reset OK"); - } - - Serial.print("Check ACK on addr request on 0x"); - Serial.print(touch.i2cAddr, HEX); - - Wire.beginTransmission(touch.i2cAddr); - int error = Wire.endTransmission(); - if(error == 0) { - Serial.println(": SUCCESS"); - } else { - Serial.print(": ERROR #"); - Serial.println(error); - } -} - -void GT911_setup() -{ - Wire.setClock(400000); - Wire.begin(); - delay(300); - - touch.setHandler(handleTouch); - touchStart(); - Log.trace(TAG_GUI, F("Goodix GT911x touch driver started")); -} -#endif - -bool IRAM_ATTR my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data) -{ -#ifdef TOUCH_CS - uint16_t touchX, touchY; - bool touched; -#if TOUCH_DRIVER == 2046 // XPT2046 Resistive touch panel driver - touched = tft_espi_get_touch(&touchX, &touchY, 300); -#elif TOUCH_DRIVER == 911 - // return false; - touched = GT911_getXY(&touchX, &touchY, true); -#elif TOUCH_DRIVER == 0xADC // Analog Digital Touch Conroller - touched = Touch_getXY(&touchX, &touchY, false); -#else - // xpt2046_alt_drv_read(indev_driver, data); - // xpt2046_read(indev_driver, data); - // if(data->state && guiSleeping != HASP_SLEEP_OFF) guiCheckSleep(); - return false; -#endif - - if(touched && guiSleeping != HASP_SLEEP_OFF) guiCheckSleep(); // update Idle - - // Ignore first press? - - /*Save the state and save the pressed coordinate for cursor position */ - data->state = touched ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; - if(touched) { - data->point.x = touchX; - data->point.y = touchY; - } -#endif - - return false; /*Return `false` because we are not buffering and no more data to read*/ -} - void guiCalibrate() { #if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 -#ifdef TOUCH_CS + #ifdef TOUCH_CS tft_espi_calibrate(calData); -#endif + #endif for(int i = 0; i < 5; i++) { Serial.print(calData[i]); @@ -447,55 +250,45 @@ void guiCalibrate() void guiSetup() { - /* TFT init */ -#if defined(USE_FSMC) - fsmc_ili9341_init(guiRotation); - xpt2046_init(guiRotation); -#else - tft_espi_init(guiRotation); -#endif - -#if TOUCH_DRIVER == 911 - GT911_setup(); -#endif + // Driver intializations: + drv_display_init(guiRotation); // Display + drv_touch_init(guiRotation); // Touch + lv_init(); // GUI #if 0 tft.begin(); tft.setSwapBytes(true); /* set endianess */ -#ifdef USE_DMA_TO_TFT + #ifdef USE_DMA_TO_TFT // DMA - should work with STM32F2xx/F4xx/F7xx processors // NOTE: >>>>>> DMA IS FOR SPI DISPLAYS ONLY <<<<<< tft.initDMA(); // Initialise the DMA engine (tested with STM32F446 and STM32F767) -#endif + #endif tft.setRotation(guiRotation); /* 1/3=Landscape or 0/2=Portrait orientation */ -#if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 + #if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 tft_espi_set_touch(calData); -#endif + #endif #endif /* Initialize the Virtual Device Buffers */ - size_t guiVDBsize = 0; - lv_init(); - #if defined(ARDUINO_ARCH_ESP32) /* allocate on iram (or psram ?) */ static lv_disp_buf_t disp_buf; -#ifdef USE_DMA_TO_TFT + #ifdef USE_DMA_TO_TFT static lv_color_t *guiVdbBuffer1, *guiVdbBuffer2 = NULL; - guiVDBsize = 4 * 1024u; // 16 KBytes * 2 - guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); + size_t guiVDBsize = 4 * 1024u; // 16 KBytes * 2 + guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); - //guiVdbBuffer2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); - //lv_disp_buf_init(&disp_buf, guiVdbBuffer1, guiVdbBuffer2, guiVDBsize); -#else + // guiVdbBuffer2 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_DMA); + // lv_disp_buf_init(&disp_buf, guiVdbBuffer1, guiVdbBuffer2, guiVDBsize); + #else static lv_color_t * guiVdbBuffer1; - guiVDBsize = 16 * 1024u; // 32 KBytes * 2 - guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_8BIT); + size_t guiVDBsize = 16 * 1024u; // 32 KBytes * 2 + guiVdbBuffer1 = (lv_color_t *)heap_caps_malloc(sizeof(lv_color_t) * guiVDBsize, MALLOC_CAP_8BIT); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); -#endif + #endif // static lv_color_t * guiVdbBuffer2 = (lv_color_t *)malloc(sizeof(lv_color_t) * guiVDBsize); // lv_disp_buf_init(&disp_buf, guiVdbBuffer1, guiVdbBuffer2, guiVDBsize); @@ -503,7 +296,7 @@ void guiSetup() /* allocate on heap */ static lv_disp_buf_t disp_buf; static lv_color_t guiVdbBuffer1[4 * 512u]; // 4 KBytes - guiVDBsize = sizeof(guiVdbBuffer1) / sizeof(guiVdbBuffer1[0]); + size_t guiVDBsize = sizeof(guiVdbBuffer1) / sizeof(guiVdbBuffer1[0]); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); // static lv_disp_buf_t disp_buf; @@ -515,7 +308,7 @@ void guiSetup() static lv_disp_buf_t disp_buf; static lv_color_t guiVdbBuffer1[16 * 512u]; // 16 KBytes // static lv_color_t guiVdbBuffer2[16 * 512u]; // 16 KBytes - guiVDBsize = sizeof(guiVdbBuffer1) / sizeof(guiVdbBuffer1[0]); + size_t guiVDBsize = sizeof(guiVdbBuffer1) / sizeof(guiVdbBuffer1[0]); // lv_disp_buf_init(&disp_buf, guiVdbBuffer1, guiVdbBuffer2, guiVDBsize); lv_disp_buf_init(&disp_buf, guiVdbBuffer1, NULL, guiVDBsize); #endif @@ -531,19 +324,15 @@ void guiSetup() lv_fs_if_init(); // auxilary file system drivers #endif - /* Dump TFT Configuration */ - // tftSetup(tft); #ifdef USE_DMA_TO_TFT Log.verbose(TAG_GUI, F("DMA : ENABLED")); #else Log.verbose(TAG_GUI, F("DMA : DISABLED")); #endif - /* Load User Settings */ - // guiSetConfig(settings); /* Setup Backlight Control Pin */ if(guiBacklightPin >= 0) { - Log.verbose(TAG_LVGL, F("Backlight: Pin %d"), guiBacklightPin); + Log.verbose(TAG_GUI, F("Backlight: Pin %d"), guiBacklightPin); #if defined(ARDUINO_ARCH_ESP32) // pinMode(guiBacklightPin, OUTPUT); @@ -555,11 +344,11 @@ void guiSetup() pinMode(guiBacklightPin, OUTPUT); #endif } + Log.verbose(TAG_GUI, F("Rotation : %d"), guiRotation); Log.verbose(TAG_LVGL, F("Version : %u.%u.%u %s"), LVGL_VERSION_MAJOR, LVGL_VERSION_MINOR, LVGL_VERSION_PATCH, PSTR(LVGL_VERSION_INFO)); - Log.verbose(TAG_LVGL, F("Rotation : %d"), guiRotation); #ifdef LV_MEM_SIZE Log.verbose(TAG_LVGL, F("MEM size : %d"), LV_MEM_SIZE); #endif @@ -573,12 +362,15 @@ void guiSetup() /* Initialize the display driver */ lv_disp_drv_t disp_drv; lv_disp_drv_init(&disp_drv); -#if defined(USE_FSMC) - disp_drv.flush_cb = fsmc_ili9341_flush; -#else - disp_drv.flush_cb = tft_espi_flush; -#endif - disp_drv.buffer = &disp_buf; + disp_drv.buffer = &disp_buf; + disp_drv.flush_cb = gui_flush_cb; // static void that uses the appropriate driver + + // #if defined(USE_FSMC) + // disp_drv.flush_cb = fsmc_ili9341_flush; + // #else + // disp_drv.flush_cb = tft_espi_flush; + // #endif + if(guiRotation == 0 || guiRotation == 2 || guiRotation == 4 || guiRotation == 6) { /* 1/3=Landscape or 0/2=Portrait orientation */ // Normal width & height @@ -590,7 +382,7 @@ void guiSetup() disp_drv.ver_res = TFT_WIDTH; } lv_disp_drv_register(&disp_drv); - guiStart(); + guiStart(); // Ticker /* Initialize Global progress bar*/ lv_obj_t * bar = lv_bar_create(lv_layer_sys(), NULL); @@ -608,13 +400,11 @@ void guiSetup() lv_obj_set_style_local_bg_color(lv_layer_sys(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK); lv_obj_set_style_local_bg_opa(lv_layer_sys(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_0); - /*Initialize the touch pad*/ + /* Initialize the touch pad */ lv_indev_drv_t indev_drv; lv_indev_drv_init(&indev_drv); - // indev_drv.type = LV_INDEV_TYPE_ENCODER; - // indev_drv.read_cb = read_encoder; indev_drv.type = LV_INDEV_TYPE_POINTER; - indev_drv.read_cb = my_touchpad_read; + indev_drv.read_cb = drv_touch_read; lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv); mouse_indev->driver.type = LV_INDEV_TYPE_POINTER; @@ -650,16 +440,11 @@ void IRAM_ATTR guiLoop(void) #if defined(STM32F4xx) tick.update(); #endif - -#if TOUCH_DRIVER == 911 - touch.loop(); -#endif } void guiEverySecond(void) { - // check if we went to sleep, wake up is handled in the event handlers - guiCheckSleep(); + // nothing } void guiStart() @@ -764,9 +549,9 @@ bool guiGetConfig(const JsonObject & settings) } else { changed = true; -#if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) + #if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) tft_espi_set_touch(calData); -#endif + #endif } i++; } @@ -779,9 +564,9 @@ bool guiGetConfig(const JsonObject & settings) } changed = true; -#if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) + #if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) tft_espi_set_touch(calData); -#endif + #endif } if(changed) configOutput(settings, TAG_GUI); @@ -838,9 +623,9 @@ bool guiSetConfig(const JsonObject & settings) oobeSetAutoCalibrate(true); } -#if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) + #if TOUCH_DRIVER == 2046 && USE_TFT_ESPI > 0 && defined(TOUCH_CS) if(status) tft_espi_set_touch(calData); -#endif + #endif changed |= status; } @@ -922,7 +707,7 @@ static void gui_screenshot_to_file(lv_disp_drv_t * disp, const lv_area_t * area, len *= sizeof(lv_color_t); /* Number of bytes */ size_t res = pFileOut.write((uint8_t *)color_p, len); if(res != len) gui_flush_not_complete(); - printscreen_flush_cb(disp, area, color_p); + gui_flush_cb(disp, area, color_p); } /** Take Screenshot. @@ -976,7 +761,7 @@ static void gui_screenshot_to_http(lv_disp_drv_t * disp, const lv_area_t * area, len *= sizeof(lv_color_t); /* Number of bytes */ size_t res = httpClientWrite((uint8_t *)color_p, len); if(res != len) gui_flush_not_complete(); - printscreen_flush_cb(disp, area, color_p); + gui_flush_cb(disp, area, color_p); } /** Take Screenshot.