SSD1306 OLED display driver  1.7.13
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){ (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 
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;
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:
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 
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 
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:299
lcduint_t m_p
number of bits, used by width value: 3 equals to 8 pixels width
Definition: canvas.h:298
Black color.
Definition: canvas.h:337
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:293
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:303
White color.
Definition: canvas.h:338
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:300
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:302
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
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:304
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:293
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:297
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:301