From 50061ddae8b98a35f875004ee42bdbe09f4de0f6 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Wed, 10 May 2023 20:14:10 +0200 Subject: [PATCH] UM BH1750 adapted to "MM style", 8266 build fixes * UM BH1750 adjustments to use new MM features * UM BH1750 check sensor status before tryig to read new value (avoids blocking LED updates) * UM temperature: ensure that measurements continue with many LEDs running (strip.isUpdating() will be true all the time) * all usermods: solved compile problems on 8266 --- usermods/BH1750_v2/usermod_bh1750.h | 44 ++++++++++++---------- usermods/Temperature/usermod_temperature.h | 4 +- usermods/sht/usermod_sht.h | 4 +- wled00/usermods_list.cpp | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/usermods/BH1750_v2/usermod_bh1750.h b/usermods/BH1750_v2/usermod_bh1750.h index 77a57efb..0cb4bf18 100644 --- a/usermods/BH1750_v2/usermod_bh1750.h +++ b/usermods/BH1750_v2/usermod_bh1750.h @@ -48,11 +48,11 @@ private: bool getLuminanceComplete = false; // flag set at startup - bool enabled = true; + //bool enabled = true; //WLEDMM not needed as we use global attributes // strings to reduce flash memory usage (used more than twice) - static const char _name[]; - static const char _enabled[]; + //static const char _name[]; //WLEDMM not needed as we use global attributes + //static const char _enabled[]; //WLEDMM not needed as we use global attributes static const char _maxReadInterval[]; static const char _minReadInterval[]; static const char _offset[]; @@ -76,7 +76,7 @@ private: bool sensorFound = false; // Home Assistant and MQTT - String mqttLuminanceTopic = F(""); + String mqttLuminanceTopic = FPSTR(""); bool mqttInitialized = false; bool HomeAssistantDiscovery = true; // Publish Home Assistant Discovery messages @@ -132,6 +132,8 @@ private: } public: + Usermod_BH1750(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM + void setup() { #if 0 @@ -157,22 +159,22 @@ public: if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously sensorFound = false; //enabled = false; - USER_PRINTLN(F("BH1750: failed to join I2C bus.")); + USER_PRINTLN(F("[BH1750]: failed to join I2C bus.")); return; } - sensorFound = lightMeter.begin(); - if (sensorFound) { USER_PRINTLN(F("BH1750 sensor found.")); } - else{ USER_PRINTLN(F("BH1750 sensor not found.")); } + sensorFound = lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE); // WLEDMM set mode explicitly + if (sensorFound) { USER_PRINTLN(F("[BH1750] sensor found.")); } + else{ USER_PRINTLN(F("[BH1750] sensor not found.")); } initDone = true; } void loop() { - if ((!enabled) || strip.isUpdating()) + if (!sensorFound || !initDone) return; // WLEDMM bugfix + if ((!enabled) || (strip.isUpdating() && (millis() - lastMeasurement < 450))) // WLEDMM be nice, but not too nice return; - if (!sensorFound) return; // WLEDMM bugfix unsigned long now = millis(); // check to see if we are due for taking a measurement @@ -184,10 +186,12 @@ public: } bool shouldUpdate = now - lastSend > maxReadingInterval; - - float lux = lightMeter.readLightLevel(); - lastMeasurement = millis(); - getLuminanceComplete = true; + float lux = lastLux; + if (lightMeter.measurementReady()) { //WLEDMM do not block in case the sensor is still busy + lux = lightMeter.readLightLevel(); + lastMeasurement = millis(); + getLuminanceComplete = true; + } if (shouldUpdate || checkBoundSensor(lux, lastLux, offset)) { @@ -255,7 +259,7 @@ public: { // we add JSON object. JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname - top[FPSTR(_enabled)] = enabled; + top[F("enabled")] = enabled; top[FPSTR(_maxReadInterval)] = maxReadingInterval; top[FPSTR(_minReadInterval)] = minReadingInterval; top[FPSTR(_HomeAssistantDiscovery)] = HomeAssistantDiscovery; @@ -269,7 +273,7 @@ public: // top[F("help4Pins")] = F("SCL,SDA"); // help for Settings page - DEBUG_PRINTLN(F("BH1750 config saved.")); + DEBUG_PRINTLN(F("[BH1750] config saved.")); } // called before setup() to populate properties from values stored in cfg.json @@ -283,13 +287,13 @@ public: if (top.isNull()) { DEBUG_PRINT(FPSTR(_name)); - DEBUG_PRINT(F("BH1750")); + DEBUG_PRINT(F("[BH1750]")); DEBUG_PRINTLN(F(": No config found. (Using defaults.)")); return false; } bool configComplete = !top.isNull(); - configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled, false); + configComplete &= getJsonValue(top[F("enabled")], enabled, false); configComplete &= getJsonValue(top[FPSTR(_maxReadInterval)], maxReadingInterval, 10000); //ms configComplete &= getJsonValue(top[FPSTR(_minReadInterval)], minReadingInterval, 500); //ms configComplete &= getJsonValue(top[FPSTR(_HomeAssistantDiscovery)], HomeAssistantDiscovery, false); @@ -335,8 +339,8 @@ public: }; // strings to reduce flash memory usage (used more than twice) -const char Usermod_BH1750::_name[] PROGMEM = "BH1750"; -const char Usermod_BH1750::_enabled[] PROGMEM = "enabled"; +//const char Usermod_BH1750::_name[] PROGMEM = "BH1750"; //WLEDMM not needed as we use global attributes +//const char Usermod_BH1750::_enabled[] PROGMEM = "enabled"; //WLEDMM not needed as we use global attributes const char Usermod_BH1750::_maxReadInterval[] PROGMEM = "max-read-interval-ms"; const char Usermod_BH1750::_minReadInterval[] PROGMEM = "min-read-interval-ms"; const char Usermod_BH1750::_HomeAssistantDiscovery[] PROGMEM = "HomeAssistantDiscoveryLux"; diff --git a/usermods/Temperature/usermod_temperature.h b/usermods/Temperature/usermod_temperature.h index 1c49163a..0d51e21a 100644 --- a/usermods/Temperature/usermod_temperature.h +++ b/usermods/Temperature/usermod_temperature.h @@ -194,7 +194,9 @@ class UsermodTemperature : public Usermod { } void loop() { - if (!enabled || !sensorFound || strip.isUpdating()) return; + unsigned long last_runtime = 0; // WLEDMM ensure that strip.isUpdating() will not block longer that 4000ms + if (!enabled || !sensorFound || (strip.isUpdating() && (millis()-last_runtime < 4000))) return; // WLEDMM be nice, but not too nice + last_runtime = millis(); static uint8_t errorCount = 0; unsigned long now = millis(); diff --git a/usermods/sht/usermod_sht.h b/usermods/sht/usermod_sht.h index 7cc9fe54..8a124bbb 100644 --- a/usermods/sht/usermod_sht.h +++ b/usermods/sht/usermod_sht.h @@ -390,7 +390,7 @@ void ShtUsermod::addToConfig(JsonObject &root) { JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname - top[FPSTR("enabled")] = enabled; + top[F("enabled")] = enabled; top[FPSTR(_shtType)] = shtType; top[FPSTR(_unitOfTemp)] = unitOfTemp; top[FPSTR(_haMqttDiscovery)] = haMqttDiscovery; @@ -421,7 +421,7 @@ bool ShtUsermod::readFromConfig(JsonObject &root) byte oldUnitOfTemp = unitOfTemp; bool oldHaMqttDiscovery = haMqttDiscovery; - getJsonValue(top[FPSTR("enabled")], enabled); + getJsonValue(top[F("enabled")], enabled); getJsonValue(top[FPSTR(_shtType)], shtType); getJsonValue(top[FPSTR(_unitOfTemp)], unitOfTemp); getJsonValue(top[FPSTR(_haMqttDiscovery)], haMqttDiscovery); diff --git a/wled00/usermods_list.cpp b/wled00/usermods_list.cpp index 24636ed1..4226821d 100644 --- a/wled00/usermods_list.cpp +++ b/wled00/usermods_list.cpp @@ -238,7 +238,7 @@ void registerUsermods() #endif #ifdef USERMOD_BH1750 - usermods.add(new Usermod_BH1750()); + usermods.add(new Usermod_BH1750("BH1750", false)); #endif #ifdef USERMOD_BME280