Skip to content

Commit

Permalink
Brightness follow sun (#4485)
Browse files Browse the repository at this point in the history
* add usermod : Brightness Follow Sun
  • Loading branch information
yangminglong authored Feb 8, 2025
1 parent 95a10c6 commit 35f8736
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
35 changes: 35 additions & 0 deletions usermods/usermod_v2_brightness_follow_sun/README.md
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
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";
1 change: 1 addition & 0 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions wled00/usermods_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
/*
Expand Down Expand Up @@ -486,4 +490,8 @@ void registerUsermods()
#ifdef USERMOD_RF433
UsermodManager::add(new RF433Usermod());
#endif

#ifdef USERMOD_BRIGHTNESS_FOLLOW_SUN
UsermodManager::add(new UsermodBrightnessFollowSun());
#endif
}

0 comments on commit 35f8736

Please sign in to comment.