6#include "helper/light_controller.h"
7#include "helper/mup_gfx_display.h"
8#include "hardware/st7735_matrix.h"
11class DisplayMatrixST7735 :
public MuppletGfxDisplay {
13 static const char *version;
19 bool blActiveLogic =
false;
24 LightController light;
50 DisplayMatrixST7735(String name, uint8_t hardware, uint8_t rotation, uint8_t csPin,
51 uint8_t dcPin, uint8_t rsPin = -1, uint8_t blPin = -1,
52 bool blActiveLogic =
false, uint8_t blChannel = 0)
53 : MuppletGfxDisplay(name, MUPDISP_FEATURE_COLOR),
54 display(csPin, dcPin, rsPin, hardware, rotation), blPin(blPin),
55 blActiveLogic(blActiveLogic), blChannel(blChannel) {
56 current_bg = ST7735_BLACK;
57 current_fg = ST7735_WHITE;
65 void begin(Scheduler *_pSched,
bool initialState =
false) {
67 tID = pSched->add([
this]() { this->loop(); }, name, 10000L);
69 pSched->subscribe(tID, name +
"/display/#", [
this](String top, String msg, String org) {
70 this->commandParser(top.substring(name.length() + 9), msg, name +
"/display");
72 if (blPin != -1 && blPin != 0) {
74 initBacklightHardware();
76 pSched->subscribe(tID, name +
"/light/#", [
this](String top, String msg, String org) {
77 this->light.commandParser(top.substring(name.length() + 7), msg);
83#ifdef USTD_FEATURE_PROGRAMPLAYER
89 display.setTextWrap(
false);
90 display.setTextColor(current_fg, current_bg);
93 if (blPin != -1 && blPin != 0) {
95 [
this](
bool state,
double level,
bool control,
bool notify) {
96 this->onLightControl(state, level, control, notify);
100 display.enableDisplay(
true);
107#ifdef USTD_FEATURE_PROGRAMPLAYER
112 void onLightControl(
bool state,
double level,
bool control,
bool notify) {
114 if (state && level == 1.0) {
117 ledcWrite(blChannel, blActiveLogic ? blPwmRange : 0);
119 digitalWrite(blPin, blActiveLogic ? HIGH : LOW);
121 }
else if (state && level > 0.0) {
123 uint16_t bri = (uint16_t)(level * (
double)blPwmRange);
125 if (!blActiveLogic) {
126 bri = blPwmRange - bri;
129 ledcWrite(blChannel, bri);
131 analogWrite(blPin, bri);
134 light.forceState(
false, 0.0);
135 onLightControl(
false, 0.0, control, notify);
140 ledcWrite(blChannel, blActiveLogic ? 0 : blPwmRange);
142 digitalWrite(blPin, blActiveLogic ? LOW : HIGH);
147 pSched->publish(name +
"/light/unitbrightness", String(level, 3));
148 pSched->publish(name +
"/light/state", state ?
"on" :
"off");
153 virtual void getDimensions(int16_t &width, int16_t &height) {
154 width = display.width();
155 height = display.height();
158 virtual bool getTextWrap() {
159 return display.getTextWrap();
162 virtual void setTextWrap(
bool wrap) {
163 display.setTextWrap(wrap);
166 virtual void setTextFont(uint8_t font, int16_t baseLineAdjustment) {
167 display.setFont(fonts[font]);
168 if (baseLineAdjustment) {
169 int16_t y = display.getCursorY();
170 display.setCursor(display.getCursorX(), y + baseLineAdjustment);
174 virtual void setTextColor(uint16_t fg, uint16_t bg) {
175 display.setTextColor(fg, bg);
178 virtual void getCursor(int16_t &x, int16_t &y) {
179 x = display.getCursorX();
180 y = display.getCursorY();
183 virtual void setCursor(int16_t x, int16_t y) {
184 display.setCursor(x, y);
187 virtual void displayClear(int16_t x, int16_t y, int16_t w, int16_t h) {
188 display.fillRect(x, y, w, h, display.getTextBackground());
191 virtual void displayClear(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t bg) {
192 display.fillRect(x, y, w, h, bg);
195 virtual void displayPrint(String content,
bool ln =
false) {
197 display.println(content);
199 display.print(content);
203 virtual bool displayFormat(int16_t x, int16_t y, int16_t w, int16_t align, String content,
204 uint8_t font, uint16_t color, uint16_t bg) {
205 display.setFont(fonts[font]);
206 display.setTextColor(color, bg);
207 bool ret = display.printFormatted(x, y, w, align, content, sizes[font].baseLine,
208 sizes[font].yAdvance);
209 display.setTextColor(current_fg, current_bg);
210 display.setFont(fonts[current_font]);
214#ifdef USTD_FEATURE_PROGRAMPLAYER
215 virtual bool initNextCharDimensions(ProgramItem &item) {
216 while (charPos < item.content.length()) {
217 int16_t minx = 0x7FFF, miny = 0x7FFF, maxx = -1, maxy = -1;
218 int16_t x = 0, y = sizes[item.font].baseLine;
219 display.getCharBounds(item.content[charPos], &x, &y, &minx, &miny, &maxx, &maxy);
222 charY = sizes[item.font].yAdvance;
223 if (item.content[charPos] ==
' ') {
228 }
else if (item.content[charPos] ==
' ') {
240 void initBacklightHardware() {
241#if defined(__ESP32__)
242 pinMode(blPin, OUTPUT);
244#define LEDC_TIMER_BITS 10
246#define LEDC_BASE_FREQ 5000
247 ledcSetup(blChannel, LEDC_BASE_FREQ, LEDC_TIMER_BITS);
248 ledcAttachPin(blPin, blChannel);
250 pinMode(blPin, OUTPUT);
259 void getTextDimensions(uint8_t font,
const char *content, int16_t &width, int16_t &height) {
260 if (!content || !*content) {
267 uint8_t old_font = current_font;
268 bool old_wrap = display.getTextWrap();
269 display.setFont(fonts[font]);
270 display.setTextWrap(
false);
271 display.getTextBounds(content, 0, sizes[font].baseLine, &x, &y, &w, &h);
272 display.setTextWrap(old_wrap);
273 display.setFont(fonts[old_font]);
The muwerk namespace.
Definition: display_digits_max72xx.h:10