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.
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  virtual int colorsCount() { return 2; }
156 
157  // abstract method of BitmappedDisplayController
158  int getViewPortWidth() { return m_viewPortWidth; }
159 
160  // abstract method of BitmappedDisplayController
161  int getViewPortHeight() { return m_viewPortHeight; }
162 
170  void setScreenCol(int value);
171 
179  void setScreenRow(int value);
180 
186  int screenCol() { return m_screenCol; }
187 
193  int screenRow() { return m_screenRow; }
194 
195  void readScreen(Rect const & rect, RGB888 * destBuf);
196 
202  void invert(bool value);
203 
215 
216 
217 private:
218 
219  // abstract method of BitmappedDisplayController
220  int getBitmapSavePixelSize() { return 1; }
221 
222 
223  bool SSD1306_sendData(uint8_t * buf, int count, uint8_t ctrl);
224  bool SSD1306_sendCmd(uint8_t c);
225  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2);
226  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2, uint8_t c3);
227 
228  void SSD1306_hardReset();
229  bool SSD1306_softReset();
230 
231  void SSD1306_sendScreenBuffer(Rect updateRect);
232 
233  void sendRefresh();
234 
235  void setupOrientation();
236 
237  void allocScreenBuffer();
238 
239  static void updateTaskFunc(void * pvParameters);
240 
241  // abstract method of BitmappedDisplayController
242  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
243 
244  // abstract method of BitmappedDisplayController
245  void clear(Rect & updateRect);
246 
247  // abstract method of BitmappedDisplayController
248  void drawEllipse(Size const & size, Rect & updateRect);
249 
250  void VScroll(int scroll, Rect & updateRect);
251 
252  void HScroll(int scroll, Rect & updateRect);
253 
254  // abstract method of BitmappedDisplayController
255  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
256 
257  // abstract method of BitmappedDisplayController
258  void swapBuffers();
259 
260  // abstract method of BitmappedDisplayController
261  void invertRect(Rect const & rect, Rect & updateRect);
262 
263  // abstract method of BitmappedDisplayController
264  void copyRect(Rect const & source, Rect & updateRect);
265 
266  // abstract method of BitmappedDisplayController
267  void swapFGBG(Rect const & rect, Rect & updateRect);
268 
269  // abstract method of BitmappedDisplayController
270  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
271 
272  // abstract method of BitmappedDisplayController
273  void rawFillRow(int y, int x1, int x2, RGB888 color);
274 
275  void rawFillRow(int y, int x1, int x2, uint8_t pattern);
276 
277  void rawInvertRow(int y, int x1, int x2);
278 
279  // abstract method of BitmappedDisplayController
280  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
281 
282  // abstract method of BitmappedDisplayController
283  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
284 
285  // abstract method of BitmappedDisplayController
286  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
287 
288  // abstract method of BitmappedDisplayController
289  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
290 
291  void rawCopyRow(int x1, int x2, int srcY, int dstY);
292 
293 
294  I2C * m_i2c;
295  uint8_t m_i2cAddress;
296  gpio_num_t m_resetGPIO;
297 
298  uint8_t * m_screenBuffer;
299 
300  int16_t m_screenWidth;
301  int16_t m_screenHeight;
302  int16_t m_screenCol;
303  int16_t m_screenRow;
304 
305  int16_t m_viewPortWidth;
306  int16_t m_viewPortHeight;
307 
308  TaskHandle_t m_updateTaskHandle;
309 
310  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
311  volatile bool m_updateTaskRunning;
312 
313  SSD1306Orientation m_orientation;
314 
315 };
316 
317 
318 } // end of namespace
319 
320 
321 
322 #endif // #ifdef ARDUINO
323 
324 
325 
326 
int16_t X2
Definition: fabutils.h:180
Represents a 24 bit RGB color.
void invert(bool value)
Inverts display colors.
void begin()
Initializes SSD1306.
int16_t Y2
Definition: fabutils.h:181
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:179
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:178
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:226
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.
virtual int colorsCount()
Determines number of colors this display can provide.
int screenRow()
Gets initial top row of the viewport.