SSD1306 OLED display driver  1.7.1
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 
38 enum
39 {
40  CANVAS_TEXT_WRAP = 0x01,
41  CANVAS_MODE_TRANSPARENT = 0x02,
42  CANVAS_TEXT_WRAP_LOCAL = 0x04,
43 };
44 
49 template <uint8_t BPP>
50 class NanoCanvasOps: public Print
51 {
52 public:
54  static const uint8_t BITS_PER_PIXEL = BPP;
55 
58 
65  {
66  }
67 
78  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
79  {
80  begin(w, h, bytes);
81  }
82 
93  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
94 
100  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
101 
108  void putPixel(lcdint_t x, lcdint_t y);
109 
115  void putPixel(const NanoPoint &p);
116 
124  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
125 
133  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
134 
143  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
144 
150  void drawLine(const NanoRect &rect);
151 
160  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
161 
167  void drawRect(const NanoRect &rect);
168 
177  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
178 
184  void fillRect(const NanoRect &rect);
185 
200  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
201 
211  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap);
212 
216  void clear();
217 
222  size_t write(uint8_t c) override;
223 
228  void printChar(uint8_t c);
229 
239  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch);
240 
250  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch);
251 
257  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
258 
263  void setColor(uint16_t color) { m_color = color; };
264 
265 protected:
266  lcduint_t m_w;
267  lcduint_t m_h;
268  lcduint_t m_p;
269  lcdint_t m_cursorX;
270  lcdint_t m_cursorY;
271  uint8_t m_textMode;
272  uint8_t * m_buf;
273  uint16_t m_color;
274 };
275 
279 template <uint8_t BPP>
280 class NanoCanvasBase: public NanoCanvasOps<BPP>
281 {
282 public:
284 
290  virtual void blt(lcdint_t x, lcdint_t y) = 0;
291 
295  virtual void blt() = 0;
296 };
297 
299 //
300 // 1-BIT GRAPHICS
301 //
303 
304 enum
305 {
306  BLACK = 0x00,
307  WHITE = 0xFF,
308 };
309 
315 class NanoCanvas1: public NanoCanvasBase<1>
316 {
317 public:
318  using NanoCanvasBase::NanoCanvasBase;
319 
325  void blt(lcdint_t x, lcdint_t y) override;
326 
330  void blt() override;
331 };
332 
339 {
340 public:
341  using NanoCanvasBase::NanoCanvasBase;
342 
348  void blt(lcdint_t x, lcdint_t y) override;
349 
353  void blt() override;
354 };
355 
357 //
358 // 8-BIT GRAPHICS
359 //
361 
367 class NanoCanvas8: public NanoCanvasBase<8>
368 {
369 public:
370  using NanoCanvasBase::NanoCanvasBase;
371 
377  void blt(lcdint_t x, lcdint_t y) override;
378 
382  void blt() override;
383 };
384 
386 //
387 // 16-BIT GRAPHICS
388 //
390 
396 class NanoCanvas16: public NanoCanvasBase<16>
397 {
398 public:
399  using NanoCanvasBase::NanoCanvasBase;
400 
406  void blt(lcdint_t x, lcdint_t y) override;
407 
411  void blt() override;
412 };
413 
414 #endif
415 
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:37
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
White color.
Definition: canvas.h:307
void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch)
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:269
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:268
lcdint_t y
Definition: point.h:40
void blt() override
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:78
void putPixel(lcdint_t x, lcdint_t y)
void blt() override
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:54
void setColor(uint16_t color)
Definition: canvas.h:263
Black color.
Definition: canvas.h:306
NanoCanvasOps()
Definition: canvas.h:64
uint8_t * m_buf
Canvas data.
Definition: canvas.h:272
void printChar(uint8_t c)
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:270
void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2)
void blt() override
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:100
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:257
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)
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch)
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:273
NanoPoint offset
Definition: canvas.h:57
lcdint_t x
Definition: point.h:38
size_t write(uint8_t c) override
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:263
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:267
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:271