LCDGFX LCD display driver  1.1.5
This library is developed to control SSD1306/SSD1325/SSD1327/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 2018-2020,2022 (C) 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 */
28 #ifndef _NANO_CANVAS_H_
29 #define _NANO_CANVAS_H_
30 
31 #include "point.h"
32 #include "rect.h"
33 #include "font.h"
34 #include "canvas_types.h"
35 
45 template <uint8_t BPP> class NanoCanvasOps
46 {
47 public:
50 
52  static const uint8_t BITS_PER_PIXEL = BPP;
53 
56 
63  {
64  }
65 
76  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
77  {
78  begin(w, h, bytes);
79  }
80 
91  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
92 
99  {
100  offset.x = ox;
101  offset.y = oy;
102  };
103 
108  const NanoPoint offsetEnd() const
109  {
110  return offset + (NanoPoint){(lcdint_t)(m_w - 1), (lcdint_t)(m_h - 1)};
111  }
112 
117  const NanoRect rect() const
118  {
119  return {offset, offsetEnd()};
120  }
121 
128  void putPixel(lcdint_t x, lcdint_t y);
129 
135  void putPixel(const NanoPoint &p);
136 
144  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
145 
153  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
154 
163  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
164 
170  void drawLine(const NanoRect &rect);
171 
180  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline));
181 
187  void drawRect(const NanoRect &rect);
188 
197  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline));
198 
204  void fillRect(const NanoRect &rect);
205 
213  void drawCircle(lcdint_t x, lcdint_t y, lcdint_t r, uint8_t options = 0x0F) __attribute__((noinline));
214 
229  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline));
230 
240  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline));
241 
251  void drawBitmap16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
252  __attribute__((noinline));
253 
257  void clear() __attribute__((noinline));
258 
263  size_t write(uint8_t c);
264 
270  uint8_t printChar(uint8_t c);
271 
282  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL)
283  __attribute__((noinline));
284 
295  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
296 
302  void setMode(uint8_t modeFlags)
303  {
304  m_textMode = modeFlags;
305  };
306 
311  void setColor(uint16_t color)
312  {
313  m_color = color;
314  }
315 
319  uint16_t getColor()
320  {
321  return m_color;
322  }
323 
328  {
329  uint16_t color = m_color;
330  m_color = m_bgColor;
331  m_bgColor = color;
332  }
333 
339  void setBackground(uint16_t color)
340  {
341  m_bgColor = color;
342  }
343 
352  void setFont(NanoFont &font)
353  {
354  m_font = &font;
355  };
356 
361  {
362  return *m_font;
363  }
364 
369  void setFontSpacing(uint8_t spacing)
370  {
371  if ( m_font )
372  m_font->setSpacing(spacing);
373  }
374 
384  void setFixedFont(const uint8_t *progmemFont)
385  {
386  g_canvas_font.loadFixedFont(progmemFont);
387  setFont(g_canvas_font);
388  }
389 
400  void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont = nullptr)
401  {
402  (void)(secondaryFont);
403  g_canvas_font.loadFreeFont(progmemFont);
404  setFont(g_canvas_font);
405  }
406 
408  uint8_t *getData()
409  {
410  return m_buf;
411  }
412 
415  {
416  return m_w;
417  }
418 
421  {
422  return m_h;
423  }
424 
426  void rotateCW(T &out);
427 
428 protected:
433  uint8_t m_textMode;
435  uint8_t *m_buf;
436  uint16_t m_color;
437  uint16_t m_bgColor;
438  NanoFont *m_font = nullptr;
439 };
440 
444 template <uint8_t BPP> class NanoCanvasBase: public NanoCanvasOps<BPP>
445 {
446 public:
448 };
449 
455 template <lcduint_t W, lcduint_t H, uint8_t BPP> class NanoCanvas: public NanoCanvasBase<BPP>
456 {
457 public:
458  NanoCanvas()
459  : NanoCanvasBase<BPP>(W, H, m_buffer)
460  {
461  }
462 
463 private:
464  uint8_t m_buffer[W * H * BPP / 8]{};
465 };
466 
468 //
469 // 1-BIT GRAPHICS
470 //
472 
473 enum
474 {
475  BLACK = 0x00,
476  WHITE = 0xFF,
477 };
478 
483 class NanoCanvas1: public NanoCanvasBase<1>
484 {
485 public:
486  using NanoCanvasBase::NanoCanvasBase;
487 };
488 
495 {
496 public:
497  using NanoCanvasBase::NanoCanvasBase;
498 };
499 
506 {
507 public:
508  using NanoCanvasBase::NanoCanvasBase;
509 };
510 
512 //
513 // 8-BIT GRAPHICS
514 //
516 
521 class NanoCanvas4: public NanoCanvasBase<4>
522 {
523 public:
524  using NanoCanvasBase::NanoCanvasBase;
525 };
526 
528 //
529 // 8-BIT GRAPHICS
530 //
532 
537 class NanoCanvas8: public NanoCanvasBase<8>
538 {
539 public:
540  using NanoCanvasBase::NanoCanvasBase;
541 };
542 
544 //
545 // 16-BIT GRAPHICS
546 //
548 
553 class NanoCanvas16: public NanoCanvasBase<16>
554 {
555 public:
556  using NanoCanvasBase::NanoCanvasBase;
557 };
558 
563 #endif
const NanoPoint offsetEnd() const
Definition: canvas.h:108
uint8_t lcduint_t
Definition: canvas_types.h:79
struct _NanoPoint NanoPoint
void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
NanoFont * m_font
current set font to use with NanoCanvas
Definition: canvas.h:438
Definition: rect.h:42
size_t write(uint8_t c)
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
NanoCanvasOps< BPP > T
Definition: canvas.h:49
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:431
int8_t lcdint_t
Definition: canvas_types.h:77
uint16_t m_bgColor
current background color
Definition: canvas.h:437
uint16_t getColor()
Definition: canvas.h:319
void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline))
Draws 8-bit color bitmap in color buffer. Draws 8-bit color bitmap in color buffer.
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL) __attribute__((noinline))
Black color.
Definition: canvas.h:475
uint8_t * getData()
Definition: canvas.h:408
lcdint_t y
Definition: point.h:44
lcduint_t width()
Definition: canvas.h:414
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:76
void invertColors()
Definition: canvas.h:327
void clear() __attribute__((noinline))
void putPixel(lcdint_t x, lcdint_t y)
Definition: font.h:45
void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
lcduint_t height()
Definition: canvas.h:420
void setSpacing(uint8_t spacing)
Definition: font.h:152
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:52
void setFixedFont(const uint8_t *progmemFont)
Definition: canvas.h:384
void setColor(uint16_t color)
Definition: canvas.h:311
NanoCanvasOps()
Definition: canvas.h:62
uint8_t * m_buf
Canvas data.
Definition: canvas.h:435
void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline))
Draws monochrome bitmap in color buffer using color, specified via setColor() method Draws monochrome...
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:432
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)
NanoFont & getFont()
Definition: canvas.h:360
void setOffset(lcdint_t ox, lcdint_t oy)
Definition: canvas.h:98
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:434
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:302
void setFont(NanoFont &font)
Definition: canvas.h:352
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
White color.
Definition: canvas.h:476
void loadFixedFont(const uint8_t *progmemFont)
uint16_t m_color
current color
Definition: canvas.h:436
EFontStyle
Definition: canvas_types.h:88
NanoPoint offset
Definition: canvas.h:55
void rotateCW(T &out)
lcdint_t x
Definition: point.h:42
const NanoRect rect() const
Definition: canvas.h:117
void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont=nullptr)
Definition: canvas.h:400
void drawBitmap16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline))
Draws 16-bit color bitmap in color buffer. Draws 16-bit color bitmap in color buffer.
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:429
void loadFreeFont(const uint8_t *progmemFont)
void setFontSpacing(uint8_t spacing)
Definition: canvas.h:369
uint8_t printChar(uint8_t c)
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:430
void drawCircle(lcdint_t x, lcdint_t y, lcdint_t r, uint8_t options=0x0F) __attribute__((noinline))
void setBackground(uint16_t color)
Definition: canvas.h:339
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:433