FabGL
ESP32 Display Controller and Graphics Library
ST7789Controller.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 #include "SPI.h"
38 
39 #include "freertos/FreeRTOS.h"
40 
41 #include "esp32-hal.h"
42 #include "driver/spi_master.h"
43 
44 #include "fabglconf.h"
45 #include "fabutils.h"
46 #include "displaycontroller.h"
47 
48 
49 
50 
51 
52 namespace fabgl {
53 
54 
55 
56 
74 class ST7789Controller : public GenericDisplayController {
75 
76 public:
77 
78  // unwanted methods
79  ST7789Controller(ST7789Controller const&) = delete;
80  void operator=(ST7789Controller const&) = delete;
81 
83 
85 
103  void begin(SPIClass * spi, gpio_num_t DC, gpio_num_t RESX = GPIO_UNUSED, gpio_num_t CS = GPIO_UNUSED);
104 
122  void begin(SPIClass * spi, int DC, int RESX = -1, int CS = -1);
123 
143  void begin(int SCK, int MOSI, int DC, int RESX, int CS, int host);
144 
145  void end();
146 
161  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
162 
163  // abstract method of DisplayController
165 
166  // abstract method of DisplayController
168 
169  // abstract method of DisplayController
171 
172  // abstract method of DisplayController
173  int getViewPortWidth() { return m_viewPortWidth; }
174 
175  // abstract method of DisplayController
176  int getViewPortHeight() { return m_viewPortHeight; }
177 
178  // abstract method of DisplayController
179  int getScreenWidth() { return m_screenWidth; }
180 
181  // abstract method of DisplayController
182  int getScreenHeight() { return m_screenHeight; }
183 
191  void setScreenCol(int value);
192 
200  void setScreenRow(int value);
201 
207  int screenCol() { return m_screenCol; }
208 
214  int screenRow() { return m_screenRow; }
215 
216  void readScreen(Rect const & rect, RGB888 * destBuf);
217 
221  void reset() { hardReset(); softReset(); }
222 
223 
224 private:
225 
226  // abstract method of DisplayController
227  int getBitmapSavePixelSize() { return 2; }
228 
229  void setupGPIO();
230 
231  void hardReset();
232  void softReset();
233 
234  void sendScreenBuffer(Rect updateRect);
235  void writeCommand(uint8_t cmd);
236  void writeByte(uint8_t data);
237  void writeWord(uint16_t data);
238  void writeData(void * data, size_t size);
239 
240  void SPIBegin();
241  void SPIEnd();
242 
243  void SPIBeginWrite();
244  void SPIEndWrite();
245  void SPIWriteByte(uint8_t data);
246  void SPIWriteWord(uint16_t data);
247  void SPIWriteBuffer(void * data, size_t size);
248 
249  void allocViewPort();
250  void freeViewPort();
251 
252  static void updateTaskFunc(void * pvParameters);
253 
254  // abstract method of DisplayController
255  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
256 
257  // abstract method of DisplayController
258  void clear(Rect & updateRect);
259 
260  // abstract method of DisplayController
261  void drawEllipse(Size const & size, Rect & updateRect);
262 
263  void VScroll(int scroll, Rect & updateRect);
264 
265  void HScroll(int scroll, Rect & updateRect);
266 
267  // abstract method of DisplayController
268  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
269 
270  // abstract method of DisplayController
271  void swapBuffers();
272 
273  // abstract method of DisplayController
274  void invertRect(Rect const & rect, Rect & updateRect);
275 
276  // abstract method of DisplayController
277  void copyRect(Rect const & source, Rect & updateRect);
278 
279  // abstract method of DisplayController
280  void swapFGBG(Rect const & rect, Rect & updateRect);
281 
282  // abstract method of DisplayController
283  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
284 
285  // abstract method of DisplayController
286  void rawFillRow(int y, int x1, int x2, RGB888 color);
287 
288  void rawFillRow(int y, int x1, int x2, uint16_t pattern);
289 
290  void swapRows(int yA, int yB, int x1, int x2);
291 
292  void rawInvertRow(int y, int x1, int x2);
293 
294  // abstract method of DisplayController
295  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
296 
297  // abstract method of DisplayController
298  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
299 
300  // abstract method of DisplayController
301  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
302 
303  // abstract method of DisplayController
304  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
305 
306 
307  SPIClass * m_spi;
308 
309  spi_host_device_t m_SPIHost;
310  gpio_num_t m_SCK;
311  gpio_num_t m_MOSI;
312  gpio_num_t m_DC;
313  gpio_num_t m_RESX;
314  gpio_num_t m_CS;
315 
316  spi_device_handle_t m_SPIDevHandle;
317 
318  // when double buffer is enabled the "drawing" view port is always m_viewPort, while the "visible" view port is always m_viewPortVisible
319  // when double buffer is not enabled then m_viewPort = m_viewPortVisible
320  uint16_t * * m_viewPort;
321  uint16_t * * m_viewPortVisible;
322 
323  int16_t m_screenWidth;
324  int16_t m_screenHeight;
325  int16_t m_screenCol;
326  int16_t m_screenRow;
327 
328  int16_t m_viewPortWidth;
329  int16_t m_viewPortHeight;
330 
331  TaskHandle_t m_updateTaskHandle;
332 
333  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
334  volatile bool m_updateTaskRunning;
335 
336 };
337 
338 
339 } // end of namespace
340 
341 
342 
343 
344 
345 
346 
Represents a 24 bit RGB color.
Definition: displaycontroller.h:218
void suspendBackgroundPrimitiveExecution()
Suspends drawings.
Definition: ST7789Controller.cpp:696
int getViewPortWidth()
Determines horizontal size of the viewport.
Definition: ST7789Controller.h:173
void setScreenRow(int value)
Set initial top row of the viewport.
Definition: ST7789Controller.cpp:257
void begin(SPIClass *spi, gpio_num_t DC, gpio_num_t RESX=GPIO_UNUSED, gpio_num_t CS=GPIO_UNUSED)
Initializes ST7789 display controller with Arduino style SPIClass object.
Definition: ST7789Controller.cpp:167
This file contains fabgl::DisplayController definition.
NativePixelFormat
This enum defines the display controller native pixel format.
Definition: displaycontroller.h:402
int16_t Y1
Definition: fabutils.h:106
int getViewPortHeight()
Determines vertical size of the viewport.
Definition: ST7789Controller.h:176
int getScreenWidth()
Determines the screen width in pixels.
Definition: ST7789Controller.h:179
int screenCol()
Gets initial left column of the viewport.
Definition: ST7789Controller.h:207
uint8_t const * data
Definition: displaycontroller.h:290
This file contains some utility classes and functions.
NativePixelFormat nativePixelFormat()
Represents the native pixel format used by this display.
Definition: ST7789Controller.h:170
Definition: canvas.cpp:31
Represents a rectangle.
Definition: fabutils.h:162
int getScreenHeight()
Determines the screen height in pixels.
Definition: ST7789Controller.h:182
int16_t X2
Definition: fabutils.h:107
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
Definition: ST7789Controller.cpp:704
void setScreenCol(int value)
Set initial left column of the viewport.
Definition: ST7789Controller.cpp:247
This file contains FabGL library configuration settings, like number of supported colors...
Display driver for ST7789 based OLED display, with SPI connection.
Definition: ST7789Controller.h:74
int16_t Y2
Definition: fabutils.h:108
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets ST7789 resolution and viewport size.
Definition: ST7789Controller.cpp:214
int screenRow()
Gets initial top row of the viewport.
Definition: ST7789Controller.h:214
void reset()
Performs display hardware and software.
Definition: ST7789Controller.h:221
int16_t X1
Definition: fabutils.h:105