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