SSD1306 OLED display driver  1.7.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"
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 
71 template <uint8_t BPP>
72 class AdafruitCanvasOps: public Adafruit_GFX
73 {
74 public:
77 
79  static const uint8_t BITS_PER_PIXEL = BPP;
80 
89  AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
90  : Adafruit_GFX(w, h)
91  , offset{0}
92  , m_buffer(buffer)
93  {
94  }
95 
104  void drawPixel(int16_t x, int16_t y, uint16_t color) override;
105 
111  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
112 
113 #ifndef DOXYGEN_SHOULD_SKIP_THIS
114  // We need to override Adafruit GFX implementation of fillScreen, because
115  // NanoEngine uses offsets, when refreshing screen content.
116  void fillScreen(uint16_t color) override
117  {
118  fillRect(offset.x, offset.y, _width, _height, color);
119  }
120 #endif
121 
122 protected:
123 
125  uint8_t *m_buffer;
126 
127 private:
128  inline void rotatePosition(int16_t &x, int16_t &y)
129  {
130  switch (getRotation()) {
131  case 1:
132  ssd1306_swap_data(x, y, int16_t);
133  x = WIDTH - x - 1;
134  break;
135  case 2:
136  x = WIDTH - x - 1;
137  y = HEIGHT - y - 1;
138  break;
139  case 3:
140  ssd1306_swap_data(x, y, int16_t);
141  y = HEIGHT - y - 1;
142  break;
143  }
144 
145  }
146 };
147 
151 template <uint8_t BPP>
153 {
154 public:
156 
162  virtual void blt(lcdint_t x, lcdint_t y) = 0;
163 
167  virtual void blt() = 0;
168 };
169 
171 //
172 // 1-BIT GRAPHICS
173 //
175 
182 {
183 public:
184  using AdafruitCanvasBase::AdafruitCanvasBase;
185 
191  void blt(lcdint_t x, lcdint_t y) override
192  {
193  ssd1306_drawBufferFast(x, y, WIDTH, HEIGHT, m_buffer);
194  }
195 
199  void blt() override
200  {
201  ssd1306_drawBufferFast(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
202  }
203 };
204 
205 #ifndef DOXYGEN_SHOULD_SKIP_THIS
206 template <>
207 void AdafruitCanvasOps<1>::drawPixel(int16_t x, int16_t y, uint16_t color)
208 {
209  x -= offset.x;
210  y -= offset.y;
211  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
212  {
213  return;
214  }
215  rotatePosition(x, y);
216 
217  switch (color)
218  {
219  case 1: m_buffer[x+ (y/8)*WIDTH] |= (1 << (y&7)); break;
220  case 0: m_buffer[x+ (y/8)*WIDTH] &= ~(1 << (y&7)); break;
221  case 2: m_buffer[x+ (y/8)*WIDTH] ^= (1 << (y&7)); break;
222  }
223 }
224 #endif // DOXYGEN_SHOULD_SKIP_THIS
225 
227 //
228 // 8-BIT GRAPHICS
229 //
231 
238 {
239 public:
240  using AdafruitCanvasBase::AdafruitCanvasBase;
241 
247  void blt(lcdint_t x, lcdint_t y) override
248  {
249  ssd1331_drawBufferFast8(x, y, WIDTH, HEIGHT, m_buffer);
250  }
251 
255  void blt() override
256  {
257  ssd1331_drawBufferFast8(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
258  }
259 };
260 
261 #ifndef DOXYGEN_SHOULD_SKIP_THIS
262 template <>
263 void AdafruitCanvasOps<8>::drawPixel(int16_t x, int16_t y, uint16_t color)
264 {
265  x -= offset.x;
266  y -= offset.y;
267  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
268  {
269  return;
270  }
271  rotatePosition(x, y);
272 
273  m_buffer[x+y*WIDTH] = color;
274 }
275 #endif // DOXYGEN_SHOULD_SKIP_THIS
276 
278 //
279 // 16-BIT GRAPHICS
280 //
282 
290 {
291 public:
292  using AdafruitCanvasBase::AdafruitCanvasBase;
293 
299  void blt(lcdint_t x, lcdint_t y) override
300  {
301  ssd1331_drawBufferFast16(x, y, WIDTH, HEIGHT, m_buffer);
302  }
303 
307  void blt() override
308  {
309  ssd1331_drawBufferFast16(offset.x, offset.y, WIDTH, HEIGHT, m_buffer);
310  }
311 };
312 
313 #ifndef DOXYGEN_SHOULD_SKIP_THIS
314 template <>
315 void AdafruitCanvasOps<16>::drawPixel(int16_t x, int16_t y, uint16_t color)
316 {
317  x -= offset.x;
318  y -= offset.y;
319  if ((x < 0) || (x >= width()) || (y < 0) || (y >= height()))
320  {
321  return;
322  }
323  rotatePosition(x, y);
324 
325  m_buffer[(x+y*WIDTH) * 2 + 0] = color;
326  m_buffer[(x+y*WIDTH) * 2 + 1] = color >> 8;
327 }
328 #endif // DOXYGEN_SHOULD_SKIP_THIS
329 
334 #endif // CONFIG_ADAFRUIT_GFX_ENABLE
335 
336 #endif
AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
Definition: adafruit.h:89
void blt() override
Definition: adafruit.h:199
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:299
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: adafruit.h:111
virtual void blt()=0
lcdint_t y
Definition: point.h:45
void ssd1331_drawBufferFast16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_16bit.c:30
uint8_t * m_buffer
Definition: adafruit.h:111
void ssd1331_drawBufferFast8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *data)
Definition: ssd1306_8bit.c:109
static const uint8_t BITS_PER_PIXEL
Definition: adafruit.h:79
void blt() override
Definition: adafruit.h:307
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:247
void blt() override
Definition: adafruit.h:255
lcdint_t x
Definition: point.h:43
void blt(lcdint_t x, lcdint_t y) override
Definition: adafruit.h:191
void ssd1306_drawBufferFast(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *buf)
Definition: ssd1306_1bit.c:620
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:76