FastLED 3.9.15
Loading...
Searching...
No Matches
time_alpha.h
Go to the documentation of this file.
1
2#pragma once
3
4#include "fl/stdint.h"
5
6#include "fl/math_macros.h"
7#include "fl/warn.h"
8
9namespace fl {
10
11// Use this function to compute the alpha value based on the time elapsed
12// 0 -> 255
13u8 time_alpha8(u32 now, u32 start, u32 end);
14// 0 -> 65535
15u16 time_alpha16(u32 now, u32 start, u32 end);
16
17inline float time_alphaf(u32 now, u32 start, u32 end) {
18 if (now < start) {
19 return 0.0f;
20 }
21 u32 elapsed = now - start;
22 u32 total = end - start;
23 float out = static_cast<float>(elapsed) / static_cast<float>(total);
24 return out;
25}
26
27class TimeAlpha {
28 public:
29 virtual ~TimeAlpha() = default;
30 virtual void trigger(u32 now) = 0;
31 virtual u8 update8(u32 now) = 0;
32 virtual u16 update16(u32 now) {
33 return static_cast<u16>(update8(now) << 8) + 0xFF;
34 }
35 virtual float updatef(u32 now) {
36 return static_cast<float>(update16(now)) / 65535.0f;
37 }
38 virtual bool isActive(u32 now) const = 0;
39};
40
41/*
42 * amplitude
43 * ^
44 * 255 ───────────────────────
45 * / \
46 * / \
47 * / \
48 * / \
49 * 0 ────────────┴ ┴──────────────────> time (ms)
50 * t0 t1 t2 t4
51 *
52 *
53 *
54 */
55class TimeRamp : public TimeAlpha {
56 public:
60 TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime);
61
63 void trigger(u32 now) override;
64
65 void trigger(u32 now, u32 risingTime, u32 latchMs,
66 u32 fallingTime);
67
69 bool isActive(u32 now) const override;
70
73 u8 update8(u32 now) override;
74
75 private:
79
83
84 u32 mStart = 0;
86};
87
88/*
89 * amplitude
90 * ^
91 * 255 ──────────────────────────────────────
92 * /
93 * /
94 * /
95 * /
96 * 0 ────────────┴ --> time (ms)
97 * t0 t1
98 *
99 *
100 *
101 */
103 public:
104 TimeClampedTransition(u32 duration) : mDuration(duration) {}
105
106 void trigger(u32 now) override {
107 mStart = now;
108 mEnd = now + mDuration;
109 }
110
111 bool isActive(u32 now) const override {
112 bool not_started = (mEnd == 0) && (mStart == 0);
113 if (not_started) {
114 // if we have not started, we are not active
115 return false;
116 }
117 if (now < mStart) {
118 // if the time is before the start, we are not active
119 return false;
120 }
121 if (now > mEnd) {
122 // if the time is after the finished rising, we are not active
123 return false;
124 }
125 return true;
126 }
127
128 u8 update8(u32 now) override {
129 bool not_started = (mEnd == 0) && (mStart == 0);
130 if (not_started) {
131 // if we have not started, we are not active
132 return 0;
133 }
134 u8 out = time_alpha8(now, mStart, mEnd);
135 return out;
136 }
137
138 float updatef(u32 now) override {
139 bool not_started = (mEnd == 0) && (mStart == 0);
140 if (not_started) {
141 return 0;
142 }
143 float out = time_alphaf(now, mStart, mEnd);
144 if (mMaxClamp > 0.f) {
145 out = MIN(out, mMaxClamp);
146 }
147 return out;
148 }
149
150 void set_max_clamp(float max) { mMaxClamp = max; }
151
152 private:
153 u32 mStart = 0;
154 u32 mDuration = 0;
155 u32 mEnd = 0;
156 float mMaxClamp = -1.f; // default disabled.
157};
158
159} // namespace fl
virtual u8 update8(u32 now)=0
virtual float updatef(u32 now)
Definition time_alpha.h:35
virtual bool isActive(u32 now) const =0
virtual u16 update16(u32 now)
Definition time_alpha.h:32
virtual ~TimeAlpha()=default
virtual void trigger(u32 now)=0
u8 update8(u32 now) override
Definition time_alpha.h:128
bool isActive(u32 now) const override
Definition time_alpha.h:111
TimeClampedTransition(u32 duration)
Definition time_alpha.h:104
void trigger(u32 now) override
Definition time_alpha.h:106
float updatef(u32 now) override
Definition time_alpha.h:138
void set_max_clamp(float max)
Definition time_alpha.h:150
u32 mFinishedPlateauTime
Definition time_alpha.h:81
TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime)
void trigger(u32 now) override
Call this when you want to (re)start the ramp cycle.
u8 update8(u32 now) override
Compute current 0–255 output based on how much time has elapsed since trigger().
bool isActive(u32 now) const override
u32 mFinishedRisingTime
Definition time_alpha.h:80
u32 mFinishedFallingTime
Definition time_alpha.h:82
#define MIN(a, b)
Definition math_macros.h:41
unsigned char u8
Definition int.h:17
u16 time_alpha16(u32 now, u32 start, u32 end)
constexpr T * end(T(&array)[N]) noexcept
float time_alphaf(u32 now, u32 start, u32 end)
Definition time_alpha.h:17
u8 time_alpha8(u32 now, u32 start, u32 end)
Definition time_alpha.cpp:8
IMPORTANT!
Definition crgb.h:20