TFT_eSPI.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /***************************************************
  2. Arduino TFT graphics library targeted at ESP8266
  3. and ESP32 based boards.
  4. This is a stand-alone library that contains the
  5. hardware driver, the graphics functions and the
  6. proportional fonts.
  7. The built-in fonts 4, 6, 7 and 8 are Run Length
  8. Encoded (RLE) to reduce the FLASH footprint.
  9. Last review/edit by Bodmer: 21/04/21
  10. ****************************************************/
  11. // Stop fonts etc being loaded multiple times
  12. #ifndef _TFT_eSPIH_
  13. #define _TFT_eSPIH_
  14. #define TFT_ESPI_VERSION "2.3.70"
  15. // Bit level feature flags
  16. // Bit 0 set: viewport capability
  17. #define TFT_ESPI_FEATURES 1
  18. /***************************************************************************************
  19. ** Section 1: Load required header files
  20. ***************************************************************************************/
  21. //Standard support
  22. #include <Arduino.h>
  23. #include <Print.h>
  24. #include <SPI.h>
  25. #define __FS_LITTLEFS__
  26. #ifdef __FS_LITTLEFS__
  27. #include <FS.h>
  28. #if defined(ESP8266)
  29. #include <LittleFS.h>
  30. #elif defined(ESP32)
  31. #include <LITTLEFS.h>
  32. #endif
  33. #endif
  34. /***************************************************************************************
  35. ** Section 2: Load library and processor specific header files
  36. ***************************************************************************************/
  37. // Include header file that defines the fonts loaded, the TFT drivers
  38. // available and the pins to be used, etc, etc
  39. #include <User_Setup_Select.h>
  40. // Handle FLASH based storage e.g. PROGMEM
  41. #if defined(ARDUINO_ARCH_RP2040)
  42. #undef pgm_read_byte
  43. #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  44. #undef pgm_read_word
  45. #define pgm_read_word(addr) ({ \
  46. typeof(addr) _addr = (addr); \
  47. *(const unsigned short *)(_addr); \
  48. })
  49. #undef pgm_read_dword
  50. #define pgm_read_dword(addr) ({ \
  51. typeof(addr) _addr = (addr); \
  52. *(const unsigned long *)(_addr); \
  53. })
  54. #elif defined(__AVR__)
  55. #include <avr/pgmspace.h>
  56. #elif defined(ESP8266) || defined(ESP32)
  57. #include <pgmspace.h>
  58. #else
  59. #define PROGMEM
  60. #endif
  61. // Include the processor specific drivers
  62. #if defined (ESP32)
  63. #include "Processors/TFT_eSPI_ESP32.h"
  64. #elif defined (ESP8266)
  65. #include "Processors/TFT_eSPI_ESP8266.h"
  66. #elif defined (STM32)
  67. #include "Processors/TFT_eSPI_STM32.h"
  68. #elif defined(ARDUINO_ARCH_RP2040)
  69. #include "Processors/TFT_eSPI_RP2040.h"
  70. #else
  71. #include "Processors/TFT_eSPI_Generic.h"
  72. #endif
  73. /***************************************************************************************
  74. ** Section 3: Interface setup
  75. ***************************************************************************************/
  76. #ifndef TAB_COLOUR
  77. #define TAB_COLOUR 0
  78. #endif
  79. // If the SPI frequency is not defined, set a default
  80. #ifndef SPI_FREQUENCY
  81. #define SPI_FREQUENCY 20000000
  82. #endif
  83. // If the SPI read frequency is not defined, set a default
  84. #ifndef SPI_READ_FREQUENCY
  85. #define SPI_READ_FREQUENCY 10000000
  86. #endif
  87. // Some ST7789 boards do not work with Mode 0
  88. #if defined(ST7789_DRIVER) || defined(ST7789_2_DRIVER)
  89. #define TFT_SPI_MODE SPI_MODE3
  90. #else
  91. #define TFT_SPI_MODE SPI_MODE0
  92. #endif
  93. // If the XPT2046 SPI frequency is not defined, set a default
  94. #ifndef SPI_TOUCH_FREQUENCY
  95. #define SPI_TOUCH_FREQUENCY 2500000
  96. #endif
  97. #ifndef SPI_BUSY_CHECK
  98. #define SPI_BUSY_CHECK
  99. #endif
  100. /***************************************************************************************
  101. ** Section 4: Setup fonts
  102. ***************************************************************************************/
  103. // Use GLCD font in error case where user requests a smooth font file
  104. // that does not exist (this is a temporary fix to stop ESP32 reboot)
  105. #ifdef SMOOTH_FONT
  106. #ifndef LOAD_GLCD
  107. #define LOAD_GLCD
  108. #endif
  109. #endif
  110. // Only load the fonts defined in User_Setup.h (to save space)
  111. // Set flag so RLE rendering code is optionally compiled
  112. #ifdef LOAD_GLCD
  113. #include <Fonts/glcdfont.c>
  114. #endif
  115. #ifdef LOAD_FONT2
  116. #include <Fonts/Font16.h>
  117. #endif
  118. #ifdef LOAD_FONT4
  119. #include <Fonts/Font32rle.h>
  120. #define LOAD_RLE
  121. #endif
  122. #ifdef LOAD_FONT6
  123. #include <Fonts/Font64rle.h>
  124. #ifndef LOAD_RLE
  125. #define LOAD_RLE
  126. #endif
  127. #endif
  128. #ifdef LOAD_FONT7
  129. #include <Fonts/Font7srle.h>
  130. #ifndef LOAD_RLE
  131. #define LOAD_RLE
  132. #endif
  133. #endif
  134. #ifdef LOAD_FONT8
  135. #include <Fonts/Font72rle.h>
  136. #ifndef LOAD_RLE
  137. #define LOAD_RLE
  138. #endif
  139. #elif defined LOAD_FONT8N // Optional narrower version
  140. #define LOAD_FONT8
  141. #include <Fonts/Font72x53rle.h>
  142. #ifndef LOAD_RLE
  143. #define LOAD_RLE
  144. #endif
  145. #endif
  146. #ifdef LOAD_GFXFF
  147. // We can include all the free fonts and they will only be built into
  148. // the sketch if they are used
  149. #include <Fonts/GFXFF/gfxfont.h>
  150. // Call up any user custom fonts
  151. #include <User_Setups/User_Custom_Fonts.h>
  152. #endif // #ifdef LOAD_GFXFF
  153. // Create a null default font in case some fonts not used (to prevent crash)
  154. const uint8_t widtbl_null[1] = {0};
  155. PROGMEM const uint8_t chr_null[1] = {0};
  156. PROGMEM const uint8_t* const chrtbl_null[1] = {chr_null};
  157. // This is a structure to conveniently hold information on the default fonts
  158. // Stores pointer to font character image address table, width table and height
  159. typedef struct {
  160. const uint8_t *chartbl;
  161. const uint8_t *widthtbl;
  162. uint8_t height;
  163. uint8_t baseline;
  164. } fontinfo;
  165. // Now fill the structure
  166. const PROGMEM fontinfo fontdata [] = {
  167. #ifdef LOAD_GLCD
  168. { (const uint8_t *)font, widtbl_null, 0, 0 },
  169. #else
  170. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  171. #endif
  172. // GLCD font (Font 1) does not have all parameters
  173. { (const uint8_t *)chrtbl_null, widtbl_null, 8, 7 },
  174. #ifdef LOAD_FONT2
  175. { (const uint8_t *)chrtbl_f16, widtbl_f16, chr_hgt_f16, baseline_f16},
  176. #else
  177. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  178. #endif
  179. // Font 3 current unused
  180. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  181. #ifdef LOAD_FONT4
  182. { (const uint8_t *)chrtbl_f32, widtbl_f32, chr_hgt_f32, baseline_f32},
  183. #else
  184. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  185. #endif
  186. // Font 5 current unused
  187. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  188. #ifdef LOAD_FONT6
  189. { (const uint8_t *)chrtbl_f64, widtbl_f64, chr_hgt_f64, baseline_f64},
  190. #else
  191. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  192. #endif
  193. #ifdef LOAD_FONT7
  194. { (const uint8_t *)chrtbl_f7s, widtbl_f7s, chr_hgt_f7s, baseline_f7s},
  195. #else
  196. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
  197. #endif
  198. #ifdef LOAD_FONT8
  199. { (const uint8_t *)chrtbl_f72, widtbl_f72, chr_hgt_f72, baseline_f72}
  200. #else
  201. { (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 }
  202. #endif
  203. };
  204. /***************************************************************************************
  205. ** Section 5: Font datum enumeration
  206. ***************************************************************************************/
  207. //These enumerate the text plotting alignment (reference datum point)
  208. #define TL_DATUM 0 // Top left (default)
  209. #define TC_DATUM 1 // Top centre
  210. #define TR_DATUM 2 // Top right
  211. #define ML_DATUM 3 // Middle left
  212. #define CL_DATUM 3 // Centre left, same as above
  213. #define MC_DATUM 4 // Middle centre
  214. #define CC_DATUM 4 // Centre centre, same as above
  215. #define MR_DATUM 5 // Middle right
  216. #define CR_DATUM 5 // Centre right, same as above
  217. #define BL_DATUM 6 // Bottom left
  218. #define BC_DATUM 7 // Bottom centre
  219. #define BR_DATUM 8 // Bottom right
  220. #define L_BASELINE 9 // Left character baseline (Line the 'A' character would sit on)
  221. #define C_BASELINE 10 // Centre character baseline
  222. #define R_BASELINE 11 // Right character baseline
  223. /***************************************************************************************
  224. ** Section 6: Colour enumeration
  225. ***************************************************************************************/
  226. // Default color definitions
  227. #define TFT_BLACK 0x0000 /* 0, 0, 0 */
  228. #define TFT_NAVY 0x000F /* 0, 0, 128 */
  229. #define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
  230. #define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
  231. #define TFT_MAROON 0x7800 /* 128, 0, 0 */
  232. #define TFT_PURPLE 0x780F /* 128, 0, 128 */
  233. #define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
  234. #define TFT_LIGHTGREY 0xD69A /* 211, 211, 211 */
  235. #define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
  236. #define TFT_BLUE 0x001F /* 0, 0, 255 */
  237. #define TFT_GREEN 0x07E0 /* 0, 255, 0 */
  238. #define TFT_CYAN 0x07FF /* 0, 255, 255 */
  239. #define TFT_RED 0xF800 /* 255, 0, 0 */
  240. #define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
  241. #define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
  242. #define TFT_WHITE 0xFFFF /* 255, 255, 255 */
  243. #define TFT_ORANGE 0xFDA0 /* 255, 180, 0 */
  244. #define TFT_GREENYELLOW 0xB7E0 /* 180, 255, 0 */
  245. #define TFT_PINK 0xFE19 /* 255, 192, 203 */ //Lighter pink, was 0xFC9F
  246. #define TFT_BROWN 0x9A60 /* 150, 75, 0 */
  247. #define TFT_GOLD 0xFEA0 /* 255, 215, 0 */
  248. #define TFT_SILVER 0xC618 /* 192, 192, 192 */
  249. #define TFT_SKYBLUE 0x867D /* 135, 206, 235 */
  250. #define TFT_VIOLET 0x915C /* 180, 46, 226 */
  251. // Next is a special 16 bit colour value that encodes to 8 bits
  252. // and will then decode back to the same 16 bit value.
  253. // Convenient for 8 bit and 16 bit transparent sprites.
  254. #define TFT_TRANSPARENT 0x0120 // This is actually a dark green
  255. // Default palette for 4 bit colour sprites
  256. static const uint16_t default_4bit_palette[] PROGMEM = {
  257. TFT_BLACK, // 0 ^
  258. TFT_BROWN, // 1 |
  259. TFT_RED, // 2 |
  260. TFT_ORANGE, // 3 |
  261. TFT_YELLOW, // 4 Colours 0-9 follow the resistor colour code!
  262. TFT_GREEN, // 5 |
  263. TFT_BLUE, // 6 |
  264. TFT_PURPLE, // 7 |
  265. TFT_DARKGREY, // 8 |
  266. TFT_WHITE, // 9 v
  267. TFT_CYAN, // 10 Blue+green mix
  268. TFT_MAGENTA, // 11 Blue+red mix
  269. TFT_MAROON, // 12 Darker red colour
  270. TFT_DARKGREEN,// 13 Darker green colour
  271. TFT_NAVY, // 14 Darker blue colour
  272. TFT_PINK // 15
  273. };
  274. /***************************************************************************************
  275. ** Section 7: Diagnostic support
  276. ***************************************************************************************/
  277. // #define TFT_eSPI_DEBUG // Switch on debug support serial messages (not used yet)
  278. // #define TFT_eSPI_FNx_DEBUG // Switch on debug support for function "x" (not used yet)
  279. // This structure allows sketches to retrieve the user setup parameters at runtime
  280. // by calling getSetup(), zero impact on code size unless used, mainly for diagnostics
  281. typedef struct
  282. {
  283. String version = TFT_ESPI_VERSION;
  284. int32_t esp; // Processor code
  285. uint8_t trans; // SPI transaction support
  286. uint8_t serial; // Serial (SPI) or parallel
  287. uint8_t overlap; // ESP8266 overlap mode
  288. #if defined (ESP32) // TODO: make generic for other processors
  289. #if defined (USE_HSPI_PORT)
  290. uint8_t port = HSPI;
  291. #else
  292. uint8_t port = VSPI;
  293. #endif
  294. #endif
  295. uint16_t tft_driver; // Hexadecimal code
  296. uint16_t tft_width; // Rotation 0 width and height
  297. uint16_t tft_height;
  298. uint8_t r0_x_offset; // Display offsets, not all used yet
  299. uint8_t r0_y_offset;
  300. uint8_t r1_x_offset;
  301. uint8_t r1_y_offset;
  302. uint8_t r2_x_offset;
  303. uint8_t r2_y_offset;
  304. uint8_t r3_x_offset;
  305. uint8_t r3_y_offset;
  306. int8_t pin_tft_mosi; // SPI pins
  307. int8_t pin_tft_miso;
  308. int8_t pin_tft_clk;
  309. int8_t pin_tft_cs;
  310. int8_t pin_tft_dc; // Control pins
  311. int8_t pin_tft_rd;
  312. int8_t pin_tft_wr;
  313. int8_t pin_tft_rst;
  314. int8_t pin_tft_d0; // Parallel port pins
  315. int8_t pin_tft_d1;
  316. int8_t pin_tft_d2;
  317. int8_t pin_tft_d3;
  318. int8_t pin_tft_d4;
  319. int8_t pin_tft_d5;
  320. int8_t pin_tft_d6;
  321. int8_t pin_tft_d7;
  322. int8_t pin_tft_led;
  323. int8_t pin_tft_led_on;
  324. int8_t pin_tch_cs; // Touch chip select pin
  325. int16_t tft_spi_freq;// TFT write SPI frequency
  326. int16_t tft_rd_freq; // TFT read SPI frequency
  327. int16_t tch_spi_freq;// Touch controller read/write SPI frequency
  328. } setup_t;
  329. /***************************************************************************************
  330. ** Section 8: Class member and support functions
  331. ***************************************************************************************/
  332. // Swap any type
  333. template <typename T> static inline void
  334. swap_coord(T& a, T& b) { T t = a; a = b; b = t; }
  335. // Callback prototype for smooth font pixel colour read
  336. typedef uint16_t (*getColorCallback)(uint16_t x, uint16_t y);
  337. // Class functions and variables
  338. class TFT_eSPI : public Print { friend class TFT_eSprite; // Sprite class has access to protected members
  339. //--------------------------------------- public ------------------------------------//
  340. public:
  341. TFT_eSPI(int16_t _W = TFT_WIDTH, int16_t _H = TFT_HEIGHT);
  342. // init() and begin() are equivalent, begin() included for backwards compatibility
  343. // Sketch defined tab colour option is for ST7735 displays only
  344. void init(uint8_t tc = TAB_COLOUR), begin(uint8_t tc = TAB_COLOUR);
  345. // These are virtual so the TFT_eSprite class can override them with sprite specific functions
  346. virtual void drawPixel(int32_t x, int32_t y, uint32_t color),
  347. drawChar(int32_t x, int32_t y, uint16_t c, uint32_t color, uint32_t bg, uint8_t size),
  348. drawLine(int32_t xs, int32_t ys, int32_t xe, int32_t ye, uint32_t color),
  349. drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color),
  350. drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color),
  351. fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
  352. virtual int16_t drawChar(uint16_t uniCode, int32_t x, int32_t y, uint8_t font),
  353. drawChar(uint16_t uniCode, int32_t x, int32_t y),
  354. height(void),
  355. width(void);
  356. void setRotation(uint8_t r); // Set the display image orientation to 0, 1, 2 or 3
  357. uint8_t getRotation(void); // Read the current rotation
  358. void invertDisplay(bool i); // Tell TFT to invert all displayed colours
  359. // The TFT_eSprite class inherits the following functions (not all are useful to Sprite class
  360. void setAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h), // Note: start coordinates + width and height
  361. setWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye); // Note: start + end coordinates
  362. // Viewport commands, see "Viewport_Demo" sketch
  363. void setViewport(int32_t x, int32_t y, int32_t w, int32_t h, bool vpDatum = true);
  364. bool checkViewport(int32_t x, int32_t y, int32_t w, int32_t h);
  365. int32_t getViewportX(void);
  366. int32_t getViewportY(void);
  367. int32_t getViewportWidth(void);
  368. int32_t getViewportHeight(void);
  369. bool getViewportDatum(void);
  370. void frameViewport(uint16_t color, int32_t w);
  371. void resetViewport(void);
  372. // Push (aka write pixel) colours to the TFT (use setAddrWindow() first)
  373. void pushColor(uint16_t color),
  374. pushColor(uint16_t color, uint32_t len), // Deprecated, use pushBlock()
  375. pushColors(uint16_t *data, uint32_t len, bool swap = true), // With byte swap option
  376. pushColors(uint8_t *data, uint32_t len); // Deprecated, use pushPixels()
  377. // Write a solid block of a single colour
  378. void pushBlock(uint16_t color, uint32_t len);
  379. // Write a set of pixels stored in memory, use setSwapBytes(true/false) function to correct endianess
  380. void pushPixels(const void * data_in, uint32_t len);
  381. // Read the colour of a pixel at x,y and return value in 565 format
  382. uint16_t readPixel(int32_t x, int32_t y);
  383. // Support for half duplex (bi-directional SDA) SPI bus where MOSI must be switched to input
  384. #ifdef TFT_SDA_READ
  385. #if defined (TFT_eSPI_ENABLE_8_BIT_READ)
  386. uint8_t tft_Read_8(void); // Read 8 bit value from TFT command register
  387. #endif
  388. void begin_SDA_Read(void); // Begin a read on a half duplex (bi-directional SDA) SPI bus - sets MOSI to input
  389. void end_SDA_Read(void); // Restore MOSI to output
  390. #endif
  391. // Graphics drawing
  392. void fillScreen(uint32_t color),
  393. drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color),
  394. drawRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color),
  395. fillRoundRect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t radius, uint32_t color);
  396. void drawCircle(int32_t x, int32_t y, int32_t r, uint32_t color),
  397. drawCircleHelper(int32_t x, int32_t y, int32_t r, uint8_t cornername, uint32_t color),
  398. fillCircle(int32_t x, int32_t y, int32_t r, uint32_t color),
  399. fillCircleHelper(int32_t x, int32_t y, int32_t r, uint8_t cornername, int32_t delta, uint32_t color),
  400. drawEllipse(int16_t x, int16_t y, int32_t rx, int32_t ry, uint16_t color),
  401. fillEllipse(int16_t x, int16_t y, int32_t rx, int32_t ry, uint16_t color),
  402. // Corner 1 Corner 2 Corner 3
  403. drawTriangle(int32_t x1,int32_t y1, int32_t x2,int32_t y2, int32_t x3,int32_t y3, uint32_t color),
  404. fillTriangle(int32_t x1,int32_t y1, int32_t x2,int32_t y2, int32_t x3,int32_t y3, uint32_t color);
  405. // Image rendering
  406. // Swap the byte order for pushImage() and pushPixels() - corrects endianness
  407. void setSwapBytes(bool swap);
  408. bool getSwapBytes(void);
  409. // Draw bitmap
  410. void drawBitmap( int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor),
  411. drawBitmap( int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor),
  412. drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor),
  413. drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor),
  414. setBitmapColor(uint16_t fgcolor, uint16_t bgcolor); // Define the 2 colours for 1bpp sprites
  415. // Set TFT pivot point (use when rendering rotated sprites)
  416. void setPivot(int16_t x, int16_t y);
  417. int16_t getPivotX(void), // Get pivot x
  418. getPivotY(void); // Get pivot y
  419. // The next functions can be used as a pair to copy screen blocks (or horizontal/vertical lines) to another location
  420. // Read a block of pixels to a data buffer, buffer is 16 bit and the size must be at least w * h
  421. void readRect(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data);
  422. // Write a block of pixels to the screen which have been read by readRect()
  423. void pushRect(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data);
  424. // These are used to render images or sprites stored in RAM arrays (used by Sprite class for 16bpp Sprites)
  425. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data);
  426. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data, uint16_t transparent);
  427. // These are used to render images stored in FLASH (PROGMEM)
  428. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data, uint16_t transparent);
  429. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data);
  430. // These are used by Sprite class pushSprite() member function for 1, 4 and 8 bits per pixel (bpp) colours
  431. // They are not intended to be used with user sketches (but could be)
  432. // Set bpp8 true for 8bpp sprites, false otherwise. The cmap pointer must be specified for 4bpp
  433. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t *data, bool bpp8 = true, uint16_t *cmap = nullptr);
  434. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t *data, uint8_t transparent, bool bpp8 = true, uint16_t *cmap = nullptr);
  435. // FLASH version
  436. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint8_t *data, bool bpp8, uint16_t *cmap = nullptr);
  437. // This next function has been used successfully to dump the TFT screen to a PC for documentation purposes
  438. // It reads a screen area and returns the 3 RGB 8 bit colour values of each pixel in the buffer
  439. // Set w and h to 1 to read 1 pixel's colour. The data buffer must be at least w * h * 3 bytes
  440. void readRectRGB(int32_t x, int32_t y, int32_t w, int32_t h, uint8_t *data);
  441. // Text rendering - value returned is the pixel width of the rendered text
  442. int16_t drawNumber(long intNumber, int32_t x, int32_t y, uint8_t font), // Draw integer using specified font number
  443. drawNumber(long intNumber, int32_t x, int32_t y), // Draw integer using current font
  444. // Decimal is the number of decimal places to render
  445. // Use with setTextDatum() to position values on TFT, and setTextPadding() to blank old displayed values
  446. drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y, uint8_t font), // Draw float using specified font number
  447. drawFloat(float floatNumber, uint8_t decimal, int32_t x, int32_t y), // Draw float using current font
  448. // Handle char arrays
  449. // Use with setTextDatum() to position string on TFT, and setTextPadding() to blank old displayed strings
  450. drawString(const char *string, int32_t x, int32_t y, uint8_t font), // Draw string using specified font number
  451. drawString(const char *string, int32_t x, int32_t y), // Draw string using current font
  452. drawString(const String& string, int32_t x, int32_t y, uint8_t font),// Draw string using specified font number
  453. drawString(const String& string, int32_t x, int32_t y), // Draw string using current font
  454. drawCentreString(const char *string, int32_t x, int32_t y, uint8_t font), // Deprecated, use setTextDatum() and drawString()
  455. drawRightString(const char *string, int32_t x, int32_t y, uint8_t font), // Deprecated, use setTextDatum() and drawString()
  456. drawCentreString(const String& string, int32_t x, int32_t y, uint8_t font),// Deprecated, use setTextDatum() and drawString()
  457. drawRightString(const String& string, int32_t x, int32_t y, uint8_t font); // Deprecated, use setTextDatum() and drawString()
  458. // Text rendering and font handling support funtions
  459. void setCursor(int16_t x, int16_t y), // Set cursor for tft.print()
  460. setCursor(int16_t x, int16_t y, uint8_t font); // Set cursor and font number for tft.print()
  461. int16_t getCursorX(void), // Read current cursor x position (moves with tft.print())
  462. getCursorY(void); // Read current cursor y position
  463. void setTextColor(uint16_t color), // Set character (glyph) color only (background not over-written)
  464. setTextColor(uint16_t fgcolor, uint16_t bgcolor),// Set character (glyph) foreground and backgorund colour
  465. setTextSize(uint8_t size); // Set character size multiplier (this increases pixel size)
  466. void setTextWrap(bool wrapX, bool wrapY = false); // Turn on/off wrapping of text in TFT width and/or height
  467. void setTextDatum(uint8_t datum); // Set text datum position (default is top left), see Section 6 above
  468. uint8_t getTextDatum(void);
  469. void setTextPadding(uint16_t x_width); // Set text padding (background blanking/over-write) width in pixels
  470. uint16_t getTextPadding(void); // Get text padding
  471. #ifdef LOAD_GFXFF
  472. void setFreeFont(const GFXfont *f = NULL), // Select the GFX Free Font
  473. setTextFont(uint8_t font); // Set the font number to use in future
  474. #else
  475. void setFreeFont(uint8_t font), // Not used, historical fix to prevent an error
  476. setTextFont(uint8_t font); // Set the font number to use in future
  477. #endif
  478. int16_t textWidth(const char *string, uint8_t font), // Returns pixel width of string in specified font
  479. textWidth(const char *string), // Returns pixel width of string in current font
  480. textWidth(const String& string, uint8_t font), // As above for String types
  481. textWidth(const String& string),
  482. fontHeight(int16_t font), // Returns pixel height of string in specified font
  483. fontHeight(void); // Returns pixel width of string in current font
  484. // Used by library and Smooth font class to extract Unicode point codes from a UTF8 encoded string
  485. uint16_t decodeUTF8(uint8_t *buf, uint16_t *index, uint16_t remaining),
  486. decodeUTF8(uint8_t c);
  487. // Support function to UTF8 decode and draw characters piped through print stream
  488. size_t write(uint8_t);
  489. // Used by Smooth font class to fetch a pixel colour for the anti-aliasing
  490. void setCallback(getColorCallback getCol);
  491. uint16_t fontsLoaded(void); // Each bit in returned value represents a font type that is loaded - used for debug/error handling only
  492. // Low level read/write
  493. void spiwrite(uint8_t); // legacy support only
  494. void writecommand(uint8_t c), // Send a command, function resets DC/RS high ready for data
  495. writedata(uint8_t d); // Send data with DC/RS set high
  496. void commandList(const uint8_t *addr); // Send a initialisation sequence to TFT stored in FLASH
  497. uint8_t readcommand8( uint8_t cmd_function, uint8_t index = 0); // read 8 bits from TFT
  498. uint16_t readcommand16(uint8_t cmd_function, uint8_t index = 0); // read 16 bits from TFT
  499. uint32_t readcommand32(uint8_t cmd_function, uint8_t index = 0); // read 32 bits from TFT
  500. // Colour conversion
  501. // Convert 8 bit red, green and blue to 16 bits
  502. uint16_t color565(uint8_t red, uint8_t green, uint8_t blue);
  503. // Convert 8 bit colour to 16 bits
  504. uint16_t color8to16(uint8_t color332);
  505. // Convert 16 bit colour to 8 bits
  506. uint8_t color16to8(uint16_t color565);
  507. // Convert 16 bit colour to/from 24 bit, R+G+B concatenated into LS 24 bits
  508. uint32_t color16to24(uint16_t color565);
  509. uint32_t color24to16(uint32_t color888);
  510. // Alpha blend 2 colours, see generic "alphaBlend_Test" example
  511. // alpha = 0 = 100% background colour
  512. // alpha = 255 = 100% foreground colour
  513. uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc);
  514. // 16 bit colour alphaBlend with alpha dither (dither reduces colour banding)
  515. uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc, uint8_t dither);
  516. // 24 bit colour alphaBlend with optional alpha dither
  517. uint32_t alphaBlend24(uint8_t alpha, uint32_t fgc, uint32_t bgc, uint8_t dither = 0);
  518. // DMA support functions - these are currently just for SPI writes when using the ESP32 or STM32 processors
  519. // Bear in mind DMA will only be of benefit in particular circumstances and can be tricky
  520. // to manage by noobs. The functions have however been designed to be noob friendly and
  521. // avoid a few DMA behaviour "gotchas".
  522. //
  523. // At best you will get a 2x TFT rendering performance improvement when using DMA because
  524. // this library handles the SPI bus so efficiently during normal (non DMA) transfers. The best
  525. // performance improvement scenario is the DMA transfer time is exactly the same as the time it
  526. // takes for the processor to prepare the next image buffer and initiate another DMA transfer.
  527. //
  528. // DMA transfer to the TFT is done while the processor moves on to handle other tasks. Bear
  529. // this in mind and watch out for "gotchas" like the image buffer going out of scope as the
  530. // processor leaves a function or its content being changed while the DMA engine is reading it.
  531. //
  532. // The compiler MAY change the implied scope of a buffer which has been set aside by creating
  533. // an array. For example a buffer defined before a "for-next" loop may get de-allocated when
  534. // the loop ends. To avoid this use, for example, malloc() and free() to take control of when
  535. // the buffer space is available and ensure it is not released until DMA is complete.
  536. //
  537. // Clearly you should not modify a buffer that is being DMA'ed to the TFT until the DMA is over.
  538. // Use the dmaBusy() function to check this. Use tft.startWrite() before invoking DMA so the
  539. // TFT chip select stays low. If you use tft.endWrite() before DMA is complete then the endWrite
  540. // function will wait for the DMA to complete, so this may defeat any DMA performance benefit.
  541. //
  542. bool initDMA(bool ctrl_cs = false); // Initialise the DMA engine and attach to SPI bus - typically used in setup()
  543. // Parameter "true" enables DMA engine control of TFT chip select (ESP32 only)
  544. // For ESP32 only, TFT reads will not work if parameter is true
  545. void deInitDMA(void); // De-initialise the DMA engine and detach from SPI bus - typically not used
  546. // Push an image to the TFT using DMA, buffer is optional and grabs (double buffers) a copy of the image
  547. // Use the buffer if the image data will get over-written or destroyed while DMA is in progress
  548. // If swapping colour bytes is defined, and the double buffer option is NOT used, then the bytes
  549. // in the original data image will be swapped by the function before DMA is initiated.
  550. // The function will wait for the last DMA to complete if it is called while a previous DMA is still
  551. // in progress, this simplifies the sketch and helps avoid "gotchas".
  552. void pushImageDMA(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t* data, uint16_t* buffer = nullptr);
  553. // Push a block of pixels into a window set up using setAddrWindow()
  554. void pushPixelsDMA(uint16_t* image, uint32_t len);
  555. // Check if the DMA is complete - use while(tft.dmaBusy); for a blocking wait
  556. bool dmaBusy(void); // returns true if DMA is still in progress
  557. void dmaWait(void); // wait until DMA is complete
  558. bool DMA_Enabled = false; // Flag for DMA enabled state
  559. uint8_t spiBusyCheck = 0; // Number of ESP32 transfer buffers to check
  560. // Bare metal functions
  561. void startWrite(void); // Begin SPI transaction
  562. void writeColor(uint16_t color, uint32_t len); // Deprecated, use pushBlock()
  563. void endWrite(void); // End SPI transaction
  564. // Set/get an arbitrary library configuration attribute or option
  565. // Use to switch ON/OFF capabilities such as UTF8 decoding - each attribute has a unique ID
  566. // id = 0: reserved - may be used in future to reset all attributes to a default state
  567. // id = 1: Turn on (a=true) or off (a=false) GLCD cp437 font character error correction
  568. // id = 2: Turn on (a=true) or off (a=false) UTF8 decoding
  569. // id = 3: Enable or disable use of ESP32 PSRAM (if available)
  570. #define CP437_SWITCH 1
  571. #define UTF8_SWITCH 2
  572. #define PSRAM_ENABLE 3
  573. void setAttribute(uint8_t id = 0, uint8_t a = 0); // Set attribute value
  574. uint8_t getAttribute(uint8_t id = 0); // Get attribute value
  575. // Used for diagnostic sketch to see library setup adopted by compiler, see Section 7 above
  576. void getSetup(setup_t& tft_settings); // Sketch provides the instance to populate
  577. // Global variables
  578. static SPIClass& getSPIinstance(void); // Get SPI class handle
  579. uint32_t textcolor, textbgcolor; // Text foreground and background colours
  580. uint32_t bitmap_fg, bitmap_bg; // Bitmap foreground (bit=1) and background (bit=0) colours
  581. uint8_t textfont, // Current selected font number
  582. textsize, // Current font size multiplier
  583. textdatum, // Text reference datum
  584. rotation; // Display rotation (0-3)
  585. uint8_t decoderState = 0; // UTF8 decoder state - not for user access
  586. uint16_t decoderBuffer; // Unicode code-point buffer - not for user access
  587. //--------------------------------------- private ------------------------------------//
  588. private:
  589. // Legacy begin and end prototypes - deprecated TODO: delete
  590. void spi_begin();
  591. void spi_end();
  592. void spi_begin_read();
  593. void spi_end_read();
  594. // New begin and end prototypes
  595. // begin/end a TFT write transaction
  596. // For SPI bus the transmit clock rate is set
  597. inline void begin_tft_write() __attribute__((always_inline));
  598. inline void end_tft_write() __attribute__((always_inline));
  599. // begin/end a TFT read transaction
  600. // For SPI bus: begin lowers SPI clock rate, end reinstates transmit clock rate
  601. inline void begin_tft_read() __attribute__((always_inline));
  602. inline void end_tft_read() __attribute__((always_inline));
  603. // Temporary library development function TODO: remove need for this
  604. void pushSwapBytePixels(const void* data_in, uint32_t len);
  605. // Same as setAddrWindow but exits with CGRAM in read mode
  606. void readAddrWindow(int32_t xs, int32_t ys, int32_t w, int32_t h);
  607. // Byte read prototype
  608. uint8_t readByte(void);
  609. // GPIO parallel bus input/output direction control
  610. void busDir(uint32_t mask, uint8_t mode);
  611. // Single GPIO input/output direction control
  612. void gpioMode(uint8_t gpio, uint8_t mode);
  613. // Display variant settings
  614. uint8_t tabcolor, // ST7735 screen protector "tab" colour (now invalid)
  615. colstart = 0, rowstart = 0; // Screen display area to CGRAM area coordinate offsets
  616. // Port and pin masks for control signals (ESP826 only) - TODO: remove need for this
  617. volatile uint32_t *dcport, *csport;
  618. uint32_t cspinmask, dcpinmask, wrpinmask, sclkpinmask;
  619. #if defined(ESP32_PARALLEL)
  620. // Bit masks for ESP32 parallel bus interface
  621. uint32_t xclr_mask, xdir_mask; // Port set/clear and direction control masks
  622. // Lookup table for ESP32 parallel bus interface uses 1kbyte RAM,
  623. uint32_t xset_mask[256]; // Makes Sprite rendering test 33% faster, for slower macro equivalent
  624. // see commented out #define set_mask(C) within TFT_eSPI_ESP32.h
  625. #endif
  626. //uint32_t lastColor = 0xFFFF; // Last colour - used to minimise bit shifting overhead
  627. getColorCallback getColor = nullptr; // Smooth font callback function pointer
  628. //-------------------------------------- protected ----------------------------------//
  629. protected:
  630. //int32_t win_xe, win_ye; // Window end coords - not needed
  631. int32_t _init_width, _init_height; // Display w/h as input, used by setRotation()
  632. int32_t _width, _height; // Display w/h as modified by current rotation
  633. int32_t addr_row, addr_col; // Window position - used to minimise window commands
  634. int16_t _xPivot; // TFT x pivot point coordinate for rotated Sprites
  635. int16_t _yPivot; // TFT x pivot point coordinate for rotated Sprites
  636. // Viewport variables
  637. int32_t _vpX, _vpY, _vpW, _vpH; // Note: x start, y start, x end + 1, y end + 1
  638. int32_t _xDatum;
  639. int32_t _yDatum;
  640. int32_t _xWidth;
  641. int32_t _yHeight;
  642. bool _vpDatum;
  643. bool _vpOoB;
  644. int32_t cursor_x, cursor_y, padX; // Text cursor x,y and padding setting
  645. uint32_t fontsloaded; // Bit field of fonts loaded
  646. uint8_t glyph_ab, // Smooth font glyph delta Y (height) above baseline
  647. glyph_bb; // Smooth font glyph delta Y (height) below baseline
  648. bool isDigits; // adjust bounding box for numbers to reduce visual jiggling
  649. bool textwrapX, textwrapY; // If set, 'wrap' text at right and optionally bottom edge of display
  650. bool _swapBytes; // Swap the byte order for TFT pushImage()
  651. bool locked, inTransaction, lockTransaction; // SPI transaction and mutex lock flags
  652. bool _booted; // init() or begin() has already run once
  653. // User sketch manages these via set/getAttribute()
  654. bool _cp437; // If set, use correct CP437 charset (default is ON)
  655. bool _utf8; // If set, use UTF-8 decoder in print stream 'write()' function (default ON)
  656. bool _psram_enable; // Enable PSRAM use for library functions (TBD) and Sprites
  657. uint32_t _lastColor; // Buffered value of last colour used
  658. #ifdef LOAD_GFXFF
  659. GFXfont *gfxFont;
  660. #endif
  661. /***************************************************************************************
  662. ** Section 9: TFT_eSPI class conditional extensions
  663. ***************************************************************************************/
  664. // Load the Touch extension
  665. #ifdef TOUCH_CS
  666. #include "Extensions/Touch.h" // Loaded if TOUCH_CS is defined by user
  667. #endif
  668. // Load the Anti-aliased font extension
  669. #ifdef SMOOTH_FONT
  670. #include "Extensions/Smooth_font.h" // Loaded if SMOOTH_FONT is defined by user
  671. #endif
  672. }; // End of class TFT_eSPI
  673. /***************************************************************************************
  674. ** Section 10: Additional extension classes
  675. ***************************************************************************************/
  676. // Load the Button Class
  677. #include "Extensions/Button.h"
  678. // Load the Sprite Class
  679. #include "Extensions/Sprite.h"
  680. #endif // ends #ifndef _TFT_eSPIH_