From f96a5ec77462576b5c338bd815808f310fd2fb2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Kristan?= Date: Fri, 23 Apr 2021 14:32:18 +0200 Subject: [PATCH] Changed version to 0.12.2-bl1 Optimised strings in Animated Staircase usermod. Minor typos. --- package.json | 2 +- .../Animated_Staircase/Animated_Staircase.h | 130 +++++++++++------- .../usermod_v2_four_line_display.h | 2 +- wled00/wled.h | 2 +- 4 files changed, 86 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index cf1eb70c3..8ac8318c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wled", - "version": "0.12.1-a1", + "version": "0.12.2-bl1", "description": "Tools for WLED project", "main": "tools/cdata.js", "directories": { diff --git a/usermods/Animated_Staircase/Animated_Staircase.h b/usermods/Animated_Staircase/Animated_Staircase.h index 3a849cdc9..89066ecc1 100644 --- a/usermods/Animated_Staircase/Animated_Staircase.h +++ b/usermods/Animated_Staircase/Animated_Staircase.h @@ -77,6 +77,21 @@ class Animated_Staircase : public Usermod { bool bottomSensorRead = false; bool bottomSensorWrite = false; + // strings to reduce flash memory usage (used more than twice) + static const char _name[]; + static const char _enabled[]; + static const char _segmentDelay[]; + static const char _onTime[]; + static const char _useTopUltrasoundSensor[]; + static const char _topPIRorTrigger_pin[]; + static const char _topEcho_pin[]; + static const char _useBottomUltrasoundSensor[]; + static const char _bottomPIRorTrigger_pin[]; + static const char _bottomEcho_pin[]; + static const char _topEchoTime[]; + static const char _bottomEchoTime[]; + static const char _[]; + void updateSegments() { // mainSegmentId = strip.getMainSegmentId(); // WS2812FX::Segment mainsegment = strip.getSegment(mainSegmentId); @@ -183,6 +198,7 @@ class Animated_Staircase : public Usermod { } void autoPowerOff() { + // TODO: add logic to wait until PIR sensor deactivates if (on && ((millis() - lastSwitchTime) > on_time_ms)) { // Swipe OFF in the direction of the last sensor detection swipe = lastSensor; @@ -219,17 +235,14 @@ class Animated_Staircase : public Usermod { } } - void writeSensorsToJson(JsonObject& root) { - JsonObject staircase = root[F("staircase")]; - if (staircase.isNull()) { - staircase = root.createNestedObject(F("staircase")); - } + // send sesnor values to JSON API + void writeSensorsToJson(JsonObject& staircase) { staircase[F("top-sensor")] = topSensorRead; staircase[F("bottom-sensor")] = bottomSensorRead; } - void readSensorsFromJson(JsonObject& root) { - JsonObject staircase = root[F("staircase")]; + // allow overrides from JSON API + void readSensorsFromJson(JsonObject& staircase) { bottomSensorWrite = bottomSensorRead || (staircase[F("bottom-sensor")].as()); topSensorWrite = topSensorRead || (staircase[F("top-sensor")].as()); } @@ -243,6 +256,7 @@ class Animated_Staircase : public Usermod { DEBUG_PRINT(on_time_ms / 1000); DEBUG_PRINTLN(F(" seconds.")); + // TODO: attach interrupts if (!useUSSensorBottom) pinMode(bottomPIRorTriggerPin, INPUT); else { @@ -292,6 +306,7 @@ class Animated_Staircase : public Usermod { if (!pinManager.allocatePin(bottomPIRorTriggerPin,false)) bottomEchoPin = -1; } + // TODO: attach interrupts in enable() // validate pins if ( topPIRorTriggerPin < 0 || bottomPIRorTriggerPin < 0 || @@ -312,9 +327,12 @@ class Animated_Staircase : public Usermod { uint16_t getId() { return USERMOD_ID_ANIMATED_STAIRCASE; } void addToJsonState(JsonObject& root) { -// writeSettingsToJson(root); -// writeSensorsToJson(root); -// DEBUG_PRINTLN(F("Staircase config exposed in API.")); + JsonObject staircase = root[FPSTR(_name)]; + if (staircase.isNull()) { + staircase = root.createNestedObject(FPSTR(_name)); + } + writeSensorsToJson(staircase); + DEBUG_PRINTLN(F("Staircase sensor state exposed in API.")); } /* @@ -323,14 +341,16 @@ class Animated_Staircase : public Usermod { */ void readFromJsonState(JsonObject& root) { if (!initDone) return; // prevent crash on boot applyPreset() - JsonObject staircase = root[F("staircase")]; + JsonObject staircase = root[FPSTR(_name)]; if (!staircase.isNull()) { - if (staircase[F("enabled")].is()) { - enabled = staircase[F("enabled")].as(); + if (staircase[FPSTR(_enabled)].is()) { + enabled = staircase[FPSTR(_enabled)].as(); } else { - String str = staircase[F("enabled")]; // checkbox -> off or on + String str = staircase[FPSTR(_enabled)]; // checkbox -> off or on enabled = (bool)(str!="off"); // off is guaranteed to be present } + readSensorsFromJson(root); + DEBUG_PRINTLN(F("Staircase sensor state read from API.")); } } @@ -338,21 +358,21 @@ class Animated_Staircase : public Usermod { * Writes the configuration to internal flash memory. */ void addToConfig(JsonObject& root) { - JsonObject staircase = root[F("staircase")]; + JsonObject staircase = root[FPSTR(_name)]; if (staircase.isNull()) { - staircase = root.createNestedObject(F("staircase")); + staircase = root.createNestedObject(FPSTR(_name)); } - staircase[F("enabled")] = enabled; - staircase[F("segment-delay-ms")] = segment_delay_ms; - staircase[F("on-time-s")] = on_time_ms / 1000; - staircase[F("useTopUltrasoundSensor")] = useUSSensorTop; - staircase[F("topPIRorTrigger_pin")] = topPIRorTriggerPin; - staircase[F("topEcho_pin")] = topEchoPin; - staircase[F("useBottomUltrasoundSensor")] = useUSSensorBottom; - staircase[F("bottomPIRorTrigger_pin")] = bottomPIRorTriggerPin; - staircase[F("bottomEcho_pin")] = bottomEchoPin; - staircase[F("top-echo-us")] = topMaxTimeUs; - staircase[F("bottom-echo-us")] = bottomMaxTimeUs; + staircase[FPSTR(_enabled)] = enabled; + staircase[FPSTR(_segmentDelay)] = segment_delay_ms; + staircase[FPSTR(_onTime)] = on_time_ms / 1000; + staircase[FPSTR(_useTopUltrasoundSensor)] = useUSSensorTop; + staircase[FPSTR(_topPIRorTrigger_pin)] = topPIRorTriggerPin; + staircase[FPSTR(_topEcho_pin)] = useUSSensorTop ? topEchoPin : -1; + staircase[FPSTR(_useBottomUltrasoundSensor)] = useUSSensorBottom; + staircase[FPSTR(_bottomPIRorTrigger_pin)] = bottomPIRorTriggerPin; + staircase[FPSTR(_bottomEcho_pin)] = useUSSensorBottom ? bottomEchoPin : -1; + staircase[FPSTR(_topEchoTime)] = topMaxTimeUs; + staircase[FPSTR(_bottomEchoTime)] = bottomMaxTimeUs; DEBUG_PRINTLN(F("Staircase config saved.")); } @@ -367,35 +387,37 @@ class Animated_Staircase : public Usermod { int8_t oldBottomAPin = bottomPIRorTriggerPin; int8_t oldBottomBPin = bottomEchoPin; - JsonObject staircase = root[F("staircase")]; + JsonObject staircase = root[FPSTR(_name)]; if (!staircase.isNull()) { - if (staircase[F("enabled")].is()) { - enabled = staircase[F("enabled")].as(); + if (staircase[FPSTR(_enabled)].is()) { + enabled = staircase[FPSTR(_enabled)].as(); } else { - String str = staircase[F("enabled")]; // checkbox -> off or on + String str = staircase[FPSTR(_enabled)]; // checkbox -> off or on enabled = (bool)(str!="off"); // off is guaranteed to be present } - segment_delay_ms = staircase[F("segment-delay-ms")]; - on_time_ms = (int)staircase[F("on-time-s")] * 1000; - if (staircase[F("useTopUltrasoundSensor")].is()) { - useUSSensorTop = staircase[F("useTopUltrasoundSensor")].as(); + segment_delay_ms = min(10000,max(10,staircase[FPSTR(_segmentDelay)].as())); // max delay 10s + on_time_ms = min(900,max(10,staircase[FPSTR(_onTime)].as())) * 1000; // min 10s, max 15min + + if (staircase[FPSTR(_useTopUltrasoundSensor)].is()) { + useUSSensorTop = staircase[FPSTR(_useTopUltrasoundSensor)].as(); } else { - String str = staircase[F("useTopUltrasoundSensor")]; // checkbox -> off or on + String str = staircase[FPSTR(_useTopUltrasoundSensor)]; // checkbox -> off or on useUSSensorTop = (bool)(str!="off"); // off is guaranteed to be present } - topPIRorTriggerPin = staircase[F("topPIRorTrigger_pin")]; - topEchoPin = staircase[F("topEcho_pin")]; - useUSSensorBottom = staircase[F("useBottomUltrasoundSensor")].as(); - if (staircase[F("useBottomUltrasoundSensor")].is()) { - useUSSensorBottom = staircase[F("useBottomUltrasoundSensor")].as(); + + topPIRorTriggerPin = min(39,max(-1,staircase[FPSTR(_topPIRorTrigger_pin)].as())); + topEchoPin = min(39,max(-1,staircase[FPSTR(_topEcho_pin)].as())); + + if (staircase[FPSTR(_useBottomUltrasoundSensor)].is()) { + useUSSensorBottom = staircase[FPSTR(_useBottomUltrasoundSensor)].as(); } else { - String str = staircase[F("useBottomUltrasoundSensor")]; // checkbox -> off or on + String str = staircase[FPSTR(_useBottomUltrasoundSensor)]; // checkbox -> off or on useUSSensorBottom = (bool)(str!="off"); // off is guaranteed to be present } - bottomPIRorTriggerPin = staircase[F("bottomPIRorTrigger_pin")]; - bottomEchoPin = staircase[F("bottomEcho_pin")]; - topMaxTimeUs = staircase[F("top-echo-us")]; - bottomMaxTimeUs = staircase[F("bottom-echo-us")]; + bottomPIRorTriggerPin = min(39,max(-1,staircase[FPSTR(_bottomPIRorTrigger_pin)].as())); + bottomEchoPin = min(39,max(-1,staircase[FPSTR(_bottomEcho_pin)].as())); + topMaxTimeUs = min(18000,max(300,staircase[FPSTR(_topEchoTime)].as())); // max distnace ~3m (a noticable lag of 18ms may be expected) + bottomMaxTimeUs = min(18000,max(300,staircase[FPSTR(_bottomEchoTime)].as())); // max distance ~3m (a noticable lag of 18ms may be expected) DEBUG_PRINTLN(F("Staircase config (re)loaded.")); } else { DEBUG_PRINTLN(F("No config found. (Using defaults.)")); @@ -447,4 +469,18 @@ class Animated_Staircase : public Usermod { usermodEnabled.add("no"); // value } } -}; \ No newline at end of file +}; + +// strings to reduce flash memory usage (used more than twice) +const char Animated_Staircase::_name[] PROGMEM = "staircase"; +const char Animated_Staircase::_enabled[] PROGMEM = "enabled"; +const char Animated_Staircase::_segmentDelay[] PROGMEM = "segment-delay-ms"; +const char Animated_Staircase::_onTime[] PROGMEM = "on-time-s"; +const char Animated_Staircase::_useTopUltrasoundSensor[] PROGMEM = "useTopUltrasoundSensor"; +const char Animated_Staircase::_topPIRorTrigger_pin[] PROGMEM = "topPIRorTrigger_pin"; +const char Animated_Staircase::_topEcho_pin[] PROGMEM = "topEcho_pin"; +const char Animated_Staircase::_useBottomUltrasoundSensor[] PROGMEM = "useBottomUltrasoundSensor"; +const char Animated_Staircase::_bottomPIRorTrigger_pin[] PROGMEM = "bottomPIRorTrigger_pin"; +const char Animated_Staircase::_bottomEcho_pin[] PROGMEM = "bottomEcho_pin"; +const char Animated_Staircase::_topEchoTime[] PROGMEM = "top-echo-us"; +const char Animated_Staircase::_bottomEchoTime[] PROGMEM = "bottom-echo-us"; diff --git a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h index 25ba08283..674fa38c0 100644 --- a/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h +++ b/usermods/usermod_v2_four_line_display/usermod_v2_four_line_display.h @@ -65,7 +65,7 @@ typedef enum { NONE = 0, SSD1306, // U8X8_SSD1306_128X32_UNIVISION_HW_I2C SH1106, // U8X8_SH1106_128X64_WINSTAR_HW_I2C - SSD1306_64 // U8X8_SSD1306_128X64_UNIVISION_HW_I2C + SSD1306_64 // U8X8_SSD1306_128X64_NONAME_HW_I2C } DisplayType; class FourLineDisplayUsermod : public Usermod { diff --git a/wled00/wled.h b/wled00/wled.h index d7f3853ad..903e8e414 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2104231 +#define VERSION 2104241 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG