mirror of
https://github.com/wled/WLED.git
synced 2025-07-21 17:56:33 +00:00
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:
parent
ff99c7de70
commit
1cd3a97c51
@ -2,84 +2,27 @@
|
|||||||
#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");
|
||||||
|
|
||||||
if (HomeAssistantDiscovery) _createMqttSensor(F("Brightness"), mqttLuminanceTopic, F("Illuminance"), F(" lx"));
|
if (HomeAssistantDiscovery) _createMqttSensor(F("Brightness"), mqttLuminanceTopic, F("Illuminance"), F(" lx"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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");
|
||||||
|
|
||||||
StaticJsonDocument<600> doc;
|
StaticJsonDocument<600> doc;
|
||||||
@ -106,18 +49,17 @@ private:
|
|||||||
DEBUG_PRINTLN(temp);
|
DEBUG_PRINTLN(temp);
|
||||||
|
|
||||||
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,16 +98,12 @@ 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())
|
||||||
user = root.createNestedObject(F("u"));
|
user = root.createNestedObject(F("u"));
|
||||||
@ -187,11 +125,11 @@ public:
|
|||||||
lux_json.add(lastLux);
|
lux_json.add(lastLux);
|
||||||
lux_json.add(F(" lx"));
|
lux_json.add(F(" lx"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// (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
|
||||||
top[FPSTR(_enabled)] = enabled;
|
top[FPSTR(_enabled)] = enabled;
|
||||||
@ -201,11 +139,11 @@ public:
|
|||||||
top[FPSTR(_offset)] = offset;
|
top[FPSTR(_offset)] = offset;
|
||||||
|
|
||||||
DEBUG_PRINTLN(F("BH1750 config saved."));
|
DEBUG_PRINTLN(F("BH1750 config saved."));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)];
|
||||||
if (top.isNull())
|
if (top.isNull())
|
||||||
@ -232,14 +170,8 @@ public:
|
|||||||
|
|
||||||
return configComplete;
|
return configComplete;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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";
|
||||||
|
92
usermods/BH1750_v2/BH1750_v2.h
Normal file
92
usermods/BH1750_v2/BH1750_v2.h
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
@ -1,76 +1,18 @@
|
|||||||
#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
|
||||||
float volts = analogRead(PHOTORESISTOR_PIN) * (referenceVoltage / adcPrecision);
|
float volts = analogRead(PHOTORESISTOR_PIN) * (referenceVoltage / adcPrecision);
|
||||||
@ -80,17 +22,16 @@ private:
|
|||||||
lastMeasurement = millis();
|
lastMeasurement = millis();
|
||||||
getLuminanceComplete = true;
|
getLuminanceComplete = true;
|
||||||
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;
|
||||||
|
|
||||||
@ -123,15 +64,11 @@ 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())
|
||||||
user = root.createNestedObject(F("u"));
|
user = root.createNestedObject(F("u"));
|
||||||
@ -149,18 +86,14 @@ public:
|
|||||||
|
|
||||||
lux.add(lastLDRValue);
|
lux.add(lastLDRValue);
|
||||||
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
|
||||||
top[FPSTR(_enabled)] = !disabled;
|
top[FPSTR(_enabled)] = !disabled;
|
||||||
@ -171,13 +104,13 @@ public:
|
|||||||
top[FPSTR(_offset)] = offset;
|
top[FPSTR(_offset)] = offset;
|
||||||
|
|
||||||
DEBUG_PRINTLN(F("Photoresistor config saved."));
|
DEBUG_PRINTLN(F("Photoresistor config saved."));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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)];
|
||||||
if (top.isNull()) {
|
if (top.isNull()) {
|
||||||
@ -197,8 +130,8 @@ 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);
|
90
usermods/SN_Photoresistor/SN_Photoresistor.h
Normal file
90
usermods/SN_Photoresistor/SN_Photoresistor.h
Normal 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);
|
||||||
|
};
|
@ -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
|
|
||||||
}
|
|
@ -1,3 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name:": "seven_segment_display_reloaded"
|
"name": "seven_segment_display_reloaded",
|
||||||
|
"build": {
|
||||||
|
"extraScript": "setup_deps.py"
|
||||||
|
}
|
||||||
}
|
}
|
9
usermods/seven_segment_display_reloaded/setup_deps.py
Normal file
9
usermods/seven_segment_display_reloaded/setup_deps.py
Normal 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")])
|
@ -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."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user