SSD1306 OLED display driver  1.6.3
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 "ssd1306_hal/io.h"
34 #include "nano_gfx_types.h"
35 
36 enum
37 {
38  CANVAS_TEXT_WRAP = 0x01,
39  CANVAS_MODE_TRANSPARENT = 0x02,
40  CANVAS_TEXT_WRAP_LOCAL = 0x04,
41 };
42 
43 
45 typedef struct _NanoPoint
46 {
51 
57  void setPoint(lcdint_t px, lcdint_t py) { x=px; y=py; };
58 
63  _NanoPoint& operator>>=(const uint8_t bits)
64  {
65  x >>= bits;
66  y >>= bits;
67  return *this;
68  }
69 
74  _NanoPoint& operator<<=(const uint8_t bits)
75  {
76  x <<= bits;
77  y <<= bits;
78  return *this;
79  }
80 
86  {
87  x += p.x;
88  y += p.y;
89  return *this;
90  };
91 
97  {
98  x -= p.x;
99  y -= p.y;
100  return *this;
101  };
102 
108  {
109  return { static_cast<lcdint_t>(x - p.x),
110  static_cast<lcdint_t>(y - p.y) };
111  };
112 
118  {
119  return { static_cast<lcdint_t>(x + p.x),
120  static_cast<lcdint_t>(y + p.y) };
121  };
122 
127  _NanoPoint operator>>(const uint8_t bits)
128  {
129  return { static_cast<lcdint_t>(x >> bits),
130  static_cast<lcdint_t>(y >> bits) };
131  };
132 
137  _NanoPoint operator<<(const uint8_t bits)
138  {
139  return { static_cast<lcdint_t>(x << bits),
140  static_cast<lcdint_t>(y << bits) };
141  };
142 
143 } NanoPoint;
144 
148 typedef struct _NanoRect
149 {
152 
155 
161  void move(lcdint_t dx, lcdint_t dy)
162  {
163  p1.x += dx; p2.x += dx;
164  p1.y += dy; p2.y += dy;
165  }
166 
171  void addH(lcdint_t dx)
172  {
173  p1.x += dx; p2.x += dx;
174  };
175 
180  void addV(lcdint_t dy)
181  {
182  p1.y += dy;
183  p2.y += dy;
184  };
185 
194  {
195  p1.x = l; p1.y = t;
196  p2.x = r; p2.y = b;
197  };
198 
203  bool collisionX(lcdint_t x) const { return (x >= p1.x) && (x <= p2.x); };
204 
209  bool collisionY(lcdint_t y) const { return (y >= p1.y) && (y <= p2.y); };
210 
215  bool collision(const NanoPoint &p) const { return collisionX(p.x) && collisionY(p.y); };
216 
221  bool above(const NanoPoint &p) const { return (p.y < p1.y); };
222 
227  bool below(const NanoPoint &p) const { return (p.y > p2.y); };
228 
234  {
235  return { {static_cast<lcdint_t>(p1.x - p.x), static_cast<lcdint_t>(p1.y - p.y) },
236  {static_cast<lcdint_t>(p2.x - p.x), static_cast<lcdint_t>(p2.y - p.y) } };
237  };
238 
244  {
245  return { {static_cast<lcdint_t>(p1.x + p.x), static_cast<lcdint_t>(p1.y + p.y) },
246  {static_cast<lcdint_t>(p2.x + p.x), static_cast<lcdint_t>(p2.y + p.y) } };
247  };
248 
254  {
255  p1.x += p.x;
256  p1.y += p.y;
257  p2.x += p.x;
258  p2.y += p.y;
259  return *this;
260  };
261 
262 } NanoRect;
263 
268 template <uint8_t BPP>
269 class NanoCanvasOps: public Print
270 {
271 public:
273  static const uint8_t BITS_PER_PIXEL = BPP;
274 
277 
284  {
285  }
286 
297  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
298  {
299  begin(w, h, bytes);
300  }
301 
312  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
313 
319  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
320 
327  void putPixel(lcdint_t x, lcdint_t y);
328 
334  void putPixel(const NanoPoint &p);
335 
343  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
344 
352  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
353 
362  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
363 
369  void drawLine(const NanoRect &rect);
370 
379  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
380 
386  void drawRect(const NanoRect &rect);
387 
396  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
397 
403  void fillRect(const NanoRect &rect);
404 
419  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
420 
430  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
431 
435  void clear();
436 
441  size_t write(uint8_t c) override;
442 
447  void printChar(uint8_t c);
448 
458  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
459 
469  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
470 
476  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
477 
482  void setColor(uint16_t color) { m_color = color; };
483 
484 protected:
485  lcduint_t m_w;
490  uint8_t m_textMode;
491  uint8_t * m_buf;
492  uint16_t m_color;
493 };
494 
498 template <uint8_t BPP>
499 class NanoCanvasBase: public NanoCanvasOps<BPP>
500 {
501 public:
503 
509  virtual void blt(lcdint_t x, lcdint_t y) = 0;
510 
514  virtual void blt() = 0;
515 };
516 
518 //
519 // 8-BIT GRAPHICS
520 //
522 
528 class NanoCanvas8: public NanoCanvasBase<8>
529 {
530 public:
531  using NanoCanvasBase::NanoCanvasBase;
532 
538  void blt(lcdint_t x, lcdint_t y) override;
539 
543  void blt() override;
544 };
545 
547 //
548 // 1-BIT GRAPHICS
549 //
551 
552 enum
553 {
554  BLACK = 0x00,
555  WHITE = 0xFF,
556 };
557 
563 class NanoCanvas1: public NanoCanvasBase<1>
564 {
565 public:
566  using NanoCanvasBase::NanoCanvasBase;
567 
573  void blt(lcdint_t x, lcdint_t y) override;
574 
578  void blt() override;
579 };
580 
587 {
588 public:
589  using NanoCanvasBase::NanoCanvasBase;
590 
596  void blt(lcdint_t x, lcdint_t y) override;
597 
601  void blt() override;
602 };
603 
605 //
606 // 16-BIT GRAPHICS
607 //
609 
615 class NanoCanvas16: public NanoCanvasBase<16>
616 {
617 public:
618  using NanoCanvasBase::NanoCanvasBase;
619 
625  void blt(lcdint_t x, lcdint_t y) override;
626 
630  void blt() override;
631 };
632 
633 #endif
634 
bool below(const NanoPoint &p) const
Definition: canvas.h:227
bool collisionX(lcdint_t x) const
Definition: canvas.h:203
_NanoPoint operator-(const _NanoPoint &p)
Definition: canvas.h:107
unsigned int lcduint_t
Definition: io.h:42
struct _NanoPoint NanoPoint
_NanoRect operator-(const _NanoPoint &p)
Definition: canvas.h:233
_NanoRect operator+(const _NanoPoint &p)
Definition: canvas.h:243
void setPoint(lcdint_t px, lcdint_t py)
Definition: canvas.h:57
_NanoPoint operator+(const _NanoPoint &p)
Definition: canvas.h:117
White color.
Definition: canvas.h:555
_NanoPoint operator>>(const uint8_t bits)
Definition: canvas.h:127
void addV(lcdint_t dy)
Definition: canvas.h:180
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:488
_NanoPoint & operator<<=(const uint8_t bits)
Definition: canvas.h:74
NanoPoint p2
Definition: canvas.h:154
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:487
_NanoPoint & operator>>=(const uint8_t bits)
Definition: canvas.h:63
lcdint_t y
Definition: canvas.h:50
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:297
bool collisionY(lcdint_t y) const
Definition: canvas.h:209
void setColor(uint16_t color)
Definition: canvas.h:482
Black color.
Definition: canvas.h:554
_NanoPoint & operator+=(const _NanoPoint &p)
Definition: canvas.h:85
NanoCanvasOps()
Definition: canvas.h:283
uint8_t * m_buf
Canvas data.
Definition: canvas.h:491
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:489
struct _NanoRect NanoRect
void setRect(lcdint_t l, lcdint_t t, lcdint_t r, lcdint_t b)
Definition: canvas.h:193
_NanoRect & operator+=(const _NanoPoint &p)
Definition: canvas.h:253
_NanoPoint & operator-=(const _NanoPoint &p)
Definition: canvas.h:96
int lcdint_t
Definition: io.h:40
void addH(lcdint_t dx)
Definition: canvas.h:171
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:319
void move(lcdint_t dx, lcdint_t dy)
Definition: canvas.h:161
bool collision(const NanoPoint &p) const
Definition: canvas.h:215
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:476
bool above(const NanoPoint &p) const
Definition: canvas.h:221
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:492
NanoPoint offset
Definition: canvas.h:276
NanoPoint p1
Definition: canvas.h:151
lcdint_t x
Definition: canvas.h:48
_NanoPoint operator<<(const uint8_t bits)
Definition: canvas.h:137
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:486
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:490