LCDGFX LCD display driver  1.1.5
This library is developed to control SSD1306/SSD1325/SSD1327/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-2019, 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 #pragma once
36 
37 #include "lcd_hal/io.h"
38 
39 #if defined(CONFIG_ADAFRUIT_GFX_ENABLE)
40 
41 #include "nano_gfx_types.h"
42 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 /* This is special case for non-Arduino platforms, since Adafruit requires *
45  * Arduino libraries support */
46 #ifndef ARDUINO
47 #define ARDUINO 100
48 #include "Adafruit_GFX.h"
49 #undef ARDUINO
50 #else
51 #include "Adafruit_GFX.h"
52 #endif
53 
54 #endif // DOXYGEN_SHOULD_SKIP_THIS
55 
67 template <uint8_t BPP> 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 
107  {
108  offset.x = ox;
109  offset.y = oy;
110  };
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:
123  uint8_t *m_buffer;
124 
125 private:
126  inline void rotatePosition(int16_t &x, int16_t &y)
127  {
128  switch ( getRotation() )
129  {
130  case 1:
131  canvas_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  canvas_swap_data(x, y, int16_t);
140  y = HEIGHT - y - 1;
141  break;
142  }
143  }
144 };
145 
149 template <uint8_t BPP> class AdafruitCanvasBase: public AdafruitCanvasOps<BPP>
150 {
151 public:
153 
159  virtual void blt(lcdint_t x, lcdint_t y) = 0;
160 
164  virtual void blt() = 0;
165 };
166 
168 //
169 // 1-BIT GRAPHICS
170 //
172 
178 {
179 public:
180  using AdafruitCanvasBase::AdafruitCanvasBase;
181 };
182 
183 #ifndef DOXYGEN_SHOULD_SKIP_THIS
184 template <> void AdafruitCanvasOps<1>::drawPixel(int16_t x, int16_t y, uint16_t color)
185 {
186  x -= offset.x;
187  y -= offset.y;
188  if ( (x < 0) || (x >= width()) || (y < 0) || (y >= height()) )
189  {
190  return;
191  }
192  rotatePosition(x, y);
193 
194  switch ( color )
195  {
196  case 1: m_buffer[x + (y / 8) * WIDTH] |= (1 << (y & 7)); break;
197  case 0: m_buffer[x + (y / 8) * WIDTH] &= ~(1 << (y & 7)); break;
198  case 2: m_buffer[x + (y / 8) * WIDTH] ^= (1 << (y & 7)); break;
199  }
200 }
201 #endif // DOXYGEN_SHOULD_SKIP_THIS
202 
204 //
205 // 8-BIT GRAPHICS
206 //
208 
215 {
216 public:
217  using AdafruitCanvasBase::AdafruitCanvasBase;
218 };
219 
220 #ifndef DOXYGEN_SHOULD_SKIP_THIS
221 template <> void AdafruitCanvasOps<8>::drawPixel(int16_t x, int16_t y, uint16_t color)
222 {
223  x -= offset.x;
224  y -= offset.y;
225  if ( (x < 0) || (x >= width()) || (y < 0) || (y >= height()) )
226  {
227  return;
228  }
229  rotatePosition(x, y);
230 
231  m_buffer[x + y * WIDTH] = color;
232 }
233 #endif // DOXYGEN_SHOULD_SKIP_THIS
234 
236 //
237 // 16-BIT GRAPHICS
238 //
240 
248 {
249 public:
250  using AdafruitCanvasBase::AdafruitCanvasBase;
251 };
252 
253 #ifndef DOXYGEN_SHOULD_SKIP_THIS
254 template <> void AdafruitCanvasOps<16>::drawPixel(int16_t x, int16_t y, uint16_t color)
255 {
256  x -= offset.x;
257  y -= offset.y;
258  if ( (x < 0) || (x >= width()) || (y < 0) || (y >= height()) )
259  {
260  return;
261  }
262  rotatePosition(x, y);
263 
264  m_buffer[(x + y * WIDTH) * 2 + 0] = color;
265  m_buffer[(x + y * WIDTH) * 2 + 1] = color >> 8;
266 }
267 #endif // DOXYGEN_SHOULD_SKIP_THIS
268 
273 #endif // CONFIG_ADAFRUIT_GFX_ENABLE
AdafruitCanvasOps(lcduint_t w, lcduint_t h, uint8_t *buffer)
Definition: adafruit.h:84
uint8_t lcduint_t
Definition: canvas_types.h:79
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: adafruit.h:106
int8_t lcdint_t
Definition: canvas_types.h:77
lcdint_t y
Definition: point.h:44
#define canvas_swap_data(a, b, type)
uint8_t * m_buffer
Definition: adafruit.h:110
static const uint8_t BITS_PER_PIXEL
Definition: adafruit.h:74
lcdint_t x
Definition: point.h:42
void drawPixel(int16_t x, int16_t y, uint16_t color) override
NanoPoint offset
Definition: adafruit.h:71