FastLED 3.9.15
Loading...
Searching...
No Matches
time_alpha.cpp
Go to the documentation of this file.
1
2#include "fl/time_alpha.h"
3#include "fl/warn.h"
4#include "math_macros.h"
5
6namespace fl {
7
8u8 time_alpha8(u32 now, u32 start, u32 end) {
9 if (now < start) {
10 return 0;
11 }
12 if (now > end) {
13 return 255;
14 }
15 u32 elapsed = now - start;
16 u32 total = end - start;
17 u32 out = (elapsed * 255) / total;
18 if (out > 255) {
19 out = 255;
20 }
21 return static_cast<u8>(out);
22}
23
24u16 time_alpha16(u32 now, u32 start, u32 end) {
25 if (now < start) {
26 return 0;
27 }
28 if (now > end) {
29 return 65535;
30 }
31 u32 elapsed = now - start;
32 u32 total = end - start;
33 u32 out = (elapsed * 65535) / total;
34 if (out > 65535) {
35 out = 65535;
36 }
37 return static_cast<u16>(out);
38}
39
40TimeRamp::TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime)
41 : mLatchMs(latchMs), mRisingTime(risingTime), mFallingTime(fallingTime) {}
42
51
52void TimeRamp::trigger(u32 now, u32 risingTime, u32 latchMs,
53 u32 fallingTime) {
54 mRisingTime = risingTime;
55 mLatchMs = latchMs;
56 mFallingTime = fallingTime;
57 trigger(now);
58}
59
60bool TimeRamp::isActive(u32 now) const {
61
62 bool not_started = (mFinishedRisingTime == 0) &&
63 (mFinishedPlateauTime == 0) &&
65 if (not_started) {
66 // if we have not started, we are not active
67 return false;
68 }
69
70 if (now < mStart) {
71 // if the time is before the start, we are not active
72 return false;
73 }
74
75 if (now > mFinishedFallingTime) {
76 // if the time is after the finished rising, we are not active
77 return false;
78 }
79
80 return true;
81}
82
84 if (!isActive(now)) {
85 return 0;
86 }
87 // u32 elapsed = now - mStart;
88 u8 out = 0;
89 if (now < mFinishedRisingTime) {
91 } else if (now < mFinishedPlateauTime) {
92 // plateau
93 out = 255;
94 } else if (now < mFinishedFallingTime) {
95 // ramp down
96 u8 alpha =
98 out = 255 - alpha;
99 } else {
100 // finished
101 out = 0;
102 }
103
104 mLastValue = out;
105 return out;
106}
107
108} // namespace fl
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
unsigned char u8
Definition int.h:17
u16 time_alpha16(u32 now, u32 start, u32 end)
constexpr T * end(T(&array)[N]) noexcept
u8 time_alpha8(u32 now, u32 start, u32 end)
Definition time_alpha.cpp:8
IMPORTANT!
Definition crgb.h:20