SSD1306 OLED display driver  1.8.2
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-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 */
29 #ifndef _NANO_CANVAS_H_
30 #define _NANO_CANVAS_H_
31 
32 #include "point.h"
33 #include "rect.h"
34 #include "ssd1306_hal/io.h"
36 #include "nano_gfx_types.h"
37 
43 enum
44 {
45  CANVAS_MODE_BASIC = 0x00,
52 };
53 
58 template <uint8_t BPP>
59 class NanoCanvasOps: public Print
60 {
61 public:
63  static const uint8_t BITS_PER_PIXEL = BPP;
64 
66  NanoPoint offset = { 0, 0 };
67 
74  {
75  }
76 
87  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
88  {
89  begin(w, h, bytes);
90  }
91 
102  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
103 
109  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
110 
115  const NanoPoint offsetEnd() const
116  {
117  return offset + (NanoPoint){ (lcdint_t)(m_w-1), (lcdint_t)(m_h-1) };
118  }
119 
124  const NanoRect rect() const
125  {
126  return { offset, offsetEnd() };
127  }
128 
135  void putPixel(lcdint_t x, lcdint_t y);
136 
142  void putPixel(const NanoPoint &p);
143 
151  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
152 
160  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
161 
170  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
171 
177  void drawLine(const NanoRect &rect);
178 
187  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
188 
194  void drawRect(const NanoRect &rect);
195 
204  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
205 
211  void fillRect(const NanoRect &rect);
212 
229  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
230 
247  void drawXBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
248 
258  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
259 
263  void clear();
264 
269  size_t write(uint8_t c) override;
270 
276  uint8_t printChar(uint8_t c);
277 
288  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
289 
300  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
301 
307  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
308 
313  void setColor(uint16_t color) { m_color = color; };
314 
315 protected:
316  lcduint_t m_w;
317  lcduint_t m_h;
318  lcduint_t m_p;
319  lcdint_t m_cursorX;
320  lcdint_t m_cursorY;
321  uint8_t m_textMode;
323  uint8_t * m_buf;
324  uint16_t m_color;
325 };
326 
330 template <uint8_t BPP>
331 class NanoCanvasBase: public NanoCanvasOps<BPP>
332 {
333 public:
335 
341  virtual void blt(lcdint_t x, lcdint_t y) = 0;
342 
346  virtual void blt() = 0;
347 
359  virtual void blt(const NanoRect &rect) = 0;
360 };
361 
363 //
364 // 1-BIT GRAPHICS
365 //
367 
368 enum
369 {
370  BLACK = 0x00,
371  WHITE = 0xFF,
372 };
373 
379 class NanoCanvas1: public NanoCanvasBase<1>
380 {
381 public:
382  using NanoCanvasBase::NanoCanvasBase;
383 
389  void blt(lcdint_t x, lcdint_t y) override;
390 
394  void blt() override;
395 
407  void blt(const NanoRect &rect) override;
408 };
409 
416 {
417 public:
418  using NanoCanvasBase::NanoCanvasBase;
419 
425  void blt(lcdint_t x, lcdint_t y) override;
426 
430  void blt() override;
431 
443  void blt(const NanoRect &rect) override;
444 };
445 
452 {
453 public:
454  using NanoCanvasBase::NanoCanvasBase;
455 
461  void blt(lcdint_t x, lcdint_t y) override;
462 
466  void blt() override;
467 
479  void blt(const NanoRect &rect) override;
480 };
481 
483 //
484 // 4-BIT GRAPHICS
485 //
487 
494 {
495 public:
496  using NanoCanvasBase::NanoCanvasBase;
497 
503  void blt(lcdint_t x, lcdint_t y) override;
504 
508  void blt() override;
509 
521  void blt(const NanoRect &rect) override;
522 };
523 
525 //
526 // 8-BIT GRAPHICS
527 //
529 
535 class NanoCanvas8: public NanoCanvasBase<8>
536 {
537 public:
538  using NanoCanvasBase::NanoCanvasBase;
539 
545  void blt(lcdint_t x, lcdint_t y) override;
546 
550  void blt() override;
551 
563  void blt(const NanoRect &rect) override;
564 };
565 
567 //
568 // 16-BIT GRAPHICS
569 //
571 
577 class NanoCanvas16: public NanoCanvasBase<16>
578 {
579 public:
580  using NanoCanvasBase::NanoCanvasBase;
581 
587  void blt(lcdint_t x, lcdint_t y) override;
588 
592  void blt() override;
593 
605  void blt(const NanoRect &rect) override;
606 };
607 
612 #endif
613 
const NanoPoint offsetEnd() const
Definition: canvas.h:115
void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Draws 8-bit color bitmap in color buffer. Draws 8-bit color bitmap in color buffer.
void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Draws monochrome bitmap in color buffer using color, specified via setColor() method Draws monochrome...
Definition: rect.h:42
struct _NanoPoint NanoPoint
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
int lcdint_t
Definition: io.h:63
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:319
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:318
Black color.
Definition: canvas.h:370
lcdint_t y
Definition: point.h:45
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:87
void putPixel(lcdint_t x, lcdint_t y)
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:63
void setColor(uint16_t color)
Definition: canvas.h:313
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
NanoCanvasOps()
Definition: canvas.h:73
uint8_t * m_buf
Canvas data.
Definition: canvas.h:323
White color.
Definition: canvas.h:371
void drawXBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
Draws monochrome bitmap in color buffer using color, specified via setColor() method Draws monochrome...
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:320
void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2)
void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:109
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:322
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:307
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:324
NanoPoint offset
Definition: canvas.h:66
lcdint_t x
Definition: point.h:43
const NanoRect rect() const
Definition: canvas.h:124
size_t write(uint8_t c) override
EFontStyle
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:313
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
uint8_t printChar(uint8_t c)
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:317
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:321