1 Star 2 Fork 1

uncommon_ljl/grbl_controller_esp32

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
TFT_eSPI.h 30.81 KB
一键复制 编辑 原始数据 按行查看 历史
mstrens 提交于 6年前 . avoid lib TFT_eSPI
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
/***************************************************
Arduino TFT graphics library targeted at ESP8266
and ESP32 based boards.
This is a standalone library that contains the
hardware driver, the graphics functions and the
proportional fonts.
The larger fonts are Run Length Encoded to reduce
their FLASH footprint.
****************************************************/
// Stop fonts etc being loaded multiple times
#ifndef _TFT_eSPIH_
#define _TFT_eSPIH_
//#define ESP32 //Just used to test ESP32 options
// Include header file that defines the fonts loaded, the TFT drivers
// available and the pins to be used
#include "User_Setup_Select.h"
#ifndef TAB_COLOUR
#define TAB_COLOUR 0
#endif
// If the frequency is not defined, set a default
#ifndef SPI_FREQUENCY
#define SPI_FREQUENCY 20000000
#endif
// If the frequency is not defined, set a default
#ifndef SPI_READ_FREQUENCY
#define SPI_READ_FREQUENCY SPI_FREQUENCY
#endif
#ifdef ST7789_DRIVER
#define TFT_SPI_MODE SPI_MODE3
#else
#define TFT_SPI_MODE SPI_MODE0
#endif
// If the frequency is not defined, set a default
#ifndef SPI_TOUCH_FREQUENCY
#define SPI_TOUCH_FREQUENCY 2500000
#endif
// Use GLCD font in error case where user requests a smooth font file
// that does not exist (this is a temporary fix to stop ESP32 reboot)
#ifdef SMOOTH_FONT
#ifndef LOAD_GLCD
#define LOAD_GLCD
#endif
#endif
// Only load the fonts defined in User_Setup.h (to save space)
// Set flag so RLE rendering code is optionally compiled
#ifdef LOAD_GLCD
#include "Fonts/glcdfont.c"
#endif
#ifdef LOAD_FONT2
#include "Fonts/Font16.h"
#endif
#ifdef LOAD_FONT4
#include "Fonts/Font32rle.h"
#define LOAD_RLE
#endif
#ifdef LOAD_FONT6
#include "Fonts/Font64rle.h"
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#ifdef LOAD_FONT7
#include "Fonts/Font7srle.h"
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#ifdef LOAD_FONT8
#include "Fonts/Font72rle.h"
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#elif defined LOAD_FONT8N
#define LOAD_FONT8
#include "Fonts/Font72x53rle.h"
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#include <Arduino.h>
#include <Print.h>
#include <pgmspace.h>
#include <SPI.h>
#ifdef ESP32
#include "soc/spi_reg.h"
#define SPI_NUM 0x3
#endif
#ifdef SMOOTH_FONT
// Call up the SPIFFS FLASH filing system for the anti-aliased fonts
#define FS_NO_GLOBALS
#include <FS.h>
#ifdef ESP32
#include "SPIFFS.h"
#endif
#endif
#ifndef TFT_DC
#define DC_C // No macro allocated so it generates no code
#define DC_D // No macro allocated so it generates no code
#else
#if defined (ESP8266) && defined (D0_USED_FOR_DC)
#define DC_C digitalWrite(TFT_DC, LOW)
#define DC_D digitalWrite(TFT_DC, HIGH)
#elif defined (ESP32)
#if defined (ESP32_PARALLEL)
#define DC_C GPIO.out_w1tc = (1 << TFT_DC)
#define DC_D GPIO.out_w1ts = (1 << TFT_DC)
#else
#if TFT_DC >= 32
#ifdef RPI_ILI9486_DRIVER // RPi display needs a slower DC change
#define DC_C GPIO.out1_w1ts.val = (1 << (TFT_DC - 32)); \
GPIO.out1_w1tc.val = (1 << (TFT_DC - 32))
#define DC_D GPIO.out1_w1tc.val = (1 << (TFT_DC - 32)); \
GPIO.out1_w1ts.val = (1 << (TFT_DC - 32))
#else
#define DC_C GPIO.out1_w1tc.val = (1 << (TFT_DC - 32))
#define DC_D GPIO.out1_w1ts.val = (1 << (TFT_DC - 32))
#endif
#else
#if TFT_DC >= 0
#ifdef RPI_ILI9486_DRIVER // RPi display needs a slower DC change
#define DC_C GPIO.out_w1ts = (1 << TFT_DC); \
GPIO.out_w1tc = (1 << TFT_DC)
#define DC_D GPIO.out_w1tc = (1 << TFT_DC); \
GPIO.out_w1ts = (1 << TFT_DC)
#else
#define DC_C GPIO.out_w1tc = (1 << TFT_DC)
#define DC_D GPIO.out_w1ts = (1 << TFT_DC)
#endif
#else
#define DC_C
#define DC_D
#endif
#endif
#endif
#else
#define DC_C GPOC=dcpinmask
#define DC_D GPOS=dcpinmask
#endif
#endif
#if defined (TFT_SPI_OVERLAP)
#undef TFT_CS
#endif
#ifndef TFT_CS
#define CS_L // No macro allocated so it generates no code
#define CS_H // No macro allocated so it generates no code
#else
#if defined (ESP8266) && defined (D0_USED_FOR_CS)
#define CS_L digitalWrite(TFT_CS, LOW)
#define CS_H digitalWrite(TFT_CS, HIGH)
#elif defined (ESP32)
#if defined (ESP32_PARALLEL)
#define CS_L // The TFT CS is set permanently low during init()
#define CS_H
#else
#if TFT_CS >= 32
#ifdef RPI_ILI9486_DRIVER // RPi display needs a slower CS change
#define CS_L GPIO.out1_w1ts.val = (1 << (TFT_CS - 32)); \
GPIO.out1_w1tc.val = (1 << (TFT_CS - 32))
#define CS_H GPIO.out1_w1tc.val = (1 << (TFT_CS - 32)); \
GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))
#else
#define CS_L GPIO.out1_w1tc.val = (1 << (TFT_CS - 32))
#define CS_H GPIO.out1_w1ts.val = (1 << (TFT_CS - 32))
#endif
#else
#if TFT_CS >= 0
#ifdef RPI_ILI9486_DRIVER // RPi display needs a slower CS change
#define CS_L GPIO.out_w1ts = (1 << TFT_CS); GPIO.out_w1tc = (1 << TFT_CS)
#define CS_H GPIO.out_w1tc = (1 << TFT_CS); GPIO.out_w1ts = (1 << TFT_CS)
#else
#define CS_L GPIO.out_w1tc = (1 << TFT_CS)
#define CS_H GPIO.out_w1ts = (1 << TFT_CS)
#endif
#else
#define CS_L
#define CS_H
#endif
#endif
#endif
#else
#define CS_L GPOC=cspinmask
#define CS_H GPOS=cspinmask
#endif
#endif
// Use single register write for CS_L and DC_C if pins are both in range 0-31
#ifdef ESP32
#if (TFT_CS >= 0) && (TFT_CS < 32) && (TFT_DC >= 0) && (TFT_DC < 32)
#ifdef RPI_ILI9486_DRIVER // RPi display needs a slower CD and DC change
#define CS_L_DC_C GPIO.out_w1tc = ((1 << TFT_CS) | (1 << TFT_DC)); \
GPIO.out_w1tc = ((1 << TFT_CS) | (1 << TFT_DC))
#else
#define CS_L_DC_C GPIO.out_w1tc = ((1 << TFT_CS) | (1 << TFT_DC))
#endif
#else
#define CS_L_DC_C CS_L; DC_C
#endif
#else // ESP8266
#define CS_L_DC_C CS_L; DC_C
#endif
// chip select signal for touchscreen
#ifndef TOUCH_CS
#define T_CS_L // No macro allocated so it generates no code
#define T_CS_H // No macro allocated so it generates no code
#else
#define T_CS_L digitalWrite(TOUCH_CS, LOW)
#define T_CS_H digitalWrite(TOUCH_CS, HIGH)
#endif
#ifdef TFT_WR
#if defined (ESP32)
#define WR_L GPIO.out_w1tc = (1 << TFT_WR)
#define WR_H GPIO.out_w1ts = (1 << TFT_WR)
#else
#define WR_L GPOC=wrpinmask
#define WR_H GPOS=wrpinmask
#endif
#endif
#ifdef ESP8266
// Concatenate two 16 bit values for the SPI 32 bit register write
#define SPI_32(H,L) ( (H)<<16 | (L) )
#define COL_32(H,L) ( (H)<<16 | (L) )
#else
#if defined (ESP32_PARALLEL) || defined (ILI9488_DRIVER)
#define SPI_32(H,L) ( (H)<<16 | (L) )
#else
#define SPI_32(H,L) ( ((H)<<8 | (H)>>8) | (((L)<<8 | (L)>>8)<<16 ) )
#endif
// Swap byte order for concatenated 16 bit colors
// AB CD -> DCBA for 32 bit register write
#define COL_32(H,L) ( ((H)<<8 | (H)>>8) | (((L)<<8 | (L)>>8)<<16 ) )
#endif
#if defined (ESP32) && defined (ESP32_PARALLEL)
// Mask for the 8 data bits to set pin directions
#define dir_mask ((1 << TFT_D0) | (1 << TFT_D1) | (1 << TFT_D2) | (1 << TFT_D3) | (1 << TFT_D4) | (1 << TFT_D5) | (1 << TFT_D6) | (1 << TFT_D7))
// Data bits and the write line are cleared to 0 in one step
#define clr_mask (dir_mask | (1 << TFT_WR))
// A lookup table is used to set the different bit patterns, this uses 1kByte of RAM
#define set_mask(C) xset_mask[C] // 63fps Sprite rendering test 33% faster, graphicstest only 1.8% faster than shifting in real time
// Real-time shifting alternative to above to save 1KByte RAM, 47 fps Sprite rendering test
/*#define set_mask(C) ((C&0x80)>>7)<<TFT_D7 | ((C&0x40)>>6)<<TFT_D6 | ((C&0x20)>>5)<<TFT_D5 | ((C&0x10)>>4)<<TFT_D4 | \
((C&0x08)>>3)<<TFT_D3 | ((C&0x04)>>2)<<TFT_D2 | ((C&0x02)>>1)<<TFT_D1 | ((C&0x01)>>0)<<TFT_D0
//*/
// Write 8 bits to TFT
#define tft_Write_8(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)C); WR_H
// Write 16 bits to TFT
#ifdef PSEUDO_8_BIT
#define tft_Write_16(C) WR_L;GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 0)); WR_H
#else
#define tft_Write_16(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 8)); WR_H; \
GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t)(C >> 0)); WR_H
#endif
// 16 bit write with swapped bytes
#define tft_Write_16S(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 0)); WR_H; \
GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 8)); WR_H
// Write 32 bits to TFT
#define tft_Write_32(C) GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 24)); WR_H; \
GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 16)); WR_H; \
GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 8)); WR_H; \
GPIO.out_w1tc = clr_mask; GPIO.out_w1ts = set_mask((uint8_t) (C >> 0)); WR_H
#ifdef TFT_RD
#define RD_L GPIO.out_w1tc = (1 << TFT_RD)
//#define RD_L digitalWrite(TFT_WR, LOW)
#define RD_H GPIO.out_w1ts = (1 << TFT_RD)
//#define RD_H digitalWrite(TFT_WR, HIGH)
#endif
#elif defined (ILI9488_DRIVER) // 16 bit colour converted to 3 bytes for 18 bit RGB
// Write 8 bits to TFT
#define tft_Write_8(C) SPI.transfer(C)
// Convert 16 bit colour to 18 bit and write in 3 bytes
#define tft_Write_16(C) SPI.transfer((C & 0xF800)>>8); \
SPI.transfer((C & 0x07E0)>>3); \
SPI.transfer((C & 0x001F)<<3)
// Convert swapped byte 16 bit colour to 18 bit and write in 3 bytes
#define tft_Write_16S(C) SPI.transfer(C & 0xF8); \
SPI.transfer((C & 0xE0)>>11 | (C & 0x07)<<5); \
SPI.transfer((C & 0x1F00)>>5)
// Write 32 bits to TFT
#define tft_Write_32(C) SPI.write32(C)
#elif defined (RPI_ILI9486_DRIVER)
#define tft_Write_8(C) SPI.transfer(0); SPI.transfer(C)
#define tft_Write_16(C) SPI.write16(C)
#define tft_Write_16S(C) SPI.write16(C<<8 | C>>8)
#define tft_Write_32(C) SPI.write32(C)
#elif defined ESP8266
#define tft_Write_8(C) SPI.write(C)
#define tft_Write_16(C) SPI.write16(C)
#define tft_Write_32(C) SPI.write32(C)
#else // ESP32 using SPI with 16 bit color display
// ESP32 low level SPI writes for 8, 16 and 32 bit values
// to avoid the function call overhead
// Write 8 bits
#define tft_Write_8(C) \
WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_NUM), 8-1); \
WRITE_PERI_REG(SPI_W0_REG(SPI_NUM), C); \
SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); \
while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM))&SPI_USR);
// Write 16 bits with corrected endianess for 16 bit colours
#define tft_Write_16(C) \
WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_NUM), 16-1); \
WRITE_PERI_REG(SPI_W0_REG(SPI_NUM), C<<8 | C>>8); \
SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); \
while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM))&SPI_USR);
// Write 16 bits
#define tft_Write_16S(C) \
WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_NUM), 16-1); \
WRITE_PERI_REG(SPI_W0_REG(SPI_NUM), C); \
SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); \
while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM))&SPI_USR);
// Write 32 bits
#define tft_Write_32(C) \
WRITE_PERI_REG(SPI_MOSI_DLEN_REG(SPI_NUM), 32-1); \
WRITE_PERI_REG(SPI_W0_REG(SPI_NUM), C); \
SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); \
while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM))&SPI_USR);
#endif
#if !defined (ESP32_PARALLEL)
// Read from display using SPI or software SPI
#if defined (ESP8266) && defined (TFT_SDA_READ)
// Use a bit banged function call for ESP8266 and bi-directional SDA pin
#define SCLK_L GPOC=sclkpinmask
#define SCLK_H GPOS=sclkpinmask
#else
// Use a SPI read transfer
#define tft_Read_8() SPI.transfer(0)
#endif
#endif
#ifdef LOAD_GFXFF
// We can include all the free fonts and they will only be built into
// the sketch if they are used
#include "Fonts/GFXFF/gfxfont.h"
// Call up any user custom fonts
#include "User_Setups/User_Custom_Fonts.h"
// Original Adafruit_GFX "Free Fonts"
#include "Fonts/GFXFF/TomThumb.h" // TT1
#include "Fonts/GFXFF/FreeMono9pt7b.h" // FF1 or FM9
#include "Fonts/GFXFF/FreeMono12pt7b.h" // FF2 or FM12
#include "Fonts/GFXFF/FreeMono18pt7b.h" // FF3 or FM18
#include "Fonts/GFXFF/FreeMono24pt7b.h" // FF4 or FM24
#include "Fonts/GFXFF/FreeMonoOblique9pt7b.h" // FF5 or FMO9
#include "Fonts/GFXFF/FreeMonoOblique12pt7b.h" // FF6 or FMO12
#include "Fonts/GFXFF/FreeMonoOblique18pt7b.h" // FF7 or FMO18
#include "Fonts/GFXFF/FreeMonoOblique24pt7b.h" // FF8 or FMO24
#include "Fonts/GFXFF/FreeMonoBold9pt7b.h" // FF9 or FMB9
#include "Fonts/GFXFF/FreeMonoBold12pt7b.h" // FF10 or FMB12
#include "Fonts/GFXFF/FreeMonoBold18pt7b.h" // FF11 or FMB18
#include "Fonts/GFXFF/FreeMonoBold24pt7b.h" // FF12 or FMB24
#include "Fonts/GFXFF/FreeMonoBoldOblique9pt7b.h" // FF13 or FMBO9
#include "Fonts/GFXFF/FreeMonoBoldOblique12pt7b.h" // FF14 or FMBO12
#include "Fonts/GFXFF/FreeMonoBoldOblique18pt7b.h" // FF15 or FMBO18
#include "Fonts/GFXFF/FreeMonoBoldOblique24pt7b.h" // FF16 or FMBO24
// Sans serif fonts
#include "Fonts/GFXFF/FreeSans9pt7b.h" // FF17 or FSS9
#include "Fonts/GFXFF/FreeSans12pt7b.h" // FF18 or FSS12
#include "Fonts/GFXFF/FreeSans18pt7b.h" // FF19 or FSS18
#include "Fonts/GFXFF/FreeSans24pt7b.h" // FF20 or FSS24
#include "Fonts/GFXFF/FreeSansOblique9pt7b.h" // FF21 or FSSO9
#include "Fonts/GFXFF/FreeSansOblique12pt7b.h" // FF22 or FSSO12
#include "Fonts/GFXFF/FreeSansOblique18pt7b.h" // FF23 or FSSO18
#include "Fonts/GFXFF/FreeSansOblique24pt7b.h" // FF24 or FSSO24
#include "Fonts/GFXFF/FreeSansBold9pt7b.h" // FF25 or FSSB9
#include "Fonts/GFXFF/FreeSansBold12pt7b.h" // FF26 or FSSB12
#include "Fonts/GFXFF/FreeSansBold18pt7b.h" // FF27 or FSSB18
#include "Fonts/GFXFF/FreeSansBold24pt7b.h" // FF28 or FSSB24
#include "Fonts/GFXFF/FreeSansBoldOblique9pt7b.h" // FF29 or FSSBO9
#include "Fonts/GFXFF/FreeSansBoldOblique12pt7b.h" // FF30 or FSSBO12
#include "Fonts/GFXFF/FreeSansBoldOblique18pt7b.h" // FF31 or FSSBO18
#include "Fonts/GFXFF/FreeSansBoldOblique24pt7b.h" // FF32 or FSSBO24
// Serif fonts
#include "Fonts/GFXFF/FreeSerif9pt7b.h" // FF33 or FS9
#include "Fonts/GFXFF/FreeSerif12pt7b.h" // FF34 or FS12
#include "Fonts/GFXFF/FreeSerif18pt7b.h" // FF35 or FS18
#include "Fonts/GFXFF/FreeSerif24pt7b.h" // FF36 or FS24
#include "Fonts/GFXFF/FreeSerifItalic9pt7b.h" // FF37 or FSI9
#include "Fonts/GFXFF/FreeSerifItalic12pt7b.h" // FF38 or FSI12
#include "Fonts/GFXFF/FreeSerifItalic18pt7b.h" // FF39 or FSI18
#include "Fonts/GFXFF/FreeSerifItalic24pt7b.h" // FF40 or FSI24
#include "Fonts/GFXFF/FreeSerifBold9pt7b.h" // FF41 or FSB9
#include "Fonts/GFXFF/FreeSerifBold12pt7b.h" // FF42 or FSB12
#include "Fonts/GFXFF/FreeSerifBold18pt7b.h" // FF43 or FSB18
#include "Fonts/GFXFF/FreeSerifBold24pt7b.h" // FF44 or FSB24
#include "Fonts/GFXFF/FreeSerifBoldItalic9pt7b.h" // FF45 or FSBI9
#include "Fonts/GFXFF/FreeSerifBoldItalic12pt7b.h" // FF46 or FSBI12
#include "Fonts/GFXFF/FreeSerifBoldItalic18pt7b.h" // FF47 or FSBI18
#include "Fonts/GFXFF/FreeSerifBoldItalic24pt7b.h" // FF48 or FSBI24
#endif // #ifdef LOAD_GFXFF
//These enumerate the text plotting alignment (reference datum point)
#define TL_DATUM 0 // Top left (default)
#define TC_DATUM 1 // Top centre
#define TR_DATUM 2 // Top right
#define ML_DATUM 3 // Middle left
#define CL_DATUM 3 // Centre left, same as above
#define MC_DATUM 4 // Middle centre
#define CC_DATUM 4 // Centre centre, same as above
#define MR_DATUM 5 // Middle right
#define CR_DATUM 5 // Centre right, same as above
#define BL_DATUM 6 // Bottom left
#define BC_DATUM 7 // Bottom centre
#define BR_DATUM 8 // Bottom right
#define L_BASELINE 9 // Left character baseline (Line the 'A' character would sit on)
#define C_BASELINE 10 // Centre character baseline
#define R_BASELINE 11 // Right character baseline
// New color definitions use for all my libraries
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
#define TFT_NAVY 0x000F /* 0, 0, 128 */
#define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
#define TFT_MAROON 0x7800 /* 128, 0, 0 */
#define TFT_PURPLE 0x780F /* 128, 0, 128 */
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
#define TFT_BLUE 0x001F /* 0, 0, 255 */
#define TFT_GREEN 0x07E0 /* 0, 255, 0 */
#define TFT_CYAN 0x07FF /* 0, 255, 255 */
#define TFT_RED 0xF800 /* 255, 0, 0 */
#define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
#define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
#define TFT_WHITE 0xFFFF /* 255, 255, 255 */
#define TFT_ORANGE 0xFDA0 /* 255, 180, 0 */
#define TFT_GREENYELLOW 0xB7E0 /* 180, 255, 0 */
#define TFT_PINK 0xFC9F
// Next is a special 16 bit colour value that encodes to 8 bits
// and will then decode back to the same 16 bit value.
// Convenient for 8 bit and 16 bit transparent sprites.
#define TFT_TRANSPARENT 0x0120
// Swap any type
template <typename T> static inline void
swap_coord(T& a, T& b) { T t = a; a = b; b = t; }
#ifndef min
// Return minimum of two numbers, may already be defined
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
// This structure allows sketches to retrieve the user setup parameters at runtime
// by calling getSetup(), zero impact on code size unless used, mainly for diagnostics
typedef struct
{
int16_t esp;
uint8_t trans;
uint8_t serial;
uint8_t overlap;
uint16_t tft_driver; // Hexadecimal code
uint16_t tft_width; // Rotation 0 width and height
uint16_t tft_height;
uint8_t r0_x_offset; // Offsets, not all used yet
uint8_t r0_y_offset;
uint8_t r1_x_offset;
uint8_t r1_y_offset;
uint8_t r2_x_offset;
uint8_t r2_y_offset;
uint8_t r3_x_offset;
uint8_t r3_y_offset;
int8_t pin_tft_mosi;
int8_t pin_tft_miso;
int8_t pin_tft_clk;
int8_t pin_tft_cs;
int8_t pin_tft_dc;
int8_t pin_tft_rd;
int8_t pin_tft_wr;
int8_t pin_tft_rst;
int8_t pin_tft_d0;
int8_t pin_tft_d1;
int8_t pin_tft_d2;
int8_t pin_tft_d3;
int8_t pin_tft_d4;
int8_t pin_tft_d5;
int8_t pin_tft_d6;
int8_t pin_tft_d7;
int8_t pin_tch_cs;
int16_t tft_spi_freq;
int16_t tch_spi_freq;
} setup_t;
// This is a structure to conveniently hold information on the default fonts
// Stores pointer to font character image address table, width table and height
// Create a null set in case some fonts not used (to prevent crash)
const uint8_t widtbl_null[1] = {0};
PROGMEM const uint8_t chr_null[1] = {0};
PROGMEM const uint8_t* const chrtbl_null[1] = {chr_null};
typedef struct {
const uint8_t *chartbl;
const uint8_t *widthtbl;
uint8_t height;
uint8_t baseline;
} fontinfo;
// Now fill the structure
const PROGMEM fontinfo fontdata [] = {
#ifdef LOAD_GLCD
{ (const uint8_t *)font, widtbl_null, 0, 0 },
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
// GLCD font (Font 1) does not have all parameters
{ (const uint8_t *)chrtbl_null, widtbl_null, 8, 7 },
#ifdef LOAD_FONT2
{ (const uint8_t *)chrtbl_f16, widtbl_f16, chr_hgt_f16, baseline_f16},
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
// Font 3 current unused
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#ifdef LOAD_FONT4
{ (const uint8_t *)chrtbl_f32, widtbl_f32, chr_hgt_f32, baseline_f32},
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
// Font 5 current unused
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#ifdef LOAD_FONT6
{ (const uint8_t *)chrtbl_f64, widtbl_f64, chr_hgt_f64, baseline_f64},
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
#ifdef LOAD_FONT7
{ (const uint8_t *)chrtbl_f7s, widtbl_f7s, chr_hgt_f7s, baseline_f7s},
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 },
#endif
#ifdef LOAD_FONT8
{ (const uint8_t *)chrtbl_f72, widtbl_f72, chr_hgt_f72, baseline_f72}
#else
{ (const uint8_t *)chrtbl_null, widtbl_null, 0, 0 }
#endif
};
// Class functions and variables
class TFT_eSPI : public Print {
public:
TFT_eSPI(int16_t _W = TFT_WIDTH, int16_t _H = TFT_HEIGHT);
void init(uint8_t tc = TAB_COLOUR), begin(uint8_t tc = TAB_COLOUR); // Same - begin included for backwards compatibility
// These are virtual so the TFT_eSprite class can override them with sprite specific functions
virtual void drawPixel(uint32_t x, uint32_t y, uint32_t color),
drawChar(int32_t x, int32_t y, unsigned char c, uint32_t color, uint32_t bg, uint8_t size),
drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t color),
drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t color),
drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t color),
fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color);
virtual int16_t drawChar(unsigned int uniCode, int x, int y, int font),
drawChar(unsigned int uniCode, int x, int y),
height(void),
width(void);
// The TFT_eSprite class inherits the following functions
void setWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1),
pushColor(uint16_t color),
pushColor(uint16_t color, uint32_t len),
pushColors(uint16_t *data, uint32_t len, bool swap = true), // With byte swap option
pushColors(uint8_t *data, uint32_t len),
fillScreen(uint32_t color);
void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color),
drawRoundRect(int32_t x0, int32_t y0, int32_t w, int32_t h, int32_t radius, uint32_t color),
fillRoundRect(int32_t x0, int32_t y0, int32_t w, int32_t h, int32_t radius, uint32_t color),
setRotation(uint8_t r),
invertDisplay(boolean i),
drawCircle(int32_t x0, int32_t y0, int32_t r, uint32_t color),
drawCircleHelper(int32_t x0, int32_t y0, int32_t r, uint8_t cornername, uint32_t color),
fillCircle(int32_t x0, int32_t y0, int32_t r, uint32_t color),
fillCircleHelper(int32_t x0, int32_t y0, int32_t r, uint8_t cornername, int32_t delta, uint32_t color),
drawEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color),
fillEllipse(int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color),
drawTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color),
fillTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t color),
drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color),
drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color),
drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t fgcolor, uint16_t bgcolor),
setBitmapColor(uint16_t fgcolor, uint16_t bgcolor), // For 1bpp sprites
setCursor(int16_t x, int16_t y),
setCursor(int16_t x, int16_t y, uint8_t font),
setTextColor(uint16_t color),
setTextColor(uint16_t fgcolor, uint16_t bgcolor),
setTextSize(uint8_t size),
setTextWrap(boolean wrapX, boolean wrapY = false),
setTextDatum(uint8_t datum),
setTextPadding(uint16_t x_width),
#ifdef LOAD_GFXFF
setFreeFont(const GFXfont *f = NULL),
setTextFont(uint8_t font),
#else
setFreeFont(uint8_t font),
setTextFont(uint8_t font),
#endif
spiwrite(uint8_t),
writecommand(uint8_t c),
writedata(uint8_t d),
commandList(const uint8_t *addr);
uint8_t readcommand8(uint8_t cmd_function, uint8_t index);
uint16_t readcommand16(uint8_t cmd_function, uint8_t index);
uint32_t readcommand32(uint8_t cmd_function, uint8_t index);
// Read the colour of a pixel at x,y and return value in 565 format
uint16_t readPixel(int32_t x0, int32_t y0);
// The next functions can be used as a pair to copy screen blocks (or horizontal/vertical lines) to another location
// Read a block of pixels to a data buffer, buffer is 16 bit and the array size must be at least w * h
void readRect(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint16_t *data);
// Write a block of pixels to the screen
void pushRect(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint16_t *data);
// These are used to render images or sprites stored in RAM arrays
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, uint16_t *data);
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, uint16_t *data, uint16_t transparent);
// These are used to render images stored in FLASH (PROGMEM)
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, const uint16_t *data, uint16_t transparent);
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, const uint16_t *data);
// These are used by pushSprite for 1 and 8 bit colours
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, uint8_t *data, bool bpp8 = true);
void pushImage(int32_t x0, int32_t y0, uint32_t w, uint32_t h, uint8_t *data, uint8_t transparent, bool bpp8 = true);
// Swap the byte order for pushImage() - corrects endianness
void setSwapBytes(bool swap);
bool getSwapBytes(void);
// This next function has been used successfully to dump the TFT screen to a PC for documentation purposes
// It reads a screen area and returns the RGB 8 bit colour values of each pixel
// Set w and h to 1 to read 1 pixel's colour. The data buffer must be at least w * h * 3 bytes
void readRectRGB(int32_t x0, int32_t y0, int32_t w, int32_t h, uint8_t *data);
uint8_t getRotation(void),
getTextDatum(void),
color16to8(uint16_t color565); // Convert 16 bit colour to 8 bits
int16_t getCursorX(void),
getCursorY(void);
uint16_t fontsLoaded(void),
color565(uint8_t red, uint8_t green, uint8_t blue), // Convert 8 bit red, green and blue to 16 bits
color8to16(uint8_t color332); // Convert 8 bit colour to 16 bits
int16_t drawNumber(long long_num,int poX, int poY, int font),
drawNumber(long long_num,int poX, int poY),
drawFloat(float floatNumber,int decimal,int poX, int poY, int font),
drawFloat(float floatNumber,int decimal,int poX, int poY),
// Handle char arrays
drawString(const char *string, int poX, int poY, int font),
drawString(const char *string, int poX, int poY),
drawCentreString(const char *string, int dX, int poY, int font), // Deprecated, use setTextDatum() and drawString()
drawRightString(const char *string, int dX, int poY, int font), // Deprecated, use setTextDatum() and drawString()
// Handle String type
drawString(const String& string, int poX, int poY, int font),
drawString(const String& string, int poX, int poY),
drawCentreString(const String& string, int dX, int poY, int font), // Deprecated, use setTextDatum() and drawString()
drawRightString(const String& string, int dX, int poY, int font); // Deprecated, use setTextDatum() and drawString()
int16_t textWidth(const char *string, int font),
textWidth(const char *string),
textWidth(const String& string, int font),
textWidth(const String& string),
fontHeight(int16_t font),
fontHeight(void);
void setAddrWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye);
// These 3 functions are used together, for every startWrite() there must be an endWrite()
void startWrite(void); // Begin SPI transaction
void writeColor(uint16_t color, uint32_t len); // Write a colour without transaction overhead
void endWrite(void); // End SPI transaction
size_t write(uint8_t);
#ifdef TFT_SDA_READ
#if defined (ESP8266) && defined (TFT_SDA_READ)
uint8_t tft_Read_8(void);
#endif
void begin_SDA_Read(void);
void end_SDA_Read(void);
#endif
void getSetup(setup_t& tft_settings); // Sketch provides the instance to populate
int32_t cursor_x, cursor_y, padX;
uint32_t textcolor, textbgcolor;
uint32_t bitmap_fg, bitmap_bg;
uint8_t textfont, // Current selected font
textsize, // Current font size multiplier
textdatum, // Text reference datum
rotation; // Display rotation (0-3)
private:
inline void spi_begin() __attribute__((always_inline));
inline void spi_end() __attribute__((always_inline));
inline void spi_begin_read() __attribute__((always_inline));
inline void spi_end_read() __attribute__((always_inline));
void readAddrWindow(int32_t xs, int32_t ys, int32_t xe, int32_t ye);
uint8_t tabcolor,
colstart = 0, rowstart = 0; // some ST7735 displays need this changed
volatile uint32_t *dcport, *csport;
uint32_t cspinmask, dcpinmask, wrpinmask, sclkpinmask;
#if defined(ESP32_PARALLEL)
uint32_t xclr_mask, xdir_mask, xset_mask[256];
#endif
uint32_t lastColor = 0xFFFF;
protected:
int32_t win_xe, win_ye;
uint32_t _init_width, _init_height; // Display w/h as input, used by setRotation()
uint32_t _width, _height; // Display w/h as modified by current rotation
uint32_t addr_row, addr_col;
uint32_t fontsloaded;
uint8_t glyph_ab, // glyph height above baseline
glyph_bb; // glyph height below baseline
bool isDigits; // adjust bounding box for numbers to reduce visual jiggling
bool textwrapX, textwrapY; // If set, 'wrap' text at right and optionally bottom edge of display
bool _swapBytes; // Swap the byte order for TFT pushImage()
bool locked, inTransaction; // Transaction and mutex lock flags for ESP32
bool _booted;
uint32_t _lastColor;
#ifdef LOAD_GFXFF
GFXfont *gfxFont;
#endif
// Load the Touch extension
#ifdef TOUCH_CS
#include "Extensions/Touch.h"
#endif
// Load the Anti-aliased font extension
#ifdef SMOOTH_FONT
#include "Extensions/Smooth_font.h"
#endif
}; // End of class TFT_eSPI
// Load the Button Class
#include "Extensions/Button.h"
// Load the Sprite Class
#include "Extensions/Sprite.h"
#endif
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/uncommon_ljl_admin/grbl_controller_esp32.git
git@gitee.com:uncommon_ljl_admin/grbl_controller_esp32.git
uncommon_ljl_admin
grbl_controller_esp32
grbl_controller_esp32
master

搜索帮助