diff --git a/include/lv_drv_conf.h b/include/lv_drv_conf.h index 797d8e1c..ea6a588c 100644 --- a/include/lv_drv_conf.h +++ b/include/lv_drv_conf.h @@ -73,8 +73,13 @@ /********************* * DISPLAY DRIVERS *********************/ -#define USE_TFT_ESPI 1 +#if defined(USE_FSMC) +#define USE_TFT_ESPI 0 #define USE_FSMC_ILI9341 1 +#else +#define USE_TFT_ESPI 1 +#define USE_FSMC_ILI9341 0 +#endif /*------------------- * Monitor of PC @@ -238,17 +243,22 @@ /********************* * INPUT DEVICES *********************/ +#define USE_XPT2046_ALT_DRV 0 /*-------------- * XPT2046 *--------------*/ #ifndef USE_XPT2046 +#if defined(USE_FSMC) #define USE_XPT2046 1 +#else +#define USE_XPT2046 0 +#endif #endif #if USE_XPT2046 -#define XPT2046_HOR_RES 480 -#define XPT2046_VER_RES 320 +#define XPT2046_HOR_RES TFT_WIDTH +#define XPT2046_VER_RES TFT_HEIGHT #define XPT2046_X_MIN 200 #define XPT2046_Y_MIN 200 #define XPT2046_X_MAX 3800 diff --git a/lib/lv_drivers/display/fsmc_ili9341.cpp b/lib/lv_drivers/display/fsmc_ili9341.cpp index 6d890d52..9448fcbe 100644 --- a/lib/lv_drivers/display/fsmc_ili9341.cpp +++ b/lib/lv_drivers/display/fsmc_ili9341.cpp @@ -8,7 +8,7 @@ *********************/ #include "fsmc_ili9341.h" -#if USE_FSMC_ILI9341 != 0 +#if USE_FSMC_ILI9341 > 0 #include #include @@ -17,12 +17,12 @@ #include #include "GxTFT_GFX.h" // Hardware-specific library -#include "GxTFT.h" // Hardware-specific library +#include "GxTFT.h" // Hardware-specific library #define TFT_Class GxTFT #include "GxIO/GxIO.h" -// select one GxIO class, +// select one GxIO class, // note: "error: 'GxIO_Class' does not name a type": indicates target board selection mismatch // this version is for use with Arduino package STM32GENERIC, board "BLACK F407VE/ZE/ZG boards". // Specific Board "BLACK F407ZG (M4 DEMO)" @@ -30,7 +30,14 @@ // For Serial I use a Serial to USB converter on PA9, PA10, "SerialUART1". // https://github.com/danieleff/STM32GENERIC #include "GxIO/STM32DUINO/GxIO_STM32F4_FSMC/GxIO_STM32F4_FSMC.h" -#include "myTFTs/my_3.2_TFT_320x240_ILI9341_STM32F407ZGM4_FSMC.h" +//#include "myTFTs/my_3.2_TFT_320x240_ILI9341_STM32F407ZGM4_FSMC.h" + +// #include "../GxIO/STM32GENERIC/GxIO_STM32F407ZGM4_FSMC/GxIO_STM32F407ZGM4_FSMC.h" +//#include "../GxIO/STM32DUINO/GxIO_STM32F4_FSMC/GxIO_STM32F4_FSMC.h" +#include "GxCTRL/GxCTRL_ILI9341/GxCTRL_ILI9341.h" // 240x320 +GxIO_Class io; // #define GxIO_Class is in the selected header file +GxCTRL_Class controller(io); // #define GxCTRL_Class is in the selected header file +TFT_Class tft(io, controller, TFT_WIDTH, TFT_HEIGHT); #include LV_DRV_DISP_INCLUDE #include LV_DRV_DELAY_INCLUDE @@ -76,17 +83,49 @@ void fsmc_ili9341_init(uint8_t rotation) { tft.init(); tft.setRotation(rotation); + // tft.fillScreen(BLUE); } -void fsmc_ili9341_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p) +/* GxTFT::pushColors only supports writing up to 255 at a time */ +/* This local function circumvents this artificial limit */ +static inline void pushColors(uint16_t * data, uint32_t len) { + io.startTransaction(); + while(len--) { + uint16_t color = *data++; + io.writeData16(color); + } + io.endTransaction(); +} + +void fsmc_ili9341_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t * color_p) +{ + tft.setWindow(x1, y1, x2, y2); size_t len = (x2 - x1 + 1) * (y2 - y1 + 1); /* Number of pixels */ +#if 1 + pushColors((uint16_t *)color_p, len); +#else + // tft.init(); + // Serial.print("flush "); + // Serial.print(x1); + // Serial.print(" "); + // Serial.print(y1); + // Serial.print(" "); + // Serial.print(x2); + // Serial.print(" "); + // Serial.print(y2); + // Serial.print(" "); + // Serial.println(len); + /* Update TFT */ - // tft.startWrite(); /* Start new TFT transaction */ - tft.setWindow(x1, y1, x2, y2); /* set the working window */ - tft.pushColors((uint16_t *)color_p, len); /* Write words at once */ - // tft.endWrite(); /* terminate TFT transaction */ + while(len > 255) { + tft.pushColors((uint16_t *)color_p, 255); + len -= 255; + color_p += 255; + } + tft.pushColors((uint16_t *)color_p, len); // remainder +#endif /* Tell lvgl that flushing is done */ // lv_disp_flush_ready(); @@ -97,7 +136,7 @@ void fsmc_ili9341_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_ tft.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1, color.full); } -void fsmc_ili9341_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p) +void fsmc_ili9341_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t * color_p) { fsmc_ili9341_flush(x1, y1, x2, y2, color_p); } diff --git a/lib/lv_drivers/display/fsmc_ili9341.h b/lib/lv_drivers/display/fsmc_ili9341.h index 1ed9c49d..0f9689a7 100644 --- a/lib/lv_drivers/display/fsmc_ili9341.h +++ b/lib/lv_drivers/display/fsmc_ili9341.h @@ -21,7 +21,7 @@ extern "C" { #endif #endif -#if USE_FSMC_ILI9341 +#if USE_FSMC_ILI9341>0 #ifdef LV_LVGL_H_INCLUDE_SIMPLE #include "lvgl.h" @@ -32,16 +32,6 @@ extern "C" { /********************* * DEFINES *********************/ -#define ILI9341_DRIVER 1 -#define TFT_WIDTH 240 -#define TFT_HEIGHT 320 -#define TFT_ROTATION 2 // 0=0, 1=90, 2=180 or 3=270 degree -#define SPI_FREQUENCY 40000000 -#define SPI_TOUCH_FREQUENCY 2500000 -#define SPI_READ_FREQUENCY 20000000 -#define USER_SETUP_LOADED 1 -#define TOUCH_DRIVER 0 // XPT2606 Resistive touch panel driver -#define SUPPORT_TRANSACTIONS /********************** * TYPEDEFS @@ -51,9 +41,9 @@ extern "C" { * GLOBAL PROTOTYPES **********************/ void fsmc_ili9341_init(uint8_t rotation); -void fsmc_ili9341_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p); +void fsmc_ili9341_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t * color_p); void fsmc_ili9341_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color); -void fsmc_ili9341_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p); +void fsmc_ili9341_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t * color_p); /********************** * MACROS **********************/ diff --git a/lib/lv_drivers/display/tft_espi_drv.cpp b/lib/lv_drivers/display/tft_espi_drv.cpp index 01d3e93e..39195ba6 100644 --- a/lib/lv_drivers/display/tft_espi_drv.cpp +++ b/lib/lv_drivers/display/tft_espi_drv.cpp @@ -7,7 +7,6 @@ * INCLUDES *********************/ #include "tft_espi_drv.h" -#include "../../../src/hasp_tft.h" #if USE_TFT_ESPI != 0 @@ -16,6 +15,7 @@ #include LV_DRV_DISP_INCLUDE #include LV_DRV_DELAY_INCLUDE +#include "../../../src/hasp_tft.h" /********************* * DEFINES @@ -90,6 +90,13 @@ void tft_espi_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color tft_espi_flush(x1, y1, x2, y2, color_p); } +#if defined(TOUCH_CS) +bool tft_espi_get_touch(uint16_t * touchX, uint16_t * touchY, uint16_t threshold) +{ + return tft.getTouch(touchX, touchY, threshold); +} +#endif + /********************** * STATIC FUNCTIONS **********************/ diff --git a/lib/lv_drivers/display/tft_espi_drv.h b/lib/lv_drivers/display/tft_espi_drv.h index c7791372..55ee7250 100644 --- a/lib/lv_drivers/display/tft_espi_drv.h +++ b/lib/lv_drivers/display/tft_espi_drv.h @@ -32,16 +32,7 @@ extern "C" { /********************* * DEFINES *********************/ -#define ILI9341_DRIVER 1 -#define TFT_WIDTH 240 -#define TFT_HEIGHT 320 -#define TFT_ROTATION 2 // 0=0, 1=90, 2=180 or 3=270 degree -#define SPI_FREQUENCY 40000000 -#define SPI_TOUCH_FREQUENCY 2500000 -#define SPI_READ_FREQUENCY 20000000 -#define USER_SETUP_LOADED 1 -#define TOUCH_DRIVER 0 // XPT2606 Resistive touch panel driver -#define SUPPORT_TRANSACTIONS + /********************** * TYPEDEFS @@ -54,6 +45,11 @@ void tft_espi_init(uint8_t rotation); void tft_espi_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p); void tft_espi_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color); void tft_espi_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p); + +#if defined(TOUCH_CS) +bool tft_espi_get_touch(uint16_t * touchX, uint16_t * touchY, uint16_t threshold); +#endif + /********************** * MACROS **********************/ diff --git a/lib/lv_drivers/indev/XPT2046.c b/lib/lv_drivers/indev/XPT2046.cpp similarity index 51% rename from lib/lv_drivers/indev/XPT2046.c rename to lib/lv_drivers/indev/XPT2046.cpp index f27fa763..2794fcdd 100644 --- a/lib/lv_drivers/indev/XPT2046.c +++ b/lib/lv_drivers/indev/XPT2046.cpp @@ -12,12 +12,14 @@ #include #include LV_DRV_INDEV_INCLUDE #include LV_DRV_DELAY_INCLUDE +#include +#include "XPT2046_Touchscreen.h" /********************* * DEFINES *********************/ -#define CMD_X_READ 0b10010000 -#define CMD_Y_READ 0b11010000 +#define CMD_X_READ 0b10010000 +#define CMD_Y_READ 0b11010000 /********************** * TYPEDEFS @@ -35,6 +37,8 @@ static void xpt2046_avg(int16_t * x, int16_t * y); int16_t avg_buf_x[XPT2046_AVG]; int16_t avg_buf_y[XPT2046_AVG]; uint8_t avg_last; +SPIClass xpt2046_spi(PB15, PB14, PB13, PB12); +XPT2046_Touchscreen ts(PB12); /********************** * MACROS @@ -47,9 +51,10 @@ uint8_t avg_last; /** * Initialize the XPT2046 */ -void xpt2046_init(void) +void xpt2046_init(uint8_t rotation) { - + ts.begin(); + ts.setRotation(rotation); } /** @@ -66,22 +71,31 @@ bool xpt2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) int16_t x = 0; int16_t y = 0; - uint8_t irq = LV_DRV_INDEV_IRQ_READ; + // uint8_t irq = LV_DRV_INDEV_IRQ_READ; + data->state = ts.touched(); - if(irq == 0) { - LV_DRV_INDEV_SPI_CS(0); + if(data->state) { + TS_Point p = ts.getPoint(); + x = p.x; + y = p.y; + xpt2046_corr(&x, &y); - LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_X_READ); /*Start x read*/ +#if 0 + // LV_DRV_INDEV_SPI_CS(0); + xpt2046_spi.beginTransaction(SPI_SETTING); + digitalWrite(PB12, LOW); - buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read x MSB*/ - x = buf << 8; - buf = LV_DRV_INDEV_SPI_XCHG_BYTE(CMD_Y_READ); /*Until x LSB converted y command can be sent*/ + xpt2046_spi.transfer(CMD_X_READ); /*Start x read*/ + + buf = xpt2046_spi.transfer(0); /*Read x MSB*/ + x = buf << 8; + buf = xpt2046_spi.transfer(CMD_Y_READ); /*Until x LSB converted y command can be sent*/ x += buf; - buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read y MSB*/ - y = buf << 8; + buf = xpt2046_spi.transfer(0); /*Read y MSB*/ + y = buf << 8; - buf = LV_DRV_INDEV_SPI_XCHG_BYTE(0); /*Read y LSB*/ + buf = xpt2046_spi.transfer(0); /*Read y LSB*/ y += buf; /*Normalize Data*/ @@ -90,20 +104,38 @@ bool xpt2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) xpt2046_corr(&x, &y); xpt2046_avg(&x, &y); - last_x = x; - last_y = y; - data->state = LV_INDEV_STATE_PR; + last_x = x; + last_y = y; + data->state = LV_INDEV_STATE_PR; - LV_DRV_INDEV_SPI_CS(1); + if(data->state) { + Serial.print(x); + Serial.print(" - "); + Serial.println(y); + } else { + Serial.print("."); + } + + // LV_DRV_INDEV_SPI_CS(1); + digitalWrite(PB12, HIGH); + xpt2046_spi.endTransaction(); +#endif } else { - x = last_x; - y = last_y; - avg_last = 0; - data->state = LV_INDEV_STATE_REL; + x = last_x; + y = last_y; + avg_last = 0; + data->state = LV_INDEV_STATE_REL; } data->point.x = x; data->point.y = y; + if(data->state) { + Serial.print(x); + Serial.print(" - "); + Serial.println(y); + } else { + // Serial.print("."); + } return false; } @@ -116,39 +148,38 @@ static void xpt2046_corr(int16_t * x, int16_t * y) #if XPT2046_XY_SWAP != 0 int16_t swap_tmp; swap_tmp = *x; - *x = *y; - *y = swap_tmp; + *x = *y; + *y = swap_tmp; #endif - if((*x) > XPT2046_X_MIN)(*x) -= XPT2046_X_MIN; - else(*x) = 0; + if((*x) > XPT2046_X_MIN) + (*x) -= XPT2046_X_MIN; + else + (*x) = 0; - if((*y) > XPT2046_Y_MIN)(*y) -= XPT2046_Y_MIN; - else(*y) = 0; + if((*y) > XPT2046_Y_MIN) + (*y) -= XPT2046_Y_MIN; + else + (*y) = 0; - (*x) = (uint32_t)((uint32_t)(*x) * XPT2046_HOR_RES) / - (XPT2046_X_MAX - XPT2046_X_MIN); + (*x) = (uint32_t)((uint32_t)(*x) * XPT2046_HOR_RES) / (XPT2046_X_MAX - XPT2046_X_MIN); - (*y) = (uint32_t)((uint32_t)(*y) * XPT2046_VER_RES) / - (XPT2046_Y_MAX - XPT2046_Y_MIN); + (*y) = (uint32_t)((uint32_t)(*y) * XPT2046_VER_RES) / (XPT2046_Y_MAX - XPT2046_Y_MIN); #if XPT2046_X_INV != 0 - (*x) = XPT2046_HOR_RES - (*x); + (*x) = XPT2046_HOR_RES - (*x); #endif #if XPT2046_Y_INV != 0 - (*y) = XPT2046_VER_RES - (*y); + (*y) = XPT2046_VER_RES - (*y); #endif - - } - static void xpt2046_avg(int16_t * x, int16_t * y) { /*Shift out the oldest data*/ uint8_t i; - for(i = XPT2046_AVG - 1; i > 0 ; i--) { + for(i = XPT2046_AVG - 1; i > 0; i--) { avg_buf_x[i] = avg_buf_x[i - 1]; avg_buf_y[i] = avg_buf_y[i - 1]; } @@ -161,7 +192,7 @@ static void xpt2046_avg(int16_t * x, int16_t * y) /*Sum the x and y coordinates*/ int32_t x_sum = 0; int32_t y_sum = 0; - for(i = 0; i < avg_last ; i++) { + for(i = 0; i < avg_last; i++) { x_sum += avg_buf_x[i]; y_sum += avg_buf_y[i]; } diff --git a/lib/lv_drivers/indev/XPT2046.h b/lib/lv_drivers/indev/XPT2046.h index 7eee8c00..7a8c047f 100644 --- a/lib/lv_drivers/indev/XPT2046.h +++ b/lib/lv_drivers/indev/XPT2046.h @@ -40,7 +40,7 @@ extern "C" { /********************** * GLOBAL PROTOTYPES **********************/ -void xpt2046_init(void); +void xpt2046_init(uint8_t rotation); bool xpt2046_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); /********************** diff --git a/lib/lv_drivers/indev/XPT2046_alt_drv.cpp b/lib/lv_drivers/indev/XPT2046_alt_drv.cpp new file mode 100644 index 00000000..fab4ffcf --- /dev/null +++ b/lib/lv_drivers/indev/XPT2046_alt_drv.cpp @@ -0,0 +1,75 @@ +/** + * @file XPT2046.c + * + */ + +/********************* + * INCLUDES + *********************/ +#include "XPT2046_alt_drv.h" +#if USE_XPT2046_ALT_DRV + +#include +#include LV_DRV_INDEV_INCLUDE +#include LV_DRV_DELAY_INCLUDE +#include + +/********************* + * DEFINES + *********************/ +#define CS_PIN PB12 + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * STATIC VARIABLES + **********************/ +XPT2046_Touchscreen ts(CS_PIN); + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Initialize the XPT2046 + */ +void xpt2046_alt_drv_init(uint8_t rotation) +{ + ts.begin(); + ts.setRotation(rotation); +} + +/** + * Get the current position and state of the touchpad + * @param data store the read data here + * @return false: because no more data to be read + */ +bool xpt2046_alt_drv_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data) +{ + data->state = ts.touched(); + if(data->state) { + TS_Point p = ts.getPoint(); + data->point.x = p.x; + data->point.y = p.y; + Serial.print(p.x); + Serial.print(" - "); + Serial.println(p.y); + } + return false; +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +#endif diff --git a/lib/lv_drivers/indev/XPT2046_alt_drv.h b/lib/lv_drivers/indev/XPT2046_alt_drv.h new file mode 100644 index 00000000..7918ed68 --- /dev/null +++ b/lib/lv_drivers/indev/XPT2046_alt_drv.h @@ -0,0 +1,56 @@ +/** + * @file XPT2046_alt_drv.h + * + */ + +#ifndef XPT2046_ALT_DRV_H +#define XPT2046_ALT_DRV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#ifndef LV_DRV_NO_CONF +#ifdef LV_CONF_INCLUDE_SIMPLE +#include "lv_drv_conf.h" +#else +#include "../../lv_drv_conf.h" +#endif +#endif + +#if USE_XPT2046_ALT_DRV + +#ifdef LV_LVGL_H_INCLUDE_SIMPLE +#include "lvgl.h" +#else +#include "lvgl/lvgl.h" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +void xpt2046_alt_drv_init(uint8_t rotation); +bool xpt2046_alt_drv_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data); + +/********************** + * MACROS + **********************/ + +#endif /* USE_XPT2046_ALT_DRV */ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* XPT2046_ALT_DRV_H */