usermods: Fix 7sd_reloaded cross module binding

Fix up the cross module binding for
usermods/seven_segment_display_reloaded.  This requires splitting off
headers for BH1750_v2 and SN_Photoresistor.
This commit is contained in:
Will Miles 2025-03-28 20:15:36 -04:00
parent ff99c7de70
commit 1cd3a97c51
8 changed files with 453 additions and 403 deletions

View File

@ -2,75 +2,18 @@
#warning **** Included USERMOD_BH1750 **** #warning **** Included USERMOD_BH1750 ****
#include "wled.h" #include "wled.h"
#include <BH1750.h> #include "BH1750_v2.h"
#ifdef WLED_DISABLE_MQTT #ifdef WLED_DISABLE_MQTT
#error "This user mod requires MQTT to be enabled." #error "This user mod requires MQTT to be enabled."
#endif #endif
// the max frequency to check photoresistor, 10 seconds static bool checkBoundSensor(float newValue, float prevValue, float maxDiff)
#ifndef USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL
#define USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL 10000
#endif
// the min frequency to check photoresistor, 500 ms
#ifndef USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL
#define USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL 500
#endif
// how many seconds after boot to take first measurement, 10 seconds
#ifndef USERMOD_BH1750_FIRST_MEASUREMENT_AT
#define USERMOD_BH1750_FIRST_MEASUREMENT_AT 10000
#endif
// only report if difference grater than offset value
#ifndef USERMOD_BH1750_OFFSET_VALUE
#define USERMOD_BH1750_OFFSET_VALUE 1
#endif
class Usermod_BH1750 : public Usermod
{
private:
int8_t offset = USERMOD_BH1750_OFFSET_VALUE;
unsigned long maxReadingInterval = USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL;
unsigned long minReadingInterval = USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL;
unsigned long lastMeasurement = UINT32_MAX - (USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL - USERMOD_BH1750_FIRST_MEASUREMENT_AT);
unsigned long lastSend = UINT32_MAX - (USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL - USERMOD_BH1750_FIRST_MEASUREMENT_AT);
// flag to indicate we have finished the first readLightLevel call
// allows this library to report to the user how long until the first
// measurement
bool getLuminanceComplete = false;
// flag set at startup
bool enabled = true;
// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _maxReadInterval[];
static const char _minReadInterval[];
static const char _offset[];
static const char _HomeAssistantDiscovery[];
bool initDone = false;
bool sensorFound = false;
// Home Assistant and MQTT
String mqttLuminanceTopic;
bool mqttInitialized = false;
bool HomeAssistantDiscovery = true; // Publish Home Assistant Discovery messages
BH1750 lightMeter;
float lastLux = -1000;
bool checkBoundSensor(float newValue, float prevValue, float maxDiff)
{ {
return isnan(prevValue) || newValue <= prevValue - maxDiff || newValue >= prevValue + maxDiff || (newValue == 0.0 && prevValue > 0.0); return isnan(prevValue) || newValue <= prevValue - maxDiff || newValue >= prevValue + maxDiff || (newValue == 0.0 && prevValue > 0.0);
} }
// set up Home Assistant discovery entries void Usermod_BH1750::_mqttInitialize()
void _mqttInitialize()
{ {
mqttLuminanceTopic = String(mqttDeviceTopic) + F("/brightness"); mqttLuminanceTopic = String(mqttDeviceTopic) + F("/brightness");
@ -78,7 +21,7 @@ private:
} }
// Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. // Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement) void Usermod_BH1750::_createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement)
{ {
String t = String(F("homeassistant/sensor/")) + mqttClientID + F("/") + name + F("/config"); String t = String(F("homeassistant/sensor/")) + mqttClientID + F("/") + name + F("/config");
@ -108,15 +51,14 @@ private:
mqtt->publish(t.c_str(), 0, true, temp.c_str()); mqtt->publish(t.c_str(), 0, true, temp.c_str());
} }
public: void Usermod_BH1750::setup()
void setup()
{ {
if (i2c_scl<0 || i2c_sda<0) { enabled = false; return; } if (i2c_scl<0 || i2c_sda<0) { enabled = false; return; }
sensorFound = lightMeter.begin(); sensorFound = lightMeter.begin();
initDone = true; initDone = true;
} }
void loop() void Usermod_BH1750::loop()
{ {
if ((!enabled) || strip.isUpdating()) if ((!enabled) || strip.isUpdating())
return; return;
@ -141,7 +83,7 @@ public:
{ {
lastLux = lux; lastLux = lux;
lastSend = millis(); lastSend = millis();
#ifndef WLED_DISABLE_MQTT
if (WLED_MQTT_CONNECTED) if (WLED_MQTT_CONNECTED)
{ {
if (!mqttInitialized) if (!mqttInitialized)
@ -156,15 +98,11 @@ public:
{ {
DEBUG_PRINTLN(F("Missing MQTT connection. Not publishing data")); DEBUG_PRINTLN(F("Missing MQTT connection. Not publishing data"));
} }
#endif
} }
} }
inline float getIlluminance() {
return (float)lastLux;
}
void addToJsonInfo(JsonObject &root) void Usermod_BH1750::addToJsonInfo(JsonObject &root)
{ {
JsonObject user = root[F("u")]; JsonObject user = root[F("u")];
if (user.isNull()) if (user.isNull())
@ -190,7 +128,7 @@ public:
} }
// (called from set.cpp) stores persistent properties to cfg.json // (called from set.cpp) stores persistent properties to cfg.json
void addToConfig(JsonObject &root) void Usermod_BH1750::addToConfig(JsonObject &root)
{ {
// we add JSON object. // we add JSON object.
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
@ -204,7 +142,7 @@ public:
} }
// called before setup() to populate properties from values stored in cfg.json // called before setup() to populate properties from values stored in cfg.json
bool readFromConfig(JsonObject &root) bool Usermod_BH1750::readFromConfig(JsonObject &root)
{ {
// we look for JSON object. // we look for JSON object.
JsonObject top = root[FPSTR(_name)]; JsonObject top = root[FPSTR(_name)];
@ -234,12 +172,6 @@ public:
} }
uint16_t getId()
{
return USERMOD_ID_BH1750;
}
};
// strings to reduce flash memory usage (used more than twice) // strings to reduce flash memory usage (used more than twice)
const char Usermod_BH1750::_name[] PROGMEM = "BH1750"; const char Usermod_BH1750::_name[] PROGMEM = "BH1750";

View File

@ -0,0 +1,92 @@
#pragma once
#include "wled.h"
#include <BH1750.h>
#ifdef WLED_DISABLE_MQTT
#error "This user mod requires MQTT to be enabled."
#endif
// the max frequency to check photoresistor, 10 seconds
#ifndef USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL
#define USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL 10000
#endif
// the min frequency to check photoresistor, 500 ms
#ifndef USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL
#define USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL 500
#endif
// how many seconds after boot to take first measurement, 10 seconds
#ifndef USERMOD_BH1750_FIRST_MEASUREMENT_AT
#define USERMOD_BH1750_FIRST_MEASUREMENT_AT 10000
#endif
// only report if difference grater than offset value
#ifndef USERMOD_BH1750_OFFSET_VALUE
#define USERMOD_BH1750_OFFSET_VALUE 1
#endif
class Usermod_BH1750 : public Usermod
{
private:
int8_t offset = USERMOD_BH1750_OFFSET_VALUE;
unsigned long maxReadingInterval = USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL;
unsigned long minReadingInterval = USERMOD_BH1750_MIN_MEASUREMENT_INTERVAL;
unsigned long lastMeasurement = UINT32_MAX - (USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL - USERMOD_BH1750_FIRST_MEASUREMENT_AT);
unsigned long lastSend = UINT32_MAX - (USERMOD_BH1750_MAX_MEASUREMENT_INTERVAL - USERMOD_BH1750_FIRST_MEASUREMENT_AT);
// flag to indicate we have finished the first readLightLevel call
// allows this library to report to the user how long until the first
// measurement
bool getLuminanceComplete = false;
// flag set at startup
bool enabled = true;
// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _maxReadInterval[];
static const char _minReadInterval[];
static const char _offset[];
static const char _HomeAssistantDiscovery[];
bool initDone = false;
bool sensorFound = false;
// Home Assistant and MQTT
String mqttLuminanceTopic;
bool mqttInitialized = false;
bool HomeAssistantDiscovery = true; // Publish Home Assistant Discovery messages
BH1750 lightMeter;
float lastLux = -1000;
// set up Home Assistant discovery entries
void _mqttInitialize();
// Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement);
public:
void setup();
void loop();
inline float getIlluminance() {
return (float)lastLux;
}
void addToJsonInfo(JsonObject &root);
// (called from set.cpp) stores persistent properties to cfg.json
void addToConfig(JsonObject &root);
// called before setup() to populate properties from values stored in cfg.json
bool readFromConfig(JsonObject &root);
inline uint16_t getId()
{
return USERMOD_ID_BH1750;
}
};

View File

@ -1,75 +1,17 @@
#include "wled.h" #include "wled.h"
#include "SN_Photoresistor.h"
//Pin defaults for QuinLed Dig-Uno (A0) //Pin defaults for QuinLed Dig-Uno (A0)
#ifndef PHOTORESISTOR_PIN #ifndef PHOTORESISTOR_PIN
#define PHOTORESISTOR_PIN A0 #define PHOTORESISTOR_PIN A0
#endif #endif
// the frequency to check photoresistor, 10 seconds static bool checkBoundSensor(float newValue, float prevValue, float maxDiff)
#ifndef USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL
#define USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL 10000
#endif
// how many seconds after boot to take first measurement, 10 seconds
#ifndef USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT
#define USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT 10000
#endif
// supplied voltage
#ifndef USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE
#define USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE 5
#endif
// 10 bits
#ifndef USERMOD_SN_PHOTORESISTOR_ADC_PRECISION
#define USERMOD_SN_PHOTORESISTOR_ADC_PRECISION 1024.0f
#endif
// resistor size 10K hms
#ifndef USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE
#define USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE 10000.0f
#endif
// only report if difference grater than offset value
#ifndef USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE
#define USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE 5
#endif
class Usermod_SN_Photoresistor : public Usermod
{
private:
float referenceVoltage = USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE;
float resistorValue = USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE;
float adcPrecision = USERMOD_SN_PHOTORESISTOR_ADC_PRECISION;
int8_t offset = USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE;
unsigned long readingInterval = USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL;
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
unsigned long lastMeasurement = UINT32_MAX - (USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL - USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT);
// flag to indicate we have finished the first getTemperature call
// allows this library to report to the user how long until the first
// measurement
bool getLuminanceComplete = false;
uint16_t lastLDRValue = -1000;
// flag set at startup
bool disabled = false;
// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _readInterval[];
static const char _referenceVoltage[];
static const char _resistorValue[];
static const char _adcPrecision[];
static const char _offset[];
bool checkBoundSensor(float newValue, float prevValue, float maxDiff)
{ {
return isnan(prevValue) || newValue <= prevValue - maxDiff || newValue >= prevValue + maxDiff; return isnan(prevValue) || newValue <= prevValue - maxDiff || newValue >= prevValue + maxDiff;
} }
uint16_t getLuminance() uint16_t Usermod_SN_Photoresistor::getLuminance()
{ {
// http://forum.arduino.cc/index.php?topic=37555.0 // http://forum.arduino.cc/index.php?topic=37555.0
// https://forum.arduino.cc/index.php?topic=185158.0 // https://forum.arduino.cc/index.php?topic=185158.0
@ -82,14 +24,13 @@ private:
return uint16_t(lux); return uint16_t(lux);
} }
public: void Usermod_SN_Photoresistor::setup()
void setup()
{ {
// set pinmode // set pinmode
pinMode(PHOTORESISTOR_PIN, INPUT); pinMode(PHOTORESISTOR_PIN, INPUT);
} }
void loop() void Usermod_SN_Photoresistor::loop()
{ {
if (disabled || strip.isUpdating()) if (disabled || strip.isUpdating())
return; return;
@ -125,12 +66,8 @@ public:
#endif #endif
} }
uint16_t getLastLDRValue()
{
return lastLDRValue;
}
void addToJsonInfo(JsonObject &root) void Usermod_SN_Photoresistor::addToJsonInfo(JsonObject &root)
{ {
JsonObject user = root[F("u")]; JsonObject user = root[F("u")];
if (user.isNull()) if (user.isNull())
@ -151,15 +88,11 @@ public:
lux.add(F(" lux")); lux.add(F(" lux"));
} }
uint16_t getId()
{
return USERMOD_ID_SN_PHOTORESISTOR;
}
/** /**
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json * addToConfig() (called from set.cpp) stores persistent properties to cfg.json
*/ */
void addToConfig(JsonObject &root) void Usermod_SN_Photoresistor::addToConfig(JsonObject &root)
{ {
// we add JSON object. // we add JSON object.
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
@ -176,7 +109,7 @@ public:
/** /**
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json * readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/ */
bool readFromConfig(JsonObject &root) bool Usermod_SN_Photoresistor::readFromConfig(JsonObject &root)
{ {
// we look for JSON object. // we look for JSON object.
JsonObject top = root[FPSTR(_name)]; JsonObject top = root[FPSTR(_name)];
@ -198,7 +131,7 @@ public:
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features // use "return !top["newestParameter"].isNull();" when updating Usermod with new features
return true; return true;
} }
};
// strings to reduce flash memory usage (used more than twice) // strings to reduce flash memory usage (used more than twice)
const char Usermod_SN_Photoresistor::_name[] PROGMEM = "Photoresistor"; const char Usermod_SN_Photoresistor::_name[] PROGMEM = "Photoresistor";
@ -209,6 +142,5 @@ const char Usermod_SN_Photoresistor::_resistorValue[] PROGMEM = "resistor-value"
const char Usermod_SN_Photoresistor::_adcPrecision[] PROGMEM = "adc-precision"; const char Usermod_SN_Photoresistor::_adcPrecision[] PROGMEM = "adc-precision";
const char Usermod_SN_Photoresistor::_offset[] PROGMEM = "offset"; const char Usermod_SN_Photoresistor::_offset[] PROGMEM = "offset";
static Usermod_SN_Photoresistor sn_photoresistor; static Usermod_SN_Photoresistor sn_photoresistor;
REGISTER_USERMOD(sn_photoresistor); REGISTER_USERMOD(sn_photoresistor);

View File

@ -0,0 +1,90 @@
#pragma once
#include "wled.h"
// the frequency to check photoresistor, 10 seconds
#ifndef USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL
#define USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL 10000
#endif
// how many seconds after boot to take first measurement, 10 seconds
#ifndef USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT
#define USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT 10000
#endif
// supplied voltage
#ifndef USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE
#define USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE 5
#endif
// 10 bits
#ifndef USERMOD_SN_PHOTORESISTOR_ADC_PRECISION
#define USERMOD_SN_PHOTORESISTOR_ADC_PRECISION 1024.0f
#endif
// resistor size 10K hms
#ifndef USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE
#define USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE 10000.0f
#endif
// only report if difference grater than offset value
#ifndef USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE
#define USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE 5
#endif
class Usermod_SN_Photoresistor : public Usermod
{
private:
float referenceVoltage = USERMOD_SN_PHOTORESISTOR_REFERENCE_VOLTAGE;
float resistorValue = USERMOD_SN_PHOTORESISTOR_RESISTOR_VALUE;
float adcPrecision = USERMOD_SN_PHOTORESISTOR_ADC_PRECISION;
int8_t offset = USERMOD_SN_PHOTORESISTOR_OFFSET_VALUE;
unsigned long readingInterval = USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL;
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
unsigned long lastMeasurement = UINT32_MAX - (USERMOD_SN_PHOTORESISTOR_MEASUREMENT_INTERVAL - USERMOD_SN_PHOTORESISTOR_FIRST_MEASUREMENT_AT);
// flag to indicate we have finished the first getTemperature call
// allows this library to report to the user how long until the first
// measurement
bool getLuminanceComplete = false;
uint16_t lastLDRValue = -1000;
// flag set at startup
bool disabled = false;
// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _readInterval[];
static const char _referenceVoltage[];
static const char _resistorValue[];
static const char _adcPrecision[];
static const char _offset[];
uint16_t getLuminance();
public:
void setup();
void loop();
uint16_t getLastLDRValue()
{
return lastLDRValue;
}
void addToJsonInfo(JsonObject &root);
uint16_t getId()
{
return USERMOD_ID_SN_PHOTORESISTOR;
}
/**
* addToConfig() (called from set.cpp) stores persistent properties to cfg.json
*/
void addToConfig(JsonObject &root);
/**
* readFromConfig() is called before setup() to populate properties from values stored in cfg.json
*/
bool readFromConfig(JsonObject &root);
};

View File

@ -1,14 +0,0 @@
#include "wled.h"
/*
* Register your v2 usermods here!
*/
#ifdef USERMOD_SN_PHOTORESISTOR
#include "../usermods/SN_Photoresistor/usermod_sn_photoresistor.h"
#endif
void registerUsermods()
{
#ifdef USERMOD_SN_PHOTORESISTOR
UsermodManager::add(new Usermod_SN_Photoresistor());
#endif
}

View File

@ -1,3 +1,6 @@
{ {
"name:": "seven_segment_display_reloaded" "name": "seven_segment_display_reloaded",
"build": {
"extraScript": "setup_deps.py"
}
} }

View File

@ -0,0 +1,9 @@
Import('env')
usermods = env.GetProjectOption("custom_usermods","").split()
# Check for partner usermods
if "SN_Photoresistor" in usermods:
env.Append(CPPDEFINES=[("USERMOD_SN_PHOTORESISTOR")])
if "BH1750_v2" in usermods:
env.Append(CPPDEFINES=[("USERMOD_BH1750")])

View File

@ -1,4 +1,10 @@
#include "wled.h" #include "wled.h"
#ifdef USERMOD_SN_PHOTORESISTOR
#include "SN_Photoresistor.h"
#endif
#ifdef USERMOD_BH1750
#include "BH1750_v2.h"
#endif
#ifdef WLED_DISABLE_MQTT #ifdef WLED_DISABLE_MQTT
#error "This user mod requires MQTT to be enabled." #error "This user mod requires MQTT to be enabled."