LCDGFX LCD display driver  1.2.0
Lightweight graphics library for SSD1306, SSD1325, SSD1327, SSD1331, SSD1351, SH1106, SH1107, IL9163, ST7735, ST7789, ILI9341, PCD8544 displays over I2C/SPI
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 
53 template <uint8_t BPP> class NanoCanvasOps
54 {
55 public:
58 
60  static const uint8_t BITS_PER_PIXEL = BPP;
61 
64 
71  {
72  }
73 
84  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
85  {
86  begin(w, h, bytes);
87  }
88 
99  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
100 
107  {
108  offset.x = ox;
109  offset.y = oy;
110  };
111 
116  const NanoPoint offsetEnd() const
117  {
118  return offset + (NanoPoint){(lcdint_t)(m_w - 1), (lcdint_t)(m_h - 1)};
119  }
120 
125  const NanoRect rect() const
126  {
127  return {offset, offsetEnd()};
128  }
129 
136  void putPixel(lcdint_t x, lcdint_t y);
137 
143  void putPixel(const NanoPoint &p);
144 
153  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
154 
163  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
164 
174  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
175 
181  void drawLine(const NanoRect &rect);
182 
192  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline));
193 
200  void drawRect(const NanoRect &rect);
201 
211  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline));
212 
219  void fillRect(const NanoRect &rect);
220 
228  void drawCircle(lcdint_t x, lcdint_t y, lcdint_t r, uint8_t options = 0x0F) __attribute__((noinline));
229 
244  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline));
245 
255  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__((noinline));
256 
266  void drawBitmap16(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap)
267  __attribute__((noinline));
268 
272  void clear() __attribute__((noinline));
273 
278  size_t write(uint8_t c);
279 
285  uint8_t printChar(uint8_t c);
286 
297  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL)
298  __attribute__((noinline));
299 
310  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
311 
317  void setMode(uint8_t modeFlags)
318  {
319  m_textMode = modeFlags;
320  };
321 
326  void setColor(uint16_t color)
327  {
328  m_color = color;
329  }
330 
336  uint16_t getColor()
337  {
338  return m_color;
339  }
340 
346  {
347  uint16_t color = m_color;
348  m_color = m_bgColor;
349  m_bgColor = color;
350  }
351 
357  void setBackground(uint16_t color)
358  {
359  m_bgColor = color;
360  }
361 
370  void setFont(NanoFont &font)
371  {
372  m_font = &font;
373  };
374 
381  {
382  return *m_font;
383  }
384 
389  void setFontSpacing(uint8_t spacing)
390  {
391  if ( m_font )
392  m_font->setSpacing(spacing);
393  }
394 
404  void setFixedFont(const uint8_t *progmemFont)
405  {
406  g_canvas_font.loadFixedFont(progmemFont);
407  setFont(g_canvas_font);
408  }
409 
420  void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont = nullptr)
421  {
422  (void)(secondaryFont);
423  g_canvas_font.loadFreeFont(progmemFont);
424  setFont(g_canvas_font);
425  }
426 
428  uint8_t *getData()
429  {
430  return m_buf;
431  }
432 
435  {
436  return m_w;
437  }
438 
441  {
442  return m_h;
443  }
444 
446  void rotateCW(T &out);
447 
448 protected:
453  uint8_t m_textMode;
455  uint8_t *m_buf;
456  uint16_t m_color;
457  uint16_t m_bgColor;
458  NanoFont *m_font = nullptr;
459 };
460 
464 template <uint8_t BPP> class NanoCanvasBase: public NanoCanvasOps<BPP>
465 {
466 public:
468 };
469 
475 template <lcduint_t W, lcduint_t H, uint8_t BPP> class NanoCanvas: public NanoCanvasBase<BPP>
476 {
477 public:
478  NanoCanvas()
479  : NanoCanvasBase<BPP>(W, H, m_buffer)
480  {
481  }
482 
483 private:
484  uint8_t m_buffer[W * H * BPP / 8]{};
485 };
486 
488 //
489 // 1-BIT GRAPHICS
490 //
492 
493 enum
494 {
495  BLACK = 0x00,
496  WHITE = 0xFF,
497 };
498 
503 class NanoCanvas1: public NanoCanvasBase<1>
504 {
505 public:
506  using NanoCanvasBase::NanoCanvasBase;
507 };
508 
515 {
516 public:
517  using NanoCanvasBase::NanoCanvasBase;
518 };
519 
526 {
527 public:
528  using NanoCanvasBase::NanoCanvasBase;
529 };
530 
532 //
533 // 8-BIT GRAPHICS
534 //
536 
541 class NanoCanvas4: public NanoCanvasBase<4>
542 {
543 public:
544  using NanoCanvasBase::NanoCanvasBase;
545 };
546 
548 //
549 // 8-BIT GRAPHICS
550 //
552 
557 class NanoCanvas8: public NanoCanvasBase<8>
558 {
559 public:
560  using NanoCanvasBase::NanoCanvasBase;
561 };
562 
564 //
565 // 16-BIT GRAPHICS
566 //
568 
573 class NanoCanvas16: public NanoCanvasBase<16>
574 {
575 public:
576  using NanoCanvasBase::NanoCanvasBase;
577 };
578 
583 #endif
const NanoPoint offsetEnd() const
Returns right-bottom point of the canvas in offset terms.
Definition: canvas.h:116
uint8_t lcduint_t
internal int type, used by the library.
Definition: canvas_types.h:79
struct _NanoPoint NanoPoint
Describes point.
void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2)
Draws a line between two points using Bresenham&#39;s algorithm.
NanoCanvas1_8 represents objects for drawing in memory buffer NanoCanvas1_8 represents each pixel as ...
Definition: canvas.h:514
void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
Draws rectangle outline.
Describes point.
Definition: point.h:39
NanoFont * m_font
current set font to use with NanoCanvas
Definition: canvas.h:458
NanoRect structure describes rectangle area.
Definition: rect.h:42
size_t write(uint8_t c)
Writes single character to canvas.
void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2)
Draws vertical line from (x1,y1) to (x1,y2).
NanoCanvasOps< BPP > T
Base type for canvas class specific operations.
Definition: canvas.h:57
Point class.
lcdint_t m_cursorX
current X cursor position for text output
Definition: canvas.h:451
int8_t lcdint_t
internal int type, used by the library.
Definition: canvas_types.h:77
uint16_t m_bgColor
current background color
Definition: canvas.h:457
uint16_t getColor()
Returns currently set foreground color.
Definition: canvas.h:336
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.
void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL) __attribute__((noinline))
Print text at specified position to canvas.
Black color.
Definition: canvas.h:495
uint8_t * getData()
Return pointer to canvas pixels data.
Definition: canvas.h:428
lcdint_t y
y position in pixels
Definition: point.h:44
lcduint_t width()
Returns canvas width in pixels.
Definition: canvas.h:434
NanoCanvas1 represents objects for drawing in memory buffer NanoCanvas1 represents each pixel as sing...
Definition: canvas.h:503
NanoCanvasOps provides operations for drawing in memory buffer.
Definition: canvas.h:53
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Creates new canvas object.
Definition: canvas.h:84
void invertColors()
Swaps foreground and background colors.
Definition: canvas.h:345
void clear() __attribute__((noinline))
Clears canvas.
void putPixel(lcdint_t x, lcdint_t y)
Draws pixel on specified position.
Rectangle class.
NanoFont class implements work with fonts provided by the library: loading fonts, providing their par...
Definition: font.h:45
void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__((noinline))
Fills a rectangular area with the current color.
lcduint_t height()
Returns canvas height in pixels.
Definition: canvas.h:440
void setSpacing(uint8_t spacing)
Sets spacing in pixels between 2 nearest characters.
Definition: font.h:152
static const uint8_t BITS_PER_PIXEL
number of bits per single pixel in buffer
Definition: canvas.h:60
Basic structures of canvas gfx library.
Template for user-defined canvas object.
Definition: canvas.h:475
Base class for all NanoCanvas childs.
Definition: canvas.h:464
void setFixedFont(const uint8_t *progmemFont)
Sets new font to use with print functions.
Definition: canvas.h:404
void setColor(uint16_t color)
Sets color for monochrome operations.
Definition: canvas.h:326
NanoCanvasOps()
Creates new empty canvas object.
Definition: canvas.h:70
uint8_t * m_buf
Canvas data.
Definition: canvas.h:455
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...
NanoCanvas1_16 represents objects for drawing in memory buffer NanoCanvas1_16 represents each pixel a...
Definition: canvas.h:525
lcdint_t m_cursorY
current Y cursor position for text output
Definition: canvas.h:452
void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2)
Draws horizontal line from (x1,y1) to (x2,y1).
void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style=STYLE_NORMAL)
Print text at specified position to canvas.
NanoFont & getFont()
Returns reference to NanoFont object currently in use.
Definition: canvas.h:380
void setOffset(lcdint_t ox, lcdint_t oy)
Sets offset.
Definition: canvas.h:106
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:454
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode.
Definition: canvas.h:317
void setFont(NanoFont &font)
Sets new font to use with print functions.
Definition: canvas.h:370
NanoCanvas8 represents objects for drawing in memory buffer NanoCanvas8 represents each pixel as sing...
Definition: canvas.h:557
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Initializes canvas object.
White color.
Definition: canvas.h:496
void loadFixedFont(const uint8_t *progmemFont)
Function allows to set another fixed font for the library.
uint16_t m_color
current color
Definition: canvas.h:456
EFontStyle
Supported font styles.
Definition: canvas_types.h:88
NanoPoint offset
Fixed offset for all operation of NanoCanvasOps in pixels.
Definition: canvas.h:63
void rotateCW(T &out)
Rotates the canvas clock-wise.
lcdint_t x
x position in pixels
Definition: point.h:42
NanoCanvas4 represents objects for drawing in memory buffer NanoCanvas4 represents each pixel as 4-bi...
Definition: canvas.h:541
const NanoRect rect() const
Returns rectangle area, covered by canvas in offset terms.
Definition: canvas.h:125
void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont=nullptr)
Sets new font to use with print functions.
Definition: canvas.h:420
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.
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:449
NanoCanvas16 represents objects for drawing in memory buffer NanoCanvas16 represents each pixel as 2-...
Definition: canvas.h:573
void loadFreeFont(const uint8_t *progmemFont)
Function allows to set another free font for the library.
void setFontSpacing(uint8_t spacing)
Sets font spacing for currently active font.
Definition: canvas.h:389
Font class.
uint8_t printChar(uint8_t c)
Draws single character to canvas.
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:450
void drawCircle(lcdint_t x, lcdint_t y, lcdint_t r, uint8_t options=0x0F) __attribute__((noinline))
Draws circle outline.
void setBackground(uint16_t color)
Sets background color.
Definition: canvas.h:357
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:453