Optimizations in ALT rotary encoder & display usermods.

This commit is contained in:
Blaž Kristan 2021-12-21 11:20:11 +01:00
parent 12f2caa8d6
commit 061e055d1b
2 changed files with 58 additions and 51 deletions

View File

@ -90,9 +90,6 @@
// if SLEEP_MODE_ENABLED. // if SLEEP_MODE_ENABLED.
#define SCREEN_TIMEOUT_MS 60*1000 // 1 min #define SCREEN_TIMEOUT_MS 60*1000 // 1 min
#define TIME_INDENT 0
#define DATE_INDENT 2
// Minimum time between redrawing screen in ms // Minimum time between redrawing screen in ms
#define USER_LOOP_REFRESH_RATE_MS 1000 #define USER_LOOP_REFRESH_RATE_MS 1000
@ -101,15 +98,6 @@
#define MAX_JSON_CHARS 19+1 #define MAX_JSON_CHARS 19+1
#define MAX_MODE_LINE_SPACE 13+1 #define MAX_MODE_LINE_SPACE 13+1
typedef enum {
FLD_LINE_BRIGHTNESS = 0,
FLD_LINE_EFFECT_SPEED,
FLD_LINE_EFFECT_INTENSITY,
FLD_LINE_MODE,
FLD_LINE_PALETTE,
FLD_LINE_TIME
} Line4Type;
typedef enum { typedef enum {
NONE = 0, NONE = 0,
SSD1306, // U8X8_SSD1306_128X32_UNIVISION_HW_I2C SSD1306, // U8X8_SSD1306_128X32_UNIVISION_HW_I2C
@ -272,6 +260,7 @@ class FourLineDisplayUsermod : public Usermod {
uint32_t screenTimeout = SCREEN_TIMEOUT_MS; // in ms uint32_t screenTimeout = SCREEN_TIMEOUT_MS; // in ms
bool sleepMode = true; // allow screen sleep? bool sleepMode = true; // allow screen sleep?
bool clockMode = false; // display clock bool clockMode = false; // display clock
bool showSeconds = true; // display clock with seconds
bool enabled = true; bool enabled = true;
// needRedraw marks if redraw is required to prevent often redrawing. // needRedraw marks if redraw is required to prevent often redrawing.
@ -287,6 +276,7 @@ class FourLineDisplayUsermod : public Usermod {
uint8_t knownMode = 0; uint8_t knownMode = 0;
uint8_t knownPalette = 0; uint8_t knownPalette = 0;
uint8_t knownMinute = 99; uint8_t knownMinute = 99;
uint8_t knownHour = 99;
byte brightness100; byte brightness100;
byte fxspeed100; byte fxspeed100;
byte fxintensity100; byte fxintensity100;
@ -298,7 +288,7 @@ class FourLineDisplayUsermod : public Usermod {
unsigned long nextUpdate = 0; unsigned long nextUpdate = 0;
unsigned long lastRedraw = 0; unsigned long lastRedraw = 0;
unsigned long overlayUntil = 0; unsigned long overlayUntil = 0;
Line4Type lineType = FLD_LINE_BRIGHTNESS;
// Set to 2 or 3 to mark lines 2 or 3. Other values ignored. // Set to 2 or 3 to mark lines 2 or 3. Other values ignored.
byte markLineNum = 0; byte markLineNum = 0;
byte markColNum = 0; byte markColNum = 0;
@ -312,6 +302,7 @@ class FourLineDisplayUsermod : public Usermod {
static const char _flip[]; static const char _flip[];
static const char _sleepMode[]; static const char _sleepMode[];
static const char _clockMode[]; static const char _clockMode[];
static const char _showSeconds[];
static const char _busClkFrequency[]; static const char _busClkFrequency[];
// If display does not work or looks corrupted check the // If display does not work or looks corrupted check the
@ -413,7 +404,7 @@ class FourLineDisplayUsermod : public Usermod {
if (!enabled || strip.isUpdating()) return; if (!enabled || strip.isUpdating()) return;
unsigned long now = millis(); unsigned long now = millis();
if (now < nextUpdate) return; if (now < nextUpdate) return;
nextUpdate = now + (clockMode?1000:refreshRate); nextUpdate = now + ((clockMode && showSeconds) ? 1000 : refreshRate);
redraw(false); redraw(false);
} }
@ -491,6 +482,7 @@ class FourLineDisplayUsermod : public Usermod {
// Check if values which are shown on display changed from the last time. // Check if values which are shown on display changed from the last time.
if (forceRedraw) { if (forceRedraw) {
knownHour = 99;
needRedraw = true; needRedraw = true;
} else if ((bri == 0 && powerON) || (bri > 0 && !powerON)) { //trigger power icon } else if ((bri == 0 && powerON) || (bri > 0 && !powerON)) { //trigger power icon
powerON = !powerON; powerON = !powerON;
@ -715,7 +707,7 @@ class FourLineDisplayUsermod : public Usermod {
*/ */
bool wakeDisplay() { bool wakeDisplay() {
if (type == NONE || !enabled) return false; if (type == NONE || !enabled) return false;
//knownHour = 99; knownHour = 99;
if (displayTurnedOff) { if (displayTurnedOff) {
// Turn the display back on // Turn the display back on
sleepOrClock(false); sleepOrClock(false);
@ -776,7 +768,7 @@ class FourLineDisplayUsermod : public Usermod {
line = apPass; line = apPass;
center(line, getCols()); center(line, getCols());
drawString(0, lineHeight*3, line.c_str()); drawString(0, lineHeight*3, line.c_str());
} else if (strcmp(serverDescription, "WLED") != 0) { } else if (strcmp(serverDescription, PSTR("WLED")) != 0) {
line = serverDescription; line = serverDescription;
center(line, getCols()); center(line, getCols());
drawString(0, lineHeight*3, line.c_str()); drawString(0, lineHeight*3, line.c_str());
@ -814,34 +806,41 @@ class FourLineDisplayUsermod : public Usermod {
char lineBuffer[LINE_BUFFER_SIZE]; char lineBuffer[LINE_BUFFER_SIZE];
static byte lastSecond; static byte lastSecond;
byte secondCurrent = second(localTime); byte secondCurrent = second(localTime);
byte minuteCurrent = minute(localTime);
byte hourCurrent = hour(localTime);
if (knownMinute != minute(localTime)) { //only redraw clock if it has changed if (knownMinute != minuteCurrent) { //only redraw clock if it has changed
//updateLocalTime(); //updateLocalTime();
byte AmPmHour = hour(localTime); byte AmPmHour = hourCurrent;
boolean isitAM = true; boolean isitAM = true;
if (useAMPM) { if (useAMPM) {
if (AmPmHour > 11) AmPmHour -= 12; if (AmPmHour > 11) { AmPmHour -= 12; isitAM = false; }
if (AmPmHour == 0) AmPmHour = 12; if (AmPmHour == 0) { AmPmHour = 12; }
if (hour(localTime) > 11) isitAM = false;
} }
drawStatusIcons(); //icons power, wifi, timer, etc drawStatusIcons(); //icons power, wifi, timer, etc
sprintf_P(lineBuffer, PSTR("%s %2d "), monthShortStr(month(localTime)), day(localTime)); if (knownHour != hourCurrent) {
draw2x2String(DATE_INDENT, lineHeight==1 ? 0 : lineHeight, lineBuffer); // adjust for 8 line displays, draw month and day // only update date when hour changes
sprintf_P(lineBuffer, PSTR("%s %2d "), monthShortStr(month(localTime)), day(localTime));
draw2x2String(2, lineHeight==1 ? 0 : lineHeight, lineBuffer); // adjust for 8 line displays, draw month and day
}
sprintf_P(lineBuffer,PSTR("%2d:%02d"), (useAMPM ? AmPmHour : hour(localTime)), minute(localTime)); sprintf_P(lineBuffer,PSTR("%2d:%02d"), (useAMPM ? AmPmHour : hourCurrent), minuteCurrent);
draw2x2String(TIME_INDENT+2, lineHeight*2, lineBuffer); //draw hour, min. blink ":" depending on odd/even seconds draw2x2String(2, lineHeight*2, lineBuffer); //draw hour, min. blink ":" depending on odd/even seconds
if (useAMPM) drawString(12, lineHeight*2, (isitAM ? "AM" : "PM"), true); //draw am/pm if using 12 time if (useAMPM) drawString(12, lineHeight*2, (isitAM ? "AM" : "PM"), true); //draw am/pm if using 12 time
knownMinute = minute(localTime); knownMinute = minuteCurrent;
knownHour = hourCurrent;
} else { } else {
if (secondCurrent == lastSecond) return; if (secondCurrent == lastSecond) return;
} }
lastSecond = secondCurrent; if (showSeconds && !useAMPM) {
draw2x2String(6, lineHeight*2, secondCurrent%2 ? " " : ":"); lastSecond = secondCurrent;
sprintf_P(lineBuffer, PSTR("%02d"), secondCurrent); draw2x2String(6, lineHeight*2, secondCurrent%2 ? " " : ":");
drawString(12, lineHeight*2+1, lineBuffer, true); // even with double sized rows print seconds in 1 line sprintf_P(lineBuffer, PSTR("%02d"), secondCurrent);
drawString(12 + (lineHeight%2), lineHeight*2+1, lineBuffer, true); // even with double sized rows print seconds in 1 line
}
} }
/* /*
@ -899,6 +898,7 @@ class FourLineDisplayUsermod : public Usermod {
top[FPSTR(_screenTimeOut)] = screenTimeout/1000; top[FPSTR(_screenTimeOut)] = screenTimeout/1000;
top[FPSTR(_sleepMode)] = (bool) sleepMode; top[FPSTR(_sleepMode)] = (bool) sleepMode;
top[FPSTR(_clockMode)] = (bool) clockMode; top[FPSTR(_clockMode)] = (bool) clockMode;
top[FPSTR(_showSeconds)] = (bool) showSeconds;
top[FPSTR(_busClkFrequency)] = ioFrequency/1000; top[FPSTR(_busClkFrequency)] = ioFrequency/1000;
DEBUG_PRINTLN(F("4 Line Display config saved.")); DEBUG_PRINTLN(F("4 Line Display config saved."));
} }
@ -932,6 +932,7 @@ class FourLineDisplayUsermod : public Usermod {
screenTimeout = (top[FPSTR(_screenTimeOut)] | screenTimeout/1000) * 1000; screenTimeout = (top[FPSTR(_screenTimeOut)] | screenTimeout/1000) * 1000;
sleepMode = top[FPSTR(_sleepMode)] | sleepMode; sleepMode = top[FPSTR(_sleepMode)] | sleepMode;
clockMode = top[FPSTR(_clockMode)] | clockMode; clockMode = top[FPSTR(_clockMode)] | clockMode;
showSeconds = top[FPSTR(_showSeconds)] | showSeconds;
if (newType == SSD1306_SPI || newType == SSD1306_SPI64) if (newType == SSD1306_SPI || newType == SSD1306_SPI64)
ioFrequency = min(20000, max(500, (int)(top[FPSTR(_busClkFrequency)] | ioFrequency/1000))) * 1000; // limit frequency ioFrequency = min(20000, max(500, (int)(top[FPSTR(_busClkFrequency)] | ioFrequency/1000))) * 1000; // limit frequency
else else
@ -964,6 +965,7 @@ class FourLineDisplayUsermod : public Usermod {
if (!(type == SSD1306_SPI || type == SSD1306_SPI64)) u8x8->setBusClock(ioFrequency); // can be used for SPI too if (!(type == SSD1306_SPI || type == SSD1306_SPI64)) u8x8->setBusClock(ioFrequency); // can be used for SPI too
setContrast(contrast); setContrast(contrast);
setFlipMode(flip); setFlipMode(flip);
knownHour = 99;
if (needsRedraw && !wakeDisplay()) redraw(true); if (needsRedraw && !wakeDisplay()) redraw(true);
} }
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features // use "return !top["newestParameter"].isNull();" when updating Usermod with new features
@ -988,4 +990,5 @@ const char FourLineDisplayUsermod::_screenTimeOut[] PROGMEM = "screenTimeOutSe
const char FourLineDisplayUsermod::_flip[] PROGMEM = "flip"; const char FourLineDisplayUsermod::_flip[] PROGMEM = "flip";
const char FourLineDisplayUsermod::_sleepMode[] PROGMEM = "sleepMode"; const char FourLineDisplayUsermod::_sleepMode[] PROGMEM = "sleepMode";
const char FourLineDisplayUsermod::_clockMode[] PROGMEM = "clockMode"; const char FourLineDisplayUsermod::_clockMode[] PROGMEM = "clockMode";
const char FourLineDisplayUsermod::_showSeconds[] PROGMEM = "showSeconds";
const char FourLineDisplayUsermod::_busClkFrequency[] PROGMEM = "i2c-freq-kHz"; const char FourLineDisplayUsermod::_busClkFrequency[] PROGMEM = "i2c-freq-kHz";

View File

@ -461,41 +461,45 @@ public:
} }
/* /*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. * Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
* Below it is shown how this could be used for e.g. a light sensor * Below it is shown how this could be used for e.g. a light sensor
*/ */
/* /*
void addToJsonInfo(JsonObject& root) void addToJsonInfo(JsonObject& root)
{ {
int reading = 20; int reading = 20;
//this code adds "u":{"Light":[20," lux"]} to the info object //this code adds "u":{"Light":[20," lux"]} to the info object
JsonObject user = root["u"]; JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u"); if (user.isNull()) user = root.createNestedObject("u");
JsonArray lightArr = user.createNestedArray("Light"); //name JsonArray lightArr = user.createNestedArray("Light"); //name
lightArr.add(reading); //value lightArr.add(reading); //value
lightArr.add(" lux"); //unit lightArr.add(" lux"); //unit
} }
*/ */
/* /*
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
/*
void addToJsonState(JsonObject &root) void addToJsonState(JsonObject &root)
{ {
//root["user0"] = userVar0; //root["user0"] = userVar0;
} }
*/
/* /*
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). * readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
/*
void readFromJsonState(JsonObject &root) void readFromJsonState(JsonObject &root)
{ {
//userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value //userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value
//if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!")); //if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!"));
} }
*/
/** /**
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json * addToConfig() (called from set.cpp) stores persistent properties to cfg.json