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"
35 #include "ssd1306_hal/Print_internal.h"
36 #include "nano_gfx_types.h"
37
43 enum
44 {
45 CANVAS_MODE_BASIC = 0x00,
47 CANVAS_TEXT_WRAP = 0x01,
49 CANVAS_MODE_TRANSPARENT = 0x02,
51 CANVAS_TEXT_WRAP_LOCAL = 0x04,
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
73 NanoCanvasOps()
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;
322 EFontStyle m_fontStyle;
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:
334 using NanoCanvasOps<BPP>::NanoCanvasOps;
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
415 class NanoCanvas1_8: public NanoCanvasBase<1>
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
451 class NanoCanvas1_16: public NanoCanvasBase<1>
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
493 class NanoCanvas1_4: public NanoCanvasBase<4>
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
NanoCanvasOps::offsetEnd
const NanoPoint offsetEnd() const
Definition: canvas.h:115
NanoCanvasOps::drawBitmap8
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.
NanoCanvasOps::drawLine
void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
NanoCanvasOps::drawBitmap1
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...
NanoCanvas1_8
Definition: canvas.h:415
_NanoPoint
Definition: point.h:40
_NanoRect
Definition: rect.h:42
NanoPoint
struct _NanoPoint NanoPoint
NanoCanvasOps::drawVLine
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
point.h
lcdint_t
int lcdint_t
Definition: io.h:63
NanoCanvasOps::m_cursorX
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:319
io.h
CANVAS_MODE_TRANSPARENT
Definition: canvas.h:49
NanoCanvasOps::m_p
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:318
CANVAS_TEXT_WRAP
Definition: canvas.h:47
BLACK
Black color.
Definition: canvas.h:370
_NanoPoint::y
lcdint_t y
Definition: point.h:45
NanoCanvas1
Definition: canvas.h:379
NanoCanvasOps
Definition: canvas.h:59
NanoCanvasOps::NanoCanvasOps
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:87
NanoCanvasOps::putPixel
void putPixel(lcdint_t x, lcdint_t y)
rect.h
NanoCanvasOps::BITS_PER_PIXEL
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:63
NanoCanvasBase
Definition: canvas.h:331
Print_internal.h
NanoCanvasOps::setColor
void setColor(uint16_t color)
Definition: canvas.h:313
NanoCanvasOps::printFixed
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
NanoCanvasOps::NanoCanvasOps
NanoCanvasOps()
Definition: canvas.h:73
NanoCanvasOps::m_buf
uint8_t * m_buf
Canvas data.
Definition: canvas.h:323
nano_gfx_types.h
NanoCanvas1_16
Definition: canvas.h:451
WHITE
White color.
Definition: canvas.h:371
NanoCanvasOps::drawXBitmap1
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...
NanoCanvasOps::m_cursorY
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:320
Print
Definition: Print_internal.h:43
NanoCanvasOps::drawHLine
void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2)
NanoCanvasOps::printFixedPgm
void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
NanoCanvasOps::clear
void clear()
NanoCanvasOps::setOffset
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:109
NanoCanvasOps::m_fontStyle
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:322
NanoCanvasOps::setMode
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
NanoCanvas8
Definition: canvas.h:535
NanoCanvasOps::begin
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
NanoCanvasOps::fillRect
void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
CANVAS_TEXT_WRAP_LOCAL
Definition: canvas.h:51
NanoCanvasOps::m_color
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:324
NanoCanvasOps::offset
NanoPoint offset
Definition: canvas.h:66
_NanoPoint::x
lcdint_t x
Definition: point.h:43
NanoCanvasOps::rect
const NanoRect rect() const
Definition: canvas.h:124
NanoCanvasOps::write
size_t write(uint8_t c) override
EFontStyle
EFontStyle
Definition: nano_gfx_types.h:69
NanoCanvasOps::m_w
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:313
NanoCanvas16
Definition: canvas.h:577
NanoCanvasOps::drawRect
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
NanoCanvasOps::printChar
uint8_t printChar(uint8_t c)
NanoCanvas1_4
Definition: canvas.h:493
NanoCanvasOps::m_h
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:317
NanoCanvasOps::m_textMode
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:321