SSD1306 OLED display driver  1.7.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, 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 
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){ 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 
255  void printChar(uint8_t c);
256 
267  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
268 
279  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
280 
286  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
287 
292  void setColor(uint16_t color) { m_color = color; };
293 
294 protected:
295  lcduint_t m_w;
296  lcduint_t m_h;
297  lcduint_t m_p;
298  lcdint_t m_cursorX;
299  lcdint_t m_cursorY;
300  uint8_t m_textMode;
302  uint8_t * m_buf;
303  uint16_t m_color;
304 };
305 
309 template <uint8_t BPP>
310 class NanoCanvasBase: public NanoCanvasOps<BPP>
311 {
312 public:
314 
320  virtual void blt(lcdint_t x, lcdint_t y) = 0;
321 
325  virtual void blt() = 0;
326 };
327 
329 //
330 // 1-BIT GRAPHICS
331 //
333 
334 enum
335 {
336  BLACK = 0x00,
337  WHITE = 0xFF,
338 };
339 
345 class NanoCanvas1: public NanoCanvasBase<1>
346 {
347 public:
348  using NanoCanvasBase::NanoCanvasBase;
349 
355  void blt(lcdint_t x, lcdint_t y) override;
356 
360  void blt() override;
361 };
362 
369 {
370 public:
371  using NanoCanvasBase::NanoCanvasBase;
372 
378  void blt(lcdint_t x, lcdint_t y) override;
379 
383  void blt() override;
384 };
385 
387 //
388 // 8-BIT GRAPHICS
389 //
391 
397 class NanoCanvas8: public NanoCanvasBase<8>
398 {
399 public:
400  using NanoCanvasBase::NanoCanvasBase;
401 
407  void blt(lcdint_t x, lcdint_t y) override;
408 
412  void blt() override;
413 };
414 
416 //
417 // 16-BIT GRAPHICS
418 //
420 
426 class NanoCanvas16: public NanoCanvasBase<16>
427 {
428 public:
429  using NanoCanvasBase::NanoCanvasBase;
430 
436  void blt(lcdint_t x, lcdint_t y) override;
437 
441  void blt() override;
442 };
443 
448 #endif
449 
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 blt() override
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)
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:298
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:297
Black color.
Definition: canvas.h:336
lcdint_t y
Definition: point.h:45
void blt() override
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:87
void putPixel(lcdint_t x, lcdint_t y)
void blt() override
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:63
void setColor(uint16_t color)
Definition: canvas.h:292
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:302
void printChar(uint8_t c)
White color.
Definition: canvas.h:337
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:299
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 blt() override
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:109
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:301
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:286
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:303
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:292
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
virtual void blt()=0
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:296
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:300