1#ifndef BRIGHTNESS_MODES_H
2#define BRIGHTNESS_MODES_H
6#include "src/system/ext/random8.h"
7#include "src/system/ext/math8.h"
11namespace lampda::modes::brightness {
15 static constexpr void loop(
auto& ctx)
33 static constexpr void loop(
auto& ctx)
35 auto& lamp = ctx.lamp;
83 static void on_enter_mode(
auto& ctx)
85 ctx.state.candle_wave1 = 0;
86 ctx.state.candle_wave2 = 0;
87 ctx.state.candle_wave3 = 0;
88 ctx.state.candle_wave2_speed = 0;
95 static void loop(
auto& ctx)
97 auto& lamp = ctx.lamp;
100 const float brightnessCorrection = maxBrightness / 255.0;
105 maxBrightness - std::min<int>(maxBrightness, (
CANDLE_AMPLITUDE + 15) * brightnessCorrection));
108 const uint8_t add = ((triwave8(ctx.state.candle_wave1) * ctx.state.candle_wave1_depth) >> 8) +
109 ((triwave8(ctx.state.candle_wave2) * ctx.state.candle_wave2_depth) >> 8) +
110 ((triwave8(ctx.state.candle_wave3) * ctx.state.candle_wave3_depth) >> 8);
111 const brightness_t brightness = base + add * brightnessCorrection;
119 ctx.state.candle_wave1 += random8() & 1;
121 ctx.state.candle_wave2 += ctx.state.candle_wave2_speed;
123 ctx.state.candle_wave3 += random8() % 37;
125 if ((random8() & 0b00111111) == 0)
126 ctx.state.candle_wave2_speed = random8() % 13;
128 if ((ctx.state.candle_wave2_depth > 0) && ((random8() & 0b00111111) == 0))
129 ctx.state.candle_wave2_depth--;
137 ctx.state.candle_wave2 = 0;
140 if ((ctx.state.candle_wave3_depth > 2) && ((random8() & 0b00011111) == 0))
141 ctx.state.candle_wave3_depth--;
142 if ((random8() & 0b01111111) == 0)
169 static void on_enter_mode(
auto& ctx)
171 ctx.state.lastCall = 0;
172 ctx.template set_config_bool<ConfigKeys::rampSaturates>(
true);
173 ctx.template set_config_bool<ConfigKeys::customRampAnimEffect>(
false);
186 const auto pulseDuration = ctx.state.pulseDuration;
189 static constexpr uint32_t onTime = 5;
190 if (ctx.lamp.now - ctx.state.lastCall >= pulseDuration)
192 ctx.blip(pulseDuration - onTime);
193 ctx.state.lastCall = ctx.lamp.now;
219 static void on_enter_mode(
auto& ctx)
221 ctx.state.lastBrightnessHandleCall = 0;
222 ctx.state.isWaitingTurnOn =
true;
223 ctx.state.lastBrightness = ctx.lamp.getSavedBrightness();
224 ctx.state.stepdown = 0;
230 auto& lamp = ctx.lamp;
232 ctx.state.lastBrightnessHandleCall = lamp.
now;
235 static void loop(
auto& ctx)
238 auto& lamp = ctx.lamp;
241 if (lamp.
now - ctx.state.lastBrightnessHandleCall < 100)
246 if (not ctx.is_bliping())
248 brightness_t currentBrightness = ctx.state.lastBrightness;
251 if (ctx.state.isWaitingTurnOn)
256 uint8_t n_brightness = 1 << (random8() % 7);
257 n_brightness += 1 << (random8() % 5);
258 n_brightness += random8() % n_brightness;
260 const brightness_t newBrightness = std::min<brightness_t>(
261 savedBrightness, lmpd_map<brightness_t>(n_brightness, 2, 159, 0, savedBrightness));
264 ctx.state.lastBrightness = newBrightness;
266 ctx.state.stepdown = std::max<uint32_t>(1, newBrightness >> 4);
268 ctx.state.isWaitingTurnOn =
false;
270 else if (currentBrightness > ctx.state.stepdown)
273 if (ctx.state.stepdown > 0)
275 if (random8() & 0x11 != 0)
277 currentBrightness >>= 1;
281 currentBrightness -= ctx.state.stepdown;
282 ctx.state.lastBrightness = currentBrightness;
287 currentBrightness = 0;
288 ctx.state.lastBrightness = 0;
295 currentBrightness = 0;
299 uint32_t rand_time = 1 << (random8() % 13);
300 rand_time += random8() % rand_time;
304 ctx.state.isWaitingTurnOn =
true;
GroupFor< StaticLightMode > StaticLightOnly
One mode, alone in its group, for static lightning.
Definition: brightness_modes.hpp:23
GroupFor< Candle, LightningMode > CalmGroup
Group for calm modes.
Definition: brightness_modes.hpp:312
GroupFor< StroboscopeMode, Pulses< 1 >, Pulses< 2 > > FlashesGroup
Group for flashing modes.
Definition: brightness_modes.hpp:314
@ Candle
1900 Kelvin
Definition: palettes.hpp:38
@ indexable
Equivalent to LMBD_LAMP_TYPE__INDEXABLE.
GroupTy< std::tuple< Modes... > > GroupFor
Group together many different modes::BasicMode.
Definition: group_type.hpp:335
uint16_t brightness_t
Define the type of the brightness parameters.
Definition: constants.h:147
Parent object for all custom user modes.
Definition: mode_type.hpp:53
Definition: brightness_modes.hpp:72
uint8_t candle_wave2_depth
Control the wave 2 depth.
Definition: brightness_modes.hpp:79
uint8_t candle_wave3_depth
Control the wave 3 depth.
Definition: brightness_modes.hpp:80
uint8_t candle_wave3
triangular wave 3 amplitude
Definition: brightness_modes.hpp:75
uint8_t candle_wave1_depth
Control the wave 1 depth.
Definition: brightness_modes.hpp:78
uint8_t candle_wave2_speed
second wave speed (candle flicker)
Definition: brightness_modes.hpp:76
uint8_t candle_wave2
triangular wave 2 amplitude
Definition: brightness_modes.hpp:74
uint8_t candle_wave1
triangular wave 1 amplitude
Definition: brightness_modes.hpp:73
Based on the candle animation from Anduril. candle-mode.c: Candle mode for Anduril....
Definition: brightness_modes.hpp:65
static const uint8_t CANDLE_AMPLITUDE
amplitude of the candle light
Definition: brightness_modes.hpp:66
static const uint8_t CANDLE_WAVE1_MAXDEPTH
Wave depth parameters.
Definition: brightness_modes.hpp:67
static const uint8_t CANDLE_WAVE3_MAXDEPTH
Wave depth parameters.
Definition: brightness_modes.hpp:69
static const uint8_t CANDLE_WAVE2_MAXDEPTH
Wave depth parameters.
Definition: brightness_modes.hpp:68
Definition: brightness_modes.hpp:208
uint32_t stepdown
Brigthness ramp down index.
Definition: brightness_modes.hpp:216
uint32_t lastBrightnessHandleCall
prevent updates when the user ramp is rolling
Definition: brightness_modes.hpp:210
bool isWaitingTurnOn
Is off and waiting to turn on.
Definition: brightness_modes.hpp:212
brightness_t lastBrightness
last brigthness before going dark
Definition: brightness_modes.hpp:214
Emulate lightning in the distance Inspired by Anduril implementation.
Definition: brightness_modes.hpp:203
static constexpr bool hasBrightCallback
Hint manager that we need a brightness callback.
Definition: brightness_modes.hpp:205
static void brightness_update(auto &ctx, brightness_t brightness)
If user update the brighness, do not update the animation.
Definition: brightness_modes.hpp:228
Pulse N times then pause one second.
Definition: brightness_modes.hpp:27
static constexpr uint32_t pulseSizeMs
lenght of a pulse, in milliseconds
Definition: brightness_modes.hpp:28
static constexpr uint32_t periodMs
period of the animation, in milliseconds
Definition: brightness_modes.hpp:29
static constexpr brightness_t minScaling
scaling of the brightness of a pulse
Definition: brightness_modes.hpp:30
static constexpr float scaleFactor
scale of the brigthness
Definition: brightness_modes.hpp:31
Basic lightning mode (does nothing, brightness may be adjusted)
Definition: brightness_modes.hpp:14
Definition: brightness_modes.hpp:162
uint32_t pulseDuration
duration of the off pulse
Definition: brightness_modes.hpp:166
uint32_t lastCall
last off pulse set
Definition: brightness_modes.hpp:164
Very fast on pulse followed by longer off pulses. Make a stroboscopic effect.
Definition: brightness_modes.hpp:153
static void custom_ramp_update(auto &ctx, uint8_t rampValue)
User ramp controls the strob frequency.
Definition: brightness_modes.hpp:179
static constexpr uint32_t stroboMinFreq
minimum allowed stroboscopic frequency in hertz
Definition: brightness_modes.hpp:159
static constexpr bool hasCustomRamp
regulate stroboscopic speed
Definition: brightness_modes.hpp:155
static constexpr uint32_t stroboMaxFreq
maximum allowed stroboscopic frequency in Hertz
Definition: brightness_modes.hpp:157
static void loop(auto &ctx)
Definition: brightness_modes.hpp:184
brightness_t LMBD_INLINE getMaxBrightness() const
Return the actual allowed maximum brightness.
Definition: lamp_type.hpp:655
void LMBD_INLINE restoreBrightness()
Reset brightness to internal saved brightness, skip callbacks.
Definition: lamp_type.hpp:706
void LMBD_INLINE setBrightness(const brightness_t newBrightness, const bool skipCallbacks=true, const bool skipUpdateBrightness=false, const bool updateSavedBrightess=false)
Set brightness of the lamp.
Definition: lamp_type.hpp:553
void LMBD_INLINE tempBrightness(const brightness_t brightness)
Temporarily set brightness, without affecting brightness navigation.
Definition: lamp_type.hpp:668
void LMBD_INLINE jumpBrightness(const brightness_t brightness)
Jump to brightness, affecting the next brightness navigation.
Definition: lamp_type.hpp:686
volatile const uint32_t now
(physical) The "now" on milliseconds, updated just before loop.
Definition: lamp_type.hpp:1075
brightness_t LMBD_INLINE getSavedBrightness()
Alias for getBrightness(true)
Definition: lamp_type.hpp:703
void LMBD_INLINE cancel_blip() const
Cancel the ongoing blip, if any.
Definition: lamp_type.hpp:526