FabGL
ESP32 Display Controller and Graphics Library
SSD1306Controller.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 
31 #ifdef ARDUINO
32 
33 
41 #include <stdint.h>
42 #include <stddef.h>
43 
44 #include "freertos/FreeRTOS.h"
45 
46 #include "fabglconf.h"
47 #include "fabutils.h"
48 #include "displaycontroller.h"
49 #include "comdrivers/tsi2c.h"
50 
51 
52 
53 
54 namespace fabgl {
55 
56 
57 
61 enum class SSD1306Orientation {
62  Normal,
65  Rotate180,
66 };
67 
68 
92 class SSD1306Controller : public GenericBitmappedDisplayController {
93 
94 public:
95 
96  // unwanted methods
97  SSD1306Controller(SSD1306Controller const&) = delete;
98  void operator=(SSD1306Controller const&) = delete;
99 
100 
102 
104 
112  void begin(I2C * i2c, int address = 0x3C, gpio_num_t resetGPIO = GPIO_UNUSED);
113 
123  void begin();
124 
125  void end();
126 
137  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
138 
144  bool available() { return m_screenBuffer != nullptr; }
145 
146  // abstract method of BitmappedDisplayController
148 
149  // abstract method of BitmappedDisplayController
151 
152  // abstract method of BitmappedDisplayController
154 
155  // abstract method of BitmappedDisplayController
156  int getViewPortWidth() { return m_viewPortWidth; }
157 
158  // abstract method of BitmappedDisplayController
159  int getViewPortHeight() { return m_viewPortHeight; }
160 
168  void setScreenCol(int value);
169 
177  void setScreenRow(int value);
178 
184  int screenCol() { return m_screenCol; }
185 
191  int screenRow() { return m_screenRow; }
192 
193  void readScreen(Rect const & rect, RGB888 * destBuf);
194 
200  void invert(bool value);
201 
213 
214 
215 private:
216 
217  // abstract method of BitmappedDisplayController
218  int getBitmapSavePixelSize() { return 1; }
219 
220 
221  bool SSD1306_sendData(uint8_t * buf, int count, uint8_t ctrl);
222  bool SSD1306_sendCmd(uint8_t c);
223  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2);
224  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2, uint8_t c3);
225 
226  void SSD1306_hardReset();
227  bool SSD1306_softReset();
228 
229  void SSD1306_sendScreenBuffer(Rect updateRect);
230 
231  void sendRefresh();
232 
233  void setupOrientation();
234 
235  void allocScreenBuffer();
236 
237  static void updateTaskFunc(void * pvParameters);
238 
239  // abstract method of BitmappedDisplayController
240  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
241 
242  // abstract method of BitmappedDisplayController
243  void clear(Rect & updateRect);
244 
245  // abstract method of BitmappedDisplayController
246  void drawEllipse(Size const & size, Rect & updateRect);
247 
248  void VScroll(int scroll, Rect & updateRect);
249 
250  void HScroll(int scroll, Rect & updateRect);
251 
252  // abstract method of BitmappedDisplayController
253  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
254 
255  // abstract method of BitmappedDisplayController
256  void swapBuffers();
257 
258  // abstract method of BitmappedDisplayController
259  void invertRect(Rect const & rect, Rect & updateRect);
260 
261  // abstract method of BitmappedDisplayController
262  void copyRect(Rect const & source, Rect & updateRect);
263 
264  // abstract method of BitmappedDisplayController
265  void swapFGBG(Rect const & rect, Rect & updateRect);
266 
267  // abstract method of BitmappedDisplayController
268  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
269 
270  // abstract method of BitmappedDisplayController
271  void rawFillRow(int y, int x1, int x2, RGB888 color);
272 
273  void rawFillRow(int y, int x1, int x2, uint8_t pattern);
274 
275  void rawInvertRow(int y, int x1, int x2);
276 
277  // abstract method of BitmappedDisplayController
278  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
279 
280  // abstract method of BitmappedDisplayController
281  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
282 
283  // abstract method of BitmappedDisplayController
284  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
285 
286  // abstract method of BitmappedDisplayController
287  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
288 
289  void rawCopyRow(int x1, int x2, int srcY, int dstY);
290 
291 
292  I2C * m_i2c;
293  uint8_t m_i2cAddress;
294  gpio_num_t m_resetGPIO;
295 
296  uint8_t * m_screenBuffer;
297 
298  int16_t m_screenWidth;
299  int16_t m_screenHeight;
300  int16_t m_screenCol;
301  int16_t m_screenRow;
302 
303  int16_t m_viewPortWidth;
304  int16_t m_viewPortHeight;
305 
306  TaskHandle_t m_updateTaskHandle;
307 
308  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
309  volatile bool m_updateTaskRunning;
310 
311  SSD1306Orientation m_orientation;
312 
313 };
314 
315 
316 } // end of namespace
317 
318 
319 
320 #endif // #ifdef ARDUINO
321 
322 
323 
324 
int16_t X2
Definition: fabutils.h:179
Represents a 24 bit RGB color.
void invert(bool value)
Inverts display colors.
void begin()
Initializes SSD1306.
int16_t Y2
Definition: fabutils.h:180
void setOrientation(SSD1306Orientation value)
Set display orientation and rotation.
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets SSD1306 resolution and viewport size.
int screenCol()
Gets initial left column of the viewport.
int16_t Y1
Definition: fabutils.h:178
int getViewPortHeight()
Determines vertical size of the viewport.
void setScreenCol(int value)
Set initial left column of the viewport.
This file contains fabgl::BitmappedDisplayController definition.
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:80
int16_t X1
Definition: fabutils.h:177
NativePixelFormat nativePixelFormat()
Represents the native pixel format used by this display.
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
This file contains some utility classes and functions.
Definition: canvas.cpp:36
SSD1306Orientation
This enum defines SSD1306 orientation.
NativePixelFormat
This enum defines the display controller native pixel format.
void setScreenRow(int value)
Set initial top row of the viewport.
Represents a rectangle.
Definition: fabutils.h:225
int getViewPortWidth()
Determines horizontal size of the viewport.
This file contains fabgl::I2C definition.
bool available()
Checks the SSD1306 device availability.
This file contains FabGL library configuration settings, like number of supported colors...
void suspendBackgroundPrimitiveExecution()
Suspends drawings.
Display driver for SSD1306 based OLED display, with I2C connection.
int screenRow()
Gets initial top row of the viewport.