WLED/wled00/led.cpp

304 lines
8.1 KiB
C++
Raw Normal View History

#include "wled.h"
/*
* LED methods
*/
2019-12-03 14:15:12 +01:00
void setValuesFromMainSeg()
{
WS2812FX::Segment& seg = strip.getSegment(strip.getMainSegmentId());
colorFromUint32(seg.colors[0]);
colorFromUint32(seg.colors[1], true);
effectCurrent = seg.mode;
effectSpeed = seg.speed;
effectIntensity = seg.intensity;
effectPalette = seg.palette;
}
void resetTimebase()
{
strip.timebase = 0 - millis();
}
void toggleOnOff()
{
if (bri == 0)
{
bri = briLast;
} else
{
briLast = bri;
bri = 0;
}
}
2018-11-24 11:52:23 +01:00
//scales the brightness with the briMultiplier factor
byte scaledBri(byte in)
{
2021-10-22 21:31:03 +02:00
uint16_t val = ((uint16_t)in*briMultiplier)/100;
if (val > 255) val = 255;
return (byte)val;
}
void setAllLeds() {
2021-01-09 00:35:48 +01:00
strip.setColor(0, col[0], col[1], col[2], col[3]);
strip.setColor(1, colSec[0], colSec[1], colSec[2], colSec[3]);
if (!realtimeMode || !arlsForceMaxBri)
{
strip.setBrightness(scaledBri(briT));
}
}
2018-11-24 11:52:23 +01:00
2021-01-09 00:35:48 +01:00
void setLedsStandard()
{
briOld = bri;
briT = bri;
setAllLeds();
}
2018-11-24 11:52:23 +01:00
bool colorChanged()
{
for (byte i=0; i<4; i++)
{
if (col[i] != colIT[i]) return true;
if (colSec[i] != colSecIT[i]) return true;
}
if (bri != briIT) return true;
return false;
}
2018-11-24 11:52:23 +01:00
void colorUpdated(int callMode)
{
2018-11-24 11:52:23 +01:00
//call for notifier -> 0: init 1: direct change 2: button 3: notification 4: nightlight 5: other (No notification)
// 6: fx changed 7: hue 8: preset cycle 9: blynk 10: alexa 11: ws send only 12: button preset
if (callMode != CALL_MODE_INIT &&
callMode != CALL_MODE_DIRECT_CHANGE &&
callMode != CALL_MODE_NO_NOTIFY &&
callMode != CALL_MODE_BUTTON_PRESET) strip.applyToAllSelected = true; //if not from JSON api, which directly sets segments
2020-12-14 23:32:57 +01:00
bool someSel = false;
if (callMode == CALL_MODE_NOTIFICATION) {
2021-12-17 17:29:39 +01:00
someSel = (receiveNotificationBrightness || receiveNotificationColor || receiveNotificationEffects || receiveSegmentOptions);
2020-12-14 23:32:57 +01:00
}
2019-12-02 12:41:35 +01:00
2020-12-14 23:32:57 +01:00
//Notifier: apply received FX to selected segments only if actually receiving FX
if (someSel) strip.applyToAllSelected = receiveNotificationEffects;
bool fxChanged = strip.setEffectConfig(effectCurrent, effectSpeed, effectIntensity, effectPalette) || effectChanged;
2020-02-24 17:25:40 +01:00
bool colChanged = colorChanged();
2020-12-14 23:32:57 +01:00
//Notifier: apply received color to selected segments only if actually receiving color
if (someSel) strip.applyToAllSelected = receiveNotificationColor;
2020-02-24 17:25:40 +01:00
if (fxChanged || colChanged)
{
effectChanged = false;
2020-02-24 17:25:40 +01:00
if (realtimeTimeout == UINT32_MAX) realtimeTimeout = 0;
currentPreset = 0; //something changed, so we are no longer in the preset
2020-02-24 17:25:40 +01:00
notify(callMode);
2021-07-02 00:24:14 +02:00
//set flag to update blynk, ws and mqtt
2020-12-10 16:27:23 +01:00
interfaceUpdateCallMode = callMode;
2020-02-24 17:25:40 +01:00
} else {
2020-02-22 16:17:32 +01:00
if (nightlightActive && !nightlightActiveOld &&
callMode != CALL_MODE_NOTIFICATION &&
callMode != CALL_MODE_NO_NOTIFY)
{
notify(CALL_MODE_NIGHTLIGHT);
interfaceUpdateCallMode = CALL_MODE_NIGHTLIGHT;
}
}
2020-02-24 17:25:40 +01:00
if (!colChanged) return; //following code is for e.g. initiating transitions
if (callMode != CALL_MODE_NO_NOTIFY && nightlightActive && (nightlightMode == NL_MODE_FADE || nightlightMode == NL_MODE_COLORFADE))
{
briNlT = bri;
nightlightDelayMs -= (millis() - nightlightStartTime);
nightlightStartTime = millis();
}
for (byte i=0; i<4; i++)
{
colIT[i] = col[i];
colSecIT[i] = colSec[i];
}
2020-02-09 10:35:32 +01:00
if (briT == 0)
{
if (callMode != CALL_MODE_NOTIFICATION) resetTimebase(); //effect start from beginning
2020-02-09 10:35:32 +01:00
}
briIT = bri;
if (bri > 0) briLast = bri;
//deactivate nightlight if target brightness is reached
if (bri == nightlightTargetBri && callMode != CALL_MODE_NO_NOTIFY && nightlightMode != NL_MODE_SUN) nightlightActive = false;
if (fadeTransition)
{
//set correct delay if not using notification delay
if (callMode != CALL_MODE_NOTIFICATION && !jsonTransitionOnce) transitionDelayTemp = transitionDelay;
jsonTransitionOnce = false;
2021-01-09 00:35:48 +01:00
strip.setTransition(transitionDelayTemp);
if (transitionDelayTemp == 0) {setLedsStandard(); strip.trigger(); return;}
if (transitionActive)
{
briOld = briT;
tperLast = 0;
}
strip.setTransitionMode(true);
transitionActive = true;
transitionStartTime = millis();
} else
{
2021-01-09 00:35:48 +01:00
strip.setTransition(0);
setLedsStandard();
strip.trigger();
}
}
2018-11-24 11:52:23 +01:00
void updateInterfaces(uint8_t callMode)
{
sendDataWs();
if (callMode == CALL_MODE_WS_SEND) {
2021-07-02 00:24:14 +02:00
lastInterfaceUpdate = millis();
return;
}
2019-03-01 17:10:42 +01:00
#ifndef WLED_DISABLE_ALEXA
if (espalexaDevice != nullptr && callMode != CALL_MODE_ALEXA) {
2019-03-01 17:10:42 +01:00
espalexaDevice->setValue(bri);
espalexaDevice->setColor(col[0], col[1], col[2]);
}
#endif
#ifndef WLED_DISABLE_BLYNK
if (callMode != CALL_MODE_BLYNK &&
callMode != CALL_MODE_NO_NOTIFY) updateBlynk();
#endif
2019-10-20 17:38:25 +02:00
doPublishMqtt = true;
lastInterfaceUpdate = millis();
}
2018-11-24 11:52:23 +01:00
void handleTransitions()
{
//handle still pending interface update
if (interfaceUpdateCallMode && millis() - lastInterfaceUpdate > 2000)
{
updateInterfaces(interfaceUpdateCallMode);
interfaceUpdateCallMode = 0; //disable
}
2019-10-20 17:38:25 +02:00
if (doPublishMqtt) publishMqtt();
if (transitionActive && transitionDelayTemp > 0)
{
float tper = (millis() - transitionStartTime)/(float)transitionDelayTemp;
if (tper >= 1.0)
{
strip.setTransitionMode(false);
transitionActive = false;
tperLast = 0;
setLedsStandard();
return;
}
2019-02-05 19:40:24 +01:00
if (tper - tperLast < 0.004) return;
tperLast = tper;
2019-02-05 19:40:24 +01:00
briT = briOld +((bri - briOld )*tper);
setAllLeds();
2016-11-27 16:45:54 +01:00
}
}
2018-11-24 11:52:23 +01:00
2016-11-27 16:45:54 +01:00
void handleNightlight()
{
if (nightlightActive)
{
if (!nightlightActiveOld) //init
2016-11-27 16:45:54 +01:00
{
nightlightStartTime = millis();
2016-11-27 16:45:54 +01:00
nightlightDelayMs = (int)(nightlightDelayMins*60000);
nightlightActiveOld = true;
briNlT = bri;
2020-09-10 11:01:05 +02:00
for (byte i=0; i<4; i++) colNlT[i] = col[i]; // remember starting color
if (nightlightMode == NL_MODE_SUN)
{
//save current
colNlT[0] = effectCurrent;
colNlT[1] = effectSpeed;
colNlT[2] = effectPalette;
2020-09-10 11:01:05 +02:00
strip.setMode(strip.getMainSegmentId(), FX_MODE_STATIC); //make sure seg runtime is reset if left in sunrise mode
effectCurrent = FX_MODE_SUNRISE;
effectSpeed = nightlightDelayMins;
effectPalette = 0;
if (effectSpeed > 60) effectSpeed = 60; //currently limited to 60 minutes
if (bri) effectSpeed += 60; //sunset if currently on
briNlT = !bri; //true == sunrise, false == sunset
if (!bri) bri = briLast;
colorUpdated(CALL_MODE_NO_NOTIFY);
}
2016-11-27 16:45:54 +01:00
}
float nper = (millis() - nightlightStartTime)/((float)nightlightDelayMs);
if (nightlightMode == NL_MODE_FADE || nightlightMode == NL_MODE_COLORFADE)
{
bri = briNlT + ((nightlightTargetBri - briNlT)*nper);
if (nightlightMode == NL_MODE_COLORFADE) // color fading only is enabled with "NF=2"
{
2020-02-24 17:27:59 +01:00
for (byte i=0; i<4; i++) col[i] = colNlT[i]+ ((colSec[i] - colNlT[i])*nper); // fading from actual color to secondary color
}
colorUpdated(CALL_MODE_NO_NOTIFY);
}
if (nper >= 1) //nightlight duration over
2016-11-27 16:45:54 +01:00
{
nightlightActive = false;
if (nightlightMode == NL_MODE_SET)
{
2018-02-20 22:29:48 +01:00
bri = nightlightTargetBri;
colorUpdated(CALL_MODE_NO_NOTIFY);
}
if (bri == 0) briLast = briNlT;
if (nightlightMode == NL_MODE_SUN)
{
if (!briNlT) { //turn off if sunset
effectCurrent = colNlT[0];
effectSpeed = colNlT[1];
effectPalette = colNlT[2];
toggleOnOff();
setLedsStandard();
}
}
#ifndef WLED_DISABLE_BLYNK
updateBlynk();
#endif
if (macroNl > 0)
2020-11-06 22:12:48 +01:00
applyPreset(macroNl);
nightlightActiveOld = false;
2016-11-27 16:45:54 +01:00
}
} else if (nightlightActiveOld) //early de-init
2016-11-27 16:45:54 +01:00
{
if (nightlightMode == NL_MODE_SUN) { //restore previous effect
effectCurrent = colNlT[0];
effectSpeed = colNlT[1];
effectPalette = colNlT[2];
colorUpdated(CALL_MODE_NO_NOTIFY);
}
nightlightActiveOld = false;
2016-11-27 16:45:54 +01:00
}
}
2020-08-25 17:23:17 +02:00
//utility for FastLED to use our custom timer
uint32_t get_millisecond_timer()
{
return strip.now;
}