/ MCUME_teensy / teensycastaway / tft_t_dma.h
tft_t_dma.h
  1  /*
  2    Based on C64 ILI9341 dma driver from Frank Bösing, 2017
  3  */
  4  
  5  #ifndef _TFT_T_DMAH_
  6  #define _TFT_T_DMAH_
  7  
  8  #ifdef __cplusplus
  9  #include <Arduino.h>
 10  #include <SPI.h>
 11  #include <DMAChannel.h>
 12  #endif
 13  
 14  #include "tft_t_dma_config.h"
 15  
 16  #define RGBVAL32(r,g,b)  ( (r<<16) | (g<<8) | b )
 17  #define RGBVAL16(r,g,b)  ( (((r>>3)&0x1f)<<11) | (((g>>2)&0x3f)<<5) | (((b>>3)&0x1f)<<0) )
 18  #define RGBVAL8(r,g,b)   ( (((r>>5)&0x07)<<5) | (((g>>5)&0x07)<<2) | (((b>>6)&0x3)<<0) )
 19  #define R16(rgb) ((rgb>>8)&0xf8) 
 20  #define G16(rgb) ((rgb>>3)&0xfc) 
 21  #define B16(rgb) ((rgb<<3)&0xf8) 
 22  
 23  
 24  #ifdef LOHRES
 25  #define TFT_WIDTH      240 
 26  #define TFT_REALWIDTH  240
 27  #else
 28  #define TFT_WIDTH      320 
 29  #define TFT_REALWIDTH  320
 30  #endif
 31  #define TFT_HEIGHT     200
 32  #define TFT_REALHEIGHT 240
 33  
 34  
 35  #define LINES_PER_BLOCK         64
 36  #define NR_OF_BLOCK             4
 37  #define SCREEN_DMA_NUM_SETTINGS NR_OF_BLOCK
 38  
 39  
 40  #ifdef ILI9341
 41  
 42  #define ILI9341_NOP     0x00
 43  #define ILI9341_SWRESET 0x01
 44  #define ILI9341_RDDID   0x04
 45  #define ILI9341_RDDST   0x09
 46  
 47  #define ILI9341_SLPIN   0x10
 48  #define ILI9341_SLPOUT  0x11
 49  #define ILI9341_PTLON   0x12
 50  #define ILI9341_NORON   0x13
 51  
 52  #define ILI9341_RDMODE  0x0A
 53  #define ILI9341_RDMADCTL  0x0B
 54  #define ILI9341_RDPIXFMT  0x0C
 55  #define ILI9341_RDIMGFMT  0x0D
 56  #define ILI9341_RDSELFDIAG  0x0F
 57  
 58  #define ILI9341_INVOFF  0x20
 59  #define ILI9341_INVON   0x21
 60  #define ILI9341_GAMMASET 0x26
 61  #define ILI9341_DISPOFF 0x28
 62  #define ILI9341_DISPON  0x29
 63  
 64  #define ILI9341_CASET   0x2A
 65  #define ILI9341_PASET   0x2B
 66  #define ILI9341_RAMWR   0x2C
 67  #define ILI9341_RAMRD   0x2E
 68  
 69  #define ILI9341_PTLAR    0x30
 70  #define ILI9341_MADCTL   0x36
 71  #define ILI9341_VSCRSADD 0x37
 72  #define ILI9341_PIXFMT   0x3A
 73  
 74  #define ILI9341_FRMCTR1 0xB1
 75  #define ILI9341_FRMCTR2 0xB2
 76  #define ILI9341_FRMCTR3 0xB3
 77  #define ILI9341_INVCTR  0xB4
 78  #define ILI9341_DFUNCTR 0xB6
 79  
 80  #define ILI9341_PWCTR1  0xC0
 81  #define ILI9341_PWCTR2  0xC1
 82  #define ILI9341_PWCTR3  0xC2
 83  #define ILI9341_PWCTR4  0xC3
 84  #define ILI9341_PWCTR5  0xC4
 85  #define ILI9341_VMCTR1  0xC5
 86  #define ILI9341_VMCTR2  0xC7
 87  
 88  #define ILI9341_RDID1   0xDA
 89  #define ILI9341_RDID2   0xDB
 90  #define ILI9341_RDID3   0xDC
 91  #define ILI9341_RDID4   0xDD
 92  
 93  #define ILI9341_GMCTRP1 0xE0
 94  #define ILI9341_GMCTRN1 0xE1
 95  
 96  #define ILI9341_MADCTL_MY  0x80
 97  #define ILI9341_MADCTL_MX  0x40
 98  #define ILI9341_MADCTL_MV  0x20
 99  #define ILI9341_MADCTL_ML  0x10
