SSD1306 OLED display driver  1.5.6
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
canvas.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 */
29 #ifndef _NANO_CANVAS_H_
30 #define _NANO_CANVAS_H_
31 
32 #include "hal/io.h"
33 #include "nano_gfx_types.h"
34 
35 enum
36 {
37  CANVAS_TEXT_WRAP = 1,
38  CANVAS_MODE_TRANSPARENT = 2,
39 };
40 
41 
43 typedef struct _NanoPoint
44 {
49 
55  void setPoint(lcdint_t px, lcdint_t py) { x=px; y=py; };
56 
61  _NanoPoint& operator>>=(const uint8_t bits)
62  {
63  x >>= bits;
64  y >>= bits;
65  return *this;
66  }
67 
72  _NanoPoint& operator<<=(const uint8_t bits)
73  {
74  x <<= bits;
75  y <<= bits;
76  return *this;
77  }
78 
84  {
85  x += p.x;
86  y += p.y;
87  return *this;
88  };
89 
95  {
96  x -= p.x;
97  y -= p.y;
98  return *this;
99  };
100 
106  {
107  return { static_cast<lcdint_t>(x - p.x),
108  static_cast<lcdint_t>(y - p.y) };
109  };
110 
116  {
117  return { static_cast<lcdint_t>(x + p.x),
118  static_cast<lcdint_t>(y + p.y) };
119  };
120 
125  _NanoPoint operator>>(const uint8_t bits)
126  {
127  return { static_cast<lcdint_t>(x >> bits),
128  static_cast<lcdint_t>(y >> bits) };
129  };
130 
135  _NanoPoint operator<<(const uint8_t bits)
136  {
137  return { static_cast<lcdint_t>(x << bits),
138  static_cast<lcdint_t>(y << bits) };
139  };
140 
141 } NanoPoint;
142 
146 typedef struct _NanoRect
147 {
150 
153 
159  void move(lcdint_t dx, lcdint_t dy)
160  {
161  p1.x += dx; p2.x += dx;
162  p1.y += dy; p2.y += dy;
163  }
164 
169  void addH(lcdint_t dx)
170  {
171  p1.x += dx; p2.x += dx;
172  };
173 
178  void addV(lcdint_t dy)
179  {
180  p1.y += dy;
181  p2.y += dy;
182  };
183 
192  {
193  p1.x = l; p1.y = t;
194  p2.x = r; p2.y = b;
195  };
196 
201  bool collisionX(lcdint_t x) const { return (x >= p1.x) && (x <= p2.x); };
202 
207  bool collisionY(lcdint_t y) const { return (y >= p1.y) && (y <= p2.y); };
208 
213  bool collision(const NanoPoint &p) const { return collisionX(p.x) && collisionY(p.y); };
214 
219  bool above(const NanoPoint &p) const { return (p.y < p1.y); };
220 
225  bool below(const NanoPoint &p) const { return (p.y > p2.y); };
226 
232  {
233  return { {static_cast<lcdint_t>(p1.x - p.x), static_cast<lcdint_t>(p1.y - p.y) },
234  {static_cast<lcdint_t>(p2.x - p.x), static_cast<lcdint_t>(p2.y - p.y) } };
235  };
236 
242  {
243  return { {static_cast<lcdint_t>(p1.x + p.x), static_cast<lcdint_t>(p1.y + p.y) },
244  {static_cast<lcdint_t>(p2.x + p.x), static_cast<lcdint_t>(p2.y + p.y) } };
245  };
246 
252  {
253  p1.x += p.x;
254  p1.y += p.y;
255  p2.x += p.x;
256  p2.y += p.y;
257  return *this;
258  };
259 
260 } NanoRect;
261 
266 template <uint8_t BPP>
268 {
269 public:
271  static const uint8_t BITS_PER_PIXEL = BPP;
272 
275 
282  {
283  }
284 
295  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
296  {
297  begin(w, h, bytes);
298  }
299 
310  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
311 
317  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
318 
325  void putPixel(lcdint_t x, lcdint_t y);
326 
332  void putPixel(const NanoPoint &p);
333 
341  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
342 
350  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
351 
360  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
361 
367  void drawRect(const NanoRect &rect);
368 
377  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
378 
384  void fillRect(const NanoRect &rect);
385 
400  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
401 
411  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
412 
416  void clear();
417 
422  void write(uint8_t c);
423 
428  void printChar(uint8_t c);
429 
439  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
440 
450  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
451 
457  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
458 
463  void setColor(uint16_t color) { m_color = color; };
464 
465 protected:
466  lcduint_t m_w;
471  uint8_t m_textMode;
472  uint8_t * m_buf;
473  uint16_t m_color;
474 };
475 
479 template <uint8_t BPP>
480 class NanoCanvasBase: public NanoCanvasOps<BPP>
481 {
482 public:
484 
490  virtual void blt(lcdint_t x, lcdint_t y) = 0;
491 
495  virtual void blt() = 0;
496 };
497 
499 //
500 // 8-BIT GRAPHICS
501 //
503 
509 class NanoCanvas8: public NanoCanvasBase<8>
510 {
511 public:
512  using NanoCanvasBase::NanoCanvasBase;
513 
519  void blt(lcdint_t x, lcdint_t y) override;
520 
524  void blt() override;
525 };
526 
528 //
529 // 1-BIT GRAPHICS
530 //
532 
533 enum
534 {
535  BLACK = 0x00,
536  WHITE = 0xFF,
537 };
538 
544 class NanoCanvas1: public NanoCanvasBase<1>
545 {
546 public:
547  using NanoCanvasBase::NanoCanvasBase;
548 
554  void blt(lcdint_t x, lcdint_t y) override;
555 
559  void blt() override;
560 };
561 
563 //
564 // 16-BIT GRAPHICS
565 //
567 
573 class NanoCanvas16: public NanoCanvasBase<16>
574 {
575 public:
576  using NanoCanvasBase::NanoCanvasBase;
577 
583  void blt(lcdint_t x, lcdint_t y) override;
584 
588  void blt() override;
589 };
590 
591 #endif
592 
bool below(const NanoPoint &p) const
Definition: canvas.h:225
bool collisionX(lcdint_t x) const
Definition: canvas.h:201
_NanoPoint operator-(const _NanoPoint &p)
Definition: canvas.h:105
unsigned int lcduint_t
Definition: io.h:42
struct _NanoPoint NanoPoint
_NanoRect operator-(const _NanoPoint &p)
Definition: canvas.h:231
_NanoRect operator+(const _NanoPoint &p)
Definition: canvas.h:241
void setPoint(lcdint_t px, lcdint_t py)
Definition: canvas.h:55
_NanoPoint operator+(const _NanoPoint &p)
Definition: canvas.h:115
White color.
Definition: canvas.h:536
_NanoPoint operator>>(const uint8_t bits)
Definition: canvas.h:125
void addV(lcdint_t dy)
Definition: canvas.h:178
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:469
_NanoPoint & operator<<=(const uint8_t bits)
Definition: canvas.h:72
NanoPoint p2
Definition: canvas.h:152
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:468
_NanoPoint & operator>>=(const uint8_t bits)
Definition: canvas.h:61
lcdint_t y
Definition: canvas.h:48
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:295
bool collisionY(lcdint_t y) const
Definition: canvas.h:207
void setColor(uint16_t color)
Definition: canvas.h:463
Black color.
Definition: canvas.h:535
_NanoPoint & operator+=(const _NanoPoint &p)
Definition: canvas.h:83
NanoCanvasOps()
Definition: canvas.h:281
uint8_t * m_buf
Canvas data.
Definition: canvas.h:472
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:470
struct _NanoRect NanoRect
void setRect(lcdint_t l, lcdint_t t, lcdint_t r, lcdint_t b)
Definition: canvas.h:191
_NanoRect & operator+=(const _NanoPoint &p)
Definition: canvas.h:251
_NanoPoint & operator-=(const _NanoPoint &p)
Definition: canvas.h:94
int lcdint_t
Definition: io.h:40
void addH(lcdint_t dx)
Definition: canvas.h:169
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:317
void move(lcdint_t dx, lcdint_t dy)
Definition: canvas.h:159
bool collision(const NanoPoint &p) const
Definition: canvas.h:213
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:457
bool above(const NanoPoint &p) const
Definition: canvas.h:219
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:473
NanoPoint offset
Definition: canvas.h:274
NanoPoint p1
Definition: canvas.h:149
lcdint_t x
Definition: canvas.h:46
_NanoPoint operator<<(const uint8_t bits)
Definition: canvas.h:135
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:467
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:471