SSD1306 OLED display driver  1.6.99
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 
101 protected:
102 
104  uint8_t *m_buffer;
105 
106 private:
107  inline void rotatePosition(int16_t &x, int16_t &y)
108  {
109  switch (getRotation()) {
110  case 1:
111  ssd1306_swap_data(x, y, int16_t);
112  x = WIDTH - x - 1;
113  break;
114  case 2:
115  x = WIDTH - x - 1;
116  y = HEIGHT - y - 1;
117  break;
118  case 3:
119  ssd1306_swap_data(x, y, int16_t);
120  y = HEIGHT - y - 1;
121  break;
122  }
123 
124  }
125 };
126 
130 template <uint8_t BPP>
132 {
133 public:
135 
141  virtual void blt(lcdint_t x, lcdint_t y) = 0;
142 
146  virtual void blt() = 0;
147 };
148 
150 //
151 // 1-BIT GRAPHICS
152 //
154 
161 {
162 public:
163  using AdafruitCanvasBase::AdafruitCanvasBase;
164 
170  void blt(lcdint_t x, lcdint_t y) override
171  {
172  ssd1306_drawBufferFast(x, y, WIDTH, HEIGHT, m_buffer);
173  }
174 
178  void blt() override
179  {
180  ssd1306_drawBufferFast(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
181  }
182 };
183 
184 #ifndef DOXYGEN_SHOULD_SKIP_THIS
185 template <>
186 void AdafruitCanvasOps<1>::drawPixel(int16_t x, int16_t y, uint16_t color)
187 {
188  x -= offset.x;
189  y -= offset.y;
190  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
191  {
192  return;
193  }
194  rotatePosition(x, y);
195 
196  switch (color)
197  {
198  case 1: m_buffer[x+ (y/8)*WIDTH] |= (1 << (y&7)); break;
199  case 0: m_buffer[x+ (y/8)*WIDTH] &= ~(1 << (y&7)); break;
200  case 2: m_buffer[x+ (y/8)*WIDTH] ^= (1 << (y&7)); break;
201  }
202 }
203 #endif // DOXYGEN_SHOULD_SKIP_THIS
204 
206 //
207 // 8-BIT GRAPHICS
208 //
210 
217 {
218 public:
219  using AdafruitCanvasBase::AdafruitCanvasBase;
220 
226  void blt(lcdint_t x, lcdint_t y) override
227  {
228  ssd1331_drawBufferFast8(x, y, WIDTH, HEIGHT, m_buffer);
229  }
230 
234  void blt() override
235  {
236  ssd1331_drawBufferFast8(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
237  }
238 };
239 
240 #ifndef DOXYGEN_SHOULD_SKIP_THIS
241 template <>
242 void AdafruitCanvasOps<8>::drawPixel(int16_t x, int16_t y, uint16_t color)
243 {
244  x -= offset.x;
245  y -= offset.y;
246  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
247  {
248  return;
249  }
250  rotatePosition(x, y);
251 
252  m_buffer[x+y*WIDTH] = color;
253 }
254 #endif // DOXYGEN_SHOULD_SKIP_THIS
255 
257 //
258 // 16-BIT GRAPHICS
259 //
261 
269 {
270 public:
271  using AdafruitCanvasBase::AdafruitCanvasBase;
272 
278  void blt(lcdint_t x, lcdint_t y) override
279  {
280  ssd1331_drawBufferFast16(x, y, WIDTH, HEIGHT, m_buffer);
281  }
282 
286  void blt() override
287  {
288  ssd1331_drawBufferFast16(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
289  }
290 };
291 
292 #ifndef DOXYGEN_SHOULD_SKIP_THIS
293 template <>
294 void AdafruitCanvasOps<16>::drawPixel(int16_t x, int16_t y, uint16_t color)
295 {
296  x -= offset.x;
297  y -= offset.y;
298  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
299  {
300  return;
301  }
302  rotatePosition(x, y);
303 
304  m_buffer[(x+y*WIDTH) * 2 + 0] = color;
305  m_buffer[(x+y*WIDTH) * 2 + 1] = color >> 8;
306 }
307 #endif // DOXYGEN_SHOULD_SKIP_THIS
308 
309 #endif // CONFIG_ADAFRUIT_GFX_ENABLE
310 
311 #endif
AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
Definition: adafruit.h:84
#define ssd1306_swap_data(a, b, type)
Definition: io.h:58
void ssd1331_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1331_api.c:108
void blt() override
Definition: adafruit.h:178
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:278
void ssd1306_drawBufferFast(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *buf)
lcdint_t y
Definition: canvas.h:50
void ssd1331_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1331_api.c:96
uint8_t * m_buffer
Definition: adafruit.h:104
static const uint8_t BITS_PER_PIXEL
Definition: adafruit.h:74
void blt() override
Definition: adafruit.h:286
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:226
void blt() override
Definition: adafruit.h:234
lcdint_t x
Definition: canvas.h:48
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:170
void drawPixel(int16_t x, int16_t y, uint16_t color) override
NanoPoint offset
Definition: adafruit.h:71