FastLED 3.9.15
Loading...
Searching...
No Matches
time_alpha.h
Go to the documentation of this file.
1
2#pragma once
3
4
5#include "fl/math/math.h"
6#include "fl/stl/int.h"
7#include "fl/stl/noexcept.h"
8
9namespace fl {
10
11// Phase of the TimeRamp transition
12enum class RampPhase {
13 Inactive, // Not started or finished
14 Rising, // Ramping up from 0 to 255
15 Plateau, // Holding at 255
16 Falling // Ramping down from 255 to 0
17};
18
19// Use this function to compute the alpha value based on the time elapsed
20// 0 -> 255
21u8 time_alpha8(u32 now, u32 start, u32 end) FL_NOEXCEPT;
22// 0 -> 65535
23u16 time_alpha16(u32 now, u32 start, u32 end) FL_NOEXCEPT;
24
25inline float time_alphaf(u32 now, u32 start, u32 end) FL_NOEXCEPT {
26 if (now < start) {
27 return 0.0f;
28 }
29 u32 elapsed = now - start;
30 u32 total = end - start;
31 float out = static_cast<float>(elapsed) / static_cast<float>(total);
32 return out;
33}
34
35class TimeAlpha {
36 public:
37 virtual ~TimeAlpha() FL_NOEXCEPT = default;
38 virtual void trigger(u32 now) FL_NOEXCEPT = 0;
39 virtual u8 update8(u32 now) FL_NOEXCEPT = 0;
40 virtual u16 update16(u32 now) FL_NOEXCEPT {
41 return static_cast<u16>(update8(now) << 8) + 0xFF;
42 }
43 virtual float updatef(u32 now) FL_NOEXCEPT {
44 return static_cast<float>(update16(now)) / 65535.0f;
45 }
46 virtual bool isActive(u32 now) const FL_NOEXCEPT = 0;
47};
48
49/*
50 * amplitude
51 * ^
52 * 255 ───────────────────────
53 * / \
54 * / \
55 * / \
56 * / \
57 * 0 ────────────┴ ┴──────────────────> time (ms)
58 * t0 t1 t2 t4
59 *
60 *
61 *
62 */
63class TimeRamp : public TimeAlpha {
64 public:
68 TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime) FL_NOEXCEPT;
69
76 void trigger(u32 now) FL_NOEXCEPT override;
77
79 bool isActive(u32 now) const FL_NOEXCEPT override;
80
83
86 u8 update8(u32 now) FL_NOEXCEPT override;
87
88 private:
92
96
97 u32 mStart = 0;
99};
100
101/*
102 * amplitude
103 * ^
104 * 255 ──────────────────────────────────────
105 * /
106 * /
107 * /
108 * /
109 * 0 ────────────┴ --> time (ms)
110 * t0 t1
111 *
112 *
113 *
114 */
116 public:
117 TimeClampedTransition(u32 duration) FL_NOEXCEPT : mDuration(duration) {}
118
119 void trigger(u32 now) FL_NOEXCEPT override {
120 mStart = now;
121 mEnd = now + mDuration;
122 }
123
124 bool isActive(u32 now) const FL_NOEXCEPT override {
125 bool not_started = (mEnd == 0) && (mStart == 0);
126 if (not_started) {
127 // if we have not started, we are not active
128 return false;
129 }
130 if (now < mStart) {
131 // if the time is before the start, we are not active
132 return false;
133 }
134 if (now > mEnd) {
135 // if the time is after the finished rising, we are not active
136 return false;
137 }
138 return true;
139 }
140
141 u8 update8(u32 now) FL_NOEXCEPT override {
142 bool not_started = (mEnd == 0) && (mStart == 0);
143 if (not_started) {
144 // if we have not started, we are not active
145 return 0;
146 }
147 u8 out = time_alpha8(now, mStart, mEnd);
148 return out;
149 }
150
151 float updatef(u32 now) FL_NOEXCEPT override {
152 bool not_started = (mEnd == 0) && (mStart == 0);
153 if (not_started) {
154 return 0;
155 }
156 float out = time_alphaf(now, mStart, mEnd);
157 if (mMaxClamp > 0.f) {
158 out = fl::min(out, mMaxClamp);
159 }
160 return out;
161 }
162
164
165 private:
166 u32 mStart = 0;
167 u32 mDuration = 0;
168 u32 mEnd = 0;
169 float mMaxClamp = -1.f; // default disabled.
170};
171
172} // namespace fl
virtual ~TimeAlpha() FL_NOEXCEPT=default
virtual float updatef(u32 now) FL_NOEXCEPT
Definition time_alpha.h:43
virtual u16 update16(u32 now) FL_NOEXCEPT
Definition time_alpha.h:40
virtual void trigger(u32 now) FL_NOEXCEPT=0
virtual u8 update8(u32 now) FL_NOEXCEPT=0
virtual bool isActive(u32 now) const FL_NOEXCEPT=0
void set_max_clamp(float max) FL_NOEXCEPT
Definition time_alpha.h:163
u8 update8(u32 now) FL_NOEXCEPT override
Definition time_alpha.h:141
void trigger(u32 now) FL_NOEXCEPT override
Definition time_alpha.h:119
float updatef(u32 now) FL_NOEXCEPT override
Definition time_alpha.h:151
TimeClampedTransition(u32 duration) FL_NOEXCEPT
Definition time_alpha.h:117
bool isActive(u32 now) const FL_NOEXCEPT override
Definition time_alpha.h:124
u32 mFinishedPlateauTime
Definition time_alpha.h:94
RampPhase getCurrentPhase(u32 now) const FL_NOEXCEPT
Get the current phase of the ramp.
TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime) FL_NOEXCEPT
u32 mFinishedRisingTime
Definition time_alpha.h:93
u8 update8(u32 now) FL_NOEXCEPT override
Compute current 0–255 output based on how much time has elapsed since trigger().
bool isActive(u32 now) const FL_NOEXCEPT override
void trigger(u32 now) FL_NOEXCEPT override
Call this when you want to (re)start the ramp cycle.
u32 mFinishedFallingTime
Definition time_alpha.h:95
unsigned char u8
Definition stdint.h:131
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
unsigned char u8
Definition stdint.h:131
float time_alphaf(u32 now, u32 start, u32 end) FL_NOEXCEPT
Definition time_alpha.h:25
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
RampPhase
Definition time_alpha.h:12
constexpr T * end(T(&array)[N]) FL_NOEXCEPT
u16 time_alpha16(u32 now, u32 start, u32 end) FL_NOEXCEPT
u8 time_alpha8(u32 now, u32 start, u32 end) FL_NOEXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT