Brightness follow sun (#4485)

* add usermod : Brightness Follow Sun
This commit is contained in:
yangminglong 2025-02-08 17:11:14 +08:00 committed by GitHub
parent 95a10c692c
commit 35f87365c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 174 additions and 0 deletions

View File

@ -0,0 +1,35 @@
# Update Brightness Follow Sun
This UserMod can set brightness by mapping [minimum-maximum-minimum] from [sunrise-suntop-sunset], I use this UserMod to adjust the brightness of my plant growth light (pwm led), and I think it will make my plants happy.
This UserMod will adjust brightness from sunrise to sunset, reaching maximum brightness at the zenith of the sun. It can also maintain the lowest brightness within 0-6 hours before sunrise and after sunset according to the settings.
## Installation
define `USERMOD_BRIGHTNESS_FOLLOW_SUN` e.g. `#define USERMOD_BRIGHTNESS_FOLLOW_SUN` in my_config.h
or add `-D USERMOD_BRIGHTNESS_FOLLOW_SUN` to `build_flags` in platformio_override.ini
### Options
Open Usermod Settings in WLED to change settings:
`Enable` - When checked `Enable`, turn on the `Brightness Follow Sun` Usermod, which will automatically turn on the lights, adjust the brightness, and turn off the lights. If you need to completely turn off the lights, please unchecked `Enable`.
`Update Interval Sec` - The unit is seconds, and the brightness will be automatically refreshed according to the set parameters.
`Min Brightness` - set brightness by map of min-max-min : sunrise-suntop-sunset
`Max Brightness` - It needs to be set to a value greater than `Min Brightness`, otherwise it will always remain at `Min Brightness`.
`Relax Hour` - The unit is in hours, with an effective range of 0-6. According to the settings, maintain the lowest brightness for 0-6 hours before sunrise and after sunset.
### PlatformIO requirements
No special requirements.
## Change Log
2025-01-02
* init

View File

@ -0,0 +1,130 @@
#pragma once
#include "wled.h"
//v2 usermod that allows to change brightness and color using a rotary encoder,
//change between modes by pressing a button (many encoders have one included)
class UsermodBrightnessFollowSun : public Usermod
{
private:
static const char _name[];
static const char _enabled[];
static const char _update_interval[];
static const char _min_bri[];
static const char _max_bri[];
static const char _relax_hour[];
private:
bool enabled = false; //WLEDMM
unsigned long update_interval = 60;
unsigned long update_interval_ms = 60000;
int min_bri = 1;
int max_bri = 255;
float relax_hour = 0;
int relaxSec = 0;
unsigned long lastUMRun = 0;
public:
void setup() {};
float mapFloat(float inputValue, float inMin, float inMax, float outMin, float outMax) {
if (inMax == inMin)
return outMin;
inputValue = constrain(inputValue, inMin, inMax);
return ((inputValue - inMin) * (outMax - outMin) / (inMax - inMin)) + outMin;
}
uint16_t getId() override
{
return USERMOD_ID_BRIGHTNESS_FOLLOW_SUN;
}
void update()
{
if (sunrise == 0 || sunset == 0 || localTime == 0)
return;
int curSec = elapsedSecsToday(localTime);
int sunriseSec = elapsedSecsToday(sunrise);
int sunsetSec = elapsedSecsToday(sunset);
int sunMiddleSec = sunriseSec + (sunsetSec-sunriseSec)/2;
int relaxSecH = sunriseSec-relaxSec;
int relaxSecE = sunsetSec+relaxSec;
int briSet = 0;
if (curSec >= relaxSecH && curSec <= relaxSecE) {
float timeMapToAngle = curSec < sunMiddleSec ?
mapFloat(curSec, sunriseSec, sunMiddleSec, 0, M_PI/2.0) :
mapFloat(curSec, sunMiddleSec, sunsetSec, M_PI/2.0, M_PI);
float sinValue = sin_t(timeMapToAngle);
briSet = min_bri + (max_bri-min_bri)*sinValue;
}
bri = briSet;
stateUpdated(CALL_MODE_DIRECT_CHANGE);
}
void loop() override
{
if (!enabled || strip.isUpdating())
return;
if (millis() - lastUMRun < update_interval_ms)
return;
lastUMRun = millis();
update();
}
void addToConfig(JsonObject& root)
{
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR(_enabled)] = enabled;
top[FPSTR(_update_interval)] = update_interval;
top[FPSTR(_min_bri)] = min_bri;
top[FPSTR(_max_bri)] = max_bri;
top[FPSTR(_relax_hour)] = relax_hour;
}
bool readFromConfig(JsonObject& root)
{
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) {
DEBUG_PRINTF("[%s] No config found. (Using defaults.)\n", _name);
return false;
}
bool configComplete = true;
configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled, false);
configComplete &= getJsonValue(top[FPSTR(_update_interval)], update_interval, 60);
configComplete &= getJsonValue(top[FPSTR(_min_bri)], min_bri, 1);
configComplete &= getJsonValue(top[FPSTR(_max_bri)], max_bri, 255);
configComplete &= getJsonValue(top[FPSTR(_relax_hour)], relax_hour, 0);
update_interval = constrain(update_interval, 1, SECS_PER_HOUR);
min_bri = constrain(min_bri, 1, 255);
max_bri = constrain(max_bri, 1, 255);
relax_hour = constrain(relax_hour, 0, 6);
update_interval_ms = update_interval*1000;
relaxSec = SECS_PER_HOUR*relax_hour;
lastUMRun = 0;
update();
return configComplete;
}
};
const char UsermodBrightnessFollowSun::_name[] PROGMEM = "Brightness Follow Sun";
const char UsermodBrightnessFollowSun::_enabled[] PROGMEM = "Enabled";
const char UsermodBrightnessFollowSun::_update_interval[] PROGMEM = "Update Interval Sec";
const char UsermodBrightnessFollowSun::_min_bri[] PROGMEM = "Min Brightness";
const char UsermodBrightnessFollowSun::_max_bri[] PROGMEM = "Max Brightness";
const char UsermodBrightnessFollowSun::_relax_hour[] PROGMEM = "Relax Hour";

View File

@ -205,6 +205,7 @@
#define USERMOD_ID_PIXELS_DICE_TRAY 54 //Usermod "pixels_dice_tray.h"
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
#define USERMOD_ID_RF433 56 //Usermod "usermod_v2_RF433.h"
#define USERMOD_ID_BRIGHTNESS_FOLLOW_SUN 57 //Usermod "usermod_v2_brightness_follow_sun.h"
//Access point behavior
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot

View File

@ -250,6 +250,10 @@
#include "../usermods/usermod_v2_RF433/usermod_v2_RF433.h"
#endif
#ifdef USERMOD_BRIGHTNESS_FOLLOW_SUN
#include "../usermods/usermod_v2_brightness_follow_sun/usermod_v2_brightness_follow_sun.h"
#endif
void registerUsermods()
{
/*
@ -486,4 +490,8 @@ void registerUsermods()
#ifdef USERMOD_RF433
UsermodManager::add(new RF433Usermod());
#endif
#ifdef USERMOD_BRIGHTNESS_FOLLOW_SUN
UsermodManager::add(new UsermodBrightnessFollowSun());
#endif
}