muwerk mupplet Display Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications
Loading...
Searching...
No Matches
mup_gfx_display.h
1// mup_gfx_display.h - mupplet graphic display base class
2
3#pragma once
4
5#include "gfxfont.h"
6#include "helper/mup_display.h"
7
8namespace ustd {
9
15 protected:
16 // runtime
17 static const GFXfont *default_font;
18 array<const GFXfont *> fonts;
19 array<FontSize> sizes;
20
21 public:
22 MuppletGfxDisplay(String name, uint8_t features)
23 : MuppletDisplay(name, features), fonts(4, ARRAY_MAX_SIZE, 4) {
24 FontSize default_size = {0, 6, 8, 0};
25 fonts.add(default_font);
26 sizes.add(default_size);
27 this->features |= MUPDISP_FEATURE_FONTS;
28 }
29
34 void addfont(const GFXfont *font, uint8_t baseLine) {
35 FontSize size = {baseLine, 0, 0, 0};
36 getFontSize(font, size);
37 fonts.add(font);
38 sizes.add(size);
39 }
40
46 void addfont(const GFXfont *font, const char *baseLineReference = "A") {
47 FontSize size = {0, 0, 0, 0};
48 getFontSize(font, size, *baseLineReference);
49 fonts.add(font);
50 sizes.add(size);
51 }
52
56 void setfont(uint8_t font) {
57 if (font < fonts.length()) {
58 int16_t oldBaseLine = current_font ? sizes[current_font].baseLine : 6;
59 int16_t newBaseLine = font ? sizes[font].baseLine : 6;
60 current_font = font;
61 setTextFont(font, newBaseLine - oldBaseLine);
62 }
63 }
64
65 protected:
66 // implementation
67 void getFontSize(const GFXfont *font, FontSize &size, char baseLineChar = 0) {
68 uint8_t first = pgm_read_byte(&font->first);
69 uint8_t last = pgm_read_byte(&font->last);
70 uint16_t baselineGlyphIndex = (uint16_t)baseLineChar > first ? baseLineChar - first : 0;
71 uint16_t glyphs = last - first + 1;
72
73 for (uint16_t i = 0; i < glyphs; i++) {
74 GFXglyph *glyph = pgm_read_glyph_ptr(font, i);
75 uint8_t xAdvance = (uint8_t)pgm_read_byte(&glyph->xAdvance);
76
77 if (baseLineChar && i == baselineGlyphIndex) {
78 size.baseLine = (int8_t)pgm_read_byte(&glyph->yOffset) * -1;
79 }
80 if (xAdvance > size.xAdvance) {
81 size.xAdvance = xAdvance;
82 }
83 }
84 size.yAdvance = (uint8_t)pgm_read_byte(&font->yAdvance);
85 }
86
87 static GFXglyph *pgm_read_glyph_ptr(const GFXfont *gfxFont, uint8_t c) {
88#ifdef __AVR__
89#if !defined(__INT_MAX__) || (__INT_MAX__ > 0xFFFF)
90 return &(((GFXglyph *)pgm_read_dword(&gfxFont->glyph))[c]);
91#else
92 return &(((GFXglyph *)pgm_read_word(&gfxFont->glyph))[c]);
93#endif
94#else
95 // expression in __AVR__ section may generate "dereferencing type-punned
96 // pointer will break strict-aliasing rules" warning In fact, on other
97 // platforms (such as STM32) there is no need to do this pointer magic as
98 // program memory may be read in a usual way So expression may be simplified
99 return gfxFont->glyph + c;
100#endif //__AVR__
101 }
102
103 virtual bool commandParser(String command, String args, String topic) {
104 if (MuppletDisplay::commandParser(command, args, topic)) {
105 return true;
106 } else if (command.startsWith("font/")) {
107 return fontParser(command.substring(5), args, topic + "/font");
108 }
109 return false;
110 }
111
112 bool fontParser(String command, String args, String topic) {
113 if (command == "get") {
114 pSched->publish(topic, String(current_font));
115 return true;
116 } else if (command == "set") {
117 long font = parseRangedLong(args, 0, fonts.length() - 1, -1, -1);
118 if (font >= 0) {
119 setfont(font);
120 pSched->publish(topic, String(current_font));
121 return true;
122 }
123 }
124 return false;
125 }
126
127 // abstract methods implementation
128 virtual FontSize getTextFontSize() {
129 return sizes[current_font];
130 }
131
132 virtual uint8_t getTextFontCount() {
133 return fonts.length();
134 }
135};
136
137const GFXfont *MuppletGfxDisplay::default_font = nullptr;
138
139} // namespace ustd
The base class for all display mupplets.
Definition: mup_display.h:24
The base class for all matrix display mupplets.
Definition: mup_gfx_display.h:14
void addfont(const GFXfont *font, uint8_t baseLine)
Definition: mup_gfx_display.h:34
void addfont(const GFXfont *font, const char *baseLineReference="A")
Definition: mup_gfx_display.h:46
void setfont(uint8_t font)
Definition: mup_gfx_display.h:56
The muwerk namespace.
Definition: display_digits_max72xx.h:10