SSD1306 OLED display driver  1.8.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
adafruit.h
Go to the documentation of this file.
1 /*
2  MIT License
3 
4  Copyright (c) 2018, Alexey Dynda
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23 */
35 #ifndef _SSD1306_ADAFRUIT_H_
36 #define _SSD1306_ADAFRUIT_H_
37 
38 #include "ssd1306_hal/io.h"
39 
40 #if defined(CONFIG_ADAFRUIT_GFX_ENABLE)
41 
42 #include "ssd1306.h"
44 #include "nano_gfx_types.h"
45 
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 /* This is special case for non-Arduino platforms, since Adafruit requires *
48  * Arduino libraries support */
49 #ifndef ARDUINO
50 #define ARDUINO 100
51 #include "Adafruit_GFX.h"
52 #undef ARDUINO
53 #else
54 #include "Adafruit_GFX.h"
55 #endif
56 
57 #endif // DOXYGEN_SHOULD_SKIP_THIS
58 
70 template <uint8_t BPP>
71 class AdafruitCanvasOps: public Adafruit_GFX
72 {
73 public:
76 
78  static const uint8_t BITS_PER_PIXEL = BPP;
79 
88  AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
89  : Adafruit_GFX(w, h)
90  , offset{0}
91  , m_buffer(buffer)
92  {
93  }
94 
103  void drawPixel(int16_t x, int16_t y, uint16_t color) override;
104 
110  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
111 
112 #ifndef DOXYGEN_SHOULD_SKIP_THIS
113  // We need to override Adafruit GFX implementation of fillScreen, because
114  // NanoEngine uses offsets, when refreshing screen content.
115  void fillScreen(uint16_t color) override
116  {
117  fillRect(offset.x, offset.y, _width, _height, color);
118  }
119 #endif
120 
121 protected:
122 
124  uint8_t *m_buffer;
125 
126 private:
127  inline void rotatePosition(int16_t &x, int16_t &y)
128  {
129  switch (getRotation()) {
130  case 1:
131  ssd1306_swap_data(x, y, int16_t);
132  x = WIDTH - x - 1;
133  break;
134  case 2:
135  x = WIDTH - x - 1;
136  y = HEIGHT - y - 1;
137  break;
138  case 3:
139  ssd1306_swap_data(x, y, int16_t);
140  y = HEIGHT - y - 1;
141  break;
142  }
143 
144  }
145 };
146 
150 template <uint8_t BPP>
152 {
153 public:
155 
161  virtual void blt(lcdint_t x, lcdint_t y) = 0;
162 
166  virtual void blt() = 0;
167 };
168 
170 //
171 // 1-BIT GRAPHICS
172 //
174 
181 {
182 public:
183  using AdafruitCanvasBase::AdafruitCanvasBase;
184 
190  void blt(lcdint_t x, lcdint_t y) override
191  {
192  ssd1306_drawBufferFast(x, y, WIDTH, HEIGHT, m_buffer);
193  }
194 
198  void blt() override
199  {
200  ssd1306_drawBufferFast(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
201  }
202 };
203 
204 #ifndef DOXYGEN_SHOULD_SKIP_THIS
205 template <>
206 void AdafruitCanvasOps<1>::drawPixel(int16_t x, int16_t y, uint16_t color)
207 {
208  x -= offset.x;
209  y -= offset.y;
210  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
211  {
212  return;
213  }
214  rotatePosition(x, y);
215 
216  switch (color)
217  {
218  case 1: m_buffer[x+ (y/8)*WIDTH] |= (1 << (y&7)); break;
219  case 0: m_buffer[x+ (y/8)*WIDTH] &= ~(1 << (y&7)); break;
220  case 2: m_buffer[x+ (y/8)*WIDTH] ^= (1 << (y&7)); break;
221  }
222 }
223 #endif // DOXYGEN_SHOULD_SKIP_THIS
224 
226 //
227 // 8-BIT GRAPHICS
228 //
230 
237 {
238 public:
239  using AdafruitCanvasBase::AdafruitCanvasBase;
240 
246  void blt(lcdint_t x, lcdint_t y) override
247  {
248  ssd1306_drawBufferFast8(x, y, WIDTH, HEIGHT, m_buffer);
249  }
250 
254  void blt() override
255  {
256  ssd1306_drawBufferFast8(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
257  }
258 };
259 
260 #ifndef DOXYGEN_SHOULD_SKIP_THIS
261 template <>
262 void AdafruitCanvasOps<8>::drawPixel(int16_t x, int16_t y, uint16_t color)
263 {
264  x -= offset.x;
265  y -= offset.y;
266  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
267  {
268  return;
269  }
270  rotatePosition(x, y);
271 
272  m_buffer[x+y*WIDTH] = color;
273 }
274 #endif // DOXYGEN_SHOULD_SKIP_THIS
275 
277 //
278 // 16-BIT GRAPHICS
279 //
281 
289 {
290 public:
291  using AdafruitCanvasBase::AdafruitCanvasBase;
292 
298  void blt(lcdint_t x, lcdint_t y) override
299  {
300  ssd1306_drawBufferFast16(x, y, WIDTH, HEIGHT, m_buffer);
301  }
302 
306  void blt() override
307  {
308  ssd1306_drawBufferFast16(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
309  }
310 };
311 
312 #ifndef DOXYGEN_SHOULD_SKIP_THIS
313 template <>
314 void AdafruitCanvasOps<16>::drawPixel(int16_t x, int16_t y, uint16_t color)
315 {
316  x -= offset.x;
317  y -= offset.y;
318  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
319  {
320  return;
321  }
322  rotatePosition(x, y);
323 
324  m_buffer[(x+y*WIDTH) * 2 + 0] = color;
325  m_buffer[(x+y*WIDTH) * 2 + 1] = color >> 8;
326 }
327 #endif // DOXYGEN_SHOULD_SKIP_THIS
328 
333 #endif // CONFIG_ADAFRUIT_GFX_ENABLE
334 
335 #endif
AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
Definition: adafruit.h:88
void blt() override
Definition: adafruit.h:198
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:298
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: adafruit.h:110
lcdint_t y
Definition: point.h:45
void ssd1306_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_16bit.c:61
uint8_t * m_buffer
Definition: adafruit.h:110
static const uint8_t BITS_PER_PIXEL
Definition: adafruit.h:78
void blt() override
Definition: adafruit.h:306
void ssd1306_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_8bit.c:106
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:246
void blt() override
Definition: adafruit.h:254
lcdint_t x
Definition: point.h:43
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:190
void ssd1306_drawBufferFast(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *buf)
Definition: ssd1306_1bit.c:725
void drawPixel(int16_t x, int16_t y, uint16_t color) override
#define ssd1306_swap_data(a, b, type)
Definition: io.h:69
NanoPoint offset
Definition: adafruit.h:75