diff --git a/.gitignore b/.gitignore index 6cda5aa..2e6be77 100644 --- a/.gitignore +++ b/.gitignore @@ -26,9 +26,12 @@ sdkconfig.lobo BUILD PBUILD build/ +cmake-build-debug/ +.idea/ temp/ local/ *.dis *.elf *.map **/.DS_Store +mkspiffs/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..edc507d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +# The following lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --save-temps") +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(tft_demo) diff --git a/README.md b/README.md index 23d5fa5..9ee8b5c 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,61 @@ +## Not Maintained + +I'm not currently doing any ESP32 development at all, and so I'm unable to address issues in this library. +If anyone will volunteer to maintain the active fork I'm happy to link to it from this README. Just open a PR with the README changes. + +For my own project I ended up using [LVGL](https://github.com/lvgl/lv_port_esp32) instead of this library. + +The license of this project is in complete doubt because upstream [never released it under an open source license](https://github.com/loboris/ESP32_TFT_library/issues/80). + +Technically it is not open source and no one can use it in their project without risk of a copyright infringement, though loboris clearly intended for people to use it freely. ### TFT library for ESP32 --- -**This library must be built with the latest esp-idf master branch and xtensa toolchain** - -If you are using the esp-idf v2.1, checkout the commit *0518df81a6566820352dad7bf6c539995d41ad18* +**This library must be built with the esp-idf release/v4.0 branch.** +ESP-IDF 4.0 is currently in beta and instructions are found [here]( +https://docs.espressif.com/projects/esp-idf/en/v4.0-beta1/get-started/index.html) --- #### Features -* Full support for **ILI9341**, **ILI9488**, **ST7789V** and **ST7735** based TFT modules in 4-wire SPI mode. Support for other controllers will be added later +* Full support for **ILI9341**, **ILI9488**, **ST7789V** and **ST7735** based TFT modules in 4-wire SPI mode. * **18-bit (RGB)** color mode used * **SPI displays oriented SPI driver library** based on *spi-master* driver * Combined **DMA SPI** transfer mode and **direct SPI** for maximal speed * **Grayscale mode** can be selected during runtime which converts all colors to gray scale -* SPI speeds up to **40 MHz** are tested and works without problems +* SPI speeds up to **40 MHz** are tested and work without problems * **Demo application** included which demonstrates most of the library features * **Graphics drawing functions**: * **TFT_drawPixel** Draw pixel at given x,y coordinates * **TFT_drawLine** Draw line between two points - * **TFT_drawFastVLine**, **TFT_drawFastHLine** Draw vertical or horizontal line of given lenght + * **TFT_drawFastVLine**, **TFT_drawFastHLine** Draw vertical or horizontal line of given length * **TFT_drawLineByAngle** Draw line on screen from (x,y) point at given angle * **TFT_drawRect**, **TFT_fillRect** Draw rectangle on screen or fill given rectangular screen region with color * **TFT_drawRoundRect**, **TFT_fillRoundRect** Draw rectangle on screen or fill given rectangular screen region with color with rounded corners * **TFT_drawCircle**, **TFT_fillCircle** Draw or fill circle on screen * **TFT_drawEllipse**, **TFT_fillEllipse** Draw or fill ellipse on screen - * **TFT_drawTriangel**, **TFT_fillTriangle** Draw or fill triangle on screen + * **TFT_drawTriangle**, **TFT_fillTriangle** Draw or fill triangle on screen * **TFT_drawArc** Draw circle arc on screen, from ~ to given angles, with given thickness. Can be outlined with different color - * **TFT_drawPolygon** Draw poligon on screen with given number of sides (3~60). Can be outlined with different color and rotated by given angle. + * **TFT_drawPolygon** Draw polygon on screen with given number of sides (3~60). Can be outlined with different color and rotated by given angle * **Fonts**: - * **fixed** width and proportional fonts are supported; 8 fonts embeded + * **fixed** width and proportional fonts are supported; 8 fonts embedded * unlimited number of **fonts from file** * **7-segment vector font** with variable width/height is included (only numbers and few characters) - * Proportional fonts can be used in fixed width mode. + * Proportional fonts can be used in fixed width mode * Related functions: * **TFT_setFont** Set current font from one of embeded fonts or font file - * **TFT_getfontsize** Returns current font height & width in pixels. - * **TFT_getfontheight** Returns current font height in pixels. - * **set_7seg_font_atrib** Set atributes for 7 segment vector font + * **TFT_getfontsize** Returns current font height & width in pixels + * **TFT_getfontheight** Returns current font height in pixels + * **set_7seg_font_atrib** Set attributes for 7 segment vector font * **getFontCharacters** Get all font's characters to buffer * **String write function**: - * **TFT_print** Write text to display. - * Strings can be printed at **any angle**. Rotation of the displayed text depends on *font_ratate* variable (0~360) + * **TFT_print** Write text to display + * Strings can be printed at **any angle**. Rotation of the displayed text depends on *tft_font_rotate* variable (0~360) * if *font_transparent* variable is set to 1, no background pixels will be printed * If the text does not fit the screen/window width it will be clipped ( if *text_wrap=0* ), or continued on next line ( if *text_wrap=1* ) * Two special characters are allowed in strings: *\r* CR (0x0D), clears the display to EOL, *\n* LF (ox0A), continues to the new line, x=0 @@ -57,15 +67,15 @@ If you are using the esp-idf v2.1, checkout the commit *0518df81a6566820352dad7b * *CENTER* centers the text verticaly * *BOTTOM* bottom justifies the text * *LASTY* continues from last Y position; offset can be used: *LASTY+n* - * **TFT_getStringWidth** Returns the string width in pixels based on current font characteristics. Useful for positioning strings on the screen. + * **TFT_getStringWidth** Returns the string width in pixels based on current font characteristics. Useful for positioning strings on the screen * **TFT_clearStringRect** Fills the rectangle occupied by string with current background color * **Images**: - * **TFT_jpg_image** Decodes and displays JPG images + * **TFT_jpg_image** Decodes and displays JPEG images * Limits: - * Baseline only. Progressive and Lossless JPEG format are not supported. + * Baseline only. Progressive and Lossless JPEG format are not supported * Image size: Up to 65520 x 65520 pixels - * Color space: YCbCr three components only. Gray scale image is not supported. - * Sampling factor: 4:4:4, 4:2:2 or 4:2:0. + * Color space: YCbCr three components only. Gray scale image is not supported + * Sampling factor: 4:4:4, 4:2:2 or 4:2:0 * Can display the image **from file** or **memory buffer** * Image can be **scaled** by factor 0 ~ 3 (1/1, 1/2, 1/4 or 1/8) * Image is displayed from X,Y position on screen/window: @@ -89,9 +99,9 @@ If you are using the esp-idf v2.1, checkout the commit *0518df81a6566820352dad7b * **TFT_fillWindow** Fill *window* area with color * **Touch screen** supported (for now only **XPT2046** controllers) * **TFT_read_touch** Detect if touched and return X,Y coordinates. **Raw** touch screen or **calibrated** values can be returned. - * calibrated coordinates are adjusted for screen orientation. + * calibrated coordinates are adjusted for screen orientation * **Read from display memory** supported - * **TFT_readPixel** Read pixel color value from display GRAM at given x,y coordinates. + * **TFT_readPixel** Read pixel color value from display GRAM at given x,y coordinates * **TFT_readData** Read color data from rectangular screen area * **Other display functions**: * **TFT_fillScreen** Fill the whole screen with color @@ -101,32 +111,32 @@ If you are using the esp-idf v2.1, checkout the commit *0518df81a6566820352dad7b * **disp_select()** Activate display's CS line * **disp_deselect()** Deactivate display's CS line * **find_rd_speed()** Find maximum spi clock for successful read from display RAM - * **TFT_display_init()** Perform display initialization sequence. Sets orientation to landscape; clears the screen. SPI interface must already be setup, *tft_disp_type*, *_width*, *_height* variables must be set. - * **HSBtoRGB** Converts the components of a color, as specified by the HSB model to an equivalent set of values for the default RGB model. + * **TFT_display_init()** Perform display initialization sequence. Sets orientation to landscape; clears the screen. SPI interface must already be set up, *tft_disp_type*, *tft_width*, *tft_height* variables must be set. + * **HSBtoRGB** Converts the components of a color, as specified by the HSB model to an equivalent set of values for the default RGB model * **TFT_setGammaCurve()** Select one of 4 Gamma curves * **compile_font_file** Function which compiles font c source file to font file which can be used in *TFT_setFont()* function to select external font. Created file have the same name as source file and extension *.fnt* -* **Global wariables** - * **orientation** current screen orientation - * **font_ratate** current font rotate angle (0~395) - * **font_transparent** if not 0 draw fonts transparent - * **font_forceFixed** if not zero force drawing proportional fonts with fixed width - * **text_wrap** if not 0 wrap long text to the new line, else clip - * **_fg** current foreground color for fonts - * **_bg** current background for non transparent fonts - * **dispWin** current display clip window - * **_angleOffset** angle offset for arc, polygon and line by angle functions - * **image_debug** print debug messages during image decode if set to 1 - * **cfont** Currently used font structure - * **TFT_X** X position of the next character after TFT_print() function - * **TFT_Y** Y position of the next character after TFT_print() function - * **tp_calx** touch screen X calibration constant - * **tp_caly** touch screen Y calibration constant - * **gray_scale** convert all colors to gray scale if set to 1 - * **max_rdclock** current spi clock for reading from display RAM - * **_width** screen width (smaller dimension) in pixels - * **_height** screen height (larger dimension) in pixels +* **Global variables** + * **tft_orientation** current screen orientation + * **tft_font_rotate** current font rotate angle (0~395) + * **tft_font_transparent** if not 0 draw fonts transparent + * **tft_font_forceFixed** if not zero force drawing proportional fonts with fixed width + * **tft_text_wrap** if not 0 wrap long text to the new line, else clip + * **tft_fg** current foreground color for fonts + * **tft_bg** current background for non transparent fonts + * **tft_dispWin** current display clip window + * **tft_angleOffset** angle offset for arc, polygon and line by angle functions + * **tft_image_debug** print debug messages during image decode if set to 1 + * **tft_cfont** Currently used font structure + * **tft_x** X position of the next character after TFT_print() function + * **tft_y** Y position of the next character after TFT_print() function + * **tft_tp_calx** touch screen X calibration constant + * **tft_tp_caly** touch screen Y calibration constant + * **tft_gray_scale** convert all colors to gray scale if set to 1 + * **tft_max_rdclock** current spi clock for reading from display RAM + * **tft_width** screen width (smaller dimension) in pixels + * **tft_height** screen height (larger dimension) in pixels * **tft_disp_type** current display type (DISP_TYPE_ILI9488 or DISP_TYPE_ILI9341) --- @@ -146,13 +156,13 @@ Full **demo application**, well documented, is included, please **analyze it** t | Any output pin | SCK | SPI clock input on Display module | | Any output pin | CS | SPI CS input on Display module | | Any output pin | DC | DC (data/command) input on Display module | -| Any output pin | TCS | Touch pannel CS input (if touch panel is used | +| Any output pin | TCS | Touch pannel CS input (if touch panel is used) | | Any output pin | RST | **optional**, reset input of the display module, if not used **pullup the reset input** to Vcc | | Any output pin | BL | **optional**, backlight input of the display module, if not used connect to +3.3V (or +5V) | | GND | GND | Power supply ground | | 3.3V or +5V | Vcc | Power supply positive | -**Make shure the display module has 3.3V compatible interface, if not you must use level shifter!** +**Make sure the display module has 3.3V compatible interface, if not you must use level shifter!** --- @@ -164,65 +174,110 @@ To run the demo, attach ILI9341, ILI9488 or ST7735 based display module to ESP32 * DC: 26 (display DC) * TCS: 25 (touch screen CS) +**Custom PINS can be defined in `idf.py menuconfig` in Components -> TFT Display menu** --- -*To run the demo on* **ESP-WROWER-KIT v3** *select the following pin configuration:* -* mosi: 23 -* miso: 25 -* sck: 19 -* CS: 22 (display CS) -* DC: 21 (display DC) -* TCS: 0 (touch screen CS), not used -* RST: 18 (display RESET) -* BKLIT: 5 (Display Back light) +#### Display Kits + +Predefined display configurations are available that will set pins, display size, and inversion properly for the specified kit. -Also set **TFT_RGB_BGR** to 0x00 and **TFT_INVERT_ROTATION1** to 1 in *tftspi.h* +Access these through the `idf.py menuconfig` in Components->TFT Display -**You can also select EXAMPLE_ESP_WROVER_KIT in menuconfig to automaticaly define correct configuration** +Configurations are available for: + + "ESP-WROVER-KIT v3 Display (ST7789V)" + "ESP-WROVER-KIT v4.1 Display (ILI9341)" + "Adafruit TFT Feather Display" + "M5Stack TFT Display" --- -**If you want to use different pins, change them in** *tftspi.h* +#### Other config notes -**if you want to use the touch screen functions, set** `#define USE_TOUCH 1` in *tftspi.h* +Touch screen can be enabled in Components -> TFT Display as well. -Using *make menuconfig* **select tick rate 1000** ( → Component config → FreeRTOS → Tick rate (Hz) ) to get more accurate timings +Using *idf.py menuconfig* **select tick rate 1000** ( → Component config → FreeRTOS → Tick rate (Hz) ) to get more accurate timings --- -#### How to build +#### Installing as Library +This repository is intended to be installable as a component library using the ESP-IDF 4.0 build system. + +It is recommended though that you first follow the [demo instructions](#building-the-demo) below to build this repository as a standalone example to validate your hardware and have a basis to learn the library features. + +When you are ready to incorporate it into your existing project, it is recommended to: + +```shell +mkdir -p externals +git submodule add https://github.com/jeremyjh/ESP32_TFT_library.git externals/ESP32_TFT_library +``` + +In your project's root CMakeLists.txt add the components folder to `EXTRA_COMPONENT_DIRS` - make sure this is before the project() config e.g. + +```cmake +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + set(EXTRA_COMPONENT_DIRS + externals/ESP32_TFT_library/components) -Configure your esp32 build environment as for **esp-idf examples** +project(hello-world) +``` -Clone the repository +In your own components//CMakeLists.txt or main/CMakeLists.txt add `tft` and `spiffs` (if you are using spiffs) as `REQUIRES` e.g. + +```cmake +set(SOURCES tft_demo.c) +idf_component_register( + SRCS ${SOURCES} + INCLUDE_DIRS + ${CMAKE_CURRENT_LIST_DIR} + $ENV{IDF_PATH}/components + REQUIRES + tft + spiffs +) +``` -`git clone https://github.com/loboris/ESP32_TFT_library.git` +--- -Execute menuconfig and configure your Serial flash config and other settings. Included *sdkconfig.defaults* sets some defaults to be used. +#### Building the Demo -Navigate to **TFT Display DEMO Configuration** and set **SPIFFS** options. +Clone the repository to your esp folder (same level as esp-idf, as explained [in instructions](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html)). -Select if you want to use **wifi** (recommended) to get the time from **NTP** server and set your WiFi SSID and password. +`git clone https://github.com/jeremyjh/ESP32_TFT_library.git` -`make menuconfig` +Execute `idf.py menuconfig` and configure your Serial flash config and other settings. Included *sdkconfig.defaults* sets some defaults to be used. + +Navigate to **Components -> TFT Display** and set **display and pin** options or select a pre-defined display configuration for a kit. + +To enable **Wifi** in the demo (recommended - gets time from NTP), select **TFT Display DEMO Configuration** from the top-level menu and select those options. Make and flash the example. -`make all && make flash` +`idf.py build && idf.py -p flash monitor` + +Deploy the SPIFFS image as below to make the image and font examples work. --- #### Prepare **SPIFFS** image -*The demo uses some image and font files and it is necessary to flash the spiffs image* +*The demo uses some image and font files and it is necessary to flash the spiffs image.* **To flash already prepared image to flash** execute: -`make copyfs` +`ESPPORT= make copyfs` --- -You can also prepare different SFPIFFS **image** and flash it to ESP32. *This feature is only tested on Linux.* +You can also prepare different SFPIFFS **image** and flash it to ESP32. + +The example partition file reserves 1MB for SPIFFS. +If you change the spiffs partition size, update the SPIFFS configuration as well. + +`idf.py menuconfig` +Navigate to **Components -> TFT SPIFFS** and set **SPIFFS** options. Files to be included on spiffs are already in **components/spiffs_image/image/** directory. You can add or remove the files you want to include. @@ -234,7 +289,7 @@ to create **spiffs image** in *build* directory **without flashing** to ESP32 Or execute: -`make flashfs` +`ESPPORT= make flashfs` to create **spiffs image** in *build* directory and **flash** it to ESP32 diff --git a/components/mkspiffs/CMakeLists.txt b/components/mkspiffs/CMakeLists.txt new file mode 100644 index 0000000..45d489e --- /dev/null +++ b/components/mkspiffs/CMakeLists.txt @@ -0,0 +1,3 @@ +set(COMPONENT_SRCDIRS "") +set(COMPONENT_ADD_INCLUDEDIRS "") +register_component() diff --git a/components/mkspiffs/src/mkspiffs b/components/mkspiffs/src/mkspiffs new file mode 100755 index 0000000..ce47612 Binary files /dev/null and b/components/mkspiffs/src/mkspiffs differ diff --git a/components/mkspiffs/src/spiffs/esp_spiffs.c b/components/mkspiffs/src/spiffs/esp_spiffs.c index 088480c..af15bf6 100644 --- a/components/mkspiffs/src/spiffs/esp_spiffs.c +++ b/components/mkspiffs/src/spiffs/esp_spiffs.c @@ -34,7 +34,12 @@ #include "spiffs.h" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) +#include +#include +#else #include +#endif s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { u32_t aaddr; @@ -63,7 +68,11 @@ s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } @@ -72,7 +81,11 @@ s32_t esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { free(buff); } else { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)dst, addr, size) != ESP_OK) { +#else if (spi_flash_read(addr, (void *)dst, size) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } } @@ -87,7 +100,7 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) { u32_t asize; asize = size; - + // Align address to 4 byte aaddr = (addr + (4 - 1)) & -4; if (aaddr != addr) { @@ -107,21 +120,33 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) { abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } memcpy(abuff + (addr - aaddr), src, size); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_write(NULL, (uint32_t *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } free(buff); } else { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_write(NULL, (uint32_t *)src, addr, size) != ESP_OK) { +#else if (spi_flash_write(addr, (uint32_t *)src, size) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } } @@ -130,7 +155,11 @@ s32_t esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) { } s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_erase_region(NULL, addr, size) != ESP_OK) { +#else if (spi_flash_erase_sector(addr >> 12) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } diff --git a/components/spidriver/CMakeLists.txt b/components/spidriver/CMakeLists.txt new file mode 100644 index 0000000..1b3ee90 --- /dev/null +++ b/components/spidriver/CMakeLists.txt @@ -0,0 +1,3 @@ +FILE(GLOB SOURCES *.c) +idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS "." + PRIV_REQUIRES driver) diff --git a/components/spidriver/spi_master_lobo.c b/components/spidriver/spi_master_lobo.c index a6bb3f0..24eeebb 100644 --- a/components/spidriver/spi_master_lobo.c +++ b/components/spidriver/spi_master_lobo.c @@ -60,30 +60,16 @@ Main driver's function is 'spi_lobo_transfer_data()' */ #include "freertos/FreeRTOS.h" #include -#include -#include "soc/gpio_sig_map.h" #include "soc/spi_reg.h" #include "soc/dport_reg.h" -#include "soc/rtc_cntl_reg.h" -#include "rom/ets_sys.h" -#include "esp_types.h" -#include "esp_attr.h" #include "esp_log.h" -#include "esp_err.h" #include "freertos/semphr.h" -#include "freertos/xtensa_api.h" -#include "freertos/task.h" -#include "freertos/ringbuf.h" -#include "soc/soc.h" -#include "soc/dport_reg.h" -#include "soc/uart_struct.h" #include "driver/uart.h" #include "driver/gpio.h" -#include "driver/periph_ctrl.h" -#include "esp_heap_caps.h" -#include "driver/periph_ctrl.h" #include "spi_master_lobo.h" - +#include "driver/periph_ctrl.h" +#include "soc/gpio_periph.h" +#include "rom/gpio.h" static spi_lobo_host_t *spihost[3] = {NULL}; @@ -635,8 +621,8 @@ esp_err_t spi_lobo_bus_remove_device(spi_lobo_device_handle_t handle) if (spihost[handle->host_dev]->device[x] !=NULL) break; } if (x == NO_DEV) { - free(handle); spi_lobo_bus_free(handle->host_dev, 1); + free(handle); } else free(handle); diff --git a/components/spidriver/spi_master_lobo.h b/components/spidriver/spi_master_lobo.h index 89144fb..bca00ca 100644 --- a/components/spidriver/spi_master_lobo.h +++ b/components/spidriver/spi_master_lobo.h @@ -21,9 +21,8 @@ #include "freertos/semphr.h" #include "soc/spi_struct.h" -#include "esp_intr.h" #include "esp_intr_alloc.h" -#include "rom/lldesc.h" +#include "esp32/rom/lldesc.h" #ifdef __cplusplus diff --git a/components/spiffs/CMakeLists.txt b/components/spiffs/CMakeLists.txt new file mode 100644 index 0000000..fc1f498 --- /dev/null +++ b/components/spiffs/CMakeLists.txt @@ -0,0 +1,3 @@ +FILE(GLOB SOURCES *.c) +idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS "." + REQUIRES vfs spi_flash) diff --git a/components/spiffs/Kconfig b/components/spiffs/Kconfig new file mode 100644 index 0000000..5123800 --- /dev/null +++ b/components/spiffs/Kconfig @@ -0,0 +1,27 @@ +menu "TFT SPIFFS" + +config SPIFFS_BASE_ADDR + int "SPIFFS Base address" + range 1048576 33546240 + default 0x180000 + help + Starting address of the SPIFFS area in ESP32 Flash + +config SPIFFS_SIZE + int "SPIFFS Size in bytes" + range 262144 2097152 + default 1048576 + +config SPIFFS_LOG_BLOCK_SIZE + int "SPIFFS Logical block size" + range 4096 65536 + default 8192 + +config SPIFFS_LOG_PAGE_SIZE + int "SPIFFS Logical page size" + range 256 2048 + default 256 + help + Set it to the physical page size og the used SPI Flash chip. + +endmenu diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 57b8aa0..8487183 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -34,7 +34,12 @@ #include "spiffs.h" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) +#include +#include +#else #include +#endif s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { u32_t aaddr; @@ -63,7 +68,11 @@ s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & (u32_t)-4); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } @@ -72,7 +81,11 @@ s32_t IRAM_ATTR esp32_spi_flash_read(u32_t addr, u32_t size, u8_t *dst) { free(buff); } else { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)dst, addr, size) != ESP_OK) { +#else if (spi_flash_read(addr, (void *)dst, size) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } } @@ -107,21 +120,33 @@ s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) { abuff = (u8_t *)(((ptrdiff_t)buff + (4 - 1)) & -4); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_read(NULL, (void *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_read(aaddr, (void *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } memcpy(abuff + (addr - aaddr), src, size); +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_write(NULL, (uint32_t *)abuff, aaddr, asize) != ESP_OK) { +#else if (spi_flash_write(aaddr, (uint32_t *)abuff, asize) != 0) { +#endif free(buff); return SPIFFS_ERR_INTERNAL; } free(buff); } else { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_write(NULL, (uint32_t *)src, addr, size) != ESP_OK) { +#else if (spi_flash_write(addr, (uint32_t *)src, size) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } } @@ -130,7 +155,11 @@ s32_t IRAM_ATTR esp32_spi_flash_write(u32_t addr, u32_t size, const u8_t *src) { } s32_t IRAM_ATTR esp32_spi_flash_erase(u32_t addr, u32_t size) { +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) + if (esp_flash_erase_region(NULL, addr, size) != ESP_OK) { +#else if (spi_flash_erase_sector(addr >> 12) != 0) { +#endif return SPIFFS_ERR_INTERNAL; } diff --git a/components/spiffs/spiffs_nucleus.c b/components/spiffs/spiffs_nucleus.c index 44ba711..fb6104e 100644 --- a/components/spiffs/spiffs_nucleus.c +++ b/components/spiffs/spiffs_nucleus.c @@ -679,7 +679,7 @@ static s32_t spiffs_populate_ix_map_v( res = _spiffs_rd(fs, SPIFFS_OP_T_OBJ_LU2 | SPIFFS_OP_C_READ, 0, SPIFFS_PAGE_TO_PADDR(fs, pix), sizeof(spiffs_page_object_ix), (u8_t *)objix); SPIFFS_CHECK_RES(res); - SPIFFS_VALIDATE_OBJIX(objix->p_hdr, obj_id, objix->p_hdr.span_ix); + //SPIFFS_VALIDATE_OBJIX(objix->p_hdr, obj_id, objix->p_hdr.span_ix); // check if hdr is ok, and if objix range overlap with ix map range if ((objix->p_hdr.flags & (SPIFFS_PH_FLAG_DELET | SPIFFS_PH_FLAG_FINAL | SPIFFS_PH_FLAG_IXDELE)) == diff --git a/components/spiffs/spiffs_vfs.c b/components/spiffs/spiffs_vfs.c index 0e52cf4..0b8a068 100644 --- a/components/spiffs/spiffs_vfs.c +++ b/components/spiffs/spiffs_vfs.c @@ -47,12 +47,12 @@ int spiffs_is_mounted = 0; QueueHandle_t spiffs_mutex = NULL; -static int IRAM_ATTR vfs_spiffs_open(const char *path, int flags, int mode); -static ssize_t IRAM_ATTR vfs_spiffs_write(int fd, const void *data, size_t size); -static ssize_t IRAM_ATTR vfs_spiffs_read(int fd, void * dst, size_t size); -static int IRAM_ATTR vfs_spiffs_fstat(int fd, struct stat * st); -static int IRAM_ATTR vfs_spiffs_close(int fd); -static off_t IRAM_ATTR vfs_spiffs_lseek(int fd, off_t size, int mode); +static int vfs_spiffs_open(const char *path, int flags, int mode); +static ssize_t vfs_spiffs_write(int fd, const void *data, size_t size); +static ssize_t vfs_spiffs_read(int fd, void * dst, size_t size); +static int vfs_spiffs_fstat(int fd, struct stat * st); +static int vfs_spiffs_close(int fd); +static off_t vfs_spiffs_lseek(int fd, off_t size, int mode); typedef struct { DIR dir; @@ -92,7 +92,7 @@ static u8_t *my_spiffs_cache; //---------------------------------------------------- void spiffs_fs_stat(uint32_t *total, uint32_t *used) { - if (SPIFFS_info(&fs, total, used) != SPIFFS_OK) { + if (SPIFFS_info(&fs, (u32_t *)total, (u32_t *)used) != SPIFFS_OK) { *total = 0; *used = 0; } diff --git a/components/spiffs_image/CMakeLists.txt b/components/spiffs_image/CMakeLists.txt new file mode 100644 index 0000000..45d489e --- /dev/null +++ b/components/spiffs_image/CMakeLists.txt @@ -0,0 +1,3 @@ +set(COMPONENT_SRCDIRS "") +set(COMPONENT_ADD_INCLUDEDIRS "") +register_component() diff --git a/components/tft/CMakeLists.txt b/components/tft/CMakeLists.txt new file mode 100644 index 0000000..cc96dce --- /dev/null +++ b/components/tft/CMakeLists.txt @@ -0,0 +1,5 @@ +FILE(GLOB SOURCES *.c) +idf_component_register(SRCS ${SOURCES} + INCLUDE_DIRS "." + REQUIRES spidriver + PRIV_REQUIRES driver) diff --git a/components/tft/Kconfig b/components/tft/Kconfig new file mode 100644 index 0000000..78ba419 --- /dev/null +++ b/components/tft/Kconfig @@ -0,0 +1,162 @@ +menu "TFT Display" + +config TFT_PREDEFINED_DISPLAY_TYPE + int + default 0 if TFT_PREDEFINED_DISPLAY_TYPE0 + default 1 if TFT_PREDEFINED_DISPLAY_TYPE1 + default 2 if TFT_PREDEFINED_DISPLAY_TYPE2 + default 3 if TFT_PREDEFINED_DISPLAY_TYPE3 + default 4 if TFT_PREDEFINED_DISPLAY_TYPE4 + default 5 if TFT_PREDEFINED_DISPLAY_TYPE5 + default 6 if TFT_PREDEFINED_DISPLAY_TYPE6 + + + choice + prompt "Select predefined display configuration" + default TFT_PREDEFINED_DISPLAY_TYPE0 + help + Select predefined display configuration + + config TFT_PREDEFINED_DISPLAY_TYPE0 + bool "None" + config TFT_PREDEFINED_DISPLAY_TYPE1 + bool "ESP-WROVER-KIT v3 Display (ST7789V)" + config TFT_PREDEFINED_DISPLAY_TYPE4 + bool "ESP-WROVER-KIT v4.1 Display (ILI9341)" + config TFT_PREDEFINED_DISPLAY_TYPE2 + bool "Adafruit TFT Feather Display" + config TFT_PREDEFINED_DISPLAY_TYPE3 + bool "M5Stack TFT Display" + config TFT_PREDEFINED_DISPLAY_TYPE5 + bool "TTGO T-DISPLAY (ST7789V)" + config TFT_PREDEFINED_DISPLAY_TYPE6 + bool "TTGO T-WRISTBAND (ST7735)" + + endchoice + +if TFT_PREDEFINED_DISPLAY_TYPE0 + +config TFT_DISPLAY_CONTROLLER_MODEL + int + default 0 if TFT_DISPLAY_CONTROLLER_ILI9341 + default 1 if TFT_DISPLAY_CONTROLLER_ILI9488 + default 2 if TFT_DISPLAY_CONTROLLER_ST7789V + default 3 if TFT_DISPLAY_CONTROLLER_ST7735 + default 4 if TFT_DISPLAY_CONTROLLER_ST7735R + default 5 if TFT_DISPLAY_CONTROLLER_ST7735B + + choice + prompt "Select a display controller model." + default TFT_DISPLAY_CONTROLLER_ILI9341 + help + Select the controller for your display. If an TFT_PREDEFINED_DISPLAY_TYPE is set, this will be overridden. + + config TFT_DISPLAY_CONTROLLER_ILI9341 + bool "ILI9341" + config TFT_DISPLAY_CONTROLLER_ILI9488 + bool "ILI9488" + config TFT_DISPLAY_CONTROLLER_ST7789V + bool "ST7789V" + config TFT_DISPLAY_CONTROLLER_ST7735 + bool "ST7735" + config TFT_DISPLAY_CONTROLLER_ST7735R + bool "ST7735R" + config TFT_DISPLAY_CONTROLLER_ST7735B + bool "ST7735B" + endchoice + +config TFT_DISPLAY_WIDTH + int "TFT display width in pixels." + default 240 + help + The smaller dimension (in portrait). + +config TFT_DISPLAY_HEIGHT + int "TFT display height in pixels." + default 320 + help + The smaller dimension (in portrait). + +config TFT_RGB_BGR + bool "RGB (Red Green Blue)" + default n + help + Is the display RGB rather than GBR? + +config TFT_TOUCH_CONTROLLER + int + default 0 if TFT_TOUCH_CONTROLLER0 + default 1 if TFT_TOUCH_CONTROLLER1 + default 2 if TFT_TOUCH_CONTROLLER2 + + choice + prompt "Select touch controller." + default TFT_TOUCH_CONTROLLER0 + help + Select predefined display configuration + + config TFT_TOUCH_CONTROLLER0 + bool "None" + config TFT_TOUCH_CONTROLLER1 + bool "XPT2046" + config TFT_TOUCH_CONTROLLER2 + bool "STMPE610" + endchoice + +config TFT_INVERT_ROTATION1 + bool "Invert rotation1." + default n + help + If text is backwards on your display, try enabling this. + +config TFT_PIN_NUM_MOSI + int "GPIO for MOSI (Master Out Slave In)" + default 23 + help + If not using a predefined display type, configure the MOSI pin here. + +config TFT_PIN_NUM_MISO + int "GPIO for MISO (Master In Slave Out)" + default 19 + help + If not using a predefined display type, configure the MISO pin here. + +config TFT_PIN_NUM_CLK + int "GPIO for CLK (SCK / Serial Clock)" + default 18 + help + If not using a predefined display type, configure the SCK pin here. + +config TFT_PIN_NUM_CS + int "GPIO for CS (Slave Select)" + default 5 + help + If not using a predefined display type, configure the CS pin here. + +config TFT_PIN_NUM_DC + int "GPIO for DC (Data \ Command)" + default 26 + help + If not using a predefined display type, configure the DC pin here. + +config TFT_PIN_NUM_TCS + int "GPIO for TCS (Touchscreen)" + default 25 + help + Optional. If not using a predefined display type, configure the TCS (touch screen) pin here. + +config TFT_PIN_NUM_RST + int "GPIO for Reset" + default 0 + help + Optional. If not using a predefined display type, configure the reset pin here. + +config TFT_PIN_NUM_BCKL + int "GPIO for Back-light control" + default 0 + help + Optional. If not using a predefined display type, configure the blacklight pin here. + +endif + +endmenu diff --git a/components/tft/tft.c b/components/tft/tft.c index 93d55f3..842720f 100644 --- a/components/tft/tft.c +++ b/components/tft/tft.c @@ -10,21 +10,15 @@ #include #include #include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_system.h" #include "tft.h" -#include "time.h" #include -#include "rom/tjpgd.h" -#include "esp_heap_caps.h" -#include "tftspi.h" +#include "esp32/rom/tjpgd.h" #define DEG_TO_RAD 0.01745329252 -#define RAD_TO_DEG 57.295779513 #define deg_to_rad 0.01745329252 + 3.14159265359 #define swap(a, b) { int16_t t = a; a = b; b = t; } -#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt))) + #if !defined(max) #define max(A,B) ( (A) > (B) ? (A):(B)) #endif @@ -67,31 +61,31 @@ const color_t TFT_PINK = { 252, 192, 202 }; // ============================================================== // ==== Set default values of global variables ================== -uint8_t orientation = LANDSCAPE;// screen orientation -uint16_t font_rotate = 0; // font rotation -uint8_t font_transparent = 0; -uint8_t font_forceFixed = 0; -uint8_t text_wrap = 0; // character wrapping to new line -color_t _fg = { 0, 255, 0}; -color_t _bg = { 0, 0, 0}; -uint8_t image_debug = 0; - -float _angleOffset = DEFAULT_ANGLE_OFFSET; - -int TFT_X = 0; -int TFT_Y = 0; - -uint32_t tp_calx = 7472920; -uint32_t tp_caly = 122224794; - -dispWin_t dispWin = { - .x1 = 0, - .y1 = 0, - .x2 = DEFAULT_TFT_DISPLAY_WIDTH, - .y2 = DEFAULT_TFT_DISPLAY_HEIGHT, +uint8_t tft_orientation = LANDSCAPE;// screen tft_orientation +uint16_t tft_font_rotate = 0; // font rotation +uint8_t tft_font_transparent = 0; +uint8_t tft_font_forceFixed = 0; +uint8_t tft_text_wrap = 0; // character wrapping to new line +color_t tft_fg = { 0, 255, 0}; +color_t tft_bg = { 0, 0, 0}; +uint8_t tft_image_debug = 0; + +float tft_angleOffset = DEFAULT_ANGLE_OFFSET; + +int tft_x = 0; +int tft_y = 0; + +uint32_t tft_tp_calx = 7472920; +uint32_t tft_tp_caly = 122224794; + +dispWin_t tft_dispWin = { + .x1 = TFT_STATIC_WIDTH_OFFSET, + .y1 = TFT_STATIC_HEIGHT_OFFSET, + .x2 = DEFAULT_TFT_DISPLAY_WIDTH + TFT_STATIC_WIDTH_OFFSET, + .y2 = DEFAULT_TFT_DISPLAY_HEIGHT + TFT_STATIC_HEIGHT_OFFSET, }; -Font cfont = { +Font tft_cfont = { .font = tft_DefaultFont, .x_size = 0, .y_size = 0x0B, @@ -100,8 +94,8 @@ Font cfont = { .bitmap = 1, }; -uint8_t font_buffered_char = 1; -uint8_t font_line_space = 0; +uint8_t tft_font_buffered_char = 1; +uint8_t tft_font_line_space = 0; // ============================================================== @@ -124,7 +118,7 @@ static float _arcAngleMax = DEFAULT_ARC_ANGLE_MAX; // ========================================================================= -// ** All drawings are clipped to 'dispWin' ** +// ** All drawings are clipped to 'tft_dispWin' ** // ** All x,y coordinates in public functions are relative to clip window ** // =========== : Public functions // ----------- : Local functions @@ -146,20 +140,20 @@ int TFT_compare_colors(color_t c1, color_t c2) //------------------------------------------------------------------------ static void _drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { - if ((x < dispWin.x1) || (y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return; + if ((x < tft_dispWin.x1) || (y < tft_dispWin.y1) || (x > tft_dispWin.x2) || (y > tft_dispWin.y2)) return; drawPixel(x, y, color, sel); } //==================================================================== void TFT_drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { - _drawPixel(x+dispWin.x1, y+dispWin.y1, color, sel); + _drawPixel(x+tft_dispWin.x1, y+tft_dispWin.y1, color, sel); } //=========================================== color_t TFT_readPixel(int16_t x, int16_t y) { - if ((x < dispWin.x1) || (y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return TFT_BLACK; + if ((x < tft_dispWin.x1) || (y < tft_dispWin.y1) || (x > tft_dispWin.x2) || (y > tft_dispWin.y2)) return TFT_BLACK; return readPixel(x, y); } @@ -167,13 +161,13 @@ color_t TFT_readPixel(int16_t x, int16_t y) { //-------------------------------------------------------------------------- static void _drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) { // clipping - if ((x < dispWin.x1) || (x > dispWin.x2) || (y > dispWin.y2)) return; - if (y < dispWin.y1) { - h -= (dispWin.y1 - y); - y = dispWin.y1; + if ((x < tft_dispWin.x1) || (x > tft_dispWin.x2) || (y > tft_dispWin.y2)) return; + if (y < tft_dispWin.y1) { + h -= (tft_dispWin.y1 - y); + y = tft_dispWin.y1; } if (h < 0) h = 0; - if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1; + if ((y + h) > (tft_dispWin.y2+1)) h = tft_dispWin.y2 - y + 1; if (h == 0) h = 1; TFT_pushColorRep(x, y, x, y+h-1, color, (uint32_t)h); } @@ -181,13 +175,13 @@ static void _drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) { //-------------------------------------------------------------------------- static void _drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) { // clipping - if ((y < dispWin.y1) || (x > dispWin.x2) || (y > dispWin.y2)) return; - if (x < dispWin.x1) { - w -= (dispWin.x1 - x); - x = dispWin.x1; + if ((y < tft_dispWin.y1) || (x > tft_dispWin.x2) || (y > tft_dispWin.y2)) return; + if (x < tft_dispWin.x1) { + w -= (tft_dispWin.x1 - x); + x = tft_dispWin.x1; } if (w < 0) w = 0; - if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1; + if ((x + w) > (tft_dispWin.x2+1)) w = tft_dispWin.x2 - x + 1; if (w == 0) w = 1; TFT_pushColorRep(x, y, x+w-1, y, color, (uint32_t)w); @@ -195,12 +189,12 @@ static void _drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) { //====================================================================== void TFT_drawFastVLine(int16_t x, int16_t y, int16_t h, color_t color) { - _drawFastVLine(x+dispWin.x1, y+dispWin.y1, h, color); + _drawFastVLine(x+tft_dispWin.x1, y+tft_dispWin.y1, h, color); } //====================================================================== void TFT_drawFastHLine(int16_t x, int16_t y, int16_t w, color_t color) { - _drawFastHLine(x+dispWin.x1, y+dispWin.y1, w, color); + _drawFastHLine(x+tft_dispWin.x1, y+tft_dispWin.y1, w, color); } // Bresenham's algorithm - thx wikipedia - speed enhanced by Bodmer this uses @@ -268,28 +262,28 @@ static void _drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t co //============================================================================== void TFT_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color) { - _drawLine(x0+dispWin.x1, y0+dispWin.y1, x1+dispWin.x1, y1+dispWin.y1, color); + _drawLine(x0+tft_dispWin.x1, y0+tft_dispWin.y1, x1+tft_dispWin.x1, y1+tft_dispWin.y1, color); } // fill a rectangle //-------------------------------------------------------------------------------- static void _fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) { // clipping - if ((x >= dispWin.x2) || (y > dispWin.y2)) return; + if ((x >= tft_dispWin.x2) || (y > tft_dispWin.y2)) return; - if (x < dispWin.x1) { - w -= (dispWin.x1 - x); - x = dispWin.x1; + if (x < tft_dispWin.x1) { + w -= (tft_dispWin.x1 - x); + x = tft_dispWin.x1; } - if (y < dispWin.y1) { - h -= (dispWin.y1 - y); - y = dispWin.y1; + if (y < tft_dispWin.y1) { + h -= (tft_dispWin.y1 - y); + y = tft_dispWin.y1; } if (w < 0) w = 0; if (h < 0) h = 0; - if ((x + w) > (dispWin.x2+1)) w = dispWin.x2 - x + 1; - if ((y + h) > (dispWin.y2+1)) h = dispWin.y2 - y + 1; + if ((x + w) > (tft_dispWin.x2+1)) w = tft_dispWin.x2 - x + 1; + if ((y + h) > (tft_dispWin.y2+1)) h = tft_dispWin.y2 - y + 1; if (w == 0) w = 1; if (h == 0) h = 1; TFT_pushColorRep(x, y, x+w-1, y+h-1, color, (uint32_t)(h*w)); @@ -297,18 +291,18 @@ static void _fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) //============================================================================ void TFT_fillRect(int16_t x, int16_t y, int16_t w, int16_t h, color_t color) { - _fillRect(x+dispWin.x1, y+dispWin.y1, w, h, color); + _fillRect(x+tft_dispWin.x1, y+tft_dispWin.y1, w, h, color); } //================================== void TFT_fillScreen(color_t color) { - TFT_pushColorRep(0, 0, _width-1, _height-1, color, (uint32_t)(_height*_width)); + TFT_pushColorRep(TFT_STATIC_X_OFFSET, TFT_STATIC_Y_OFFSET, tft_width + TFT_STATIC_X_OFFSET -1, tft_height + TFT_STATIC_Y_OFFSET -1, color, (uint32_t)(tft_height*tft_width)); } //================================== void TFT_fillWindow(color_t color) { - TFT_pushColorRep(dispWin.x1, dispWin.y1, dispWin.x2, dispWin.y2, - color, (uint32_t)((dispWin.x2-dispWin.x1+1) * (dispWin.y2-dispWin.y1+1))); + TFT_pushColorRep(tft_dispWin.x1, tft_dispWin.y1, tft_dispWin.x2, tft_dispWin.y2, + color, (uint32_t)((tft_dispWin.x2-tft_dispWin.x1+1) * (tft_dispWin.y2-tft_dispWin.y1+1))); } // ^^^============= Basics drawing functions ================================^^^ @@ -326,7 +320,7 @@ static void _drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t col //=============================================================================== void TFT_drawRect(uint16_t x1,uint16_t y1,uint16_t w,uint16_t h, color_t color) { - _drawRect(x1+dispWin.x1, y1+dispWin.y1, w, h, color); + _drawRect(x1+tft_dispWin.x1, y1+tft_dispWin.y1, w, h, color); } //------------------------------------------------------------------------------------------------- @@ -403,8 +397,8 @@ static void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornerna //============================================================================================= void TFT_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color) { - x += dispWin.x1; - y += dispWin.y1; + x += tft_dispWin.x1; + y += tft_dispWin.y1; // smarter version _drawFastHLine(x + r, y, w - 2 * r, color); // Top @@ -423,8 +417,8 @@ void TFT_drawRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, //============================================================================================= void TFT_fillRoundRect(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t r, color_t color) { - x += dispWin.x1; - y += dispWin.y1; + x += tft_dispWin.x1; + y += tft_dispWin.y1; // smarter version _fillRect(x + r, y, w - 2 * r, h, color); @@ -443,25 +437,25 @@ static void _drawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t lengt _drawLine( x, y, - x + length * cos((angle + _angleOffset) * DEG_TO_RAD), - y + length * sin((angle + _angleOffset) * DEG_TO_RAD), color); + x + length * cos((angle + tft_angleOffset) * DEG_TO_RAD), + y + length * sin((angle + tft_angleOffset) * DEG_TO_RAD), color); } //--------------------------------------------------------------------------------------------------------------- static void _DrawLineByAngle(int16_t x, int16_t y, int16_t angle, uint16_t start, uint16_t length, color_t color) { _drawLine( - x + start * cos((angle + _angleOffset) * DEG_TO_RAD), - y + start * sin((angle + _angleOffset) * DEG_TO_RAD), - x + (start + length) * cos((angle + _angleOffset) * DEG_TO_RAD), - y + (start + length) * sin((angle + _angleOffset) * DEG_TO_RAD), color); + x + start * cos((angle + tft_angleOffset) * DEG_TO_RAD), + y + start * sin((angle + tft_angleOffset) * DEG_TO_RAD), + x + (start + length) * cos((angle + tft_angleOffset) * DEG_TO_RAD), + y + (start + length) * sin((angle + tft_angleOffset) * DEG_TO_RAD), color); } //=========================================================================================================== void TFT_drawLineByAngle(uint16_t x, uint16_t y, uint16_t start, uint16_t len, uint16_t angle, color_t color) { - x += dispWin.x1; - y += dispWin.y1; + x += tft_dispWin.x1; + y += tft_dispWin.y1; if (start == 0) _drawLineByAngle(x, y, angle, len, color); else _DrawLineByAngle(x, y, angle, start, len, color); @@ -480,12 +474,12 @@ static void _drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, ui //================================================================================================================ void TFT_drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) { - x0 += dispWin.x1; - y0 += dispWin.y1; - x1 += dispWin.x1; - y1 += dispWin.y1; - x2 += dispWin.x1; - y2 += dispWin.y1; + x0 += tft_dispWin.x1; + y0 += tft_dispWin.y1; + x1 += tft_dispWin.x1; + y1 += tft_dispWin.y1; + x2 += tft_dispWin.x1; + y2 += tft_dispWin.y1; _drawLine(x0, y0, x1, y1, color); _drawLine(x1, y1, x2, y2, color); @@ -574,16 +568,16 @@ static void _fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, ui void TFT_fillTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, color_t color) { _fillTriangle( - x0 + dispWin.x1, y0 + dispWin.y1, - x1 + dispWin.x1, y1 + dispWin.y1, - x2 + dispWin.x1, y2 + dispWin.y1, + x0 + tft_dispWin.x1, y0 + tft_dispWin.y1, + x1 + tft_dispWin.x1, y1 + tft_dispWin.y1, + x2 + tft_dispWin.x1, y2 + tft_dispWin.y1, color); } //==================================================================== void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color) { - x += dispWin.x1; - y += dispWin.y1; + x += tft_dispWin.x1; + y += tft_dispWin.y1; int f = 1 - radius; int ddF_x = 1; int ddF_y = -2 * radius; @@ -618,8 +612,8 @@ void TFT_drawCircle(int16_t x, int16_t y, int radius, color_t color) { //==================================================================== void TFT_fillCircle(int16_t x, int16_t y, int radius, color_t color) { - x += dispWin.x1; - y += dispWin.y1; + x += tft_dispWin.x1; + y += tft_dispWin.y1; _drawFastVLine(x, y-radius, 2*radius+1, color); fillCircleHelper(x, y, radius, 3, 0, color); @@ -643,8 +637,8 @@ static void _draw_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, uint16_t //===================================================================================================== void TFT_drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option) { - x0 += dispWin.x1; - y0 += dispWin.y1; + x0 += tft_dispWin.x1; + y0 += tft_dispWin.y1; uint16_t x, y; int32_t xchg, ychg; @@ -743,8 +737,8 @@ static void _draw_filled_ellipse_section(uint16_t x, uint16_t y, uint16_t x0, ui //===================================================================================================== void TFT_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t color, uint8_t option) { - x0 += dispWin.x1; - y0 += dispWin.y1; + x0 += tft_dispWin.x1; + y0 += tft_dispWin.y1; uint16_t x, y; int32_t xchg, ychg; @@ -876,8 +870,8 @@ static void _fillArcOffsetted(uint16_t cx, uint16_t cy, uint16_t radius, uint16_ //=========================================================================================================================== void TFT_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, float end, color_t color, color_t fillcolor) { - cx += dispWin.x1; - cy += dispWin.y1; + cx += tft_dispWin.x1; + cy += tft_dispWin.y1; if (th < 1) th = 1; if (th > r) th = r; @@ -887,8 +881,8 @@ void TFT_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, float astart = fmodf(start, _arcAngleMax); float aend = fmodf(end, _arcAngleMax); - astart += _angleOffset; - aend += _angleOffset; + astart += tft_angleOffset; + aend += tft_angleOffset; if (astart < 0) astart += (float)360; if (aend < 0) aend += (float)360; @@ -923,10 +917,10 @@ void TFT_drawArc(uint16_t cx, uint16_t cy, uint16_t r, uint16_t th, float start, //============================================================================================================= void TFT_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, color_t fill, int rot, uint8_t th) { - cx += dispWin.x1; - cy += dispWin.y1; + cx += tft_dispWin.x1; + cy += tft_dispWin.y1; - int deg = rot - _angleOffset; + int deg = rot - tft_angleOffset; int f = TFT_compare_colors(fill, color); if (sides < MIN_POLIGON_SIDES) sides = MIN_POLIGON_SIDES; // This ensures the minimum side number @@ -971,8 +965,8 @@ void TFT_drawPolygon(int cx, int cy, int sides, int diameter, color_t color, col //===================================================================================== void TFT_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float factor) { - cx += dispWin.x1; - cy += dispWin.y1; + cx += tft_dispWin.x1; + cy += tft_dispWin.y1; factor = constrain(factor, 1.0, 4.0); uint8_t sides = 5; @@ -1017,8 +1011,8 @@ void TFT_drawStar(int cx, int cy, int diameter, color_t color, bool fill, float // ================ Font and string functions ================================== -//-------------------------------------------------------- -static int load_file_font(const char * fontfile, int info) +//------------------------------------------------------- +static int load_file_font(const char *fontfile, int info) { int err = 0; char err_msg[256] = {'\0'}; @@ -1148,8 +1142,8 @@ static int load_file_font(const char * fontfile, int info) return err; } -//------------------------------------------------ -int compile_font_file(char *fontfile, uint8_t dbg) +//------------------------------------------------------ +int compile_font_file(const char *fontfile, uint8_t dbg) { int err = 0; char err_msg[128] = {'\0'}; @@ -1330,14 +1324,14 @@ int compile_font_file(char *fontfile, uint8_t dbg) // ----------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- -// Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1) +// Character drawing rectangle is (0, 0) (xDelta-1, tft_cfont.y_size-1) // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1) //--------------------------------------------------------------------------------------------- //---------------------------------- void getFontCharacters(uint8_t *buf) { - if (cfont.bitmap == 2) { + if (tft_cfont.bitmap == 2) { //For 7 segment font only characters 0,1,2,3,4,5,6,7,8,9, . , - , : , / are available. for (uint8_t n=0; n < 11; n++) { buf[n] = n + 0x30; @@ -1349,11 +1343,11 @@ void getFontCharacters(uint8_t *buf) return; } - if (cfont.x_size > 0) { - for (uint8_t n=0; n < cfont.numchars; n++) { - buf[n] = cfont.offset + n; + if (tft_cfont.x_size > 0) { + for (uint8_t n=0; n < tft_cfont.numchars; n++) { + buf[n] = tft_cfont.offset + n; } - buf[cfont.numchars] = '\0'; + buf[tft_cfont.numchars] = '\0'; return; } @@ -1361,12 +1355,12 @@ void getFontCharacters(uint8_t *buf) uint8_t cc, cw, ch, n; n = 0; - cc = cfont.font[tempPtr++]; + cc = tft_cfont.font[tempPtr++]; while (cc != 0xFF) { - cfont.numchars++; + tft_cfont.numchars++; tempPtr++; - cw = cfont.font[tempPtr++]; - ch = cfont.font[tempPtr++]; + cw = tft_cfont.font[tempPtr++]; + ch = tft_cfont.font[tempPtr++]; tempPtr++; tempPtr++; if (cw != 0) { @@ -1374,7 +1368,7 @@ void getFontCharacters(uint8_t *buf) tempPtr += (((cw * ch)-1) / 8) + 1; } buf[n++] = cc; - cc = cfont.font[tempPtr++]; + cc = tft_cfont.font[tempPtr++]; } buf[n] = '\0'; } @@ -1386,29 +1380,29 @@ static void getMaxWidthHeight() uint16_t tempPtr = 4; // point at first char data uint8_t cc, cw, ch, cd, cy; - cfont.numchars = 0; - cfont.max_x_size = 0; + tft_cfont.numchars = 0; + tft_cfont.max_x_size = 0; - cc = cfont.font[tempPtr++]; + cc = tft_cfont.font[tempPtr++]; while (cc != 0xFF) { - cfont.numchars++; - cy = cfont.font[tempPtr++]; - cw = cfont.font[tempPtr++]; - ch = cfont.font[tempPtr++]; + tft_cfont.numchars++; + cy = tft_cfont.font[tempPtr++]; + cw = tft_cfont.font[tempPtr++]; + ch = tft_cfont.font[tempPtr++]; tempPtr++; - cd = cfont.font[tempPtr++]; + cd = tft_cfont.font[tempPtr++]; cy += ch; - if (cw > cfont.max_x_size) cfont.max_x_size = cw; - if (cd > cfont.max_x_size) cfont.max_x_size = cd; - if (ch > cfont.y_size) cfont.y_size = ch; - if (cy > cfont.y_size) cfont.y_size = cy; + if (cw > tft_cfont.max_x_size) tft_cfont.max_x_size = cw; + if (cd > tft_cfont.max_x_size) tft_cfont.max_x_size = cd; + if (ch > tft_cfont.y_size) tft_cfont.y_size = ch; + if (cy > tft_cfont.y_size) tft_cfont.y_size = cy; if (cw != 0) { // packed bits tempPtr += (((cw * ch)-1) / 8) + 1; } - cc = cfont.font[tempPtr++]; + cc = tft_cfont.font[tempPtr++]; } - cfont.size = tempPtr; + tft_cfont.size = tempPtr; } // Return the Glyph data for an individual character in the proportional font @@ -1417,15 +1411,15 @@ static uint8_t getCharPtr(uint8_t c) { uint16_t tempPtr = 4; // point at first char data do { - fontChar.charCode = cfont.font[tempPtr++]; + fontChar.charCode = tft_cfont.font[tempPtr++]; if (fontChar.charCode == 0xFF) return 0; - fontChar.adjYOffset = cfont.font[tempPtr++]; - fontChar.width = cfont.font[tempPtr++]; - fontChar.height = cfont.font[tempPtr++]; - fontChar.xOffset = cfont.font[tempPtr++]; + fontChar.adjYOffset = tft_cfont.font[tempPtr++]; + fontChar.width = tft_cfont.font[tempPtr++]; + fontChar.height = tft_cfont.font[tempPtr++]; + fontChar.xOffset = tft_cfont.font[tempPtr++]; fontChar.xOffset = fontChar.xOffset < 0x80 ? fontChar.xOffset : -(0xFF - fontChar.xOffset); - fontChar.xDelta = cfont.font[tempPtr++]; + fontChar.xDelta = tft_cfont.font[tempPtr++]; if (c != fontChar.charCode && fontChar.charCode != 0xFF) { if (fontChar.width != 0) { @@ -1437,9 +1431,9 @@ static uint8_t getCharPtr(uint8_t c) { fontChar.dataPtr = tempPtr; if (c == fontChar.charCode) { - if (font_forceFixed > 0) { + if (tft_font_forceFixed > 0) { // fix width & offset for forced fixed width - fontChar.xDelta = cfont.max_x_size; + fontChar.xDelta = tft_cfont.max_x_size; fontChar.xOffset = (fontChar.xDelta - fontChar.width) / 2; } } @@ -1451,26 +1445,26 @@ static uint8_t getCharPtr(uint8_t c) { /* //----------------------- static void _testFont() { - if (cfont.x_size) { + if (tft_cfont.x_size) { printf("FONT TEST: fixed font\r\n"); return; } uint16_t tempPtr = 4; // point at first char data uint8_t c = 0x20; for (c=0x20; c <0xFF; c++) { - fontChar.charCode = cfont.font[tempPtr++]; + fontChar.charCode = tft_cfont.font[tempPtr++]; if (fontChar.charCode == 0xFF) break; if (fontChar.charCode != c) { printf("FONT TEST: last sequential char: %d, expected %d\r\n", fontChar.charCode, c); break; } c = fontChar.charCode; - fontChar.adjYOffset = cfont.font[tempPtr++]; - fontChar.width = cfont.font[tempPtr++]; - fontChar.height = cfont.font[tempPtr++]; - fontChar.xOffset = cfont.font[tempPtr++]; + fontChar.adjYOffset = tft_cfont.font[tempPtr++]; + fontChar.width = tft_cfont.font[tempPtr++]; + fontChar.height = tft_cfont.font[tempPtr++]; + fontChar.xOffset = tft_cfont.font[tempPtr++]; fontChar.xOffset = fontChar.xOffset < 0x80 ? fontChar.xOffset : -(0xFF - fontChar.xOffset); - fontChar.xDelta = cfont.font[tempPtr++]; + fontChar.xDelta = tft_cfont.font[tempPtr++]; if (fontChar.charCode != 0xFF) { if (fontChar.width != 0) { @@ -1479,47 +1473,47 @@ static void _testFont() { } } } - printf("FONT TEST: W=%d H=%d last char: %d [%c]; length: %d\r\n", cfont.max_x_size, cfont.y_size, c, c, tempPtr); + printf("FONT TEST: W=%d H=%d last char: %d [%c]; length: %d\r\n", tft_cfont.max_x_size, tft_cfont.y_size, c, c, tempPtr); } */ //=================================================== void TFT_setFont(uint8_t font, const char *font_file) { - cfont.font = NULL; + tft_cfont.font = NULL; if (font == FONT_7SEG) { - cfont.bitmap = 2; - cfont.x_size = 24; - cfont.y_size = 6; - cfont.offset = 0; - cfont.color = _fg; + tft_cfont.bitmap = 2; + tft_cfont.x_size = 24; + tft_cfont.y_size = 6; + tft_cfont.offset = 0; + tft_cfont.color = tft_fg; } else { if (font == USER_FONT) { - if (load_file_font(font_file, 0) != 0) cfont.font = tft_DefaultFont; - else cfont.font = userfont; + if (load_file_font(font_file, 0) != 0) tft_cfont.font = tft_DefaultFont; + else tft_cfont.font = userfont; } - else if (font == DEJAVU18_FONT) cfont.font = tft_Dejavu18; - else if (font == DEJAVU24_FONT) cfont.font = tft_Dejavu24; - else if (font == UBUNTU16_FONT) cfont.font = tft_Ubuntu16; - else if (font == COMIC24_FONT) cfont.font = tft_Comic24; - else if (font == MINYA24_FONT) cfont.font = tft_minya24; - else if (font == TOONEY32_FONT) cfont.font = tft_tooney32; - else if (font == SMALL_FONT) cfont.font = tft_SmallFont; - else if (font == DEF_SMALL_FONT) cfont.font = tft_def_small; - else cfont.font = tft_DefaultFont; - - cfont.bitmap = 1; - cfont.x_size = cfont.font[0]; - cfont.y_size = cfont.font[1]; - if (cfont.x_size > 0) { - cfont.offset = cfont.font[2]; - cfont.numchars = cfont.font[3]; - cfont.size = cfont.x_size * cfont.y_size * cfont.numchars; + else if (font == DEJAVU18_FONT) tft_cfont.font = tft_Dejavu18; + else if (font == DEJAVU24_FONT) tft_cfont.font = tft_Dejavu24; + else if (font == UBUNTU16_FONT) tft_cfont.font = tft_Ubuntu16; + else if (font == COMIC24_FONT) tft_cfont.font = tft_Comic24; + else if (font == MINYA24_FONT) tft_cfont.font = tft_minya24; + else if (font == TOONEY32_FONT) tft_cfont.font = tft_tooney32; + else if (font == SMALL_FONT) tft_cfont.font = tft_SmallFont; + else if (font == DEF_SMALL_FONT) tft_cfont.font = tft_def_small; + else tft_cfont.font = tft_DefaultFont; + + tft_cfont.bitmap = 1; + tft_cfont.x_size = tft_cfont.font[0]; + tft_cfont.y_size = tft_cfont.font[1]; + if (tft_cfont.x_size > 0) { + tft_cfont.offset = tft_cfont.font[2]; + tft_cfont.numchars = tft_cfont.font[3]; + tft_cfont.size = tft_cfont.x_size * tft_cfont.y_size * tft_cfont.numchars; } else { - cfont.offset = 4; + tft_cfont.offset = 4; getMaxWidthHeight(); } //_testFont(); @@ -1538,7 +1532,7 @@ void TFT_setFont(uint8_t font, const char *font_file) // Data[n] // ----------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- -// Character drawing rectangle is (0, 0) (xDelta-1, cfont.y_size-1) +// Character drawing rectangle is (0, 0) (xDelta-1, tft_cfont.y_size-1) // Character visible pixels rectangle is (xOffset, yOffset) (xOffset+Width-1, yOffset+Height-1) //--------------------------------------------------------------------------------------------- @@ -1551,16 +1545,16 @@ static int printProportionalChar(int x, int y) { char_width = ((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta); - if ((font_buffered_char) && (!font_transparent)) { + if ((tft_font_buffered_char) && (!tft_font_transparent)) { int len, bufPos; // === buffer Glyph data for faster sending === - len = char_width * cfont.y_size; + len = char_width * tft_cfont.y_size; color_t *color_line = heap_caps_malloc(len*3, MALLOC_CAP_DMA); if (color_line) { // fill with background color for (int n = 0; n < len; n++) { - color_line[n] = _bg; + color_line[n] = tft_bg; } // set character pixels to foreground color uint8_t mask = 0x80; @@ -1568,12 +1562,12 @@ static int printProportionalChar(int x, int y) { for (i=0; i < fontChar.width; i++) { if (((i + (j*fontChar.width)) % 8) == 0) { mask = 0x80; - ch = cfont.font[fontChar.dataPtr++]; + ch = tft_cfont.font[fontChar.dataPtr++]; } if ((ch & mask) != 0) { // visible pixel bufPos = ((j + fontChar.adjYOffset) * char_width) + (fontChar.xOffset + i); // bufY + bufX - color_line[bufPos] = _fg; + color_line[bufPos] = tft_fg; /* bufY = (j + fontChar.adjYOffset) * char_width; bufX = fontChar.xOffset + i; @@ -1581,9 +1575,9 @@ static int printProportionalChar(int x, int y) { printf("[%c] X ERR: %d\r\n", fontChar.charCode, bufX); } bufPos = bufY + bufX; - if ((bufPos < len) && (bufPos > 0)) color_line[bufPos] = _fg; + if ((bufPos < len) && (bufPos > 0)) color_line[bufPos] = tft_fg; else printf("[%c] ERR: %d > %d W=%d H=%d bufX=%d bufY=%d X=%d Y=%d\r\n", - fontChar.charCode, bufPos, len, char_width, cfont.y_size, bufX, bufY, fontChar.xOffset + i, j + fontChar.adjYOffset); + fontChar.charCode, bufPos, len, char_width, tft_cfont.y_size, bufX, bufY, fontChar.xOffset + i, j + fontChar.adjYOffset); */ } mask >>= 1; @@ -1591,7 +1585,7 @@ static int printProportionalChar(int x, int y) { } // send to display in one transaction disp_select(); - send_data(x, y, x+char_width-1, y+cfont.y_size-1, len, color_line); + send_data(x, y, x+char_width-1, y+tft_cfont.y_size-1, len, color_line); disp_deselect(); free(color_line); @@ -1601,7 +1595,7 @@ static int printProportionalChar(int x, int y) { int cx, cy; - if (!font_transparent) _fillRect(x, y, char_width+1, cfont.y_size, _bg); + if (!tft_font_transparent) _fillRect(x, y, char_width+1, tft_cfont.y_size, tft_bg); // draw Glyph uint8_t mask = 0x80; @@ -1610,13 +1604,13 @@ static int printProportionalChar(int x, int y) { for (i=0; i < fontChar.width; i++) { if (((i + (j*fontChar.width)) % 8) == 0) { mask = 0x80; - ch = cfont.font[fontChar.dataPtr++]; + ch = tft_cfont.font[fontChar.dataPtr++]; } if ((ch & mask) !=0) { cx = (uint16_t)(x+fontChar.xOffset+i); cy = (uint16_t)(y+j+fontChar.adjYOffset); - _drawPixel(cx, cy, _fg, 0); + _drawPixel(cx, cy, tft_fg, 0); } mask >>= 1; } @@ -1633,28 +1627,28 @@ static void printChar(uint8_t c, int x, int y) { uint16_t k, temp, cx, cy, len; // fz = bytes per char row - fz = cfont.x_size/8; - if (cfont.x_size % 8) fz++; + fz = tft_cfont.x_size/8; + if (tft_cfont.x_size % 8) fz++; // get character position in buffer - temp = ((c-cfont.offset)*((fz)*cfont.y_size))+4; + temp = ((c-tft_cfont.offset)*((fz)*tft_cfont.y_size))+4; - if ((font_buffered_char) && (!font_transparent)) { + if ((tft_font_buffered_char) && (!tft_font_transparent)) { // === buffer Glyph data for faster sending === - len = cfont.x_size * cfont.y_size; + len = tft_cfont.x_size * tft_cfont.y_size; color_t *color_line = heap_caps_malloc(len*3, MALLOC_CAP_DMA); if (color_line) { // fill with background color for (int n = 0; n < len; n++) { - color_line[n] = _bg; + color_line[n] = tft_bg; } // set character pixels to foreground color - for (j=0; j>= 1; } } @@ -1662,7 +1656,7 @@ static void printChar(uint8_t c, int x, int y) { } // send to display in one transaction disp_select(); - send_data(x, y, x+cfont.x_size-1, y+cfont.y_size-1, len, color_line); + send_data(x, y, x+tft_cfont.x_size-1, y+tft_cfont.y_size-1, len, color_line); disp_deselect(); free(color_line); @@ -1670,18 +1664,18 @@ static void printChar(uint8_t c, int x, int y) { } } - if (!font_transparent) _fillRect(x, y, cfont.x_size, cfont.y_size, _bg); + if (!tft_font_transparent) _fillRect(x, y, tft_cfont.x_size, tft_cfont.y_size, tft_bg); disp_select(); - for (j=0; j>= 1; } @@ -1696,7 +1690,7 @@ static void printChar(uint8_t c, int x, int y) { //--------------------------------------------------- static int rotatePropChar(int x, int y, int offset) { uint8_t ch = 0; - double radian = font_rotate * DEG_TO_RAD; + double radian = tft_font_rotate * DEG_TO_RAD; float cos_radian = cos(radian); float sin_radian = sin(radian); @@ -1706,14 +1700,14 @@ static int rotatePropChar(int x, int y, int offset) { for (int i=0; i < fontChar.width; i++) { if (((i + (j*fontChar.width)) % 8) == 0) { mask = 0x80; - ch = cfont.font[fontChar.dataPtr++]; + ch = tft_cfont.font[fontChar.dataPtr++]; } int newX = (int)(x + (((offset + i) * cos_radian) - ((j+fontChar.adjYOffset)*sin_radian))); int newY = (int)(y + (((j+fontChar.adjYOffset) * cos_radian) + ((offset + i) * sin_radian))); - if ((ch & mask) != 0) _drawPixel(newX,newY,_fg, 0); - else if (!font_transparent) _drawPixel(newX,newY,_bg, 0); + if ((ch & mask) != 0) _drawPixel(newX,newY,tft_fg, 0); + else if (!tft_font_transparent) _drawPixel(newX,newY,tft_bg, 0); mask >>= 1; } @@ -1729,26 +1723,26 @@ static void rotateChar(uint8_t c, int x, int y, int pos) { uint8_t i,j,ch,fz,mask; uint16_t temp; int newx,newy; - double radian = font_rotate*0.0175; + double radian = tft_font_rotate*0.0175; float cos_radian = cos(radian); float sin_radian = sin(radian); int zz; - if( cfont.x_size < 8 ) fz = cfont.x_size; - else fz = cfont.x_size/8; - temp=((c-cfont.offset)*((fz)*cfont.y_size))+4; + if( tft_cfont.x_size < 8 ) fz = tft_cfont.x_size; + else fz = tft_cfont.x_size/8; + temp=((c-tft_cfont.offset)*((fz)*tft_cfont.y_size))+4; disp_select(); - for (j=0; j>= 1; } } @@ -1756,34 +1750,34 @@ static void rotateChar(uint8_t c, int x, int y, int pos) { } disp_deselect(); // calculate x,y for the next char - TFT_X = (int)(x + ((pos+1) * cfont.x_size * cos_radian)); - TFT_Y = (int)(y + ((pos+1) * cfont.x_size * sin_radian)); + tft_x = (int)(x + ((pos+1) * tft_cfont.x_size * cos_radian)); + tft_y = (int)(y + ((pos+1) * tft_cfont.x_size * sin_radian)); } //---------------------- static int _7seg_width() { - return (2 * (2 * cfont.y_size + 1)) + cfont.x_size; + return (2 * (2 * tft_cfont.y_size + 1)) + tft_cfont.x_size; } //----------------------- static int _7seg_height() { - return (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size); + return (3 * (2 * tft_cfont.y_size + 1)) + (2 * tft_cfont.x_size); } // Returns the string width in pixels. // Useful for positions strings on the screen. -//=============================== -int TFT_getStringWidth(char* str) +//===================================== +int TFT_getStringWidth(const char *str) { int strWidth = 0; - if (cfont.bitmap == 2) strWidth = ((_7seg_width()+2) * strlen(str)) - 2; // 7-segment font - else if (cfont.x_size != 0) strWidth = strlen(str) * cfont.x_size; // fixed width font + if (tft_cfont.bitmap == 2) strWidth = ((_7seg_width()+2) * strlen(str)) - 2; // 7-segment font + else if (tft_cfont.x_size != 0) strWidth = strlen(str) * tft_cfont.x_size; // fixed width font else { // calculate the width of the string of proportional characters - char* tempStrptr = str; + const char *tempStrptr = str; while (*tempStrptr != 0) { if (getCharPtr(*tempStrptr++)) { strWidth += (((fontChar.width > fontChar.xDelta) ? fontChar.width : fontChar.xDelta) + 1); @@ -1794,12 +1788,12 @@ int TFT_getStringWidth(char* str) return strWidth; } -//=============================================== -void TFT_clearStringRect(int x, int y, char *str) +//===================================================== +void TFT_clearStringRect(int x, int y, const char *str) { int w = TFT_getStringWidth(str); int h = TFT_getfontheight(); - TFT_fillRect(x+dispWin.x1, y+dispWin.y1, w, h, _bg); + TFT_fillRect(x+tft_dispWin.x1, y+tft_dispWin.y1, w, h, tft_bg); } //============================================================================== @@ -1836,7 +1830,7 @@ static void barVert(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, c _fillTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, color); _fillTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, color); _fillRect(x, y+2*w+1, 2*w+1, l, color); - if (cfont.offset) { + if (tft_cfont.offset) { _drawTriangle(x+1, y+2*w, x+w, y+w+1, x+2*w-1, y+2*w, outline); _drawTriangle(x+1, y+2*w+l+1, x+w, y+3*w+l, x+2*w-1, y+2*w+l+1, outline); _drawRect(x, y+2*w+1, 2*w+1, l, outline); @@ -1848,7 +1842,7 @@ static void barHor(int16_t x, int16_t y, int16_t w, int16_t l, color_t color, co _fillTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, color); _fillTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, color); _fillRect(x+2*w+1, y, l, 2*w+1, color); - if (cfont.offset) { + if (tft_cfont.offset) { _drawTriangle(x+2*w, y+2*w-1, x+w+1, y+w, x+2*w, y+1, outline); _drawTriangle(x+2*w+l+1, y+2*w-1, x+3*w+l, y+w, x+2*w+l+1, y+1, outline); _drawRect(x+2*w+1, y, l, 2*w+1, outline); @@ -1864,122 +1858,122 @@ static void _draw7seg(int16_t x, int16_t y, int8_t num, int16_t w, int16_t l, co int16_t d = 2*w+l+1; // === Clear unused segments === - if (!(c & 0x001)) barVert(x+d, y+d, w, l, _bg, _bg); - if (!(c & 0x002)) barVert(x, y+d, w, l, _bg, _bg); - if (!(c & 0x004)) barVert(x+d, y, w, l, _bg, _bg); - if (!(c & 0x008)) barVert(x, y, w, l, _bg, _bg); - if (!(c & 0x010)) barHor(x, y+2*d, w, l, _bg, _bg); - if (!(c & 0x020)) barHor(x, y+d, w, l, _bg, _bg); - if (!(c & 0x040)) barHor(x, y, w, l, _bg, _bg); + if (!(c & 0x001)) barVert(x+d, y+d, w, l, tft_bg, tft_bg); + if (!(c & 0x002)) barVert(x, y+d, w, l, tft_bg, tft_bg); + if (!(c & 0x004)) barVert(x+d, y, w, l, tft_bg, tft_bg); + if (!(c & 0x008)) barVert(x, y, w, l, tft_bg, tft_bg); + if (!(c & 0x010)) barHor(x, y+2*d, w, l, tft_bg, tft_bg); + if (!(c & 0x020)) barHor(x, y+d, w, l, tft_bg, tft_bg); + if (!(c & 0x040)) barHor(x, y, w, l, tft_bg, tft_bg); if (!(c & 0x080)) { // low point - _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg); - if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, _bg); + _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, tft_bg); + if (tft_cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, tft_bg); } if (!(c & 0x100)) { // down middle point - _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg); - if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, _bg); + _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, tft_bg); + if (tft_cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, tft_bg); } if (!(c & 0x800)) { // up middle point - _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg); - if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, _bg); + _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, tft_bg); + if (tft_cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, tft_bg); } if (!(c & 0x200)) { // middle, minus - _fillRect(x+2*w+1, y+d, l, 2*w+1, _bg); - if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, _bg); + _fillRect(x+2*w+1, y+d, l, 2*w+1, tft_bg); + if (tft_cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, tft_bg); } // === Draw used segments === - if (c & 0x001) barVert(x+d, y+d, w, l, color, cfont.color); // down right - if (c & 0x002) barVert(x, y+d, w, l, color, cfont.color); // down left - if (c & 0x004) barVert(x+d, y, w, l, color, cfont.color); // up right - if (c & 0x008) barVert(x, y, w, l, color, cfont.color); // up left - if (c & 0x010) barHor(x, y+2*d, w, l, color, cfont.color); // down - if (c & 0x020) barHor(x, y+d, w, l, color, cfont.color); // middle - if (c & 0x040) barHor(x, y, w, l, color, cfont.color); // up + if (c & 0x001) barVert(x+d, y+d, w, l, color, tft_cfont.color); // down right + if (c & 0x002) barVert(x, y+d, w, l, color, tft_cfont.color); // down left + if (c & 0x004) barVert(x+d, y, w, l, color, tft_cfont.color); // up right + if (c & 0x008) barVert(x, y, w, l, color, tft_cfont.color); // up left + if (c & 0x010) barHor(x, y+2*d, w, l, color, tft_cfont.color); // down + if (c & 0x020) barHor(x, y+d, w, l, color, tft_cfont.color); // middle + if (c & 0x040) barHor(x, y, w, l, color, tft_cfont.color); // up if (c & 0x080) { // low point _fillRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, color); - if (cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, cfont.color); + if (tft_cfont.offset) _drawRect(x+(d/2), y+2*d, 2*w+1, 2*w+1, tft_cfont.color); } if (c & 0x100) { // down middle point _fillRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, color); - if (cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, cfont.color); + if (tft_cfont.offset) _drawRect(x+(d/2), y+d+2*w+1, 2*w+1, l/2, tft_cfont.color); } if (c & 0x800) { // up middle point _fillRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, color); - if (cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, cfont.color); + if (tft_cfont.offset) _drawRect(x+(d/2), y+(2*w)+1+(l/2), 2*w+1, l/2, tft_cfont.color); } if (c & 0x200) { // middle, minus _fillRect(x+2*w+1, y+d, l, 2*w+1, color); - if (cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, cfont.color); + if (tft_cfont.offset) _drawRect(x+2*w+1, y+d, l, 2*w+1, tft_cfont.color); } } //============================================================================== -//====================================== -void TFT_print(char *st, int x, int y) { +//============================================ +void TFT_print(const char *st, int x, int y) { int stl, i, tmpw, tmph, fh; uint8_t ch; - if (cfont.bitmap == 0) return; // wrong font selected + if (tft_cfont.bitmap == 0) return; // wrong font selected // ** Rotated strings cannot be aligned - if ((font_rotate != 0) && ((x <= CENTER) || (y <= CENTER))) return; + if ((tft_font_rotate != 0) && ((x <= CENTER) || (y <= CENTER))) return; - if ((x < LASTX) || (font_rotate == 0)) TFT_OFFSET = 0; + if ((x < LASTX) || (tft_font_rotate == 0)) TFT_OFFSET = 0; - if ((x >= LASTX) && (x < LASTY)) x = TFT_X + (x-LASTX); - else if (x > CENTER) x += dispWin.x1; + if ((x >= LASTX) && (x < LASTY)) x = tft_x + (x-LASTX); + else if (x > CENTER) x += tft_dispWin.x1; - if (y >= LASTY) y = TFT_Y + (y-LASTY); - else if (y > CENTER) y += dispWin.y1; + if (y >= LASTY) y = tft_y + (y-LASTY); + else if (y > CENTER) y += tft_dispWin.y1; // ** Get number of characters in string to print stl = strlen(st); // ** Calculate CENTER, RIGHT or BOTTOM position tmpw = TFT_getStringWidth(st); // string width in pixels - fh = cfont.y_size; // font height - if ((cfont.x_size != 0) && (cfont.bitmap == 2)) { + fh = tft_cfont.y_size; // font height + if ((tft_cfont.x_size != 0) && (tft_cfont.bitmap == 2)) { // 7-segment font - fh = (3 * (2 * cfont.y_size + 1)) + (2 * cfont.x_size); // 7-seg character height + fh = (3 * (2 * tft_cfont.y_size + 1)) + (2 * tft_cfont.x_size); // 7-seg character height } - if (x == RIGHT) x = dispWin.x2 - tmpw + dispWin.x1; - else if (x == CENTER) x = (((dispWin.x2 - dispWin.x1 + 1) - tmpw) / 2) + dispWin.x1; + if (x == RIGHT) x = tft_dispWin.x2 - tmpw + tft_dispWin.x1; + else if (x == CENTER) x = (((tft_dispWin.x2 - tft_dispWin.x1 + 1) - tmpw) / 2) + tft_dispWin.x1; - if (y == BOTTOM) y = dispWin.y2 - fh + dispWin.y1; - else if (y==CENTER) y = (((dispWin.y2 - dispWin.y1 + 1) - (fh/2)) / 2) + dispWin.y1; + if (y == BOTTOM) y = tft_dispWin.y2 - fh + tft_dispWin.y1; + else if (y==CENTER) y = (((tft_dispWin.y2 - tft_dispWin.y1 + 1) - (fh/2)) / 2) + tft_dispWin.y1; - if (x < dispWin.x1) x = dispWin.x1; - if (y < dispWin.y1) y = dispWin.y1; - if ((x > dispWin.x2) || (y > dispWin.y2)) return; + if (x < tft_dispWin.x1) x = tft_dispWin.x1; + if (y < tft_dispWin.y1) y = tft_dispWin.y1; + if ((x > tft_dispWin.x2) || (y > tft_dispWin.y2)) return; - TFT_X = x; - TFT_Y = y; + tft_x = x; + tft_y = y; // ** Adjust y position - tmph = cfont.y_size; // font height + tmph = tft_cfont.y_size; // font height // for non-proportional fonts, char width is the same for all chars - tmpw = cfont.x_size; - if (cfont.x_size != 0) { - if (cfont.bitmap == 2) { // 7-segment font + tmpw = tft_cfont.x_size; + if (tft_cfont.x_size != 0) { + if (tft_cfont.bitmap == 2) { // 7-segment font tmpw = _7seg_width(); // character width tmph = _7seg_height(); // character height } } else TFT_OFFSET = 0; // fixed font; offset not needed - if ((TFT_Y + tmph - 1) > dispWin.y2) return; + if ((tft_y + tmph - 1) > tft_dispWin.y2) return; int offset = TFT_OFFSET; @@ -1987,36 +1981,36 @@ void TFT_print(char *st, int x, int y) { ch = st[i]; // get string character if (ch == 0x0D) { // === '\r', erase to eol ==== - if ((!font_transparent) && (font_rotate==0)) _fillRect(TFT_X, TFT_Y, dispWin.x2+1-TFT_X, tmph, _bg); + if ((!tft_font_transparent) && (tft_font_rotate==0)) _fillRect(tft_x, tft_y, tft_dispWin.x2+1-tft_x, tmph, tft_bg); } else if (ch == 0x0A) { // ==== '\n', new line ==== - if (cfont.bitmap == 1) { - TFT_Y += tmph + font_line_space; - if (TFT_Y > (dispWin.y2-tmph)) break; - TFT_X = dispWin.x1; + if (tft_cfont.bitmap == 1) { + tft_y += tmph + tft_font_line_space; + if (tft_y > (tft_dispWin.y2-tmph)) break; + tft_x = tft_dispWin.x1; } } else { // ==== other characters ==== - if (cfont.x_size == 0) { + if (tft_cfont.x_size == 0) { // for proportional font get character data to 'fontChar' if (getCharPtr(ch)) tmpw = fontChar.xDelta; else continue; } // check if character can be displayed in the current line - if ((TFT_X+tmpw) > (dispWin.x2)) { - if (text_wrap == 0) break; - TFT_Y += tmph + font_line_space; - if (TFT_Y > (dispWin.y2-tmph)) break; - TFT_X = dispWin.x1; + if ((tft_x+tmpw) > (tft_dispWin.x2)) { + if (tft_text_wrap == 0) break; + tft_y += tmph + tft_font_line_space; + if (tft_y > (tft_dispWin.y2-tmph)) break; + tft_x = tft_dispWin.x1; } // Let's print the character - if (cfont.x_size == 0) { + if (tft_cfont.x_size == 0) { // == proportional font - if (font_rotate == 0) TFT_X += printProportionalChar(TFT_X, TFT_Y) + 1; + if (tft_font_rotate == 0) tft_x += printProportionalChar(tft_x, tft_y) + 1; else { // rotated proportional font offset += rotatePropChar(x, y, offset); @@ -2024,19 +2018,19 @@ void TFT_print(char *st, int x, int y) { } } else { - if (cfont.bitmap == 1) { + if (tft_cfont.bitmap == 1) { // == fixed font - if ((ch < cfont.offset) || ((ch-cfont.offset) > cfont.numchars)) ch = cfont.offset; - if (font_rotate == 0) { - printChar(ch, TFT_X, TFT_Y); - TFT_X += tmpw; + if ((ch < tft_cfont.offset) || ((ch-tft_cfont.offset) > tft_cfont.numchars)) ch = tft_cfont.offset; + if (tft_font_rotate == 0) { + printChar(ch, tft_x, tft_y); + tft_x += tmpw; } else rotateChar(ch, x, y, i); } - else if (cfont.bitmap == 2) { + else if (tft_cfont.bitmap == 2) { // == 7-segment font == - _draw7seg(TFT_X, TFT_Y, ch, cfont.y_size, cfont.x_size, _fg); - TFT_X += (tmpw + 2); + _draw7seg(tft_x, tft_y, ch, tft_cfont.y_size, tft_cfont.x_size, tft_fg); + tft_x += (tmpw + 2); } } } @@ -2058,32 +2052,38 @@ void TFT_setRotation(uint8_t rot) { } } else { - orientation = rot; + tft_orientation = rot; _tft_setRotation(rot); } - dispWin.x1 = 0; - dispWin.y1 = 0; - dispWin.x2 = _width-1; - dispWin.y2 = _height-1; + tft_dispWin.x1 = TFT_STATIC_X_OFFSET; + tft_dispWin.y1 = TFT_STATIC_Y_OFFSET; + tft_dispWin.x2 = tft_width + TFT_STATIC_X_OFFSET -1; + tft_dispWin.y2 = tft_height + TFT_STATIC_Y_OFFSET -1; - TFT_fillScreen(_bg); + TFT_fillScreen(tft_bg); } // Send the command to invert all of the colors. // Input: i 0 to disable inversion; non-zero to enable inversion //========================================== void TFT_invertDisplay(const uint8_t mode) { - if ( mode == INVERT_ON ) disp_spi_transfer_cmd(TFT_INVONN); - else disp_spi_transfer_cmd(TFT_INVOFF); + if (disp_select() == ESP_OK) { + if ( mode == INVERT_ON ) disp_spi_transfer_cmd(TFT_INVONN); + else disp_spi_transfer_cmd(TFT_INVOFF); + disp_deselect(); + } } // Select gamma curve // Input: gamma = 0~3 //================================== void TFT_setGammaCurve(uint8_t gm) { - uint8_t gamma_curve = 1 << (gm & 0x03); - disp_spi_transfer_cmd_data(TFT_CMD_GAMMASET, &gamma_curve, 1); + uint8_t gamma_curve = (uint8_t)1 << (gm & (uint8_t)0x03); + if (disp_select() == ESP_OK) { + disp_spi_transfer_cmd_data(TFT_CMD_GAMMASET, &gamma_curve, 1); + disp_deselect(); + } } //=========================================================== @@ -2157,29 +2157,29 @@ color_t HSBtoRGB(float _hue, float _sat, float _brightness) { //===================================================================== void TFT_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { - dispWin.x1 = x1; - dispWin.y1 = y1; - dispWin.x2 = x2; - dispWin.y2 = y2; + tft_dispWin.x1 = x1 + TFT_STATIC_X_OFFSET; + tft_dispWin.y1 = y1 + TFT_STATIC_Y_OFFSET; + tft_dispWin.x2 = x2 + TFT_STATIC_X_OFFSET; + tft_dispWin.y2 = y2 + TFT_STATIC_Y_OFFSET; - if (dispWin.x2 >= _width) dispWin.x2 = _width-1; - if (dispWin.y2 >= _height) dispWin.y2 = _height-1; - if (dispWin.x1 > dispWin.x2) dispWin.x1 = dispWin.x2; - if (dispWin.y1 > dispWin.y2) dispWin.y1 = dispWin.y2; + if (tft_dispWin.x2 >= tft_width + TFT_STATIC_X_OFFSET) tft_dispWin.x2 = tft_width + TFT_STATIC_X_OFFSET -1; + if (tft_dispWin.y2 >= tft_height + TFT_STATIC_Y_OFFSET) tft_dispWin.y2 = tft_height + TFT_STATIC_Y_OFFSET -1; + if (tft_dispWin.x1 > tft_dispWin.x2) tft_dispWin.x1 = tft_dispWin.x2; + if (tft_dispWin.y1 > tft_dispWin.y2) tft_dispWin.y1 = tft_dispWin.y2; } //===================== void TFT_resetclipwin() { - dispWin.x2 = _width-1; - dispWin.y2 = _height-1; - dispWin.x1 = 0; - dispWin.y1 = 0; + tft_dispWin.x2 = tft_width + TFT_STATIC_X_OFFSET -1; + tft_dispWin.y2 = tft_height + TFT_STATIC_Y_OFFSET -1; + tft_dispWin.x1 = TFT_STATIC_X_OFFSET; + tft_dispWin.y1 = TFT_STATIC_Y_OFFSET; } //========================================================================== void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color) { - if (cfont.bitmap != 2) return; + if (tft_cfont.bitmap != 2) return; if (l < 6) l = 6; if (l > 40) l = 40; @@ -2187,21 +2187,21 @@ void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color) { if (w > (l/2)) w = l/2; if (w > 12) w = 12; - cfont.x_size = l; - cfont.y_size = w; - cfont.offset = outline; - cfont.color = color; + tft_cfont.x_size = l; + tft_cfont.y_size = w; + tft_cfont.offset = outline; + tft_cfont.color = color; } //========================================== int TFT_getfontsize(int *width, int* height) { - if (cfont.bitmap == 1) { - if (cfont.x_size != 0) *width = cfont.x_size; // fixed width font - else *width = cfont.max_x_size; // proportional font - *height = cfont.y_size; + if (tft_cfont.bitmap == 1) { + if (tft_cfont.x_size != 0) *width = tft_cfont.x_size; // fixed width font + else *width = tft_cfont.max_x_size; // proportional font + *height = tft_cfont.y_size; } - else if (cfont.bitmap == 2) { + else if (tft_cfont.bitmap == 2) { // 7-segment font *width = _7seg_width(); *height = _7seg_height(); @@ -2217,27 +2217,27 @@ int TFT_getfontsize(int *width, int* height) //===================== int TFT_getfontheight() { - if (cfont.bitmap == 1) return cfont.y_size; // Bitmap font - else if (cfont.bitmap == 2) return _7seg_height(); // 7-segment font + if (tft_cfont.bitmap == 1) return tft_cfont.y_size; // Bitmap font + else if (tft_cfont.bitmap == 2) return _7seg_height(); // 7-segment font return 0; } //==================== void TFT_saveClipWin() { - dispWinTemp.x1 = dispWin.x1; - dispWinTemp.y1 = dispWin.y1; - dispWinTemp.x2 = dispWin.x2; - dispWinTemp.y2 = dispWin.y2; + dispWinTemp.x1 = tft_dispWin.x1; + dispWinTemp.y1 = tft_dispWin.y1; + dispWinTemp.x2 = tft_dispWin.x2; + dispWinTemp.y2 = tft_dispWin.y2; } //======================= void TFT_restoreClipWin() { - dispWin.x1 = dispWinTemp.x1; - dispWin.y1 = dispWinTemp.y1; - dispWin.x2 = dispWinTemp.x2; - dispWin.y2 = dispWinTemp.y2; + tft_dispWin.x1 = dispWinTemp.x1; + tft_dispWin.y1 = dispWinTemp.y1; + tft_dispWin.x2 = dispWinTemp.x2; + tft_dispWin.y2 = dispWinTemp.y2; } @@ -2325,20 +2325,20 @@ static UINT tjd_output ( int right = rect->right + dev->x; int bottom = rect->bottom + dev->y; - if ((left > dispWin.x2) || (top > dispWin.y2)) return 1; // out of screen area, return - if ((right < dispWin.x1) || (bottom < dispWin.y1)) return 1;// out of screen area, return + if ((left > tft_dispWin.x2) || (top > tft_dispWin.y2)) return 1; // out of screen area, return + if ((right < tft_dispWin.x1) || (bottom < tft_dispWin.y1)) return 1;// out of screen area, return - if (left < dispWin.x1) dleft = dispWin.x1; + if (left < tft_dispWin.x1) dleft = tft_dispWin.x1; else dleft = left; - if (top < dispWin.y1) dtop = dispWin.y1; + if (top < tft_dispWin.y1) dtop = tft_dispWin.y1; else dtop = top; - if (right > dispWin.x2) dright = dispWin.x2; + if (right > tft_dispWin.x2) dright = tft_dispWin.x2; else dright = right; - if (bottom > dispWin.y2) dbottom = dispWin.y2; + if (bottom > tft_dispWin.y2) dbottom = tft_dispWin.y2; else dbottom = bottom; - if ((dleft > dispWin.x2) || (dtop > dispWin.y2)) return 1; // out of screen area, return - if ((dright < dispWin.x1) || (dbottom < dispWin.y1)) return 1; // out of screen area, return + if ((dleft > tft_dispWin.x2) || (dtop > tft_dispWin.y2)) return 1; // out of screen area, return + if ((dright < tft_dispWin.x1) || (dbottom < tft_dispWin.y1)) return 1; // out of screen area, return uint32_t len = ((dright-dleft+1) * (dbottom-dtop+1)); // calculate length of data @@ -2363,7 +2363,7 @@ static UINT tjd_output ( } else { wait_trans_finish(1); - printf("Data size error: %d jpg: (%d,%d,%d,%d) disp: (%d,%d,%d,%d)\r\n", len, left,top,right,bottom, dleft,dtop,dright,dbottom); + printf("Data size error: %lu jpg: (%d,%d,%d,%d) disp: (%d,%d,%d,%d)\r\n", (long unsigned)len, left,top,right,bottom, dleft,dtop,dright,dbottom); return 0; // stop decompression } @@ -2372,8 +2372,8 @@ static UINT tjd_output ( // tft.jpgimage(X, Y, scale, file_name, buf, size] // X & Y can be < 0 ! -//================================================================================== -void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int size) +//======================================================================================== +void TFT_jpg_image(int x, int y, uint8_t scale, const char *fname, uint8_t *buf, int size) { JPGIODEV dev; struct stat sb; @@ -2400,13 +2400,13 @@ void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int s dev.bufptr = 0; if (stat(fname, &sb) != 0) { - if (image_debug) printf("File error: %ss\r\n", strerror(errno)); + if (tft_image_debug) printf("File error: %ss\r\n", strerror(errno)); goto exit; } dev.fhndl = fopen(fname, "r"); if (!dev.fhndl) { - if (image_debug) printf("Error opening file: %s\r\n", strerror(errno)); + if (tft_image_debug) printf("Error opening file: %s\r\n", strerror(errno)); goto exit; } } @@ -2418,28 +2418,28 @@ void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int s if (dev.membuff) rc = jd_prepare(&jd, tjd_buf_input, (void *)work, sz_work, &dev); else rc = jd_prepare(&jd, tjd_input, (void *)work, sz_work, &dev); if (rc == JDR_OK) { - if (x == CENTER) x = ((dispWin.x2 - dispWin.x1 + 1 - (int)(jd.width >> scale)) / 2) + dispWin.x1; - else if (x == RIGHT) x = dispWin.x2 + 1 - (int)(jd.width >> scale); + if (x == CENTER) x = ((tft_dispWin.x2 - tft_dispWin.x1 + 1 - (int)(jd.width >> scale)) / 2) + tft_dispWin.x1; + else if (x == RIGHT) x = tft_dispWin.x2 + 1 - (int)(jd.width >> scale); - if (y == CENTER) y = ((dispWin.y2 - dispWin.y1 + 1 - (int)(jd.height >> scale)) / 2) + dispWin.y1; - else if (y == BOTTOM) y = dispWin.y2 + 1 - (int)(jd.height >> scale); + if (y == CENTER) y = ((tft_dispWin.y2 - tft_dispWin.y1 + 1 - (int)(jd.height >> scale)) / 2) + tft_dispWin.y1; + else if (y == BOTTOM) y = tft_dispWin.y2 + 1 - (int)(jd.height >> scale); - if (x < ((dispWin.x2-1) * -1)) x = (dispWin.x2-1) * -1; - if (y < ((dispWin.y2-1)) * -1) y = (dispWin.y2-1) * -1; - if (x > (dispWin.x2-1)) x = dispWin.x2 - 1; - if (y > (dispWin.y2-1)) y = dispWin.y2-1; + if (x < ((tft_dispWin.x2-1) * -1)) x = (tft_dispWin.x2-1) * -1; + if (y < ((tft_dispWin.y2-1)) * -1) y = (tft_dispWin.y2-1) * -1; + if (x > (tft_dispWin.x2-1)) x = tft_dispWin.x2 - 1; + if (y > (tft_dispWin.y2-1)) y = tft_dispWin.y2-1; dev.x = x; dev.y = y; dev.linbuf[0] = heap_caps_malloc(JPG_IMAGE_LINE_BUF_SIZE*3, MALLOC_CAP_DMA); if (dev.linbuf[0] == NULL) { - if (image_debug) printf("Error allocating line buffer #0\r\n"); + if (tft_image_debug) printf("Error allocating line buffer #0\r\n"); goto exit; } dev.linbuf[1] = heap_caps_malloc(JPG_IMAGE_LINE_BUF_SIZE*3, MALLOC_CAP_DMA); if (dev.linbuf[1] == NULL) { - if (image_debug) printf("Error allocating line buffer #1\r\n"); + if (tft_image_debug) printf("Error allocating line buffer #1\r\n"); goto exit; } @@ -2449,16 +2449,16 @@ void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int s disp_deselect(); if (rc != JDR_OK) { - if (image_debug) printf("jpg decompression error %d\r\n", rc); + if (tft_image_debug) printf("jpg decompression error %d\r\n", rc); } - if (image_debug) printf("Jpg size: %dx%d, position; %d,%d, scale: %d, bytes used: %d\r\n", jd.width, jd.height, x, y, scale, jd.sz_pool); + if (tft_image_debug) printf("Jpg size: %dx%d, position; %d,%d, scale: %d, bytes used: %d\r\n", jd.width, jd.height, x, y, scale, jd.sz_pool); } else { - if (image_debug) printf("jpg prepare error %d\r\n", rc); + if (tft_image_debug) printf("jpg prepare error %d\r\n", rc); } } else { - if (image_debug) printf("work buffer allocation error\r\n"); + if (tft_image_debug) printf("work buffer allocation error\r\n"); } exit: @@ -2469,8 +2469,8 @@ void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int s } -//==================================================================================== -int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int size) +//========================================================================================== +int TFT_bmp_image(int x, int y, uint8_t scale, const char *fname, uint8_t *imgbuf, int size) { FILE *fhndl = NULL; struct stat sb; @@ -2551,21 +2551,21 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int img_xlen = img_xsize / scale_pix; // image display horizontal size img_ylen = img_ysize / scale_pix; // image display vertical size - if (x == CENTER) x = ((dispWin.x2 - dispWin.x1 + 1 - img_xlen) / 2) + dispWin.x1; - else if (x == RIGHT) x = dispWin.x2 + 1 - img_xlen; + if (x == CENTER) x = ((tft_dispWin.x2 - tft_dispWin.x1 + 1 - img_xlen) / 2) + tft_dispWin.x1; + else if (x == RIGHT) x = tft_dispWin.x2 + 1 - img_xlen; - if (y == CENTER) y = ((dispWin.y2 - dispWin.y1 + 1 - img_ylen) / 2) + dispWin.y1; - else if (y == BOTTOM) y = dispWin.y2 + 1 - img_ylen; + if (y == CENTER) y = ((tft_dispWin.y2 - tft_dispWin.y1 + 1 - img_ylen) / 2) + tft_dispWin.y1; + else if (y == BOTTOM) y = tft_dispWin.y2 + 1 - img_ylen; - if ((x < ((dispWin.x2 + 1) * -1)) || (x > (dispWin.x2 + 1)) || (y < ((dispWin.y2 + 1) * -1)) || (y > (dispWin.y2 + 1))) { + if ((x < ((tft_dispWin.x2 + 1) * -1)) || (x > (tft_dispWin.x2 + 1)) || (y < ((tft_dispWin.y2 + 1) * -1)) || (y > (tft_dispWin.y2 + 1))) { sprintf(err_buf, "out of display area (%d,%d", x, y); err = -10; goto exit; } // ** set display and image areas - if (x < dispWin.x1) { - disp_xstart = dispWin.x1; + if (x < tft_dispWin.x1) { + disp_xstart = tft_dispWin.x1; img_xstart = -x; // image pixel line X offset img_xlen += x; } @@ -2573,8 +2573,8 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int disp_xstart = x; img_xstart = 0; } - if (y < dispWin.y1) { - disp_ystart = dispWin.y1; + if (y < tft_dispWin.y1) { + disp_ystart = tft_dispWin.y1; img_ystart = -y; // image pixel line Y offset img_ylen += y; } @@ -2584,12 +2584,12 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int } disp_xend = disp_xstart + img_xlen - 1; disp_yend = disp_ystart + img_ylen - 1; - if (disp_xend > dispWin.x2) { - disp_xend = dispWin.x2; + if (disp_xend > tft_dispWin.x2) { + disp_xend = tft_dispWin.x2; img_xlen = disp_xend - disp_xstart + 1; } - if (disp_yend > dispWin.y2) { - disp_yend = dispWin.y2; + if (disp_yend > tft_dispWin.y2) { + disp_yend = tft_dispWin.y2; img_ylen = disp_yend - disp_ystart + 1; } @@ -2652,7 +2652,7 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int } } - if (image_debug) printf("BMP: image size: (%d,%d) scale: %d disp size: (%d,%d) img xofs: %d img yofs: %d at: %d,%d; line buf: 2* %d scale buf: %d\r\n", + if (tft_image_debug) printf("BMP: image size: (%d,%d) scale: %d disp size: (%d,%d) img xofs: %d img yofs: %d at: %d,%d; line buf: 2* %d scale buf: %d\r\n", img_xsize, img_ysize, scale_pix, img_xlen, img_ylen, img_xstart, img_ystart, disp_xstart, disp_ystart, img_xsize*3, ((scale) ? (rd_len*scale_pix) : 0)); // * Select the display @@ -2743,7 +2743,7 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int if (line_buf[0]) free(line_buf[0]); if (line_buf[1]) free(line_buf[1]); if (fhndl) fclose(fhndl); - if ((err) && (image_debug)) printf("Error: %d [%s]\r\n", err, err_buf); + if ((err) && (tft_image_debug)) printf("Error: %d [%s]\r\n", err, err_buf); return err; } @@ -2755,7 +2755,7 @@ int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int //------------------------------------------------------- static int tp_get_data_xpt2046(uint8_t type, int samples) { - if (ts_spi == NULL) return 0; + if (tft_ts_spi == NULL) return 0; int n, result, val = 0; uint32_t i = 0; @@ -2817,7 +2817,7 @@ static int tp_get_data_xpt2046(uint8_t type, int samples) static int TFT_read_touch_xpt2046(int *x, int* y) { int res = 0, result = -1; - if (spi_lobo_device_select(ts_spi, 0) != ESP_OK) return 0; + if (spi_lobo_device_select(tft_ts_spi, 0) != ESP_OK) return 0; result = tp_get_data_xpt2046(0xB0, 3); // Z; pressure; touch detect if (result <= 50) goto exit; @@ -2834,7 +2834,7 @@ static int TFT_read_touch_xpt2046(int *x, int* y) *y = result; res = 1; exit: - spi_lobo_device_deselect(ts_spi); + spi_lobo_device_deselect(tft_ts_spi); return res; } #endif @@ -2844,7 +2844,7 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) { *x = 0; *y = 0; - if (ts_spi == NULL) return 0; + if (tft_ts_spi == NULL) return 0; #if USE_TOUCH == TOUCH_TYPE_NONE return 0; #else @@ -2852,13 +2852,13 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) int X=0, Y=0; #if USE_TOUCH == TOUCH_TYPE_XPT2046 - uint32_t tp_calx = TP_CALX_XPT2046; - uint32_t tp_caly = TP_CALY_XPT2046; + uint32_t tft_tp_calx = TP_CALX_XPT2046; + uint32_t tft_tp_caly = TP_CALY_XPT2046; result = TFT_read_touch_xpt2046(&X, &Y); if (result == 0) return 0; #elif USE_TOUCH == TOUCH_TYPE_STMPE610 - uint32_t tp_calx = TP_CALX_STMPE610; - uint32_t tp_caly = TP_CALY_STMPE610; + uint32_t tft_tp_calx = TP_CALX_STMPE610; + uint32_t tft_tp_caly = TP_CALY_STMPE610; uint16_t Xx, Yy, Z=0; result = stmpe610_get_touch(&Xx, &Yy, &Z); if (result == 0) return 0; @@ -2876,16 +2876,16 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) // Calibrate the result int tmp; - int xleft = (tp_calx >> 16) & 0x3FFF; - int xright = tp_calx & 0x3FFF; - int ytop = (tp_caly >> 16) & 0x3FFF; - int ybottom = tp_caly & 0x3FFF; + int xleft = (tft_tp_calx >> 16) & 0x3FFF; + int xright = tft_tp_calx & 0x3FFF; + int ytop = (tft_tp_caly >> 16) & 0x3FFF; + int ybottom = tft_tp_caly & 0x3FFF; if (((xright - xleft) <= 0) || ((ybottom - ytop) <= 0)) return 0; #if USE_TOUCH == TOUCH_TYPE_XPT2046 - int width = _width; - int height = _height; + int width = tft_width; + int height = tft_height; X = ((X - xleft) * height) / (xright - xleft); Y = ((Y - ytop) * width) / (ybottom - ytop); @@ -2894,7 +2894,7 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) if (Y < 0) Y = 0; if (Y > width-1) Y = width-1; - switch (orientation) { + switch (tft_orientation) { case PORTRAIT: tmp = X; X = width - Y - 1; @@ -2911,11 +2911,11 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) break; } #elif USE_TOUCH == TOUCH_TYPE_STMPE610 - int width = _width; - int height = _height; - if (_width > _height) { - width = _height; - height = _width; + int width = tft_width; + int height = tft_height; + if (tft_width > tft_height) { + width = tft_height; + height = tft_width; } X = ((X - xleft) * width) / (xright - xleft); Y = ((Y - ytop) * height) / (ybottom - ytop); @@ -2925,7 +2925,7 @@ int TFT_read_touch(int *x, int* y, uint8_t raw) if (Y < 0) Y = 0; if (Y > height-1) Y = height-1; - switch (orientation) { + switch (tft_orientation) { case PORTRAIT_FLIP: X = width - X - 1; Y = height - Y - 1; diff --git a/components/tft/tft.h b/components/tft/tft.h index 7a2cede..f742046 100644 --- a/components/tft/tft.h +++ b/components/tft/tft.h @@ -8,6 +8,11 @@ #define _TFT_H_ #include + +#ifdef __cplusplus +extern "C" { +#endif + #include "tftspi.h" typedef struct { @@ -33,26 +38,26 @@ typedef struct { //========================================================================================== // ==== Global variables =================================================================== //========================================================================================== -extern uint8_t orientation; // current screen orientation -extern uint16_t font_rotate; // current font font_rotate angle (0~395) -extern uint8_t font_transparent; // if not 0 draw fonts transparent -extern uint8_t font_forceFixed; // if not zero force drawing proportional fonts with fixed width -extern uint8_t font_buffered_char; -extern uint8_t font_line_space; // additional spacing between text lines; added to font height -extern uint8_t text_wrap; // if not 0 wrap long text to the new line, else clip -extern color_t _fg; // current foreground color for fonts -extern color_t _bg; // current background for non transparent fonts -extern dispWin_t dispWin; // display clip window -extern float _angleOffset; // angle offset for arc, polygon and line by angle functions -extern uint8_t image_debug; // print debug messages during image decode if set to 1 - -extern Font cfont; // Current font structure - -extern int TFT_X; // X position of the next character after TFT_print() function -extern int TFT_Y; // Y position of the next character after TFT_print() function - -extern uint32_t tp_calx; // touch screen X calibration constant -extern uint32_t tp_caly; // touch screen Y calibration constant +extern uint8_t tft_orientation; // current screen tft_orientation +extern uint16_t tft_font_rotate; // current font tft_font_rotate angle (0~395) +extern uint8_t tft_font_transparent; // if not 0 draw fonts transparent +extern uint8_t tft_font_forceFixed; // if not zero force drawing proportional fonts with fixed width +extern uint8_t tft_font_buffered_char; +extern uint8_t tft_font_line_space; // additional spacing between text lines; added to font height +extern uint8_t tft_text_wrap; // if not 0 wrap long text to the new line, else clip +extern color_t tft_fg; // current foreground color for fonts +extern color_t tft_bg; // current background for non transparent fonts +extern dispWin_t tft_dispWin; // display clip window +extern float tft_angleOffset; // angle offset for arc, polygon and line by angle functions +extern uint8_t tft_image_debug; // print debug messages during image decode if set to 1 + +extern Font tft_cfont; // Current font structure + +extern int tft_x; // X position of the next character after TFT_print() function +extern int tft_y; // Y position of the next character after TFT_print() function + +extern uint32_t tft_tp_calx; // touch screen X calibration constant +extern uint32_t tft_tp_caly; // touch screen Y calibration constant // ========================================================================================= @@ -199,7 +204,7 @@ void TFT_drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, color_t color) /* * Draw line on screen from (x,y) point at given angle * Line drawing angle starts at lower right quadrant of the screen and is offseted by - * '_angleOffset' global variable (default: -90 degrees) + * 'tft_angleOffset' global variable (default: -90 degrees) * * Params: * x: horizontal start position @@ -382,7 +387,7 @@ void TFT_fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, color_t /* * Draw circle arc on screen * Arc drawing angle starts at lower right quadrant of the screen and is offseted by - * '_angleOffset' global variable (default: -90 degrees) + * 'tft_angleOffset' global variable (default: -90 degrees) * * Params: * cx: arc center X position @@ -442,7 +447,7 @@ void TFT_setFont(uint8_t font, const char *font_file); * height: pointer to returned font height */ //------------------------------------------- -int TFT_getfontsize(int *width, int* height); +int TFT_getfontsize(int *width, int *height); /* @@ -455,11 +460,11 @@ int TFT_getfontheight(); /* * Write text to display. * - * Rotation of the displayed text depends on 'font_rotate' variable (0~360) - * if 'font_transparent' variable is set to 1, no background pixels will be printed + * Rotation of the displayed text depends on 'tft_font_rotate' variable (0~360) + * if 'tft_font_transparent' variable is set to 1, no background pixels will be printed * - * If the text does not fit the screen width it will be clipped (if text_wrap=0), - * or continued on next line (if text_wrap=1) + * If the text does not fit the screen width it will be clipped (if tft_text_wrap=0), + * or continued on next line (if tft_text_wrap=1) * * Two special characters are allowed in strings: * ‘\r’ CR (0x0D), clears the display to EOL @@ -479,11 +484,11 @@ int TFT_getfontheight(); * LASTY, continues from last Y position; offset can be used: LASTY+n * */ -//------------------------------------- -void TFT_print(char *st, int x, int y); +//------------------------------------------- +void TFT_print(const char *st, int x, int y); /* - * Set atributes for 7 segment vector font + * Set attributes for 7 segment vector font * == 7 segment font must be the current font to this function to have effect == * * Params: @@ -510,7 +515,7 @@ void set_7seg_font_atrib(uint8_t l, uint8_t w, int outline, color_t color); void TFT_setclipwin(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); /* - * Resets the clipping area to full screen (0,0),(_wodth,_height) + * Resets the clipping area to full screen (0,0),(_wodth,tft_height) * */ //---------------------- @@ -574,14 +579,15 @@ int TFT_compare_colors(color_t c1, color_t c2); * returns the string width in pixels. * Useful for positions strings on the screen. */ -//-------------------------------- -int TFT_getStringWidth(char* str); +//-------------------------------------- +int TFT_getStringWidth(const char *str); /* * Fills the rectangle occupied by string with current background color */ -void TFT_clearStringRect(int x, int y, char *str); +//------------------------------------------------------ +void TFT_clearStringRect(int x, int y, const char *str); /* * Converts the components of a color, as specified by the HSB model, @@ -617,8 +623,8 @@ color_t HSBtoRGB(float _hue, float _sat, float _brightness); * size: size of the memory buffer from which the image will be read; used if fname=NULL & buf!=NULL * */ -//----------------------------------------------------------------------------------- -void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int size); +//----------------------------------------------------------------------------------------- +void TFT_jpg_image(int x, int y, uint8_t scale, const char *fname, uint8_t *buf, int size); /* * Decodes and displays BMP image @@ -634,12 +640,12 @@ void TFT_jpg_image(int x, int y, uint8_t scale, char *fname, uint8_t *buf, int s * size: size of the memory buffer from which the image will be read; used if fname=NULL & imgbuf!=NULL * */ -//------------------------------------------------------------------------------------- -int TFT_bmp_image(int x, int y, uint8_t scale, char *fname, uint8_t *imgbuf, int size); +//------------------------------------------------------------------------------------------- +int TFT_bmp_image(int x, int y, uint8_t scale, const char *fname, uint8_t *imgbuf, int size); /* * Get the touch panel coordinates. - * The coordinates are adjusted to screen orientation if raw=0 + * The coordinates are adjusted to screen tft_orientation if raw=0 * * Params: * x: pointer to X coordinate @@ -668,8 +674,8 @@ int TFT_read_touch(int *x, int* y, uint8_t raw); * err no on error * */ -//------------------------------------------------ -int compile_font_file(char *fontfile, uint8_t dbg); +//------------------------------------------------------- +int compile_font_file(const char *fontfile, uint8_t dbg); /* * Get all font's characters to buffer @@ -677,3 +683,7 @@ int compile_font_file(char *fontfile, uint8_t dbg); void getFontCharacters(uint8_t *buf); #endif + +#ifdef __cplusplus +} +#endif diff --git a/components/tft/tftspi.c b/components/tft/tftspi.c index 090b819..7677629 100644 --- a/components/tft/tftspi.c +++ b/components/tft/tftspi.c @@ -2,7 +2,7 @@ * Author: LoBo (loboris@gmail.com, loboris.github) * * Module supporting SPI TFT displays based on ILI9341 & ILI9488 controllers - * + * * HIGH SPEED LOW LEVEL DISPLAY FUNCTIONS * USING DIRECT or DMA SPI TRANSFER MODEs * @@ -10,30 +10,30 @@ #include #include "tftspi.h" -#include "esp_system.h" #include "freertos/task.h" -#include "esp_heap_caps.h" #include "soc/spi_reg.h" +#include "driver/gpio.h" +#include "rom/gpio.h" // ==================================================== // ==== Global variables, default values ============== // Converts colors to grayscale if set to 1 -uint8_t gray_scale = 0; +uint8_t tft_gray_scale = 0; // Spi clock for reading data from display memory in Hz -uint32_t max_rdclock = 8000000; +uint32_t tft_max_rdclock = 8000000; // Default display dimensions -int _width = DEFAULT_TFT_DISPLAY_WIDTH; -int _height = DEFAULT_TFT_DISPLAY_HEIGHT; +int tft_width = DEFAULT_TFT_DISPLAY_WIDTH; +int tft_height = DEFAULT_TFT_DISPLAY_HEIGHT; // Display type, DISP_TYPE_ILI9488 or DISP_TYPE_ILI9341 uint8_t tft_disp_type = DEFAULT_DISP_TYPE; // Spi device handles for display and touch screen -spi_lobo_device_handle_t disp_spi = NULL; -spi_lobo_device_handle_t ts_spi = NULL; +spi_lobo_device_handle_t tft_disp_spi = NULL; +spi_lobo_device_handle_t tft_ts_spi = NULL; // ==================================================== @@ -55,21 +55,21 @@ static uint8_t _dma_sending = 0; esp_err_t IRAM_ATTR wait_trans_finish(uint8_t free_line) { // Wait for SPI bus ready - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); if ((free_line) && (trans_cline)) { free(trans_cline); trans_cline = NULL; } if (_dma_sending) { //Tell common code DMA workaround that our DMA channel is idle. If needed, the code will do a DMA reset. - if (disp_spi->host->dma_chan) spi_lobo_dmaworkaround_idle(disp_spi->host->dma_chan); + if (tft_disp_spi->host->dma_chan) spi_lobo_dmaworkaround_idle(tft_disp_spi->host->dma_chan); // Reset DMA - disp_spi->host->hw->dma_conf.val |= SPI_OUT_RST|SPI_IN_RST|SPI_AHBM_RST|SPI_AHBM_FIFO_RST; - disp_spi->host->hw->dma_out_link.start=0; - disp_spi->host->hw->dma_in_link.start=0; - disp_spi->host->hw->dma_conf.val &= ~(SPI_OUT_RST|SPI_IN_RST|SPI_AHBM_RST|SPI_AHBM_FIFO_RST); - disp_spi->host->hw->dma_conf.out_data_burst_en=1; + tft_disp_spi->host->hw->dma_conf.val |= SPI_OUT_RST|SPI_IN_RST|SPI_AHBM_RST|SPI_AHBM_FIFO_RST; + tft_disp_spi->host->hw->dma_out_link.start=0; + tft_disp_spi->host->hw->dma_in_link.start=0; + tft_disp_spi->host->hw->dma_conf.val &= ~(SPI_OUT_RST|SPI_IN_RST|SPI_AHBM_RST|SPI_AHBM_FIFO_RST); + tft_disp_spi->host->hw->dma_conf.out_data_burst_en=1; _dma_sending = 0; } return ESP_OK; @@ -79,14 +79,14 @@ esp_err_t IRAM_ATTR wait_trans_finish(uint8_t free_line) esp_err_t IRAM_ATTR disp_select() { wait_trans_finish(1); - return spi_lobo_device_select(disp_spi, 0); + return spi_lobo_device_select(tft_disp_spi, 0); } //--------------------------------- esp_err_t IRAM_ATTR disp_deselect() { wait_trans_finish(1); - return spi_lobo_device_deselect(disp_spi); + return spi_lobo_device_deselect(tft_disp_spi); } //--------------------------------------------------------------------------------------------------- @@ -113,26 +113,26 @@ static void IRAM_ATTR _spi_transfer_start(spi_lobo_device_handle_t spi_dev, int //------------------------------------------------ void IRAM_ATTR disp_spi_transfer_cmd(int8_t cmd) { // Wait for SPI bus ready - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); // Set DC to 0 (command mode); gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)cmd; - _spi_transfer_start(disp_spi, 8, 0); + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)cmd; + _spi_transfer_start(tft_disp_spi, 8, 0); } // Send command with data to display, display must be selected //---------------------------------------------------------------------------------- void IRAM_ATTR disp_spi_transfer_cmd_data(int8_t cmd, uint8_t *data, uint32_t len) { // Wait for SPI bus ready - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); // Set DC to 0 (command mode); gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)cmd; - _spi_transfer_start(disp_spi, 8, 0); + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)cmd; + _spi_transfer_start(tft_disp_spi, 8, 0); if ((len == 0) || (data == NULL)) return; @@ -150,25 +150,25 @@ void IRAM_ATTR disp_spi_transfer_cmd_data(int8_t cmd, uint8_t *data, uint32_t le bits += 8; bidx += 8; if (count == len) { - disp_spi->host->hw->data_buf[idx] = wd; + tft_disp_spi->host->hw->data_buf[idx] = wd; break; } if (bidx == 32) { - disp_spi->host->hw->data_buf[idx] = wd; + tft_disp_spi->host->hw->data_buf[idx] = wd; idx++; bidx = 0; wd = 0; } if (idx == 16) { // SPI buffer full, send data - _spi_transfer_start(disp_spi, bits, 0); - + _spi_transfer_start(tft_disp_spi, bits, 0); + bits = 0; idx = 0; bidx = 0; } } - if (bits > 0) _spi_transfer_start(disp_spi, bits, 0); + if (bits > 0) _spi_transfer_start(tft_disp_spi, bits, 0); } // Set the address window for display write & read commands, display must be selected @@ -178,47 +178,47 @@ static void IRAM_ATTR disp_spi_transfer_addrwin(uint16_t x1, uint16_t x2, uint16 taskDISABLE_INTERRUPTS(); // Wait for SPI bus ready - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_CASET; - disp_spi->host->hw->user.usr_mosi_highpart = 0; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; - disp_spi->host->hw->user.usr_mosi = 1; - disp_spi->host->hw->miso_dlen.usr_miso_dbitlen = 0; - disp_spi->host->hw->user.usr_miso = 0; + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_CASET; + tft_disp_spi->host->hw->user.usr_mosi_highpart = 0; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; + tft_disp_spi->host->hw->user.usr_mosi = 1; + tft_disp_spi->host->hw->miso_dlen.usr_miso_dbitlen = 0; + tft_disp_spi->host->hw->user.usr_miso = 0; - disp_spi->host->hw->cmd.usr = 1; // Start transfer + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer wd = (uint32_t)(x1>>8); wd |= (uint32_t)(x1&0xff) << 8; wd |= (uint32_t)(x2>>8) << 16; wd |= (uint32_t)(x2&0xff) << 24; - while (disp_spi->host->hw->cmd.usr); // wait transfer end + while (tft_disp_spi->host->hw->cmd.usr); // wait transfer end gpio_set_level(PIN_NUM_DC, 1); - disp_spi->host->hw->data_buf[0] = wd; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 31; - disp_spi->host->hw->cmd.usr = 1; // Start transfer + tft_disp_spi->host->hw->data_buf[0] = wd; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 31; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_PASET; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; - disp_spi->host->hw->cmd.usr = 1; // Start transfer + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_PASET; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer wd = (uint32_t)(y1>>8); wd |= (uint32_t)(y1&0xff) << 8; wd |= (uint32_t)(y2>>8) << 16; wd |= (uint32_t)(y2&0xff) << 24; - while (disp_spi->host->hw->cmd.usr); + while (tft_disp_spi->host->hw->cmd.usr); gpio_set_level(PIN_NUM_DC, 1); - disp_spi->host->hw->data_buf[0] = wd; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 31; - disp_spi->host->hw->cmd.usr = 1; // Start transfer - while (disp_spi->host->hw->cmd.usr); + tft_disp_spi->host->hw->data_buf[0] = wd; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 31; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer + while (tft_disp_spi->host->hw->cmd.usr); taskENABLE_INTERRUPTS(); } @@ -241,7 +241,7 @@ static color_t IRAM_ATTR color2gs(color_t color) //------------------------------------------------------------------------ void IRAM_ATTR drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) { - if (!(disp_spi->cfg.flags & LB_SPI_DEVICE_HALFDUPLEX)) return; + if (!(tft_disp_spi->cfg.flags & LB_SPI_DEVICE_HALFDUPLEX)) return; if (sel) { if (disp_select()) return; @@ -250,17 +250,17 @@ void IRAM_ATTR drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) uint32_t wd = 0; color_t _color = color; - if (gray_scale) _color = color2gs(color); + if (tft_gray_scale) _color = color2gs(color); taskDISABLE_INTERRUPTS(); disp_spi_transfer_addrwin(x, x+1, y, y+1); // Send RAM WRITE command gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_RAMWR; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; - disp_spi->host->hw->cmd.usr = 1; // Start transfer - while (disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_RAMWR; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer + while (tft_disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready wd = (uint32_t)_color.r; wd |= (uint32_t)_color.g << 8; @@ -269,10 +269,10 @@ void IRAM_ATTR drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) // Set DC to 1 (data mode); gpio_set_level(PIN_NUM_DC, 1); - disp_spi->host->hw->data_buf[0] = wd; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 23; - disp_spi->host->hw->cmd.usr = 1; // Start transfer - while (disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready + tft_disp_spi->host->hw->data_buf[0] = wd; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 23; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer + while (tft_disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready taskENABLE_INTERRUPTS(); if (sel) disp_deselect(); @@ -282,18 +282,18 @@ void IRAM_ATTR drawPixel(int16_t x, int16_t y, color_t color, uint8_t sel) static void IRAM_ATTR _dma_send(uint8_t *data, uint32_t size) { //Fill DMA descriptors - spi_lobo_dmaworkaround_transfer_active(disp_spi->host->dma_chan); //mark channel as active - spi_lobo_setup_dma_desc_links(disp_spi->host->dmadesc_tx, size, data, false); - disp_spi->host->hw->user.usr_mosi_highpart=0; - disp_spi->host->hw->dma_out_link.addr=(int)(&disp_spi->host->dmadesc_tx[0]) & 0xFFFFF; - disp_spi->host->hw->dma_out_link.start=1; - disp_spi->host->hw->user.usr_mosi_highpart=0; + spi_lobo_dmaworkaround_transfer_active(tft_disp_spi->host->dma_chan); //mark channel as active + spi_lobo_setup_dma_desc_links(tft_disp_spi->host->dmadesc_tx, size, data, false); + tft_disp_spi->host->hw->user.usr_mosi_highpart=0; + tft_disp_spi->host->hw->dma_out_link.addr=(int)(&tft_disp_spi->host->dmadesc_tx[0]) & 0xFFFFF; + tft_disp_spi->host->hw->dma_out_link.start=1; + tft_disp_spi->host->hw->user.usr_mosi_highpart=0; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = (size * 8) - 1; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = (size * 8) - 1; _dma_sending = 1; // Start transfer - disp_spi->host->hw->cmd.usr = 1; + tft_disp_spi->host->hw->cmd.usr = 1; } //--------------------------------------------------------------------------- @@ -307,12 +307,12 @@ static void IRAM_ATTR _direct_send(color_t *color, uint32_t len, uint8_t rep) taskDISABLE_INTERRUPTS(); color_t _color = color[0]; - if ((rep) && (gray_scale)) _color = color2gs(color[0]); + if ((rep) && (tft_gray_scale)) _color = color2gs(color[0]); while (len) { // ** Get color data from color buffer ** if (rep == 0) { - if (gray_scale) _color = color2gs(color[cidx]); + if (tft_gray_scale) _color = color2gs(color[cidx]); else _color = color[cidx]; } @@ -321,7 +321,7 @@ static void IRAM_ATTR _direct_send(color_t *color, uint32_t len, uint8_t rep) if (wbits == 32) { bits += wbits; wbits = 0; - disp_spi->host->hw->data_buf[idx++] = wd; + tft_disp_spi->host->hw->data_buf[idx++] = wd; wd = 0; } wd |= (uint32_t)_color.g << wbits; @@ -329,7 +329,7 @@ static void IRAM_ATTR _direct_send(color_t *color, uint32_t len, uint8_t rep) if (wbits == 32) { bits += wbits; wbits = 0; - disp_spi->host->hw->data_buf[idx++] = wd; + tft_disp_spi->host->hw->data_buf[idx++] = wd; wd = 0; } wd |= (uint32_t)_color.b << wbits; @@ -337,16 +337,16 @@ static void IRAM_ATTR _direct_send(color_t *color, uint32_t len, uint8_t rep) if (wbits == 32) { bits += wbits; wbits = 0; - disp_spi->host->hw->data_buf[idx++] = wd; + tft_disp_spi->host->hw->data_buf[idx++] = wd; wd = 0; } len--; // Decrement colors counter if (rep == 0) cidx++; // if not repeating color, increment color buffer index } if (bits) { - while (disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = bits-1; // set number of bits to be sent - disp_spi->host->hw->cmd.usr = 1; // Start transfer + while (tft_disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = bits-1; // set number of bits to be sent + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer } taskENABLE_INTERRUPTS(); } @@ -361,14 +361,14 @@ static void IRAM_ATTR _direct_send(color_t *color, uint32_t len, uint8_t rep) static void IRAM_ATTR _TFT_pushColorRep(color_t *color, uint32_t len, uint8_t rep, uint8_t wait) { if (len == 0) return; - if (!(disp_spi->cfg.flags & LB_SPI_DEVICE_HALFDUPLEX)) return; + if (!(tft_disp_spi->cfg.flags & LB_SPI_DEVICE_HALFDUPLEX)) return; // Send RAM WRITE command gpio_set_level(PIN_NUM_DC, 0); - disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_RAMWR; - disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; - disp_spi->host->hw->cmd.usr = 1; // Start transfer - while (disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready + tft_disp_spi->host->hw->data_buf[0] = (uint32_t)TFT_RAMWR; + tft_disp_spi->host->hw->mosi_dlen.usr_mosi_dbitlen = 7; + tft_disp_spi->host->hw->cmd.usr = 1; // Start transfer + while (tft_disp_spi->host->hw->cmd.usr); // Wait for SPI bus ready gpio_set_level(PIN_NUM_DC, 1); // Set DC to 1 (data mode); @@ -380,7 +380,7 @@ static void IRAM_ATTR _TFT_pushColorRep(color_t *color, uint32_t len, uint8_t re else if (rep == 0) { // ==== use DMA transfer ==== // ** Prepare data - if (gray_scale) { + if (tft_gray_scale) { for (int n=0; n (_width*2)) ? (_width*2) : len); + buf_colors = ((len > (tft_width*2)) ? (tft_width*2) : len); buf_bytes = buf_colors * 3; // Prepare color buffer of maximum 2 color lines @@ -412,7 +412,7 @@ static void IRAM_ATTR _TFT_pushColorRep(color_t *color, uint32_t len, uint8_t re if (trans_cline == NULL) return; // Prepare fill color - if (gray_scale) _color = color2gs(color[0]); + if (tft_gray_scale) _color = color2gs(color[0]); else _color = color[0]; // Fill color buffer with fill color @@ -471,8 +471,8 @@ int IRAM_ATTR read_data(int x1, int y1, int x2, int y2, int len, uint8_t *buf, u if (set_sp) { if (disp_deselect() != ESP_OK) return -1; // Change spi clock if needed - current_clock = spi_lobo_get_speed(disp_spi); - if (max_rdclock < current_clock) spi_lobo_set_speed(disp_spi, max_rdclock); + current_clock = spi_lobo_get_speed(tft_disp_spi); + if (tft_max_rdclock < current_clock) spi_lobo_set_speed(tft_disp_spi, tft_max_rdclock); } if (disp_select() != ESP_OK) return -2; @@ -489,13 +489,13 @@ int IRAM_ATTR read_data(int x1, int y1, int x2, int y2, int len, uint8_t *buf, u t.rx_buffer=buf; //t.user = (void*)1; - esp_err_t res = spi_lobo_transfer_data(disp_spi, &t); // Receive using direct mode + esp_err_t res = spi_lobo_transfer_data(tft_disp_spi, &t); // Receive using direct mode disp_deselect(); if (set_sp) { // Restore spi clock if needed - if (max_rdclock < current_clock) spi_lobo_set_speed(disp_spi, current_clock); + if (tft_max_rdclock < current_clock) spi_lobo_set_speed(tft_disp_spi, current_clock); } return res; @@ -532,18 +532,18 @@ int IRAM_ATTR touch_get_data(uint8_t type) t.rx_buffer=&rxdata; t.command = type; - ret = spi_lobo_transfer_data(ts_spi, &t); // Transmit using direct mode + ret = spi_lobo_transfer_data(tft_ts_spi, &t); // Transmit using direct mode if (ret != ESP_OK) return -1; return (((int)(rxdata[0] << 8) | (int)(rxdata[1])) >> 4); */ - spi_lobo_device_select(ts_spi, 0); + spi_lobo_device_select(tft_ts_spi, 0); - ts_spi->host->hw->data_buf[0] = type; - _spi_transfer_start(ts_spi, 24, 24); - uint16_t res = (uint16_t)(ts_spi->host->hw->data_buf[0] >> 8); + tft_ts_spi->host->hw->data_buf[0] = type; + _spi_transfer_start(tft_ts_spi, 24, 24); + uint16_t res = (uint16_t)(tft_ts_spi->host->hw->data_buf[0] >> 8); - spi_lobo_device_deselect(ts_spi); + spi_lobo_device_deselect(tft_ts_spi); return res; } @@ -557,36 +557,36 @@ int IRAM_ATTR touch_get_data(uint8_t type) //--------------------------------------------------------- static void IRAM_ATTR stmpe610_write_reg(uint8_t reg, uint8_t val) { - spi_lobo_device_select(ts_spi, 0); + spi_lobo_device_select(tft_ts_spi, 0); - ts_spi->host->hw->data_buf[0] = (val << 8) | reg; - _spi_transfer_start(ts_spi, 16, 0); + tft_ts_spi->host->hw->data_buf[0] = (val << 8) | reg; + _spi_transfer_start(tft_ts_spi, 16, 0); - spi_lobo_device_deselect(ts_spi); + spi_lobo_device_deselect(tft_ts_spi); } //----------------------------------------------- static uint8_t IRAM_ATTR stmpe610_read_byte(uint8_t reg) { - spi_lobo_device_select(ts_spi, 0); + spi_lobo_device_select(tft_ts_spi, 0); - ts_spi->host->hw->data_buf[0] = (reg << 8) | (reg | 0x80); - _spi_transfer_start(ts_spi, 16, 16); - uint8_t res = ts_spi->host->hw->data_buf[0] >> 8; + tft_ts_spi->host->hw->data_buf[0] = (reg << 8) | (reg | 0x80); + _spi_transfer_start(tft_ts_spi, 16, 16); + uint8_t res = tft_ts_spi->host->hw->data_buf[0] >> 8; - spi_lobo_device_deselect(ts_spi); + spi_lobo_device_deselect(tft_ts_spi); return res; } //----------------------------------------- static uint16_t IRAM_ATTR stmpe610_read_word(uint8_t reg) { - spi_lobo_device_select(ts_spi, 0); + spi_lobo_device_select(tft_ts_spi, 0); - ts_spi->host->hw->data_buf[0] = ((((reg+1) << 8) | ((reg+1) | 0x80)) << 16) | (reg << 8) | (reg | 0x80); - _spi_transfer_start(ts_spi, 32, 32); - uint16_t res = (uint16_t)(ts_spi->host->hw->data_buf[0] & 0xFF00); - res |= (uint16_t)(ts_spi->host->hw->data_buf[0] >> 24); + tft_ts_spi->host->hw->data_buf[0] = ((((reg+1) << 8) | ((reg+1) | 0x80)) << 16) | (reg << 8) | (reg | 0x80); + _spi_transfer_start(tft_ts_spi, 32, 32); + uint16_t res = (uint16_t)(tft_ts_spi->host->hw->data_buf[0] & 0xFF00); + res |= (uint16_t)(tft_ts_spi->host->hw->data_buf[0] >> 24); - spi_lobo_device_deselect(ts_spi); + spi_lobo_device_deselect(tft_ts_spi); return res; } @@ -602,14 +602,14 @@ uint32_t stmpe610_getID() void stmpe610_Init() { stmpe610_write_reg(STMPE610_REG_SYS_CTRL1, 0x02); // Software chip reset - vTaskDelay(10 / portTICK_RATE_MS); + vTaskDelay(10 / portTICK_PERIOD_MS); stmpe610_write_reg(STMPE610_REG_SYS_CTRL2, 0x04); // Temperature sensor clock off, GPIO clock off, touch clock on, ADC clock on stmpe610_write_reg(STMPE610_REG_INT_EN, 0x00); // Don't Interrupt on INT pin stmpe610_write_reg(STMPE610_REG_ADC_CTRL1, 0x48); // ADC conversion time = 80 clock ticks, 12-bit ADC, internal voltage refernce - vTaskDelay(2 / portTICK_RATE_MS); + vTaskDelay(2 / portTICK_PERIOD_MS); stmpe610_write_reg(STMPE610_REG_ADC_CTRL2, 0x01); // ADC speed 3.25MHz stmpe610_write_reg(STMPE610_REG_GPIO_AF, 0x00); // GPIO alternate function - OFF stmpe610_write_reg(STMPE610_REG_TSC_CFG, 0xE3); // Averaging 8, touch detect delay 1ms, panel driver settling time 1ms @@ -674,44 +674,44 @@ uint32_t find_rd_speed() int line_check; color_t *color_line = NULL; uint8_t *line_rdbuf = NULL; - uint8_t gs = gray_scale; + uint8_t gs = tft_gray_scale; - gray_scale = 0; - cur_speed = spi_lobo_get_speed(disp_spi); + tft_gray_scale = 0; + cur_speed = spi_lobo_get_speed(tft_disp_spi); - color_line = malloc(_width*3); + color_line = malloc(tft_width*3); if (color_line == NULL) goto exit; - line_rdbuf = malloc((_width*3)+1); + line_rdbuf = malloc((tft_width*3)+1); if (line_rdbuf == NULL) goto exit; color_t *rdline = (color_t *)(line_rdbuf+1); // Fill test line with colors color = (color_t){0xEC,0xA8,0x74}; - for (int x=0; x<_width; x++) { + for (int x=0; x height - if (_width < _height) { - tmp = _width; - _width = _height; - _height = tmp; + if (tft_width < tft_height) { + tmp = tft_width; + tft_width = tft_height; + tft_height = tmp; } } else { // in portrait modes must be width < height - if (_width > _height) { - tmp = _width; - _width = _height; - _height = tmp; + if (tft_width > tft_height) { + tmp = tft_width; + tft_width = tft_height; + tft_height = tmp; } } #if TFT_INVERT_ROTATION @@ -875,7 +875,7 @@ void TFT_PinsInit() #if USE_TOUCH gpio_pad_select_gpio(PIN_NUM_TCS); gpio_set_direction(PIN_NUM_TCS, GPIO_MODE_OUTPUT); -#endif +#endif #if PIN_NUM_BCKL gpio_pad_select_gpio(PIN_NUM_BCKL); gpio_set_direction(PIN_NUM_BCKL, GPIO_MODE_OUTPUT); @@ -898,35 +898,35 @@ void TFT_display_init() #if PIN_NUM_RST //Reset the display gpio_set_level(PIN_NUM_RST, 0); - vTaskDelay(20 / portTICK_RATE_MS); + vTaskDelay(20 / portTICK_PERIOD_MS); gpio_set_level(PIN_NUM_RST, 1); - vTaskDelay(150 / portTICK_RATE_MS); + vTaskDelay(150 / portTICK_PERIOD_MS); #endif ret = disp_select(); assert(ret==ESP_OK); //Send all the initialization commands if (tft_disp_type == DISP_TYPE_ILI9341) { - commandList(disp_spi, ILI9341_init); + commandList(tft_disp_spi, ILI9341_init); } else if (tft_disp_type == DISP_TYPE_ILI9488) { - commandList(disp_spi, ILI9488_init); + commandList(tft_disp_spi, ILI9488_init); } else if (tft_disp_type == DISP_TYPE_ST7789V) { - commandList(disp_spi, ST7789V_init); + commandList(tft_disp_spi, ST7789V_init); } else if (tft_disp_type == DISP_TYPE_ST7735) { - commandList(disp_spi, STP7735_init); + commandList(tft_disp_spi, STP7735_init); } else if (tft_disp_type == DISP_TYPE_ST7735R) { - commandList(disp_spi, STP7735R_init); - commandList(disp_spi, Rcmd2green); - commandList(disp_spi, Rcmd3); + commandList(tft_disp_spi, STP7735R_init); + commandList(tft_disp_spi, Rcmd2green); + commandList(tft_disp_spi, Rcmd3); } else if (tft_disp_type == DISP_TYPE_ST7735B) { - commandList(disp_spi, STP7735R_init); - commandList(disp_spi, Rcmd2red); - commandList(disp_spi, Rcmd3); + commandList(tft_disp_spi, STP7735R_init); + commandList(tft_disp_spi, Rcmd2red); + commandList(tft_disp_spi, Rcmd3); uint8_t dt = 0xC0; disp_spi_transfer_cmd_data(TFT_MADCTL, &dt, 1); } @@ -937,7 +937,7 @@ void TFT_display_init() // Clear screen _tft_setRotation(PORTRAIT); - TFT_pushColorRep(0, 0, _width-1, _height-1, (color_t){0,0,0}, (uint32_t)(_height*_width)); + TFT_pushColorRep(TFT_STATIC_WIDTH_OFFSET, TFT_STATIC_HEIGHT_OFFSET, tft_width + TFT_STATIC_WIDTH_OFFSET -1, tft_height + TFT_STATIC_HEIGHT_OFFSET -1, (color_t){0,0,0}, (uint32_t)(tft_height*tft_width)); ///Enable backlight #if PIN_NUM_BCKL diff --git a/components/tft/tftspi.h b/components/tft/tftspi.h index 0cd8c32..afde266 100644 --- a/components/tft/tftspi.h +++ b/components/tft/tftspi.h @@ -1,5 +1,5 @@ /* - * + * * HIGH SPEED LOW LEVEL DISPLAY FUNCTIONS USING DIRECT TRANSFER MODE * */ @@ -7,7 +7,6 @@ #ifndef _TFTSPI_H_ #define _TFTSPI_H_ -#include "tftspi.h" #include "spi_master_lobo.h" #include "sdkconfig.h" #include "stmpe610.h" @@ -22,7 +21,7 @@ #define TP_CALX_STMPE610 21368532 #define TP_CALY_STMPE610 11800144 -// === Screen orientation constants === +// === Screen tft_orientation constants === #define PORTRAIT 0 #define LANDSCAPE 1 #define PORTRAIT_FLIP 2 @@ -34,11 +33,12 @@ #define DISP_TYPE_ST7735 3 #define DISP_TYPE_ST7735R 4 #define DISP_TYPE_ST7735B 5 -#define DISP_TYPE_MAX 6 -#if CONFIG_EXAMPLE_DISPLAY_TYPE == 1 -// ** Set the correct configuration for ESP32-WROVER-KIT v3 + +#if CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 1 + +// ** Set the correct configuration for ESP-WROVER-KIT v3 // -------------------------------------------------------- #define DEFAULT_DISP_TYPE DISP_TYPE_ST7789V #define DEFAULT_TFT_DISPLAY_WIDTH 240 @@ -65,7 +65,7 @@ #define PIN_BCKL_OFF 1 // GPIO value for backlight OFF // -------------------------------------------------------- -#elif CONFIG_EXAMPLE_DISPLAY_TYPE == 2 +#elif CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 2 // ** Set the correct configuration for Adafruit TFT Feather // --------------------------------------------------------- @@ -94,7 +94,7 @@ #define PIN_BCKL_OFF 1 // GPIO value for backlight OFF // --------------------------------------------------------- -#elif CONFIG_EXAMPLE_DISPLAY_TYPE == 3 +#elif CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 3 // ** Set the correct configuration for M5Stack TFT // --------------------------------------------------------- @@ -124,6 +124,102 @@ #define PIN_BCKL_OFF 0 // GPIO value for backlight OFF // --------------------------------------------------------- +#elif CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 4 + +// ** Set the correct configuration for ESP-WROVER-KIT v4.1 +// -------------------------------------------------------- +#define DEFAULT_DISP_TYPE DISP_TYPE_ILI9341 +#define DEFAULT_TFT_DISPLAY_WIDTH 240 +#define DEFAULT_TFT_DISPLAY_HEIGHT 320 +#define DISP_COLOR_BITS_24 0x66 +#define DEFAULT_GAMMA_CURVE 0 +#define DEFAULT_SPI_CLOCK 26000000 +#define TFT_INVERT_ROTATION 0 +#define TFT_INVERT_ROTATION1 0 +#define TFT_INVERT_ROTATION2 0 +#define TFT_RGB_BGR 0x08 + +#define USE_TOUCH TOUCH_TYPE_NONE + +#define PIN_NUM_MISO 25 // SPI MISO +#define PIN_NUM_MOSI 23 // SPI MOSI +#define PIN_NUM_CLK 19 // SPI CLOCK pin +#define PIN_NUM_CS 22 // Display CS pin +#define PIN_NUM_DC 21 // Display command/data pin +#define PIN_NUM_TCS 0 // Touch screen CS pin + +#define PIN_NUM_RST 18 // GPIO used for RESET control +#define PIN_NUM_BCKL 5 // GPIO used for backlight control +#define PIN_BCKL_ON 0 // GPIO value for backlight ON +#define PIN_BCKL_OFF 1 // GPIO value for backlight OFF +// -------------------------------------------------------- +#elif CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 5 +//CONFIG FOR TTGO T-DISPLAY +#define DEFAULT_DISP_TYPE DISP_TYPE_ST7789V +#define DEFAULT_TFT_DISPLAY_WIDTH 135 +#define DEFAULT_TFT_DISPLAY_HEIGHT 240 + +//Need to be defined together so they can be swapped for x;y when rotating +#define TFT_STATIC_WIDTH_OFFSET 53 +#define TFT_STATIC_HEIGHT_OFFSET 40 + +#define DISP_COLOR_BITS_24 0x66 +#define DEFAULT_GAMMA_CURVE 0 +#define DEFAULT_SPI_CLOCK 20000000 +#define TFT_INVERT_ROTATION 0 +#define TFT_INVERT_ROTATION1 1 +#define TFT_RGB_BGR 0x00 +//To be used by user application for initialization +#define TFT_START_COLORS_INVERTED + +#define USE_TOUCH TOUCH_TYPE_NONE + +#define PIN_NUM_MISO 0 // SPI MISO +#define PIN_NUM_MOSI 19 // SPI MOSI +#define PIN_NUM_CLK 18 // SPI CLOCK pin +#define PIN_NUM_CS 5 // Display CS pin +#define PIN_NUM_DC 16 // Display command/data pin +#define PIN_NUM_TCS 0 // Touch screen CS pin + +#define PIN_NUM_RST 23 // GPIO used for RESET control +#define PIN_NUM_BCKL 4 // GPIO used for backlight control +#define PIN_BCKL_ON 1 // GPIO value for backlight ON +#define PIN_BCKL_OFF 0 // GPIO value for backlight OFF +//END TTGO T_DISPLAY + +#elif CONFIG_TFT_PREDEFINED_DISPLAY_TYPE == 6 +//CONFIG FOR TTGO T-WRISTBAND +#define DEFAULT_DISP_TYPE DISP_TYPE_ST7735R +#define DEFAULT_TFT_DISPLAY_WIDTH 80 +#define DEFAULT_TFT_DISPLAY_HEIGHT 160 + +//Need to be defined together so they can be swapped for x;y when rotating +#define TFT_STATIC_WIDTH_OFFSET 26 +#define TFT_STATIC_HEIGHT_OFFSET 1 + +#define DISP_COLOR_BITS_24 0x66 +#define DEFAULT_GAMMA_CURVE 0 +#define DEFAULT_SPI_CLOCK 27000000 +#define TFT_INVERT_ROTATION 0 +#define TFT_INVERT_ROTATION1 1 +#define TFT_RGB_BGR 0x00 +//To be used by user application for initialization +#define TFT_START_COLORS_INVERTED + +#define USE_TOUCH TOUCH_TYPE_NONE + +#define PIN_NUM_MISO 0 // SPI MISO +#define PIN_NUM_MOSI 19 // SPI MOSI +#define PIN_NUM_CLK 18 // SPI CLOCK pin +#define PIN_NUM_CS 5 // Display CS pin +#define PIN_NUM_DC 23 // Display command/data pin +#define PIN_NUM_TCS 0 // Touch screen CS pin + +#define PIN_NUM_RST 26 // GPIO used for RESET control +#define PIN_NUM_BCKL 27 // GPIO used for backlight control +#define PIN_BCKL_ON 1 // GPIO value for backlight ON +#define PIN_BCKL_OFF 0 // GPIO value for backlight OFF +//END TTGO T_DISPLAY #else // Configuration for other boards, set the correct values for the display used @@ -131,19 +227,20 @@ #define DISP_COLOR_BITS_24 0x66 //#define DISP_COLOR_BITS_16 0x55 // Do not use! -// ############################################# -// ### Set to 1 for some displays, ### -// for example the one on ESP-WROWER-KIT ### -// ############################################# #define TFT_INVERT_ROTATION 0 -#define TFT_INVERT_ROTATION1 0 +#define TFT_INVERT_ROTATION1 CONFIG_TFT_INVERT_ROTATION1 // ################################################ // ### SET TO 0X00 FOR DISPLAYS WITH RGB MATRIX ### // ### SET TO 0X08 FOR DISPLAYS WITH BGR MATRIX ### // ### For ESP-WROWER-KIT set to 0x00 ### // ################################################ + +#if CONFIG_TFT_RGB_BGR +#define TFT_RGB_BGR 0x00 +#else #define TFT_RGB_BGR 0x08 +#endif // ############################################################## // ### Define ESP32 SPI pins to which the display is attached ### @@ -151,42 +248,51 @@ // The pins configured here are the native spi pins for HSPI interface // Any other valid pin combination can be used -#define PIN_NUM_MISO 19 // SPI MISO -#define PIN_NUM_MOSI 23 // SPI MOSI -#define PIN_NUM_CLK 18 // SPI CLOCK pin -#define PIN_NUM_CS 5 // Display CS pin -#define PIN_NUM_DC 26 // Display command/data pin -#define PIN_NUM_TCS 25 // Touch screen CS pin (NOT used if USE_TOUCH=0) -// -------------------------------------------------------------- -// ** Set Reset and Backlight pins to 0 if not used ! -// ** If you want to use them, set them to some valid GPIO number -#define PIN_NUM_RST 0 // GPIO used for RESET control +#define PIN_NUM_MISO CONFIG_TFT_PIN_NUM_MISO +#define PIN_NUM_MOSI CONFIG_TFT_PIN_NUM_MOSI +#define PIN_NUM_CLK CONFIG_TFT_PIN_NUM_CLK +#define PIN_NUM_CS CONFIG_TFT_PIN_NUM_CS +#define PIN_NUM_DC CONFIG_TFT_PIN_NUM_DC +#define PIN_NUM_TCS CONFIG_TFT_PIN_NUM_TCS +#define PIN_NUM_RST CONFIG_TFT_PIN_NUM_RST +#define PIN_NUM_BCKL CONFIG_TFT_PIN_NUM_BCKL -#define PIN_NUM_BCKL 0 // GPIO used for backlight control #define PIN_BCKL_ON 0 // GPIO value for backlight ON #define PIN_BCKL_OFF 1 // GPIO value for backlight OFF // -------------------------------------------------------------- -// ####################################################### -// Set this to 1 if you want to use touch screen functions -// ####################################################### -#define USE_TOUCH TOUCH_TYPE_NONE -// ####################################################### +#define USE_TOUCH CONFIG_TFT_TOUCH_CONTROLLER // ####################################################################### // Default display width (smaller dimension) and height (larger dimension) // ####################################################################### -#define DEFAULT_TFT_DISPLAY_WIDTH 240 -#define DEFAULT_TFT_DISPLAY_HEIGHT 320 +#define DEFAULT_TFT_DISPLAY_WIDTH CONFIG_TFT_DISPLAY_WIDTH +#define DEFAULT_TFT_DISPLAY_HEIGHT CONFIG_TFT_DISPLAY_HEIGHT // ####################################################################### #define DEFAULT_GAMMA_CURVE 0 #define DEFAULT_SPI_CLOCK 26000000 + +#if defined(CONFIG_TFT_DISPLAY_CONTROLLER_MODEL) +#define DEFAULT_DISP_TYPE CONFIG_TFT_DISPLAY_CONTROLLER_MODEL +#else #define DEFAULT_DISP_TYPE DISP_TYPE_ILI9341 +#endif //---------------------------------------------------------------------------- -#endif // CONFIG_EXAMPLE_ESP_WROVER_KIT +#endif // CONFIG_PREDEFINED_DISPLAY_TYPE + +// Define offset generation, or ignore offsets if none are needed +#ifdef TFT_STATIC_WIDTH_OFFSET +#define TFT_STATIC_X_OFFSET (tft_orientation & 1 ? TFT_STATIC_HEIGHT_OFFSET : TFT_STATIC_WIDTH_OFFSET) +#define TFT_STATIC_Y_OFFSET (tft_orientation & 1 ? TFT_STATIC_WIDTH_OFFSET : TFT_STATIC_HEIGHT_OFFSET) +#else +#define TFT_STATIC_WIDTH_OFFSET 0 +#define TFT_STATIC_X_OFFSET 0 +#define TFT_STATIC_HEIGHT_OFFSET 0 +#define TFT_STATIC_Y_OFFSET 0 +#endif // ############################################################## @@ -194,21 +300,21 @@ // ############################################################## // ==== Converts colors to grayscale if 1 ======================= -extern uint8_t gray_scale; +extern uint8_t tft_gray_scale; // ==== Spi clock for reading data from display memory in Hz ==== -extern uint32_t max_rdclock; +extern uint32_t tft_max_rdclock; // ==== Display dimensions in pixels ============================ -extern int _width; -extern int _height; +extern int tft_width; +extern int tft_height; // ==== Display type, DISP_TYPE_ILI9488 or DISP_TYPE_ILI9341 ==== extern uint8_t tft_disp_type; // ==== Spi device handles for display and touch screen ========= -extern spi_lobo_device_handle_t disp_spi; -extern spi_lobo_device_handle_t ts_spi; +extern spi_lobo_device_handle_t tft_disp_spi; +extern spi_lobo_device_handle_t tft_ts_spi; // ############################################################## @@ -347,7 +453,7 @@ static const uint8_t ST7789V_init[] = { ST_CMD_PWCTR1, 2, 0xA4, 0xA1, TFT_CMD_GMCTRP1, 14, 0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x37, 0x43, 0x47, 0x09, 0x15, 0x12, 0x16, 0x19, TFT_CMD_GMCTRN1, 14, 0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19, - TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (orientation) + TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (tft_orientation) TFT_CMD_PIXFMT, 1, DISP_COLOR_BITS_24, // *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit; 0x55 -> 16 bit TFT_CMD_SLPOUT, TFT_CMD_DELAY, 120, // Sleep out, // 120 ms delay TFT_DISPON, TFT_CMD_DELAY, 120, @@ -374,7 +480,7 @@ static const uint8_t ILI9341_init[] = { TFT_CMD_PWCTR2, 1, 0x10, //Power control SAP[2:0];BT[3:0] TFT_CMD_VMCTR1, 2, 0x3e, 0x28, //VCM control TFT_CMD_VMCTR2, 1, 0x86, //VCM control2 - TFT_MADCTL, 1, // Memory Access Control (orientation) + TFT_MADCTL, 1, // Memory Access Control (tft_orientation) (MADCTL_MX | TFT_RGB_BGR), // *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit; 0x55 -> 16 bit TFT_CMD_PIXFMT, 1, DISP_COLOR_BITS_24, @@ -418,9 +524,9 @@ static const uint8_t ILI9488_init[] = { 0x80, #if TFT_INVERT_ROTATION - TFT_MADCTL, 1, (MADCTL_MV | TFT_RGB_BGR), // Memory Access Control (orientation), set to portrait + TFT_MADCTL, 1, (MADCTL_MV | TFT_RGB_BGR), // Memory Access Control (tft_orientation), set to portrait #else - TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (orientation), set to portrait + TFT_MADCTL, 1, (MADCTL_MX | TFT_RGB_BGR), // Memory Access Control (tft_orientation), set to portrait #endif // *** INTERFACE PIXEL FORMAT: 0x66 -> 18 bit; @@ -656,10 +762,10 @@ void _tft_setRotation(uint8_t rot); void TFT_PinsInit(); // Perform display initialization sequence -// Sets orientation to landscape; clears the screen +// Sets tft_orientation to landscape; clears the screen // * All pins must be configured // * SPI interface must already be setup -// * 'tft_disp_type', 'COLOR_BITS', '_width', '_height' variables must be set +// * 'tft_disp_type', 'COLOR_BITS', 'tft_width', 'tft_height' variables must be set //====================== void TFT_display_init(); @@ -674,4 +780,4 @@ uint32_t stmpe610_getID(); // =============================================================================== -#endif \ No newline at end of file +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..e647895 --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,9 @@ +set(SOURCES tft_demo.c) +idf_component_register( + SRCS ${SOURCES} + REQUIRES + tft + spiffs + PRIV_REQUIRES + nvs_flash +) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index ce369bb..62867af 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -1,52 +1,5 @@ menu "TFT Display DEMO Configuration" -config SPIFFS_BASE_ADDR - hex "SPIFFS Base address" - range 100000 1FFE000 - default 180000 - help - Starting address of the SPIFFS area in ESP32 Flash - -config SPIFFS_SIZE - int "SPIFFS Size in bytes" - range 262144 2097152 - default 1048576 - -config SPIFFS_LOG_BLOCK_SIZE - int "SPIFFS Logical block size" - range 4098 65536 - default 8192 - -config SPIFFS_LOG_PAGE_SIZE - int "SPIFFS Logical page size" - range 256 2048 - default 256 - help - Set it to the phisycal page size og the used SPI Flash chip. - -config EXAMPLE_DISPLAY_TYPE - int - default 0 if EXAMPLE_DISPLAY_TYPE0 - default 1 if EXAMPLE_DISPLAY_TYPE1 - default 2 if EXAMPLE_DISPLAY_TYPE2 - default 3 if EXAMPLE_DISPLAY_TYPE3 - - choice - prompt "Select predefined display configuration" - default EXAMPLE_DISPLAY_TYPE0 - help - Select predefined display configuration - - config EXAMPLE_DISPLAY_TYPE0 - bool "None" - config EXAMPLE_DISPLAY_TYPE1 - bool "ESP-WROVER-KIT Display" - config EXAMPLE_DISPLAY_TYPE2 - bool "Adafruit TFT Feather display" - config EXAMPLE_DISPLAY_TYPE3 - bool "M5Stack TFT display" - endchoice - config EXAMPLE_USE_WIFI bool "Use wifi in TFT Demo" default n diff --git a/main/tft_demo.c b/main/tft_demo.c index d116d32..fe3a6db 100644 --- a/main/tft_demo.c +++ b/main/tft_demo.c @@ -8,33 +8,20 @@ */ #include -#include -#include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_system.h" -#include "driver/gpio.h" -#include "esp_system.h" -#include "esp_heap_caps.h" -#include "tftspi.h" + #include "tft.h" #include "spiffs_vfs.h" #ifdef CONFIG_EXAMPLE_USE_WIFI #include "esp_wifi.h" -#include "esp_system.h" -#include "esp_event.h" -#include "esp_event_loop.h" #include "freertos/event_groups.h" -#include "esp_attr.h" -#include -#include -#include "lwip/err.h" -#include "apps/sntp/sntp.h" +#include "esp_sntp.h" #include "esp_log.h" #include "nvs_flash.h" @@ -143,7 +130,7 @@ static int obtain_time(void) //ESP_LOGI(tag, "Waiting for system time to be set... (%d/%d)", retry, retry_count); sprintf(tmp_buff, "Wait %0d/%d", retry, retry_count); TFT_print(tmp_buff, CENTER, LASTY); - vTaskDelay(500 / portTICK_RATE_MS); + vTaskDelay(500 / portTICK_PERIOD_MS); time(&time_now); tm_info = localtime(&time_now); } @@ -176,19 +163,19 @@ static void _checkTime() TFT_saveClipWin(); TFT_resetclipwin(); - Font curr_font = cfont; - last_bg = _bg; - last_fg = _fg; - _fg = TFT_YELLOW; - _bg = (color_t){ 64, 64, 64 }; + Font curr_font = tft_cfont; + last_bg = tft_bg; + last_fg = tft_fg; + tft_fg = TFT_YELLOW; + tft_bg = (color_t){ 64, 64, 64 }; TFT_setFont(DEFAULT_FONT, NULL); - TFT_fillRect(1, _height-TFT_getfontheight()-8, _width-3, TFT_getfontheight()+6, _bg); - TFT_print(tmp_buff, CENTER, _height-TFT_getfontheight()-5); + TFT_fillRect(1, tft_height-TFT_getfontheight()-8, tft_width-3, TFT_getfontheight()+6, tft_bg); + TFT_print(tmp_buff, CENTER, tft_height-TFT_getfontheight()-5); - cfont = curr_font; - _fg = last_fg; - _bg = last_bg; + tft_cfont = curr_font; + tft_fg = last_fg; + tft_bg = last_bg; TFT_restoreClipWin(); } @@ -201,7 +188,7 @@ static int _checkTouch() int tx, ty; if (TFT_read_touch(&tx, &ty, 0)) { while (TFT_read_touch(&tx, &ty, 1)) { - vTaskDelay(20 / portTICK_RATE_MS); + vTaskDelay(20 / portTICK_PERIOD_MS); } return 1; } @@ -218,12 +205,12 @@ static int Wait(int ms) ms *= -1; } if (ms <= 50) { - vTaskDelay(ms / portTICK_RATE_MS); + vTaskDelay(ms / portTICK_PERIOD_MS); //if (_checkTouch()) return 0; } else { for (int n=0; ntm_hour, tm_info->tm_min, tm_info->tm_sec); - TFT_print(tmp_buff, CENTER, _height-TFT_getfontheight()-5); + TFT_print(tmp_buff, CENTER, tft_height-TFT_getfontheight()-5); - cfont = curr_font; + tft_cfont = curr_font; } //--------------------------------- @@ -283,22 +270,22 @@ static void disp_header(char *info) TFT_fillScreen(TFT_BLACK); TFT_resetclipwin(); - _fg = TFT_YELLOW; - _bg = (color_t){ 64, 64, 64 }; + tft_fg = TFT_YELLOW; + tft_bg = (color_t){ 64, 64, 64 }; - if (_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL); + if (tft_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL); else TFT_setFont(DEFAULT_FONT, NULL); - TFT_fillRect(0, 0, _width-1, TFT_getfontheight()+8, _bg); - TFT_drawRect(0, 0, _width-1, TFT_getfontheight()+8, TFT_CYAN); + TFT_fillRect(0, 0, tft_width-1, TFT_getfontheight()+8, tft_bg); + TFT_drawRect(0, 0, tft_width-1, TFT_getfontheight()+8, TFT_CYAN); - TFT_fillRect(0, _height-TFT_getfontheight()-9, _width-1, TFT_getfontheight()+8, _bg); - TFT_drawRect(0, _height-TFT_getfontheight()-9, _width-1, TFT_getfontheight()+8, TFT_CYAN); + TFT_fillRect(0, tft_height-TFT_getfontheight()-9, tft_width-1, TFT_getfontheight()+8, tft_bg); + TFT_drawRect(0, tft_height-TFT_getfontheight()-9, tft_width-1, TFT_getfontheight()+8, TFT_CYAN); TFT_print(info, CENTER, 4); _dispTime(); - _bg = TFT_BLACK; - TFT_setclipwin(0,TFT_getfontheight()+9, _width-1, _height-TFT_getfontheight()-10); + tft_bg = TFT_BLACK; + TFT_setclipwin(0,TFT_getfontheight()+9, tft_width-1, tft_height-TFT_getfontheight()-10); } //--------------------------------------------- @@ -309,28 +296,28 @@ static void update_header(char *hdr, char *ftr) TFT_saveClipWin(); TFT_resetclipwin(); - Font curr_font = cfont; - last_bg = _bg; - last_fg = _fg; - _fg = TFT_YELLOW; - _bg = (color_t){ 64, 64, 64 }; - if (_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL); + Font curr_font = tft_cfont; + last_bg = tft_bg; + last_fg = tft_fg; + tft_fg = TFT_YELLOW; + tft_bg = (color_t){ 64, 64, 64 }; + if (tft_width < 240) TFT_setFont(DEF_SMALL_FONT, NULL); else TFT_setFont(DEFAULT_FONT, NULL); if (hdr) { - TFT_fillRect(1, 1, _width-3, TFT_getfontheight()+6, _bg); + TFT_fillRect(1, 1, tft_width-3, TFT_getfontheight()+6, tft_bg); TFT_print(hdr, CENTER, 4); } if (ftr) { - TFT_fillRect(1, _height-TFT_getfontheight()-8, _width-3, TFT_getfontheight()+6, _bg); + TFT_fillRect(1, tft_height-TFT_getfontheight()-8, tft_width-3, TFT_getfontheight()+6, tft_bg); if (strlen(ftr) == 0) _dispTime(); - else TFT_print(ftr, CENTER, _height-TFT_getfontheight()-5); + else TFT_print(ftr, CENTER, tft_height-TFT_getfontheight()-5); } - cfont = curr_font; - _fg = last_fg; - _bg = last_bg; + tft_cfont = curr_font; + tft_fg = last_fg; + tft_bg = last_bg; TFT_restoreClipWin(); } @@ -350,26 +337,26 @@ static void test_times() { sprintf(tmp_buff, "Clear screen: %u ms", t1); TFT_print(tmp_buff, 0, 140); - color_t *color_line = heap_caps_malloc((_width*3), MALLOC_CAP_DMA); + color_t *color_line = heap_caps_malloc((tft_width*3), MALLOC_CAP_DMA); color_t *gsline = NULL; - if (gray_scale) gsline = malloc(_width*3); + if (tft_gray_scale) gsline = malloc(tft_width*3); if (color_line) { - float hue_inc = (float)((10.0 / (float)(_height-1) * 360.0)); - for (int x=0; x<_width; x++) { - color_line[x] = HSBtoRGB(hue_inc, 1.0, (float)x / (float)_width); + float hue_inc = (float)((10.0 / (float)(tft_height-1) * 360.0)); + for (int x=0; xtm_min, tm_info->tm_sec, ms); TFT_setFont(FONT_7SEG, NULL); - if ((_width < 240) || (_height < 240)) set_7seg_font_atrib(8, 1, 1, TFT_DARKGREY); + if ((tft_width < 240) || (tft_height < 240)) set_7seg_font_atrib(8, 1, 1, TFT_DARKGREY); else set_7seg_font_atrib(12, 2, 1, TFT_DARKGREY); //TFT_clearStringRect(12, y, tmp_buff); TFT_print(tmp_buff, CENTER, y); n++; - _fg = TFT_GREEN; + tft_fg = TFT_GREEN; y += TFT_getfontheight() + 12; - if ((_width < 240) || (_height < 240)) set_7seg_font_atrib(9, 1, 1, TFT_DARKGREY); + if ((tft_width < 240) || (tft_height < 240)) set_7seg_font_atrib(9, 1, 1, TFT_DARKGREY); else set_7seg_font_atrib(14, 3, 1, TFT_DARKGREY); sprintf(tmp_buff, "%02d:%02d", tm_info->tm_sec, ms / 10); //TFT_clearStringRect(12, y, tmp_buff); TFT_print(tmp_buff, CENTER, y); n++; - _fg = random_color(); + tft_fg = random_color(); y += TFT_getfontheight() + 8; set_7seg_font_atrib(6, 1, 1, TFT_DARKGREY); getFontCharacters((uint8_t *)tmp_buff); @@ -549,20 +536,20 @@ static void font_demo() TFT_saveClipWin(); TFT_resetclipwin(); - TFT_drawRect(38, 48, (_width*3/4) - 36, (_height*3/4) - 46, TFT_WHITE); - TFT_setclipwin(40, 50, _width*3/4, _height*3/4); + TFT_drawRect(38, 48, (tft_width*3/4) - 36, (tft_height*3/4) - 46, TFT_WHITE); + TFT_setclipwin(40, 50, tft_width*3/4, tft_height*3/4); - if ((_width < 240) || (_height < 240)) TFT_setFont(DEF_SMALL_FONT, NULL); + if ((tft_width < 240) || (tft_height < 240)) TFT_setFont(DEF_SMALL_FONT, NULL); else TFT_setFont(UBUNTU16_FONT, NULL); - text_wrap = 1; + tft_text_wrap = 1; end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - _fg = random_color(); + tft_fg = random_color(); TFT_print("This text is printed inside the window.\nLong line can be wrapped to the next line.\nWelcome to ESP32", 0, 0); n++; } - text_wrap = 0; + tft_text_wrap = 0; sprintf(tmp_buff, "%d STRINGS", n); update_header(NULL, tmp_buff); Wait(-GDEMO_INFO_TIME); @@ -580,10 +567,10 @@ static void rect_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(4, dispWin.x2-4); - y = rand_interval(4, dispWin.y2-2); - w = rand_interval(2, dispWin.x2-x); - h = rand_interval(2, dispWin.y2-y); + x = rand_interval(4, tft_dispWin.x2-4); + y = rand_interval(4, tft_dispWin.y2-2); + w = rand_interval(2, tft_dispWin.x2-x); + h = rand_interval(2, tft_dispWin.y2-y); TFT_drawRect(x,y,w,h,random_color()); n++; } @@ -596,10 +583,10 @@ static void rect_demo() end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(4, dispWin.x2-4); - y = rand_interval(4, dispWin.y2-2); - w = rand_interval(2, dispWin.x2-x); - h = rand_interval(2, dispWin.y2-y); + x = rand_interval(4, tft_dispWin.x2-4); + y = rand_interval(4, tft_dispWin.y2-2); + w = rand_interval(2, tft_dispWin.x2-x); + h = rand_interval(2, tft_dispWin.y2-y); TFT_fillRect(x,y,w,h,random_color()); TFT_drawRect(x,y,w,h,random_color()); n++; @@ -619,8 +606,8 @@ static void pixel_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(0, dispWin.x2); - y = rand_interval(0, dispWin.y2); + x = rand_interval(0, tft_dispWin.x2); + y = rand_interval(0, tft_dispWin.y2); TFT_drawPixel(x,y,random_color(),1); n++; } @@ -639,10 +626,10 @@ static void line_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x1 = rand_interval(0, dispWin.x2); - y1 = rand_interval(0, dispWin.y2); - x2 = rand_interval(0, dispWin.x2); - y2 = rand_interval(0, dispWin.y2); + x1 = rand_interval(0, tft_dispWin.x2); + y1 = rand_interval(0, tft_dispWin.y2); + x2 = rand_interval(0, tft_dispWin.x2); + y2 = rand_interval(0, tft_dispWin.y2); TFT_drawLine(x1,y1,x2,y2,random_color()); n++; } @@ -658,8 +645,8 @@ static void aline_demo() disp_header("LINE BY ANGLE DEMO"); - x = (dispWin.x2 - dispWin.x1) / 2; - y = (dispWin.y2 - dispWin.y1) / 2; + x = (tft_dispWin.x2 - tft_dispWin.x1) / 2; + y = (tft_dispWin.y2 - tft_dispWin.y1) / 2; if (x < y) len = x - 8; else len = y -8; @@ -698,8 +685,8 @@ static void arc_demo() disp_header("ARC DEMO"); - x = (dispWin.x2 - dispWin.x1) / 2; - y = (dispWin.y2 - dispWin.y1) / 2; + x = (tft_dispWin.x2 - tft_dispWin.x1) / 2; + y = (tft_dispWin.y2 - tft_dispWin.y1) / 2; th = 6; uint32_t end_time = clock() + GDEMO_TIME; @@ -761,8 +748,8 @@ static void circle_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(8, dispWin.x2-8); - y = rand_interval(8, dispWin.y2-8); + x = rand_interval(8, tft_dispWin.x2-8); + y = rand_interval(8, tft_dispWin.y2-8); if (x < y) r = rand_interval(2, x/2); else r = rand_interval(2, y/2); TFT_drawCircle(x,y,r,random_color()); @@ -777,8 +764,8 @@ static void circle_demo() end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(8, dispWin.x2-8); - y = rand_interval(8, dispWin.y2-8); + x = rand_interval(8, tft_dispWin.x2-8); + y = rand_interval(8, tft_dispWin.y2-8); if (x < y) r = rand_interval(2, x/2); else r = rand_interval(2, y/2); TFT_fillCircle(x,y,r,random_color()); @@ -800,8 +787,8 @@ static void ellipse_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(8, dispWin.x2-8); - y = rand_interval(8, dispWin.y2-8); + x = rand_interval(8, tft_dispWin.x2-8); + y = rand_interval(8, tft_dispWin.y2-8); if (x < y) rx = rand_interval(2, x/4); else rx = rand_interval(2, y/4); if (x < y) ry = rand_interval(2, x/4); @@ -818,8 +805,8 @@ static void ellipse_demo() end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(8, dispWin.x2-8); - y = rand_interval(8, dispWin.y2-8); + x = rand_interval(8, tft_dispWin.x2-8); + y = rand_interval(8, tft_dispWin.y2-8); if (x < y) rx = rand_interval(2, x/4); else rx = rand_interval(2, y/4); if (x < y) ry = rand_interval(2, x/4); @@ -838,8 +825,8 @@ static void ellipse_demo() n = 0; int k = 1; while ((clock() < end_time) && (Wait(0))) { - x = rand_interval(8, dispWin.x2-8); - y = rand_interval(8, dispWin.y2-8); + x = rand_interval(8, tft_dispWin.x2-8); + y = rand_interval(8, tft_dispWin.y2-8); if (x < y) rx = rand_interval(2, x/4); else rx = rand_interval(2, y/4); if (x < y) ry = rand_interval(2, x/4); @@ -864,12 +851,12 @@ static void triangle_demo() uint32_t end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x1 = rand_interval(4, dispWin.x2-4); - y1 = rand_interval(4, dispWin.y2-2); - x2 = rand_interval(4, dispWin.x2-4); - y2 = rand_interval(4, dispWin.y2-2); - x3 = rand_interval(4, dispWin.x2-4); - y3 = rand_interval(4, dispWin.y2-2); + x1 = rand_interval(4, tft_dispWin.x2-4); + y1 = rand_interval(4, tft_dispWin.y2-2); + x2 = rand_interval(4, tft_dispWin.x2-4); + y2 = rand_interval(4, tft_dispWin.y2-2); + x3 = rand_interval(4, tft_dispWin.x2-4); + y3 = rand_interval(4, tft_dispWin.y2-2); TFT_drawTriangle(x1,y1,x2,y2,x3,y3,random_color()); n++; } @@ -882,12 +869,12 @@ static void triangle_demo() end_time = clock() + GDEMO_TIME; n = 0; while ((clock() < end_time) && (Wait(0))) { - x1 = rand_interval(4, dispWin.x2-4); - y1 = rand_interval(4, dispWin.y2-2); - x2 = rand_interval(4, dispWin.x2-4); - y2 = rand_interval(4, dispWin.y2-2); - x3 = rand_interval(4, dispWin.x2-4); - y3 = rand_interval(4, dispWin.y2-2); + x1 = rand_interval(4, tft_dispWin.x2-4); + y1 = rand_interval(4, tft_dispWin.y2-2); + x2 = rand_interval(4, tft_dispWin.x2-4); + y2 = rand_interval(4, tft_dispWin.y2-2); + x3 = rand_interval(4, tft_dispWin.x2-4); + y3 = rand_interval(4, tft_dispWin.y2-2); TFT_fillTriangle(x1,y1,x2,y2,x3,y3,random_color()); TFT_drawTriangle(x1,y1,x2,y2,x3,y3,random_color()); n++; @@ -908,8 +895,8 @@ static void poly_demo() disp_header("POLYGON DEMO"); - x = (dispWin.x2 - dispWin.x1) / 2; - y = (dispWin.y2 - dispWin.y1) / 2; + x = (tft_dispWin.x2 - tft_dispWin.x1) / 2; + y = (tft_dispWin.y2 - tft_dispWin.y1) / 2; rot = 0; oldrot = 0; @@ -922,7 +909,7 @@ static void poly_demo() TFT_drawPolygon(x, y, sides[i], r, TFT_BLACK, TFT_BLACK, oldrot, 1); TFT_drawPolygon(x, y, sides[i], r, color[i], color[i], rot, 1); r -= 16; - if (r <= 0) break; + if (r <= 0) { break; }; n += 2; } Wait(100); @@ -944,7 +931,7 @@ static void poly_demo() for (i=5; i>=0; i--) { TFT_drawPolygon(x, y, sides[i], r, color[i], fill[i], rot, 2); r -= 16; - if (r <= 0) break; + if (r <= 0) { break; } n += 2; } Wait(500); @@ -963,7 +950,7 @@ static void touch_demo() disp_header("TOUCH DEMO"); TFT_setFont(DEFAULT_FONT, NULL); - _fg = TFT_YELLOW; + tft_fg = TFT_YELLOW; TFT_print("Touch to draw", CENTER, 40); TFT_print("Touch footer to clear", CENTER, 60); @@ -972,11 +959,11 @@ static void touch_demo() while (1) { if (TFT_read_touch(&tx, &ty, 0)) { // Touched - if (((tx >= dispWin.x1) && (tx <= dispWin.x2)) && - ((ty >= dispWin.y1) && (ty <= dispWin.y2))) { + if (((tx >= tft_dispWin.x1) && (tx <= tft_dispWin.x2)) && + ((ty >= tft_dispWin.y1) && (ty <= tft_dispWin.y2))) { if ((doexit > 2) || ((abs(tx-ltx) < 5) && (abs(ty-lty) < 5))) { if (((abs(tx-ltx) > 0) || (abs(ty-lty) > 0))) { - TFT_fillCircle(tx-dispWin.x1, ty-dispWin.y1, 4,random_color()); + TFT_fillCircle(tx-tft_dispWin.x1, ty-tft_dispWin.y1, 4,random_color()); sprintf(tmp_buff, "%d,%d", tx, ty); update_header(NULL, tmp_buff); } @@ -985,19 +972,19 @@ static void touch_demo() } doexit = 0; } - else if (ty > (dispWin.y2+5)) TFT_fillWindow(TFT_BLACK); + else if (ty > (tft_dispWin.y2+5)) TFT_fillWindow(TFT_BLACK); else { doexit++; if (doexit == 2) update_header(NULL, "---"); if (doexit > 50) return; - vTaskDelay(100 / portTICK_RATE_MS); + vTaskDelay(100 / portTICK_PERIOD_MS); } } else { doexit++; if (doexit == 2) update_header(NULL, "---"); if (doexit > 50) return; - vTaskDelay(100 / portTICK_RATE_MS); + vTaskDelay(100 / portTICK_PERIOD_MS); } } #endif @@ -1007,13 +994,13 @@ static void touch_demo() //=============== void tft_demo() { - font_rotate = 0; - text_wrap = 0; - font_transparent = 0; - font_forceFixed = 0; + tft_font_rotate = 0; + tft_text_wrap = 0; + tft_font_transparent = 0; + tft_font_forceFixed = 0; TFT_resetclipwin(); - image_debug = 0; + tft_image_debug = 0; char dtype[16]; @@ -1042,22 +1029,22 @@ void tft_demo() { uint8_t disp_rot = PORTRAIT; _demo_pass = 0; - gray_scale = 0; + tft_gray_scale = 0; doprint = 1; TFT_setRotation(disp_rot); disp_header("ESP32 TFT DEMO"); TFT_setFont(COMIC24_FONT, NULL); int tempy = TFT_getfontheight() + 4; - _fg = TFT_ORANGE; - TFT_print("ESP32", CENTER, (dispWin.y2-dispWin.y1)/2 - tempy); + tft_fg = TFT_ORANGE; + TFT_print("ESP32", CENTER, (tft_dispWin.y2-tft_dispWin.y1)/2 - tempy); TFT_setFont(UBUNTU16_FONT, NULL); - _fg = TFT_CYAN; + tft_fg = TFT_CYAN; TFT_print("TFT Demo", CENTER, LASTY+tempy); tempy = TFT_getfontheight() + 4; TFT_setFont(DEFAULT_FONT, NULL); - _fg = TFT_GREEN; - sprintf(tmp_buff, "Read speed: %5.2f MHz", (float)max_rdclock/1000000.0); + tft_fg = TFT_GREEN; + sprintf(tmp_buff, "Read speed: %5.2f MHz", (float)tft_max_rdclock/1000000.0); TFT_print(tmp_buff, CENTER, LASTY+tempy); Wait(4000); @@ -1066,10 +1053,10 @@ void tft_demo() { if (run_gs_demo) { if (_demo_pass == 8) doprint = 0; // Change gray scale mode on every 2nd pass - gray_scale = _demo_pass & 1; + tft_gray_scale = _demo_pass & 1; // change display rotation if ((_demo_pass % 2) == 0) { - _bg = TFT_BLACK; + tft_bg = TFT_BLACK; TFT_setRotation(disp_rot); disp_rot++; disp_rot &= 3; @@ -1078,7 +1065,7 @@ void tft_demo() { else { if (_demo_pass == 4) doprint = 0; // change display rotation - _bg = TFT_BLACK; + tft_bg = TFT_BLACK; TFT_setRotation(disp_rot); disp_rot++; disp_rot &= 3; @@ -1090,7 +1077,7 @@ void tft_demo() { if (disp_rot == 3) sprintf(tmp_buff, "PORTRAIT FLIP"); if (disp_rot == 0) sprintf(tmp_buff, "LANDSCAPE FLIP"); printf("\r\n==========================================\r\nDisplay: %s: %s %d,%d %s\r\n\r\n", - dtype, tmp_buff, _width, _height, ((gray_scale) ? "Gray" : "Color")); + dtype, tmp_buff, tft_width, tft_height, ((tft_gray_scale) ? "Gray" : "Color")); } disp_header("Welcome to ESP32"); @@ -1249,29 +1236,11 @@ void app_main() // === SET GLOBAL VARIABLES ========================== - // =================================================== - // ==== Set display type ===== - tft_disp_type = DEFAULT_DISP_TYPE; - //tft_disp_type = DISP_TYPE_ILI9341; - //tft_disp_type = DISP_TYPE_ILI9488; - //tft_disp_type = DISP_TYPE_ST7735B; - // =================================================== - - // =================================================== - // === Set display resolution if NOT using default === - // === DEFAULT_TFT_DISPLAY_WIDTH & === - // === DEFAULT_TFT_DISPLAY_HEIGHT === - _width = DEFAULT_TFT_DISPLAY_WIDTH; // smaller dimension - _height = DEFAULT_TFT_DISPLAY_HEIGHT; // larger dimension - //_width = 128; // smaller dimension - //_height = 160; // larger dimension - // =================================================== - // =================================================== // ==== Set maximum spi clock for display read ==== // operations, function 'find_rd_speed()' ==== // can be used after display initialization ==== - max_rdclock = 8000000; + tft_max_rdclock = 8000000; // =================================================== // ==================================================================== @@ -1324,7 +1293,7 @@ void app_main() // ==================================================================================================================== - vTaskDelay(500 / portTICK_RATE_MS); + vTaskDelay(500 / portTICK_PERIOD_MS); printf("\r\n==============================\r\n"); printf("TFT display DEMO, LoBo 11/2017\r\n"); printf("==============================\r\n"); @@ -1340,7 +1309,7 @@ void app_main() ret=spi_lobo_bus_add_device(SPI_BUS, &buscfg, &devcfg, &spi); assert(ret==ESP_OK); printf("SPI: display device added to spi bus (%d)\r\n", SPI_BUS); - disp_spi = spi; + tft_disp_spi = spi; // ==== Test select/deselect ==== ret = spi_lobo_device_select(spi, 1); @@ -1358,7 +1327,7 @@ void app_main() ret=spi_lobo_bus_add_device(SPI_BUS, &buscfg, &tsdevcfg, &tsspi); assert(ret==ESP_OK); printf("SPI: touch screen device added to spi bus (%d)\r\n", SPI_BUS); - ts_spi = tsspi; + tft_ts_spi = tsspi; // ==== Test select/deselect ==== ret = spi_lobo_device_select(tsspi, 1); @@ -1374,17 +1343,20 @@ void app_main() printf("SPI: display init...\r\n"); TFT_display_init(); +#ifdef TFT_START_COLORS_INVERTED + TFT_invertDisplay(1); +#endif printf("OK\r\n"); #if USE_TOUCH == TOUCH_TYPE_STMPE610 stmpe610_Init(); - vTaskDelay(10 / portTICK_RATE_MS); + vTaskDelay(10 / portTICK_PERIOD_MS); uint32_t tver = stmpe610_getID(); printf("STMPE touch initialized, ver: %04x - %02x\r\n", tver >> 8, tver & 0xFF); #endif // ---- Detect maximum read speed ---- - max_rdclock = find_rd_speed(); - printf("SPI: Max rd speed = %u\r\n", max_rdclock); + tft_max_rdclock = find_rd_speed(); + printf("SPI: Max rd speed = %u\r\n", tft_max_rdclock); // ==== Set SPI clock used for display operations ==== spi_lobo_set_speed(spi, DEFAULT_SPI_CLOCK); @@ -1394,11 +1366,11 @@ void app_main() printf("Graphics demo started\r\n"); printf("---------------------\r\n"); - font_rotate = 0; - text_wrap = 0; - font_transparent = 0; - font_forceFixed = 0; - gray_scale = 0; + tft_font_rotate = 0; + tft_text_wrap = 0; + tft_font_transparent = 0; + tft_font_forceFixed = 0; + tft_gray_scale = 0; TFT_setGammaCurve(DEFAULT_GAMMA_CURVE); TFT_setRotation(PORTRAIT); TFT_setFont(DEFAULT_FONT, NULL); @@ -1421,18 +1393,18 @@ void app_main() // Is time set? If not, tm_year will be (1970 - 1900). if (tm_info->tm_year < (2016 - 1900)) { ESP_LOGI(tag, "Time is not set yet. Connecting to WiFi and getting time over NTP."); - _fg = TFT_CYAN; + tft_fg = TFT_CYAN; TFT_print("Time is not set yet", CENTER, CENTER); TFT_print("Connecting to WiFi", CENTER, LASTY+TFT_getfontheight()+2); TFT_print("Getting time over NTP", CENTER, LASTY+TFT_getfontheight()+2); - _fg = TFT_YELLOW; + tft_fg = TFT_YELLOW; TFT_print("Wait", CENTER, LASTY+TFT_getfontheight()+2); if (obtain_time()) { - _fg = TFT_GREEN; + tft_fg = TFT_GREEN; TFT_print("System time is set.", CENTER, LASTY); } else { - _fg = TFT_RED; + tft_fg = TFT_RED; TFT_print("ERROR.", CENTER, LASTY); } time(&time_now); @@ -1442,19 +1414,20 @@ void app_main() #endif disp_header("File system INIT"); - _fg = TFT_CYAN; + tft_fg = TFT_CYAN; TFT_print("Initializing SPIFFS...", CENTER, CENTER); // ==== Initialize the file system ==== printf("\r\n\n"); vfs_spiffs_register(); if (!spiffs_is_mounted) { - _fg = TFT_RED; + tft_fg = TFT_RED; TFT_print("SPIFFS not mounted !", CENTER, LASTY+TFT_getfontheight()+2); } else { - _fg = TFT_GREEN; + tft_fg = TFT_GREEN; TFT_print("SPIFFS Mounted.", CENTER, LASTY+TFT_getfontheight()+2); } + Wait(-2000); //=========