FabGL
ESP32 Display Controller and Graphics Library
TFTControllerGeneric.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019-2021 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6 
7 * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 
9 
10 * This library and related software is available under GPL v3.
11 
12  FabGL is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  FabGL is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
29 
30 
38 #include <stdint.h>
39 #include <stddef.h>
40 
41 #ifdef ARDUINO
42  #include "SPI.h"
43 #endif
44 
45 #include "freertos/FreeRTOS.h"
46 
47 #include "driver/spi_master.h"
48 
49 #include "fabglconf.h"
50 #include "fabutils.h"
51 #include "displaycontroller.h"
52 
53 
54 #define TFT_CASET 0x2A
55 #define TFT_RASET 0x2B
56 #define TFT_RAMWR 0x2C
57 #define TFT_MADCTL 0x36
58 
59 
60 
61 namespace fabgl {
62 
63 
64 
68 enum class TFTOrientation {
69  Rotate0,
70  Rotate90,
71  Rotate180,
72  Rotate270,
73 };
74 
75 
93 class TFTController : public GenericBitmappedDisplayController {
94 
95 public:
96 
97  // unwanted methods
98  TFTController(TFTController const&) = delete;
99  void operator=(TFTController const&) = delete;
100 
101  TFTController();
102 
103  ~TFTController();
104 
122  #ifdef ARDUINO
123  void begin(SPIClass * spi, gpio_num_t DC, gpio_num_t RESX = GPIO_UNUSED, gpio_num_t CS = GPIO_UNUSED);
124  #endif
125 
143  #ifdef ARDUINO
144  void begin(SPIClass * spi, int DC, int RESX = -1, int CS = -1);
145  #endif
146 
166  void begin(int SCK, int MOSI, int DC, int RESX, int CS, int host);
167 
179  void begin();
180 
181  void end();
182 
197  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
198 
199  // abstract method of BitmappedDisplayController
201 
202  // abstract method of BitmappedDisplayController
204 
205  // abstract method of BitmappedDisplayController
207 
208  virtual int colorsCount() { return 1 << 16; }
209 
210  // abstract method of BitmappedDisplayController
211  int getViewPortWidth() { return m_viewPortWidth; }
212 
213  // abstract method of BitmappedDisplayController
214  int getViewPortHeight() { return m_viewPortHeight; }
215 
223  void setScreenCol(int value);
224 
232  void setScreenRow(int value);
233 
239  int screenCol() { return m_screenCol; }
240 
246  int screenRow() { return m_screenRow; }
247 
248  void readScreen(Rect const & rect, RGB888 * destBuf);
249 
253  void reset() { hardReset(); softReset(); }
254 
266  void setOrientation(TFTOrientation value, bool force = false);
267 
275  void setReverseHorizontal(bool value);
276 
277 
278 protected:
279 
280  virtual void softReset() = 0;
281 
282  virtual void setupOrientation();
283 
284  // abstract method of BitmappedDisplayController
285  int getBitmapSavePixelSize() { return 2; }
286 
287  void setupGPIO();
288 
289  void hardReset();
290 
291  void sendRefresh();
292 
293  void sendScreenBuffer(Rect updateRect);
294  void writeCommand(uint8_t cmd);
295  void writeByte(uint8_t data);
296  void writeWord(uint16_t data);
297  void writeData(void * data, size_t size);
298 
299  void SPIBegin();
300  void SPIEnd();
301 
302  void SPIBeginWrite();
303  void SPIEndWrite();
304  void SPIWriteByte(uint8_t data);
305  void SPIWriteWord(uint16_t data);
306  void SPIWriteBuffer(void * data, size_t size);
307 
308  void allocViewPort();
309  void freeViewPort();
310 
311  static void updateTaskFunc(void * pvParameters);
312 
313  // abstract method of BitmappedDisplayController
314  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
315 
316  // abstract method of BitmappedDisplayController
317  void clear(Rect & updateRect);
318 
319  // abstract method of BitmappedDisplayController
320  void drawEllipse(Size const & size, Rect & updateRect);
321 
322  void VScroll(int scroll, Rect & updateRect);
323 
324  void HScroll(int scroll, Rect & updateRect);
325 
326  // abstract method of BitmappedDisplayController
327  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
328 
329  // abstract method of BitmappedDisplayController
330  void swapBuffers();
331 
332  // abstract method of BitmappedDisplayController
333  void invertRect(Rect const & rect, Rect & updateRect);
334 
335  // abstract method of BitmappedDisplayController
336  void copyRect(Rect const & source, Rect & updateRect);
337 
338  // abstract method of BitmappedDisplayController
339  void swapFGBG(Rect const & rect, Rect & updateRect);
340 
341  // abstract method of BitmappedDisplayController
342  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
343 
344  // abstract method of BitmappedDisplayController
345  void rawFillRow(int y, int x1, int x2, RGB888 color);
346 
347  void rawFillRow(int y, int x1, int x2, uint16_t pattern);
348 
349  void swapRows(int yA, int yB, int x1, int x2);
350 
351  void rawInvertRow(int y, int x1, int x2);
352 
353  // abstract method of BitmappedDisplayController
354  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
355 
356  // abstract method of BitmappedDisplayController
357  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
358 
359  // abstract method of BitmappedDisplayController
360  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
361 
362  // abstract method of BitmappedDisplayController
363  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
364 
365 
366  #ifdef ARDUINO
367  SPIClass * m_spi;
368  #endif
369 
370  spi_host_device_t m_SPIHost;
371  gpio_num_t m_SCK;
372  gpio_num_t m_MOSI;
373  gpio_num_t m_DC;
374  gpio_num_t m_RESX;
375  gpio_num_t m_CS;
376 
377  spi_device_handle_t m_SPIDevHandle;
378 
379  uint16_t * * m_viewPort;
380 
381  int16_t m_screenWidth;
382  int16_t m_screenHeight;
383  int16_t m_screenCol;
384  int16_t m_screenRow;
385 
386  int16_t m_viewPortWidth;
387  int16_t m_viewPortHeight;
388 
389  // view port size when rotation is 0 degrees
390  int16_t m_rot0ViewPortWidth;
391  int16_t m_rot0ViewPortHeight;
392 
393  // maximum width and height the controller can handle (ie 240x320)
394  int16_t m_controllerWidth;
395  int16_t m_controllerHeight;
396 
397  // offsets used on rotating
398  int16_t m_rotOffsetX;
399  int16_t m_rotOffsetY;
400 
401  TaskHandle_t m_updateTaskHandle;
402 
403  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
404  volatile bool m_updateTaskRunning;
405 
406  TFTOrientation m_orientation;
407  bool m_reverseHorizontal;
408 
409 };
410 
411 
412 } // end of namespace
413 
414 
415 
416 
417 
418 
419 
int16_t X2
Definition: fabutils.h:180
Represents a 24 bit RGB color.
void reset()
Performs display hardware and software.
int screenCol()
Gets initial left column of the viewport.
NativePixelFormat nativePixelFormat()
Represents the native pixel format used by this display.
int16_t Y2
Definition: fabutils.h:181
int screenRow()
Gets initial top row of the viewport.
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
int16_t Y1
Definition: fabutils.h:179
Base abstract class for TFT drivers with SPI connection.
void setScreenRow(int value)
Set initial top row of the viewport.
void suspendBackgroundPrimitiveExecution()
Suspends drawings.
This file contains fabgl::BitmappedDisplayController definition.
uint8_t const * data
void setOrientation(TFTOrientation value, bool force=false)
Set display orientation and rotation.
int16_t X1
Definition: fabutils.h:178
This file contains some utility classes and functions.
Definition: canvas.cpp:36
TFTOrientation
This enum defines TFT orientation.
NativePixelFormat
This enum defines the display controller native pixel format.
Represents a rectangle.
Definition: fabutils.h:226
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets TFT resolution and viewport size.
virtual int colorsCount()
Determines number of colors this display can provide.
This file contains FabGL library configuration settings, like number of supported colors...
void setReverseHorizontal(bool value)
Inverts horizontal axis.
void setScreenCol(int value)
Set initial left column of the viewport.
void begin()
Initializes TFT display controller.
int getViewPortWidth()
Determines horizontal size of the viewport.
int getViewPortHeight()
Determines vertical size of the viewport.