SSD1306 OLED display driver  1.7.1
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"
43 #include "ssd1331_api.h"
45 #include "nano_gfx_types.h"
46 
47 #ifndef DOXYGEN_SHOULD_SKIP_THIS
48 /* This is special case for non-Arduino platforms, since Adafruit requires *
49  * Arduino libraries support */
50 #ifndef ARDUINO
51 #define ARDUINO 100
52 #include "Adafruit_GFX.h"
53 #undef ARDUINO
54 #else
55 #include "Adafruit_GFX.h"
56 #endif
57 
58 #endif // DOXYGEN_SHOULD_SKIP_THIS
59 
66 template <uint8_t BPP>
67 class AdafruitCanvasOps: public Adafruit_GFX
68 {
69 public:
72 
74  static const uint8_t BITS_PER_PIXEL = BPP;
75 
84  AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
85  : Adafruit_GFX(w, h)
86  , offset{0}
87  , m_buffer(buffer)
88  {
89  }
90 
99  void drawPixel(int16_t x, int16_t y, uint16_t color) override;
100 
106  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
107 
108 #ifndef DOXYGEN_SHOULD_SKIP_THIS
109  // We need to override Adafruit GFX implementation of fillScreen, because
110  // NanoEngine uses offsets, when refreshing screen content.
111  void fillScreen(uint16_t color) override
112  {
113  fillRect(offset.x, offset.y, _width, _height, color);
114  }
115 #endif
116 
117 protected:
118 
120  uint8_t *m_buffer;
121 
122 private:
123  inline void rotatePosition(int16_t &x, int16_t &y)
124  {
125  switch (getRotation()) {
126  case 1:
127  ssd1306_swap_data(x, y, int16_t);
128  x = WIDTH - x - 1;
129  break;
130  case 2:
131  x = WIDTH - x - 1;
132  y = HEIGHT - y - 1;
133  break;
134  case 3:
135  ssd1306_swap_data(x, y, int16_t);
136  y = HEIGHT - y - 1;
137  break;
138  }
139 
140  }
141 };
142 
146 template <uint8_t BPP>
148 {
149 public:
151 
157  virtual void blt(lcdint_t x, lcdint_t y) = 0;
158 
162  virtual void blt() = 0;
163 };
164 
166 //
167 // 1-BIT GRAPHICS
168 //
170 
177 {
178 public:
179  using AdafruitCanvasBase::AdafruitCanvasBase;
180 
186  void blt(lcdint_t x, lcdint_t y) override
187  {
188  ssd1306_drawBufferFast(x, y, WIDTH, HEIGHT, m_buffer);
189  }
190 
194  void blt() override
195  {
196  ssd1306_drawBufferFast(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
197  }
198 };
199 
200 #ifndef DOXYGEN_SHOULD_SKIP_THIS
201 template <>
202 void AdafruitCanvasOps<1>::drawPixel(int16_t x, int16_t y, uint16_t color)
203 {
204  x -= offset.x;
205  y -= offset.y;
206  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
207  {
208  return;
209  }
210  rotatePosition(x, y);
211 
212  switch (color)
213  {
214  case 1: m_buffer[x+ (y/8)*WIDTH] |= (1 << (y&7)); break;
215  case 0: m_buffer[x+ (y/8)*WIDTH] &= ~(1 << (y&7)); break;
216  case 2: m_buffer[x+ (y/8)*WIDTH] ^= (1 << (y&7)); break;
217  }
218 }
219 #endif // DOXYGEN_SHOULD_SKIP_THIS
220 
222 //
223 // 8-BIT GRAPHICS
224 //
226 
233 {
234 public:
235  using AdafruitCanvasBase::AdafruitCanvasBase;
236 
242  void blt(lcdint_t x, lcdint_t y) override
243  {
244  ssd1331_drawBufferFast8(x, y, WIDTH, HEIGHT, m_buffer);
245  }
246 
250  void blt() override
251  {
252  ssd1331_drawBufferFast8(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
253  }
254 };
255 
256 #ifndef DOXYGEN_SHOULD_SKIP_THIS
257 template <>
258 void AdafruitCanvasOps<8>::drawPixel(int16_t x, int16_t y, uint16_t color)
259 {
260  x -= offset.x;
261  y -= offset.y;
262  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
263  {
264  return;
265  }
266  rotatePosition(x, y);
267 
268  m_buffer[x+y*WIDTH] = color;
269 }
270 #endif // DOXYGEN_SHOULD_SKIP_THIS
271 
273 //
274 // 16-BIT GRAPHICS
275 //
277 
285 {
286 public:
287  using AdafruitCanvasBase::AdafruitCanvasBase;
288 
294  void blt(lcdint_t x, lcdint_t y) override
295  {
296  ssd1331_drawBufferFast16(x, y, WIDTH, HEIGHT, m_buffer);
297  }
298 
302  void blt() override
303  {
304  ssd1331_drawBufferFast16(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
305  }
306 };
307 
308 #ifndef DOXYGEN_SHOULD_SKIP_THIS
309 template <>
310 void AdafruitCanvasOps<16>::drawPixel(int16_t x, int16_t y, uint16_t color)
311 {
312  x -= offset.x;
313  y -= offset.y;
314  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
315  {
316  return;
317  }
318  rotatePosition(x, y);
319 
320  m_buffer[(x+y*WIDTH) * 2 + 0] = color;
321  m_buffer[(x+y*WIDTH) * 2 + 1] = color >> 8;
322 }
323 #endif // DOXYGEN_SHOULD_SKIP_THIS
324 
325 #endif // CONFIG_ADAFRUIT_GFX_ENABLE
326 
327 #endif
void ssd1331_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1331_api.c:122
AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
Definition: adafruit.h:84
void blt() override
Definition: adafruit.h:194
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:294
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: adafruit.h:106
virtual void blt()=0
lcdint_t y
Definition: point.h:40
uint8_t * m_buffer
Definition: adafruit.h:106
void ssd1331_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1331_api.c:110
static const uint8_t BITS_PER_PIXEL
Definition: adafruit.h:74
void blt() override
Definition: adafruit.h:302
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:242
void blt() override
Definition: adafruit.h:250
lcdint_t x
Definition: point.h:38
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:186
void ssd1306_drawBufferFast(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *buf)
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:71