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-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 
27 #ifdef ARDUINO
28 
29 
37 #include <stdint.h>
38 #include <stddef.h>
39 
40 #include "freertos/FreeRTOS.h"
41 
42 #include "fabglconf.h"
43 #include "fabutils.h"
44 #include "displaycontroller.h"
45 #include "comdrivers/tsi2c.h"
46 
47 
48 
49 
50 namespace fabgl {
51 
52 
53 
57 enum class SSD1306Orientation {
58  Normal,
61  Rotate180,
62 };
63 
64 
88 class SSD1306Controller : public GenericBitmappedDisplayController {
89 
90 public:
91 
92  // unwanted methods
93  SSD1306Controller(SSD1306Controller const&) = delete;
94  void operator=(SSD1306Controller const&) = delete;
95 
96 
98 
100 
108  void begin(I2C * i2c, int address = 0x3C, gpio_num_t resetGPIO = GPIO_UNUSED);
109 
119  void begin();
120 
121  void end();
122 
133  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
134 
140  bool available() { return m_screenBuffer != nullptr; }
141 
142  // abstract method of BitmappedDisplayController
144 
145  // abstract method of BitmappedDisplayController
147 
148  // abstract method of BitmappedDisplayController
150 
151  // abstract method of BitmappedDisplayController
152  int getViewPortWidth() { return m_viewPortWidth; }
153 
154  // abstract method of BitmappedDisplayController
155  int getViewPortHeight() { return m_viewPortHeight; }
156 
164  void setScreenCol(int value);
165 
173  void setScreenRow(int value);
174 
180  int screenCol() { return m_screenCol; }
181 
187  int screenRow() { return m_screenRow; }
188 
189  void readScreen(Rect const & rect, RGB888 * destBuf);
190 
196  void invert(bool value);
197 
209 
210 
211 private:
212 
213  // abstract method of BitmappedDisplayController
214  int getBitmapSavePixelSize() { return 1; }
215 
216 
217  bool SSD1306_sendData(uint8_t * buf, int count, uint8_t ctrl);
218  bool SSD1306_sendCmd(uint8_t c);
219  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2);
220  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2, uint8_t c3);
221 
222  void SSD1306_hardReset();
223  bool SSD1306_softReset();
224 
225  void SSD1306_sendScreenBuffer(Rect updateRect);
226 
227  void sendRefresh();
228 
229  void setupOrientation();
230 
231  void allocScreenBuffer();
232 
233  static void updateTaskFunc(void * pvParameters);
234 
235  // abstract method of BitmappedDisplayController
236  void setPixelAt(PixelDesc const & pixelDesc, Rect & updateRect);
237 
238  // abstract method of BitmappedDisplayController
239  void clear(Rect & updateRect);
240 
241  // abstract method of BitmappedDisplayController
242  void drawEllipse(Size const & size, Rect & updateRect);
243 
244  void VScroll(int scroll, Rect & updateRect);
245 
246  void HScroll(int scroll, Rect & updateRect);
247 
248  // abstract method of BitmappedDisplayController
249  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor, Rect & updateRect);
250 
251  // abstract method of BitmappedDisplayController
252  void swapBuffers();
253 
254  // abstract method of BitmappedDisplayController
255  void invertRect(Rect const & rect, Rect & updateRect);
256 
257  // abstract method of BitmappedDisplayController
258  void copyRect(Rect const & source, Rect & updateRect);
259 
260  // abstract method of BitmappedDisplayController
261  void swapFGBG(Rect const & rect, Rect & updateRect);
262 
263  // abstract method of BitmappedDisplayController
264  void absDrawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
265 
266  // abstract method of BitmappedDisplayController
267  void rawFillRow(int y, int x1, int x2, RGB888 color);
268 
269  void rawFillRow(int y, int x1, int x2, uint8_t pattern);
270 
271  void rawInvertRow(int y, int x1, int x2);
272 
273  // abstract method of BitmappedDisplayController
274  void rawDrawBitmap_Native(int destX, int destY, Bitmap const * bitmap, int X1, int Y1, int XCount, int YCount);
275 
276  // abstract method of BitmappedDisplayController
277  void rawDrawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
278 
279  // abstract method of BitmappedDisplayController
280  void rawDrawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
281 
282  // abstract method of BitmappedDisplayController
283  void rawDrawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, void * saveBackground, int X1, int Y1, int XCount, int YCount);
284 
285  void rawCopyRow(int x1, int x2, int srcY, int dstY);
286 
287 
288  I2C * m_i2c;
289  uint8_t m_i2cAddress;
290  gpio_num_t m_resetGPIO;
291 
292  uint8_t * m_screenBuffer;
293 
294  int16_t m_screenWidth;
295  int16_t m_screenHeight;
296  int16_t m_screenCol;
297  int16_t m_screenRow;
298 
299  int16_t m_viewPortWidth;
300  int16_t m_viewPortHeight;
301 
302  TaskHandle_t m_updateTaskHandle;
303 
304  volatile int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
305  volatile bool m_updateTaskRunning;
306 
307  SSD1306Orientation m_orientation;
308 
309 };
310 
311 
312 } // end of namespace
313 
314 
315 
316 #endif // #ifdef ARDUINO
317 
318 
319 
320 
int16_t X2
Definition: fabutils.h:150
Represents a 24 bit RGB color.
void invert(bool value)
Inverts display colors.
void begin()
Initializes SSD1306.
int16_t Y2
Definition: fabutils.h:151
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:149
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:76
int16_t X1
Definition: fabutils.h:148
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:31
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:191
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.