muwerk mupplet Display Library
muwerk applets; mupplets: functional units that support specific hardware or reusable applications
Loading...
Searching...
No Matches
display_matrix_max72xx.h
1// display_matrix_max72xx.h - mupplet for Matrix Display using MAX72xx
2
3#pragma once
4
5#include "muwerk.h"
6#include "helper/light_controller.h"
7#include "helper/mup_gfx_display.h"
8#include "hardware/max72xx_matrix.h"
9
10namespace ustd {
11
12// clang-format off
39// clang-format on
40
42 public:
43 static const char *version; // = "0.1.0";
44
45 private:
46 // hardware configuration
47 Max72xxMatrix display;
48
49 // runtime
50 LightController light;
51
52 public:
66 DisplayMatrixMAX72XX(String name, uint8_t csPin, uint8_t hDisplays = 1, uint8_t vDisplays = 1,
67 uint8_t rotation = 0)
68 : MuppletGfxDisplay(name, MUPDISP_FEATURE_MONO),
69 display(csPin, hDisplays, vDisplays, rotation) {
70 }
71
77 void begin(Scheduler *_pSched, bool initialState = false) {
78 pSched = _pSched;
79 tID = pSched->add([this]() { this->loop(); }, name, 10000L);
80
81 pSched->subscribe(tID, name + "/display/#", [this](String topic, String msg, String orig) {
82 this->commandParser(topic.substring(name.length() + 9), msg, name + "/display");
83 });
84 pSched->subscribe(tID, name + "/light/#", [this](String topic, String msg, String orig) {
85 this->light.commandParser(topic.substring(name.length() + 7), msg);
86 });
87
88 // initialize default values
89 current_font = 0;
90#ifdef USTD_FEATURE_PROGRAMPLAYER
91 programInit();
92#endif
93 // prepare hardware
94 display.begin();
95 display.setTextWrap(false);
96
97 // start light controller
98 light.begin([this](bool state, double level, bool control,
99 bool notify) { this->onLightControl(state, level, control, notify); },
100 initialState);
101 }
102
103 private:
104 void loop() {
105 light.loop();
106#ifdef USTD_FEATURE_PROGRAMPLAYER
107 programLoop();
108#endif
109 }
110
111 void onLightControl(bool state, double level, bool control, bool notify) {
112 uint8_t intensity = (level * 15000) / 1000;
113 if (control) {
114 display.setIntensity(intensity);
115 }
116 if (control) {
117 display.setPowerSave(!state);
118 }
119 if (notify) {
120 pSched->publish(name + "/light/unitbrightness", String(level, 3));
121 pSched->publish(name + "/light/state", state ? "on" : "off");
122 }
123 }
124
125 // abstract methods implementation
126 virtual void getDimensions(int16_t &width, int16_t &height) {
127 width = display.width();
128 height = display.height();
129 }
130
131 virtual bool getTextWrap() {
132 return display.getTextWrap();
133 }
134
135 virtual void setTextWrap(bool wrap) {
136 display.setTextWrap(wrap);
137 }
138
139 virtual void setTextFont(uint8_t font, int16_t baseLineAdjustment) {
140 display.setFont(fonts[font]);
141 if (baseLineAdjustment) {
142 int16_t y = display.getCursorY();
143 display.setCursor(display.getCursorX(), y + baseLineAdjustment);
144 }
145 }
146
147 virtual void setTextColor(uint16_t fg, uint16_t bg) {
148 display.setTextColor(fg, bg);
149 }
150
151 virtual void getCursor(int16_t &x, int16_t &y) {
152 x = display.getCursorX();
153 y = display.getCursorY();
154 }
155
156 virtual void setCursor(int16_t x, int16_t y) {
157 display.setCursor(x, y);
158 }
159
160 virtual void displayClear(int16_t x, int16_t y, int16_t w, int16_t h) {
161 display.fillRect(x, y, w, h, 0);
162 display.write();
163 }
164
165 virtual void displayClear(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t bg) {
166 display.fillRect(x, y, w, h, bg);
167 display.write();
168 }
169
170 virtual void displayPrint(String content, bool ln = false) {
171 if (ln) {
172 display.println(content);
173 } else {
174 display.print(content);
175 }
176 display.write();
177 }
178
179 virtual bool displayFormat(int16_t x, int16_t y, int16_t w, int16_t align, String content,
180 uint8_t font, uint16_t color, uint16_t bg) {
181 display.setFont(fonts[font]);
182 display.setTextColor(color, bg);
183 bool ret = display.printFormatted(x, y, w, align, content, sizes[font].baseLine,
184 sizes[font].yAdvance);
185 display.write();
186 display.setTextColor(current_fg, current_bg);
187 display.setFont(fonts[current_font]);
188 return ret;
189 }
190
191 // implementation
192 void getTextDimensions(uint8_t font, const char *content, int16_t &width, int16_t &height) {
193 if (!content || !*content) {
194 width = 0;
195 height = 0;
196 return;
197 }
198 int16_t x, y;
199 uint16_t w, h;
200 uint8_t old_font = current_font;
201 bool old_wrap = display.getTextWrap();
202 display.setFont(fonts[font]);
203 display.setTextWrap(false);
204 display.getTextBounds(content, 0, sizes[font].baseLine, &x, &y, &w, &h);
205 display.setTextWrap(old_wrap);
206 display.setFont(fonts[old_font]);
207 width = (int16_t)w;
208 height = (int16_t)h;
209 }
210};
211
212const char *DisplayMatrixMAX72XX::version = "0.1.0";
213
214} // namespace ustd
mupplet-display MAX7219/MAX7221 Led Matrix Display class
Definition: display_matrix_max72xx.h:41
DisplayMatrixMAX72XX(String name, uint8_t csPin, uint8_t hDisplays=1, uint8_t vDisplays=1, uint8_t rotation=0)
Definition: display_matrix_max72xx.h:66
void begin(Scheduler *_pSched, bool initialState=false)
Definition: display_matrix_max72xx.h:77
The MAX72XX Matrix Display Class.
Definition: max72xx_matrix.h:19
void setIntensity(uint8_t intensity)
Definition: max72xx_matrix.h:90
void begin()
Definition: max72xx_matrix.h:65
void write()
Definition: max72xx_matrix.h:108
bool getTextWrap() const
Definition: max72xx_matrix.h:153
int16_t getCursorY() const
Definition: max72xx_matrix.h:167
int16_t getCursorX() const
Definition: max72xx_matrix.h:160
void setPowerSave(bool powersave)
Definition: max72xx_matrix.h:83
bool printFormatted(int16_t x, int16_t y, int16_t w, int16_t align, String content, uint8_t baseLine, uint8_t yAdvance=0)
Definition: max72xx_matrix.h:206
The base class for all matrix display mupplets.
Definition: mup_gfx_display.h:14
The muwerk namespace.
Definition: display_digits_max72xx.h:10