LCDGFX LCD display driver  2.0.1
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 (c) 2018-2019, 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 "font.h"
35 #include "canvas_types.h"
36 
46 template <uint8_t BPP>
48 {
49 public:
51  static const uint8_t BITS_PER_PIXEL = BPP;
52 
55 
62  {
63  }
64 
75  NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
76  {
77  begin(w, h, bytes);
78  }
79 
90  void begin(lcdint_t w, lcdint_t h, uint8_t *bytes);
91 
97  void setOffset(lcdint_t ox, lcdint_t oy) { offset.x = ox; offset.y = oy; };
98 
103  const NanoPoint offsetEnd() const
104  {
105  return offset + (NanoPoint){ (lcdint_t)(m_w-1), (lcdint_t)(m_h-1) };
106  }
107 
112  const NanoRect rect() const
113  {
114  return { offset, offsetEnd() };
115  }
116 
123  void putPixel(lcdint_t x, lcdint_t y);
124 
130  void putPixel(const NanoPoint &p);
131 
139  void drawVLine(lcdint_t x1, lcdint_t y1, lcdint_t y2);
140 
148  void drawHLine(lcdint_t x1, lcdint_t y1, lcdint_t x2);
149 
158  void drawLine(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2);
159 
165  void drawLine(const NanoRect &rect);
166 
175  void drawRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__ ((noinline));
176 
182  void drawRect(const NanoRect &rect);
183 
192  void fillRect(lcdint_t x1, lcdint_t y1, lcdint_t x2, lcdint_t y2) __attribute__ ((noinline));
193 
199  void fillRect(const NanoRect &rect);
200 
215  void drawBitmap1(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__ ((noinline));
216 
226  void drawBitmap8(lcdint_t x, lcdint_t y, lcduint_t w, lcduint_t h, const uint8_t *bitmap) __attribute__ ((noinline));
227 
231  void clear() __attribute__ ((noinline));
232 
237  size_t write(uint8_t c);
238 
244  uint8_t printChar(uint8_t c);
245 
256  void printFixed(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL) __attribute__ ((noinline));
257 
268  void printFixedPgm(lcdint_t xpos, lcdint_t y, const char *ch, EFontStyle style = STYLE_NORMAL);
269 
275  void setMode(uint8_t modeFlags) { m_textMode = modeFlags; };
276 
281  void setColor(uint16_t color) { m_color = color; }
282 
291  void setFont( NanoFont &font ) { m_font = &font; };
292 
302  void setFixedFont( const uint8_t *progmemFont )
303  {
304  g_canvas_font.loadFixedFont( progmemFont );
305  setFont( g_canvas_font );
306  }
307 
318  void setFreeFont( const uint8_t *progmemFont, const uint8_t *secondaryFont = nullptr )
319  {
320  g_canvas_font.loadFreeFont( progmemFont );
321  setFont( g_canvas_font );
322  }
323 
325  uint8_t * getData() { return m_buf; }
326 
328  lcduint_t width() { return m_w; }
329 
331  lcduint_t height() { return m_h; }
332 
333 protected:
338  uint8_t m_textMode;
340  uint8_t * m_buf;
341  uint16_t m_color;
342  NanoFont *m_font = nullptr;
343 };
344 
348 template <uint8_t BPP>
349 class NanoCanvasBase: public NanoCanvasOps<BPP>
350 {
351 public:
353 };
354 
360 template <lcduint_t W, lcduint_t H, uint8_t BPP>
361 class NanoCanvas: public NanoCanvasBase<BPP>
362 {
363 public:
364  NanoCanvas(): NanoCanvasBase<BPP>( W, H, m_buffer )
365  {
366  }
367 
368 private:
369  uint8_t m_buffer[W * H * BPP / 8]{};
370 };
371 
373 //
374 // 1-BIT GRAPHICS
375 //
377 
378 enum
379 {
380  BLACK = 0x00,
381  WHITE = 0xFF,
382 };
383 
388 class NanoCanvas1: public NanoCanvasBase<1>
389 {
390 public:
391  using NanoCanvasBase::NanoCanvasBase;
392 };
393 
400 {
401 public:
402  using NanoCanvasBase::NanoCanvasBase;
403 };
404 
411 {
412 public:
413  using NanoCanvasBase::NanoCanvasBase;
414 };
415 
417 //
418 // 8-BIT GRAPHICS
419 //
421 
426 class NanoCanvas4: public NanoCanvasBase<4>
427 {
428 public:
429  using NanoCanvasBase::NanoCanvasBase;
430 };
431 
433 //
434 // 8-BIT GRAPHICS
435 //
437 
442 class NanoCanvas8: public NanoCanvasBase<8>
443 {
444 public:
445  using NanoCanvasBase::NanoCanvasBase;
446 };
447 
449 //
450 // 16-BIT GRAPHICS
451 //
453 
458 class NanoCanvas16: public NanoCanvasBase<16>
459 {
460 public:
461  using NanoCanvasBase::NanoCanvasBase;
462 };
463 
468 #endif
469 
const NanoPoint offsetEnd() const
Definition: canvas.h:103
uint8_t lcduint_t
Definition: canvas_types.h:81
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:342
Definition: rect.h:42
size_t write(uint8_t c)
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:336
int8_t lcdint_t
Definition: canvas_types.h:79
White color.
Definition: canvas.h:381
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))
uint8_t * getData()
Definition: canvas.h:325
lcdint_t y
Definition: point.h:45
lcduint_t width()
Definition: canvas.h:328
NanoCanvasOps(lcdint_t w, lcdint_t h, uint8_t *bytes)
Definition: canvas.h:75
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:331
static const uint8_t BITS_PER_PIXEL
Definition: canvas.h:51
void setFixedFont(const uint8_t *progmemFont)
Definition: canvas.h:302
void setColor(uint16_t color)
Definition: canvas.h:281
NanoCanvasOps()
Definition: canvas.h:61
uint8_t * m_buf
Canvas data.
Definition: canvas.h:340
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:337
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:97
EFontStyle m_fontStyle
currently active font style
Definition: canvas.h:339
void setMode(uint8_t modeFlags)
Sets canvas drawing mode Sets canvas drawing mode. The set flags define transparency of output images...
Definition: canvas.h:275
void setFont(NanoFont &font)
Definition: canvas.h:291
void begin(lcdint_t w, lcdint_t h, uint8_t *bytes)
Black color.
Definition: canvas.h:380
void loadFixedFont(const uint8_t *progmemFont)
uint16_t m_color
current color for monochrome operations
Definition: canvas.h:341
EFontStyle
Definition: canvas_types.h:90
NanoPoint offset
Definition: canvas.h:54
lcdint_t x
Definition: point.h:43
const NanoRect rect() const
Definition: canvas.h:112
void setFreeFont(const uint8_t *progmemFont, const uint8_t *secondaryFont=nullptr)
Definition: canvas.h:318
lcduint_t m_w
width of NanoCanvas area in pixels
Definition: canvas.h:334
void loadFreeFont(const uint8_t *progmemFont)
uint8_t printChar(uint8_t c)
lcduint_t m_h
height of NanoCanvas area in pixels
Definition: canvas.h:335
uint8_t m_textMode
Flags for current NanoCanvas mode.
Definition: canvas.h:338