Skip to content

Commit 35f8736

Browse files
authored
Brightness follow sun (wled#4485)
* add usermod : Brightness Follow Sun
1 parent 95a10c6 commit 35f8736

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Update Brightness Follow Sun
2+
3+
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.
4+
5+
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.
6+
7+
## Installation
8+
9+
define `USERMOD_BRIGHTNESS_FOLLOW_SUN` e.g. `#define USERMOD_BRIGHTNESS_FOLLOW_SUN` in my_config.h
10+
11+
or add `-D USERMOD_BRIGHTNESS_FOLLOW_SUN` to `build_flags` in platformio_override.ini
12+
13+
14+
### Options
15+
Open Usermod Settings in WLED to change settings:
16+
17+
`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`.
18+
19+
`Update Interval Sec` - The unit is seconds, and the brightness will be automatically refreshed according to the set parameters.
20+
21+
`Min Brightness` - set brightness by map of min-max-min : sunrise-suntop-sunset
22+
23+
`Max Brightness` - It needs to be set to a value greater than `Min Brightness`, otherwise it will always remain at `Min Brightness`.
24+
25+
`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.
26+
27+
28+
### PlatformIO requirements
29+
30+
No special requirements.
31+
32+
## Change Log
33+
34+
2025-01-02
35+
* init
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#pragma once
2+
3+
#include "wled.h"
4+
5+
//v2 usermod that allows to change brightness and color using a rotary encoder,
6+
//change between modes by pressing a button (many encoders have one included)
7+
class UsermodBrightnessFollowSun : public Usermod
8+
{
9+
private:
10+
static const char _name[];
11+
static const char _enabled[];
12+
static const char _update_interval[];
13+
static const char _min_bri[];
14+
static const char _max_bri[];
15+
static const char _relax_hour[];
16+
17+
private:
18+
bool enabled = false; //WLEDMM
19+
unsigned long update_interval = 60;
20+
unsigned long update_interval_ms = 60000;
21+
int min_bri = 1;
22+
int max_bri = 255;
23+
float relax_hour = 0;
24+
int relaxSec = 0;
25+
unsigned long lastUMRun = 0;
26+
public:
27+
28+
void setup() {};
29+
30+
float mapFloat(float inputValue, float inMin, float inMax, float outMin, float outMax) {
31+
if (inMax == inMin)
32+
return outMin;
33+
34+
inputValue = constrain(inputValue, inMin, inMax);
35+
36+
return ((inputValue - inMin) * (outMax - outMin) / (inMax - inMin)) + outMin;
37+
}
38+
39+
uint16_t getId() override
40+
{
41+
return USERMOD_ID_BRIGHTNESS_FOLLOW_SUN;
42+
}
43+
44+
void update()
45+
{
46+
if (sunrise == 0 || sunset == 0 || localTime == 0)
47+
return;
48+
49+
int curSec = elapsedSecsToday(localTime);
50+
int sunriseSec = elapsedSecsToday(sunrise);
51+
int sunsetSec = elapsedSecsToday(sunset);
52+
int sunMiddleSec = sunriseSec + (sunsetSec-sunriseSec)/2;
53+
54+
int relaxSecH = sunriseSec-relaxSec;
55+
int relaxSecE = sunsetSec+relaxSec;
56+
57+
int briSet = 0;
58+
if (curSec >= relaxSecH && curSec <= relaxSecE) {
59+
float timeMapToAngle = curSec < sunMiddleSec ?
60+
mapFloat(curSec, sunriseSec, sunMiddleSec, 0, M_PI/2.0) :
61+
mapFloat(curSec, sunMiddleSec, sunsetSec, M_PI/2.0, M_PI);
62+
float sinValue = sin_t(timeMapToAngle);
63+
briSet = min_bri + (max_bri-min_bri)*sinValue;
64+
}
65+
66+
bri = briSet;
67+
stateUpdated(CALL_MODE_DIRECT_CHANGE);
68+
}
69+
70+
void loop() override
71+
{
72+
if (!enabled || strip.isUpdating())
73+
return;
74+
75+
if (millis() - lastUMRun < update_interval_ms)
76+
return;
77+
lastUMRun = millis();
78+
79+
update();
80+
}
81+
82+
void addToConfig(JsonObject& root)
83+
{
84+
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
85+
86+
top[FPSTR(_enabled)] = enabled;
87+
top[FPSTR(_update_interval)] = update_interval;
88+
top[FPSTR(_min_bri)] = min_bri;
89+
top[FPSTR(_max_bri)] = max_bri;
90+
top[FPSTR(_relax_hour)] = relax_hour;
91+
}
92+
93+
bool readFromConfig(JsonObject& root)
94+
{
95+
JsonObject top = root[FPSTR(_name)];
96+
if (top.isNull()) {
97+
DEBUG_PRINTF("[%s] No config found. (Using defaults.)\n", _name);
98+
return false;
99+
}
100+
101+
bool configComplete = true;
102+
103+
configComplete &= getJsonValue(top[FPSTR(_enabled)], enabled, false);
104+
configComplete &= getJsonValue(top[FPSTR(_update_interval)], update_interval, 60);
105+
configComplete &= getJsonValue(top[FPSTR(_min_bri)], min_bri, 1);
106+
configComplete &= getJsonValue(top[FPSTR(_max_bri)], max_bri, 255);
107+
configComplete &= getJsonValue(top[FPSTR(_relax_hour)], relax_hour, 0);
108+
109+
update_interval = constrain(update_interval, 1, SECS_PER_HOUR);
110+
min_bri = constrain(min_bri, 1, 255);
111+
max_bri = constrain(max_bri, 1, 255);
112+
relax_hour = constrain(relax_hour, 0, 6);
113+
114+
update_interval_ms = update_interval*1000;
115+
relaxSec = SECS_PER_HOUR*relax_hour;
116+
117+
lastUMRun = 0;
118+
update();
119+
120+
return configComplete;
121+
}
122+
};
123+
124+
125+
const char UsermodBrightnessFollowSun::_name[] PROGMEM = "Brightness Follow Sun";
126+
const char UsermodBrightnessFollowSun::_enabled[] PROGMEM = "Enabled";
127+
const char UsermodBrightnessFollowSun::_update_interval[] PROGMEM = "Update Interval Sec";
128+
const char UsermodBrightnessFollowSun::_min_bri[] PROGMEM = "Min Brightness";
129+
const char UsermodBrightnessFollowSun::_max_bri[] PROGMEM = "Max Brightness";
130+
const char UsermodBrightnessFollowSun::_relax_hour[] PROGMEM = "Relax Hour";

wled00/const.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
#define USERMOD_ID_PIXELS_DICE_TRAY 54 //Usermod "pixels_dice_tray.h"
206206
#define USERMOD_ID_DEEP_SLEEP 55 //Usermod "usermod_deep_sleep.h"
207207
#define USERMOD_ID_RF433 56 //Usermod "usermod_v2_RF433.h"
208+
#define USERMOD_ID_BRIGHTNESS_FOLLOW_SUN 57 //Usermod "usermod_v2_brightness_follow_sun.h"
208209

209210
//Access point behavior
210211
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot

wled00/usermods_list.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@
250250
#include "../usermods/usermod_v2_RF433/usermod_v2_RF433.h"
251251
#endif
252252

253+
#ifdef USERMOD_BRIGHTNESS_FOLLOW_SUN
254+
#include "../usermods/usermod_v2_brightness_follow_sun/usermod_v2_brightness_follow_sun.h"
255+
#endif
256+
253257
void registerUsermods()
254258
{
255259
/*
@@ -486,4 +490,8 @@ void registerUsermods()
486490
#ifdef USERMOD_RF433
487491
UsermodManager::add(new RF433Usermod());
488492
#endif
493+
494+
#ifdef USERMOD_BRIGHTNESS_FOLLOW_SUN
495+
UsermodManager::add(new UsermodBrightnessFollowSun());
496+
#endif
489497
}

0 commit comments

Comments
 (0)