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 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 "freertos/FreeRTOS.h"
38 
39 #include "esp32-hal.h"
40 
41 #include "fabglconf.h"
42 #include "fabutils.h"
43 #include "displaycontroller.h"
44 #include "comdrivers/tsi2c.h"
45 
46 
47 
48 
49 namespace fabgl {
50 
51 
52 
53 
75 
76 public:
77 
78  // unwanted methods
79  SSD1306Controller(SSD1306Controller const&) = delete;
80  void operator=(SSD1306Controller const&) = delete;
81 
82 
84 
86 
94  void begin(I2C * i2c, int address = 0x3C, gpio_num_t resetGPIO = GPIO_NUM_39);
95 
96  void end();
97 
108  void setResolution(char const * modeline, int viewPortWidth = -1, int viewPortHeight = -1, bool doubleBuffered = false);
109 
115  bool available() { return m_screenBuffer != nullptr; }
116 
117  // abstract method of DisplayController
119 
120  // abstract method of DisplayController
122 
123  // abstract method of DisplayController
125 
126  // abstract method of DisplayController
127  int getViewPortWidth() { return m_viewPortWidth; }
128 
129  // abstract method of DisplayController
130  int getViewPortHeight() { return m_viewPortHeight; }
131 
132  // abstract method of DisplayController
133  int getScreenWidth() { return m_screenWidth; }
134 
135  // abstract method of DisplayController
136  int getScreenHeight() { return m_screenHeight; }
137 
145  void setScreenCol(int value);
146 
154  void setScreenRow(int value);
155 
161  int screenCol() { return m_screenCol; }
162 
168  int screenRow() { return m_screenRow; }
169 
170  void readScreen(Rect const & rect, RGB888 * destBuf);
171 
172 
173 private:
174 
175  // abstract method of DisplayController
176  PixelFormat getBitmapSavePixelFormat() { return PixelFormat::RGBA2222; }
177 
178 
179  bool SSD1306_sendData(uint8_t * buf, int count, uint8_t ctrl);
180  bool SSD1306_sendCmd(uint8_t c);
181  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2);
182  bool SSD1306_sendCmd(uint8_t c1, uint8_t c2, uint8_t c3);
183 
184  void SSD1306_hardReset();
185  bool SSD1306_softReset();
186 
187  void SSD1306_sendScreenBuffer();
188 
189  void allocScreenBuffer();
190 
191  static void updateTaskFunc(void * pvParameters);
192 
193  // abstract method of DisplayController
194  void setPixelAt(PixelDesc const & pixelDesc);
195 
196  // abstract method of DisplayController
197  void clear();
198 
199  // abstract method of DisplayController
200  void drawEllipse(Size const & size);
201 
202  void VScroll(int scroll);
203 
204  void HScroll(int scroll);
205 
206  // abstract method of DisplayController
207  void drawGlyph(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor);
208 
209  void drawGlyph_full(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor);
210 
211  void drawGlyph_light(Glyph const & glyph, GlyphOptions glyphOptions, RGB888 penColor, RGB888 brushColor);
212 
213  // abstract method of DisplayController
214  void swapBuffers();
215 
216  // abstract method of DisplayController
217  void invertRect(Rect const & rect);
218 
219  // abstract method of DisplayController
220  void copyRect(Rect const & source);
221 
222  // abstract method of DisplayController
223  void swapFGBG(Rect const & rect);
224 
225  // abstract method of DisplayController
226  void drawLine(int X1, int Y1, int X2, int Y2, RGB888 color);
227 
228  // abstract method of DisplayController
229  void fillRow(int y, int x1, int x2, RGB888 color);
230 
231  // abstract method of DisplayController
232  void drawBitmap_Mask(int destX, int destY, Bitmap const * bitmap, uint8_t * saveBackground, int X1, int Y1, int XCount, int YCount);
233 
234  // abstract method of DisplayController
235  void drawBitmap_RGBA2222(int destX, int destY, Bitmap const * bitmap, uint8_t * saveBackground, int X1, int Y1, int XCount, int YCount);
236 
237  // abstract method of DisplayController
238  void drawBitmap_RGBA8888(int destX, int destY, Bitmap const * bitmap, uint8_t * saveBackground, int X1, int Y1, int XCount, int YCount);
239 
240  void copyRow(int x1, int x2, int srcY, int dstY);
241 
242  uint8_t preparePixel(RGB888 const & rgb);
243 
244 
245  I2C * m_i2c;
246  uint8_t m_i2cAddress;
247  gpio_num_t m_resetGPIO;
248 
249  uint8_t * m_screenBuffer;
250  uint8_t * m_altScreenBuffer; // used to implement double buffer
251 
252  int16_t m_screenWidth;
253  int16_t m_screenHeight;
254  int16_t m_screenCol;
255  int16_t m_screenRow;
256 
257  int16_t m_viewPortWidth;
258  int16_t m_viewPortHeight;
259 
260  TaskHandle_t m_updateTaskHandle;
261 
262  int m_updateTaskFuncSuspended; // 0 = enabled, >0 suspended
263 
264 };
265 
266 
267 } // end of namespace
268 
269 
270 
271 
272 
273 
274 
Represents a 24 bit RGB color.
Definition: displaycontroller.h:206
PixelFormat
This enum defines a pixel format.
Definition: displaycontroller.h:399
int getViewPortWidth()
Determines horizontal size of the viewport.
Definition: SSD1306Controller.h:127
void setScreenCol(int value)
Set initial left column of the viewport.
Definition: SSD1306Controller.cpp:177
This file contains fabgl::DisplayController definition.
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs...
Definition: tsi2c.h:73
NativePixelFormat
This enum defines the display controller native pixel format.
Definition: displaycontroller.h:390
int getViewPortHeight()
Determines vertical size of the viewport.
Definition: SSD1306Controller.h:130
void begin(I2C *i2c, int address=0x3C, gpio_num_t resetGPIO=GPIO_NUM_39)
Initializes SSD1306 assigning I2C bus, reset pin and address.
Definition: SSD1306Controller.cpp:120
void setScreenRow(int value)
Set initial top row of the viewport.
Definition: SSD1306Controller.cpp:184
int getScreenHeight()
Determines the screen height in pixels.
Definition: SSD1306Controller.h:136
This file contains some utility classes and functions.
Definition: canvas.cpp:31
NativePixelFormat nativePixelFormat()
Represents the native pixel format used by this display.
Definition: SSD1306Controller.h:124
Represents a rectangle.
Definition: fabutils.h:158
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets SSD1306 resolution and viewport size.
Definition: SSD1306Controller.cpp:142
This file contains fabgl::I2C definition.
This file contains FabGL library configuration settings, like number of supported colors...
int screenRow()
Gets initial top row of the viewport.
Definition: SSD1306Controller.h:168
Represents the base abstract class for display controllers.
Definition: displaycontroller.h:596
bool available()
Checks the SSD1306 device availability.
Definition: SSD1306Controller.h:115
int screenCol()
Gets initial left column of the viewport.
Definition: SSD1306Controller.h:161
void suspendBackgroundPrimitiveExecution()
Suspends drawings.
Definition: SSD1306Controller.cpp:327
Display driver for SSD1306 based OLED display, with I2C connection.
Definition: SSD1306Controller.h:74
int getScreenWidth()
Determines the screen width in pixels.
Definition: SSD1306Controller.h:133
void resumeBackgroundPrimitiveExecution()
Resumes drawings after suspendBackgroundPrimitiveExecution().
Definition: SSD1306Controller.cpp:333