-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
130 changes: 130 additions & 0 deletions
130
usermods/usermod_v2_brightness_follow_sun/usermod_v2_brightness_follow_sun.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters