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-2020 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
26 
34 #include <stdint.h>
35 #include <stddef.h>
36 
37 #ifdef ARDUINO
38  #include "SPI.h"
39 #endif
40 
41 #include "freertos/FreeRTOS.h"
42 
43 #include "driver/spi_master.h"
44 
45 #include "fabglconf.h"
46 #include "fabutils.h"
47 #include "displaycontroller.h"
48 
49 
50 #define TFT_CASET 0x2A
51 #define TFT_RASET 0x2B
52 #define TFT_RAMWR 0x2C
53 #define TFT_MADCTL 0x36
54 
55 
56 
57 namespace fabgl {
58 
59 
60 
64 enum class TFTOrientation {
65  Rotate0,
66  Rotate90,
67  Rotate180,
68  Rotate270,
69 };
70 
71 
89 class TFTController : public GenericBitmappedDisplayController {
90 
91 public:
92 
93  // unwanted methods
94  TFTController(TFTController const&) = delete;
95  void operator=(TFTController const&) = delete;
96 
97  TFTController();
98 
99  ~TFTController();
100 
118  #ifdef ARDUINO
119  void begin(SPIClass * spi, gpio_num_t DC, gpio_num_t RESX = GPIO_UNUSED, gpio_num_t CS = GPIO_UNUSED);
120  #endif
121 
139  #ifdef ARDUINO
140  void begin(SPIClass * spi, int DC, int RESX = -1, int CS = -1);
141  #endif
142 
162  void begin(int SCK, int MOSI, int DC, int RESX, int CS, int host);
163 
175  void begin();
176 
177  void end();
178 
193  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
194 
195  // abstract method of BitmappedDisplayController
197 
198  // abstract method of BitmappedDisplayController
200 
201  // abstract method of BitmappedDisplayController
203 
204  // abstract method of BitmappedDisplayController
205  int getViewPortWidth() { return m_viewPortWidth; }
206 
207  // abstract method of BitmappedDisplayController
208  int getViewPortHeight() { return m_viewPortHeight; }
209 
217  void setScreenCol(int value);
218 
226  void setScreenRow(int value);
227 
233  int screenCol() { return m_screenCol; }
234 
240  int screenRow() { return m_screenRow; }
241 
242  void readScreen(Rect const & rect, RGB888 * destBuf);
243 
247  void reset() { hardReset(); softReset(); }
248 
259  void setOrientation(TFTOrientation value, bool force = false);
260 
268  void setReverseHorizontal(bool value);
269 
270 
271 protected:
272 
273  virtual void softReset() = 0;
274 
275  virtual void setupOrientation();
276 
277  // abstract method of BitmappedDisplayController
278  int getBitmapSavePixelSize() { return 2; }
279 
280  void setupGPIO();
281 
282  void hardReset();
283 
284  void sendRefresh();
285 
286  void sendScreenBuffer(Rect updateRect);
287  void writeCommand(uint8_t cmd);
288  void writeByte(uint8_t data);
289  void writeWord(uint16_t data);
290  void writeData(void * data, size_t size);
291 
292  void SPIBegin();
293  void SPIEnd();
294 
295  void SPIBeginWrite();
296  void SPIEndWrite();
297  void SPIWriteByte(uint8_t data);
298  void SPIWriteWord(uint16_t data);
299  void SPIWriteBuffer(void * data, size_t size);
300 
301  void allocViewPort();
302  void freeViewPort();
303 
304  static void updateTaskFunc(void * pvParameters);
305 
306  // abstract method of BitmappedDisplayController
307  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
308 
309  // abstract method of BitmappedDisplayController
310  void clear(Rect & updateRect);
311 
312  // abstract method of BitmappedDisplayController
313  void drawEllipse(Size const & size, Rect & updateRect);
314 
315  void VScroll(int scroll, Rect & updateRect);
316 
317  void HScroll(int scroll, Rect & updateRect);
318 
319  // abstract method of BitmappedDisplayController
320  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
321 
322  // abstract method of BitmappedDisplayController
323  void swapBuffers();
324 
325  // abstract method of BitmappedDisplayController
326  void invertRect(Rect const & rect, Rect & updateRect);
327 
328  // abstract method of BitmappedDisplayController
329  void copyRect(Rect const & source, Rect & updateRect);
330 
331  // abstract method of BitmappedDisplayController
332  void swapFGBG(Rect const & rect, Rect & updateRect);
333 
334  // abstract method of BitmappedDisplayController
335  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
336 
337  // abstract method of BitmappedDisplayController
338  void rawFillRow(int y, int x1, int x2, RGB888 color);
339 
340  void rawFillRow(int y, int x1, int x2, uint16_t pattern);
341 
342  void swapRows(int yA, int yB, int x1, int x2);
343 
344  void rawInvertRow(int y, int x1, int x2);
345 
346  // abstract method of BitmappedDisplayController
347  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
348 
349  // abstract method of BitmappedDisplayController
350  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
351 
352  // abstract method of BitmappedDisplayController
353  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
354 
355  // abstract method of BitmappedDisplayController
356  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
357 
358 
359  #ifdef ARDUINO
360  SPIClass * m_spi;
361  #endif
362 
363  spi_host_device_t m_SPIHost;
364  gpio_num_t m_SCK;
365  gpio_num_t m_MOSI;
366  gpio_num_t m_DC;
367  gpio_num_t m_RESX;
368  gpio_num_t m_CS;
369 
370  spi_device_handle_t m_SPIDevHandle;
371 
372  uint16_t * * m_viewPort;
373 
374  int16_t m_screenWidth;
375  int16_t m_screenHeight;
376  int16_t m_screenCol;
377  int16_t m_screenRow;
378 
379  int16_t m_viewPortWidth;
380  int16_t m_viewPortHeight;
381 
382  // view port size when rotation is 0 degrees
383  int16_t m_rot0ViewPortWidth;
384  int16_t m_rot0ViewPortHeight;
385 
386  // maximum width and height the controller can handle (ie 240x320)
387  int16_t m_controllerWidth;
388  int16_t m_controllerHeight;
389 
390  // offsets used on rotating
391  int16_t m_rotOffsetX;
392  int16_t m_rotOffsetY;
393 
394  TaskHandle_t m_updateTaskHandle;
395 
396  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
397  volatile bool m_updateTaskRunning;
398 
399  TFTOrientation m_orientation;
400  bool m_reverseHorizontal;
401 
402 };
403 
404 
405 } // end of namespace
406 
407 
408 
409 
410 
411 
412 
int16_t X2
Definition: fabutils.h:150
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:151
int screenRow()
Gets initial top row of the viewport.
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
int16_t Y1
Definition: fabutils.h:149
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:148
This file contains some utility classes and functions.
Definition: canvas.cpp:31
TFTOrientation
This enum defines TFT orientation.
NativePixelFormat
This enum defines the display controller native pixel format.
Represents a rectangle.
Definition: fabutils.h:191
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.