SSD1306 OLED display driver 1.7.8 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 "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;
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){ m_w-1, 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
227 void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
228
238 void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
239
243 void clear();
244
249 size_t write(uint8_t c) override;
250
256 uint8_t printChar(uint8_t c);
257
268 void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
269
280 void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
281
287 void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
288
293 void setColor(uint16_t color) { m_color = color; };
294
295 protected:
296 lcduint_t m_w;
297 lcduint_t m_h;
298 lcduint_t m_p;
299 lcdint_t m_cursorX;
300 lcdint_t m_cursorY;
301 uint8_t m_textMode;
302 EFontStyle m_fontStyle;
303 uint8_t * m_buf;
304 uint16_t m_color;
305 };
306
310 template <uint8_t BPP>
311 class NanoCanvasBase: public NanoCanvasOps<BPP>
312 {
313 public:
314 using NanoCanvasOps<BPP>::NanoCanvasOps;
315
321 virtual void blt(lcdint_t x, lcdint_t y) = 0;
322
326 virtual void blt() = 0;
327 };
328
330 //
331 // 1-BIT GRAPHICS
332 //
334
335 enum
336 {
337 BLACK = 0x00,
338 WHITE = 0xFF,
339 };
340
346 class NanoCanvas1: public NanoCanvasBase<1>
347 {
348 public:
349 using NanoCanvasBase::NanoCanvasBase;
350
356 void blt(lcdint_t x, lcdint_t y) override;
357
361 void blt() override;
362 };
363
369 class NanoCanvas1_8: public NanoCanvasBase<1>
370 {
371 public:
372 using NanoCanvasBase::NanoCanvasBase;
373
379 void blt(lcdint_t x, lcdint_t y) override;
380
384 void blt() override;
385 };
386
388 //
389 // 8-BIT GRAPHICS
390 //
392
398 class NanoCanvas8: public NanoCanvasBase<8>
399 {
400 public:
401 using NanoCanvasBase::NanoCanvasBase;
402
408 void blt(lcdint_t x, lcdint_t y) override;
409
413 void blt() override;
414 };
415
417 //
418 // 16-BIT GRAPHICS
419 //
421
427 class NanoCanvas16: public NanoCanvasBase<16>
428 {
429 public:
430 using NanoCanvasBase::NanoCanvasBase;
431
437 void blt(lcdint_t x, lcdint_t y) override;
438
442 void blt() override;
443 };
444
449 #endif
450
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.
NanoCanvas1_8::blt
void blt() override
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:369
_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
NanoCanvasOps::m_cursorX
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:299
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:298
CANVAS_TEXT_WRAP
Definition: canvas.h:47
BLACK
Black color.
Definition: canvas.h:337
_NanoPoint::y
lcdint_t y
Definition: point.h:45
NanoCanvas1
Definition: canvas.h:346
NanoCanvas1::blt
void blt() override
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
NanoCanvas8::blt
void blt() override
NanoCanvasOps::BITS_PER_PIXEL
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:63
NanoCanvasBase
Definition: canvas.h:311
Print_internal.h
NanoCanvasOps::setColor
void setColor(uint16_t color)
Definition: canvas.h:293
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:303
nano_gfx_types.h
WHITE
White color.
Definition: canvas.h:338
NanoCanvasOps::m_cursorY
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:300
Print
Definition: Print_internal.h:42
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)
NanoCanvas16::blt
void blt() override
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:302
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:287
NanoCanvas8
Definition: canvas.h:398
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:304
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:58
NanoCanvasOps::m_w
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:293
NanoCanvas16
Definition: canvas.h:427
NanoCanvasOps::drawRect
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
NanoCanvasOps::printChar
uint8_t printChar(uint8_t c)
NanoCanvasBase::blt
virtual void blt()=0
NanoCanvasOps::m_h
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:297
NanoCanvasOps::m_textMode
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:301