SSD1306 OLED display driver  1.5.0
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
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 
263 //
264 // 8-BIT GRAPHICS
265 //
267 
274 {
275 public:
277  static const uint8_t BITS_PER_PIXEL = 8;
278 
281 
288  {
289  }
290 
301  NanoCanvas8(lcdint_t w, lcdint_t h, uint8_t *bytes)
302  {
303  begin(w, h, bytes);
304  }
305 
316  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
317  {
318  m_w = w;
319  m_h = h;
320  offset.x = 0;
321  offset.y = 0;
322  m_cursorX = 0;
323  m_cursorY = 0;
324  m_color = 0xFF; // white color by default
325  m_textMode = 0;
326  m_p = 3;
327  while (w >> (m_p+1)) { m_p++; };
328  m_buf = bytes;
329  clear();
330  };
331 
337  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
338 
345  void putPixel(lcdint_t x, lcdint_t y);
346 
352  void putPixel(const NanoPoint &p);
353 
361  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
362 
370  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
371 
380  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
381 
387  void drawRect(const NanoRect &rect);
388 
397  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
398 
404  void fillRect(const NanoRect &rect);
405 
420  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
421 
431  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
432 
436  void clear();
437 
442  void write(uint8_t c);
443 
448  void printChar(uint8_t c);
449 
459  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
460 
470  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
471 
477  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
478 
483  void setColor(uint8_t color) { m_color = color; };
484 
490  void blt(lcdint_t x, lcdint_t y);
491 
495  void blt();
496 
497 private:
498  lcduint_t m_w;
499  lcduint_t m_h;
500  lcduint_t m_p;
501  lcdint_t m_cursorX;
502  lcdint_t m_cursorY;
503  uint8_t m_textMode;
504  uint8_t * m_buf;
505  uint8_t m_color;
506 };
507 
509 //
510 // 1-BIT GRAPHICS
511 //
513 
514 enum
515 {
516  BLACK = 0x00,
517  WHITE = 0xFF,
518 };
519 
526 {
527 public:
529  static const uint8_t BITS_PER_PIXEL = 1;
530 
533 
539  NanoCanvas1() { };
540 
551  NanoCanvas1(lcdint_t w, lcdint_t h, uint8_t *bytes)
552  {
553  begin(w, h, bytes);
554  };
555 
566  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
567  {
568  m_w = w;
569  m_h = h;
570  offset.x = 0;
571  offset.y = 0;
572  m_cursorX = 0;
573  m_cursorY = 0;
574  m_color = WHITE;
575  m_textMode = 0;
576  m_p = 3;
577  while (w >> (m_p+1)) { m_p++; };
578  m_buf = bytes;
579  clear();
580  };
581 
587  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
588 
595  void putPixel(lcdint_t x, lcdint_t y);
596 
602  void putPixel(const NanoPoint &p);
603 
611  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
612 
620  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
621 
630  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
631 
637  void drawRect(const NanoRect &rect);
638 
647  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
648 
654  void fillRect(const NanoRect &rect);
655 
670  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
671 
672  /*
673  * @brief Draws 8-bit color bitmap in color buffer.
674  * Draws 8-bit color bitmap in color buffer.
675  * @param x - position X in pixels
676  * @param y - position Y in pixels
677  * @param w - width in pixels
678  * @param h - height in pixels
679  * @param bitmap - 8-bit color bitmap data, located in flash
680  */
681 // void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
682 
686  void clear();
687 
692  void write(uint8_t c);
693 
698  void printChar(uint8_t c);
699 
709  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
710 
720  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
721 
727  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
728 
736  void setColor(uint8_t color) { m_color = color; };
737 
743  void blt(lcdint_t x, lcdint_t y);
744 
748  void blt();
749 
750 private:
751  lcduint_t m_w;
752  lcduint_t m_h;
753  lcduint_t m_p;
754  lcdint_t m_cursorX;
755  lcdint_t m_cursorY;
756  uint8_t m_textMode;
757  uint8_t * m_buf;
758  uint8_t m_color;
759 };
760 
762 //
763 // 16-BIT GRAPHICS
764 //
766 
773 {
774 public:
776  static const uint8_t BITS_PER_PIXEL = 16;
777 
780 
787  {
788  }
789 
800  NanoCanvas16(lcdint_t w, lcdint_t h, uint8_t *bytes)
801  {
802  begin(w, h, bytes);
803  }
804 
815  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
816  {
817  m_w = w;
818  m_h = h;
819  offset.x = 0;
820  offset.y = 0;
821  m_cursorX = 0;
822  m_cursorY = 0;
823  m_color = 0xFFFF; // white color by default
824  m_textMode = 0;
825  m_p = 3;
826  while (w >> (m_p+1)) { m_p++; };
827  m_p++;
828  m_buf = bytes;
829  clear();
830  };
831 
837  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
838 
845  void putPixel(lcdint_t x, lcdint_t y);
846 
852  void putPixel(const NanoPoint &p);
853 
861  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
862 
870  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
871 
880  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
881 
887  void drawRect(const NanoRect &rect);
888 
897  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
898 
904  void fillRect(const NanoRect &rect);
905 
920  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
921 
931  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
932 
936  void clear();
937 
942  void write(uint8_t c);
943 
948  void printChar(uint8_t c);
949 
959  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
960 
970  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
971 
977  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
978 
983  void setColor(uint16_t color) { m_color = color; };
984 
990  void blt(lcdint_t x, lcdint_t y);
991 
995  void blt();
996 
997 private:
998  lcduint_t m_w;
999  lcduint_t m_h;
1000  lcduint_t m_p;
1001  lcdint_t m_cursorX;
1002  lcdint_t m_cursorY;
1003  uint8_t m_textMode;
1004  uint8_t * m_buf;
1005  uint16_t m_color;
1006 };
1007 
1008 
1009 #endif
1010 
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:837
_NanoPoint operator-(const _NanoPoint &p)
Definition: canvas.h:105
White color.
Definition: canvas.h:517
unsigned int lcduint_t
Definition: io.h:42
struct _NanoPoint NanoPoint
NanoCanvas1()
Definition: canvas.h:539
_NanoRect operator-(const _NanoPoint &p)
Definition: canvas.h:231
_NanoRect operator+(const _NanoPoint &p)
Definition: canvas.h:241
bool collisionY(lcdint_t y) const
Definition: canvas.h:207
NanoCanvas16()
Definition: canvas.h:786
void setPoint(lcdint_t px, lcdint_t py)
Definition: canvas.h:55
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:815
NanoCanvas1(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:551
NanoCanvas16(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:800
void setColor(uint8_t color)
Definition: canvas.h:736
NanoCanvas8(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:301
_NanoPoint operator+(const _NanoPoint &p)
Definition: canvas.h:115
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:566
_NanoPoint operator>>(const uint8_t bits)
Definition: canvas.h:125
void addV(lcdint_t dy)
Definition: canvas.h:178
void setColor(uint16_t color)
Definition: canvas.h:983
_NanoPoint & operator<<=(const uint8_t bits)
Definition: canvas.h:72
NanoPoint p2
Definition: canvas.h:152
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:977
NanoPoint offset
Definition: canvas.h:779
bool above(const NanoPoint &p) const
Definition: canvas.h:219
_NanoPoint & operator>>=(const uint8_t bits)
Definition: canvas.h:61
lcdint_t y
Definition: canvas.h:48
bool collision(const NanoPoint &p) const
Definition: canvas.h:213
NanoCanvas8()
Definition: canvas.h:287
NanoPoint offset
Definition: canvas.h:532
_NanoPoint & operator+=(const _NanoPoint &p)
Definition: canvas.h:83
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:337
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:727
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
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:316
_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
bool below(const NanoPoint &p) const
Definition: canvas.h:225
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:587
bool collisionX(lcdint_t x) const
Definition: canvas.h:201
void move(lcdint_t dx, lcdint_t dy)
Definition: canvas.h:159
NanoPoint offset
Definition: canvas.h:280
void setColor(uint8_t color)
Definition: canvas.h:483
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:477
Black color.
Definition: canvas.h:516
NanoPoint p1
Definition: canvas.h:149
lcdint_t x
Definition: canvas.h:46
_NanoPoint operator<<(const uint8_t bits)
Definition: canvas.h:135