100  #define ILI9341_MADCTL_RGB 0x00
101  #define ILI9341_MADCTL_BGR 0x08
102  #define ILI9341_MADCTL_MH  0x04
103  
104  #define TFT_CASET ILI9341_CASET
105  #define TFT_PASET ILI9341_PASET
106  #define TFT_RAMWR ILI9341_RAMWR
107  #define TFT_MADCTL ILI9341_MADCTL
108  
109  #endif
110  
111  
112  #ifdef ST7789
113  
114  #define ST7735_NOP     0x00
115  #define ST7735_SWRESET 0x01
116  #define ST7735_RDDID   0x04
117  #define ST7735_RDDST   0x09
118  
119  #define ST7735_SLPIN   0x10
120  #define ST7735_SLPOUT  0x11
121  #define ST7735_PTLON   0x12
122  #define ST7735_NORON   0x13
123  
124  #define ST7735_INVOFF  0x20
125  #define ST7735_INVON   0x21
126  #define ST7735_DISPOFF 0x28
127  #define ST7735_DISPON  0x29
128  #define ST7735_CASET   0x2A
129  #define ST7735_RASET   0x2B
130  #define ST7735_RAMWR   0x2C
131  #define ST7735_RAMRD   0x2E
132  
133  #define ST7735_PTLAR   0x30
134  #define ST7735_COLMOD  0x3A
135  #define ST7735_MADCTL  0x36
136  
137  #define ST7735_FRMCTR1 0xB1
138  #define ST7735_FRMCTR2 0xB2
139  #define ST7735_FRMCTR3 0xB3
140  #define ST7735_INVCTR  0xB4
141  #define ST7735_DISSET5 0xB6
142  
143  #define ST7735_PWCTR1  0xC0
144  #define ST7735_PWCTR2  0xC1
145  #define ST7735_PWCTR3  0xC2
146  #define ST7735_PWCTR4  0xC3
147  #define ST7735_PWCTR5  0xC4
148  #define ST7735_VMCTR1  0xC5
149  
150  #define ST7735_RDID1   0xDA
151  #define ST7735_RDID2   0xDB
152  #define ST7735_RDID3   0xDC
153  #define ST7735_RDID4   0xDD
154  
155  #define ST7735_PWCTR6  0xFC
156  
157  #define ST7735_GMCTRP1 0xE0
158  #define ST7735_GMCTRN1 0xE1
159  
160  #define ST77XX_MADCTL_MY  0x80
161  #define ST77XX_MADCTL_MX  0x40
162  #define ST77XX_MADCTL_MV  0x20
163  #define ST77XX_MADCTL_ML  0x10
164  #define ST77XX_MADCTL_RGB 0x00
165  #define ST77XX_MADCTL_BGR 0x08
166  #define ST77XX_MADCTL_MH  0x04
167  
168  #define TFT_CASET ST7735_CASET
169  #define TFT_PASET ST7735_RASET
170  #define TFT_RAMWR ST7735_RAMWR
171  #define TFT_MADCTL ST7735_MADCTL
172  
173  #endif
174  
175  
176  
177  #ifdef __cplusplus
178  
179  class TFT_T_DMA
180  {
181    public:
182    	TFT_T_DMA(uint8_t _CS, uint8_t _DC, uint8_t _RST = 255, uint8_t _MOSI=11, uint8_t _SCLK=13, uint8_t _MISO=12,  uint8_t touch_cs=38,  uint8_t touch_irq=37);
183  
184      void setArea(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);  
185  	void begin(void);
186  	void flipscreen(bool flip);
187      boolean isflipped(void);
188  	void startDMA(void);
189  	void stopDMA();
190  
191      // Touch screen functions
192      #define TOUCH_ENABLED() ((_touch_cs != 255) && (_touch_irq != 255))      
193      bool isTouching(void) { return ((!TOUCH_ENABLED())?false:(digitalRead(_touch_irq) == LOW)); }
194      void readRaw(uint16_t * oX, uint16_t * oY, uint16_t * oZ);
195      void readCal(uint16_t * oX, uint16_t * oY, uint16_t * oZ);
196      void callibrateTouch(uint16_t xMin,uint16_t yMin,uint16_t xMax,uint16_t yMax);
197  
198      // NoDMA functions
199      void writeScreenNoDma(const uint16_t *pcolors);
200      void fillScreenNoDma(uint16_t color);
201      void drawTextNoDma(int16_t x, int16_t y, const char * text, uint16_t fgcolor, uint16_t bgcolor, bool doublesize);
202      void drawRectNoDma(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
203      void drawSpriteNoDma(int16_t x, int16_t y, const uint16_t *bitmap);
204      void drawSpriteNoDma(int16_t x, int16_t y, const uint16_t *bitmap, uint16_t croparx, uint16_t cropary, uint16_t croparw, uint16_t croparh);
205  
206      // DMA functions
207      uint16_t * getLineBuffer(int j);
208      void writeScreen(int width, int height, int stride, uint8_t *buffer, uint16_t *palette16);
209      void writeLine(int width, int height, int stride, uint8_t *buffer, uint16_t *palette16);
210      void  writeLine(int width, int height, int y, uint16_t *buf);
211  	void fillScreen(uint16_t color);
212      void drawText(int16_t x, int16_t y, const char * text, uint16_t fgcolor, uint16_t bgcolor, bool doublesize);
213      void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
214      void drawSprite(int16_t x, int16_t y, const uint16_t *bitmap);
215      void drawSprite(int16_t x, int16_t y, const uint16_t *bitmap, uint16_t croparx, uint16_t cropary, uint16_t croparw, uint16_t croparh);
216  
217  	
218    protected:   
219      uint8_t _rst, _cs, _dc;
220  	uint8_t _miso, _mosi, _sclk;
221      uint8_t _touch_irq=255, _touch_cs=255;
222  	bool flipped=false;
223  
224      void wait(void);  
225  	void enableTouchIrq();
226  };
227  
228  #endif
229  #endif
230