Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lights: configure mired warm / cold at runtime #1945

Merged
merged 4 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions code/espurna/config/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -1220,14 +1220,6 @@
#define LIGHT_WARMWHITE_MIRED 500 // Warmwhite Strip, Value must be __ABOVE__ W1!! (Default: 2000 Kelvin/500 MiRed)
#endif

#ifndef LIGHT_COLDWHITE_KELVIN
#define LIGHT_COLDWHITE_KELVIN 6536
#endif

#ifndef LIGHT_WARMWHITE_KELVIN
#define LIGHT_WARMWHITE_KELVIN 2000
#endif

#ifndef LIGHT_STEP
#define LIGHT_STEP 32 // Step size
#endif
Expand Down
4 changes: 4 additions & 0 deletions code/espurna/config/hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -3374,6 +3374,10 @@
#define LIGHT_CH2_PIN 4 // cold white
#define LIGHT_CH2_INVERSE 0

// https://www.xiaomitoday.com/xiaomi-mijia-mjtd01yl-led-desk-lamp-review/
#define LIGHT_COLDWHITE_MIRED 153
#define LIGHT_WARMWHITE_MIRED 370

// Encoder
// If mode is ENCODER_MODE_RATIO, the value ratio between both channels is changed
// when the button is not pressed, and the overall brightness when pressed
Expand Down
Binary file modified code/espurna/data/index.all.html.gz
Binary file not shown.
Binary file modified code/espurna/data/index.light.html.gz
Binary file not shown.
8 changes: 0 additions & 8 deletions code/espurna/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ namespace Light {
constexpr const long BRIGHTNESS_MIN = LIGHT_MIN_BRIGHTNESS;
constexpr const long BRIGHTNESS_MAX = LIGHT_MAX_BRIGHTNESS;

// Default to the Philips Hue value that HA also use.
// https://developers.meethue.com/documentation/core-concepts
constexpr const long MIREDS_COLDWHITE = LIGHT_COLDWHITE_MIRED;
constexpr const long MIREDS_WARMWHITE = LIGHT_WARMWHITE_MIRED;

constexpr const long KELVIN_WARMWHITE = LIGHT_WARMWHITE_KELVIN;
constexpr const long KELVIN_COLDWHITE = LIGHT_COLDWHITE_KELVIN;

constexpr const long PWM_MIN = LIGHT_MIN_PWM;
constexpr const long PWM_MAX = LIGHT_MAX_PWM;
constexpr const long PWM_LIMIT = LIGHT_LIMIT_PWM;
Expand Down
31 changes: 25 additions & 6 deletions code/espurna/light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ unsigned int _light_transition_time = LIGHT_TRANSITION_TIME;
bool _light_dirty = false;
bool _light_state = false;
unsigned char _light_brightness = Light::BRIGHTNESS_MAX;
unsigned int _light_mireds = lround((Light::MIREDS_COLDWHITE + Light::MIREDS_WARMWHITE) / 2);

// Default to the Philips Hue value that HA also use.
// https://developers.meethue.com/documentation/core-concepts
long _light_cold_mireds = LIGHT_COLDWHITE_MIRED;
long _light_warm_mireds = LIGHT_WARMWHITE_MIRED;

long _light_cold_kelvin = (1000000L / _light_cold_mireds);
long _light_warm_kelvin = (1000000L / _light_warm_mireds);

long _light_mireds = lround((_light_cold_mireds + _light_warm_mireds) / 2L);

using light_brightness_func_t = void();
light_brightness_func_t* _light_brightness_func = nullptr;
Expand Down Expand Up @@ -154,7 +163,7 @@ void _lightApplyBrightnessColor() {
if (_light_use_cct) {

// This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end.
double miredFactor = ((double) _light_mireds - (double) Light::MIREDS_COLDWHITE)/((double) Light::MIREDS_WARMWHITE - (double) Light::MIREDS_COLDWHITE);
double miredFactor = ((double) _light_mireds - (double) _light_cold_mireds)/((double) _light_warm_mireds - (double) _light_cold_mireds);

// set cold white
_light_channel[3].inputValue = 0;
Expand Down Expand Up @@ -320,11 +329,11 @@ void _fromHSV(const char * hsv) {
// https://github.com/stelgenhof/AiLight
// Color temperature is measured in mireds (kelvin = 1e6/mired)
long _toKelvin(const long mireds) {
return constrain(static_cast<long>(1000000L / mireds), Light::KELVIN_WARMWHITE, Light::KELVIN_COLDWHITE);
return constrain(static_cast<long>(1000000L / mireds), _light_warm_kelvin, _light_cold_kelvin);
}

long _toMireds(const long kelvin) {
return constrain(static_cast<long>(lround(1000000L / kelvin)), Light::MIREDS_COLDWHITE, Light::MIREDS_WARMWHITE);
return constrain(static_cast<long>(lround(1000000L / kelvin)), _light_cold_mireds, _light_warm_mireds);
}

void _lightMireds(const long kelvin) {
Expand All @@ -335,7 +344,7 @@ void _lightMiredsCCT(const long kelvin) {
_lightMireds(kelvin);

// This change the range from 153-500 to 0-347 so we get a value between 0 and 1 in the end.
const double factor = ((double) _light_mireds - (double) Light::MIREDS_COLDWHITE)/((double) Light::MIREDS_WARMWHITE - (double) Light::MIREDS_COLDWHITE);
const double factor = ((double) _light_mireds - (double) _light_cold_mireds)/((double) _light_warm_mireds - (double) _light_cold_mireds);
_setCCTInputValue(
lround(factor * Light::VALUE_MAX),
lround(((double) 1.0 - factor) * Light::VALUE_MAX)
Expand Down Expand Up @@ -1003,8 +1012,11 @@ void _lightWebSocketStatus(JsonObject& root) {
}
}
if (_light_use_cct) {
JsonObject& mireds = root.createNestedObject("mireds");
mireds["value"] = _light_mireds;
mireds["cold"] = _light_cold_mireds;
mireds["warm"] = _light_warm_mireds;
root["useCCT"] = _light_use_cct;
root["mireds"] = _light_mireds;
}
JsonArray& channels = root.createNestedArray("channels");
for (unsigned char id=0; id < _light_channel.size(); id++) {
Expand Down Expand Up @@ -1270,6 +1282,13 @@ void _lightConfigure() {
setSetting("useCCT", _light_use_cct);
}

if (_light_use_cct) {
_light_cold_mireds = getSetting("lightColdMired", LIGHT_COLDWHITE_MIRED).toInt();
_light_warm_mireds = getSetting("lightWarmMired", LIGHT_WARMWHITE_MIRED).toInt();
_light_cold_kelvin = (1000000L / _light_cold_mireds);
_light_warm_kelvin = (1000000L / _light_warm_mireds);
}

_light_use_gamma = getSetting("useGamma", LIGHT_USE_GAMMA).toInt() == 1;
_light_use_transitions = getSetting("useTransitions", LIGHT_USE_TRANSITIONS).toInt() == 1;
_light_transition_time = getSetting("lightTime", LIGHT_TRANSITION_TIME).toInt();
Expand Down
Loading