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. Feel free to use FabGL in free software and hardware:
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  // abstract method of BitmappedDisplayController
209  int getViewPortWidth() { return m_viewPortWidth; }
210 
211  // abstract method of BitmappedDisplayController
212  int getViewPortHeight() { return m_viewPortHeight; }
213 
221  void setScreenCol(int value);
222 
230  void setScreenRow(int value);
231 
237  int screenCol() { return m_screenCol; }
238 
244  int screenRow() { return m_screenRow; }
245 
246  void readScreen(Rect const & rect, RGB888 * destBuf);
247 
251  void reset() { hardReset(); softReset(); }
252 
264  void setOrientation(TFTOrientation value, bool force = false);
265 
273  void setReverseHorizontal(bool value);
274 
275 
276 protected:
277 
278  virtual void softReset() = 0;
279 
280  virtual void setupOrientation();
281 
282  // abstract method of BitmappedDisplayController
283  int getBitmapSavePixelSize() { return 2; }
284 
285  void setupGPIO();
286 
287  void hardReset();
288 
289  void sendRefresh();
290 
291  void sendScreenBuffer(Rect updateRect);
292  void writeCommand(uint8_t cmd);
293  void writeByte(uint8_t data);
294  void writeWord(uint16_t data);
295  void writeData(void * data, size_t size);
296 
297  void SPIBegin();
298  void SPIEnd();
299 
300  void SPIBeginWrite();
301  void SPIEndWrite();
302  void SPIWriteByte(uint8_t data);
303  void SPIWriteWord(uint16_t data);
304  void SPIWriteBuffer(void * data, size_t size);
305 
306  void allocViewPort();
307  void freeViewPort();
308 
309  static void updateTaskFunc(void * pvParameters);
310 
311  // abstract method of BitmappedDisplayController
312  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
313 
314  // abstract method of BitmappedDisplayController
315  void clear(Rect & updateRect);
316 
317  // abstract method of BitmappedDisplayController
318  void drawEllipse(Size const & size, Rect & updateRect);
319 
320  void VScroll(int scroll, Rect & updateRect);
321 
322  void HScroll(int scroll, Rect & updateRect);
323 
324  // abstract method of BitmappedDisplayController
325  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
326 
327  // abstract method of BitmappedDisplayController
328  void swapBuffers();
329 
330  // abstract method of BitmappedDisplayController
331  void invertRect(Rect const & rect, Rect & updateRect);
332 
333  // abstract method of BitmappedDisplayController
334  void copyRect(Rect const & source, Rect & updateRect);
335 
336  // abstract method of BitmappedDisplayController
337  void swapFGBG(Rect const & rect, Rect & updateRect);
338 
339  // abstract method of BitmappedDisplayController
340  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
341 
342  // abstract method of BitmappedDisplayController
343  void rawFillRow(int y, int x1, int x2, RGB888 color);
344 
345  void rawFillRow(int y, int x1, int x2, uint16_t pattern);
346 
347  void swapRows(int yA, int yB, int x1, int x2);
348 
349  void rawInvertRow(int y, int x1, int x2);
350 
351  // abstract method of BitmappedDisplayController
352  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
353 
354  // abstract method of BitmappedDisplayController
355  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
356 
357  // abstract method of BitmappedDisplayController
358  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
359 
360  // abstract method of BitmappedDisplayController
361  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
362 
363 
364  #ifdef ARDUINO
365  SPIClass * m_spi;
366  #endif
367 
368  spi_host_device_t m_SPIHost;
369  gpio_num_t m_SCK;
370  gpio_num_t m_MOSI;
371  gpio_num_t m_DC;
372  gpio_num_t m_RESX;
373  gpio_num_t m_CS;
374 
375  spi_device_handle_t m_SPIDevHandle;
376 
377  uint16_t * * m_viewPort;
378 
379  int16_t m_screenWidth;
380  int16_t m_screenHeight;
381  int16_t m_screenCol;
382  int16_t m_screenRow;
383 
384  int16_t m_viewPortWidth;
385  int16_t m_viewPortHeight;
386 
387  // view port size when rotation is 0 degrees
388  int16_t m_rot0ViewPortWidth;
389  int16_t m_rot0ViewPortHeight;
390 
391  // maximum width and height the controller can handle (ie 240x320)
392  int16_t m_controllerWidth;
393  int16_t m_controllerHeight;
394 
395  // offsets used on rotating
396  int16_t m_rotOffsetX;
397  int16_t m_rotOffsetY;
398 
399  TaskHandle_t m_updateTaskHandle;
400 
401  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
402  volatile bool m_updateTaskRunning;
403 
404  TFTOrientation m_orientation;
405  bool m_reverseHorizontal;
406 
407 };
408 
409 
410 } // end of namespace
411 
412 
413 
414 
415 
416 
417 
int16_t X2
Definition: fabutils.h:179
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:180
int screenRow()
Gets initial top row of the viewport.
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
int16_t Y1
Definition: fabutils.h:178
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:177
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:225
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets TFT resolution and viewport size.
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.