FastLED 3.9.15
Loading...
Searching...
No Matches
time_alpha.cpp.hpp
Go to the documentation of this file.
1
3#include "fl/log/log.h"
4#include "fl/math/math.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8
9u8 time_alpha8(u32 now, u32 start, u32 end) FL_NOEXCEPT {
10 if (now < start) {
11 return 0;
12 }
13 if (now > end) {
14 return 255;
15 }
16 u32 elapsed = now - start;
17 u32 total = end - start;
18 u32 out = (elapsed * 255) / total;
19 if (out > 255) {
20 out = 255;
21 }
22 return static_cast<u8>(out);
23}
24
25u16 time_alpha16(u32 now, u32 start, u32 end) FL_NOEXCEPT {
26 if (now < start) {
27 return 0;
28 }
29 if (now > end) {
30 return 65535;
31 }
32 u32 elapsed = now - start;
33 u32 total = end - start;
34 u32 out = (elapsed * 65535) / total;
35 if (out > 65535) {
36 out = 65535;
37 }
38 return static_cast<u16>(out);
39}
40
41TimeRamp::TimeRamp(u32 risingTime, u32 latchMs, u32 fallingTime) FL_NOEXCEPT
42 : mLatchMs(latchMs), mRisingTime(risingTime), mFallingTime(fallingTime) {}
43
44bool TimeRamp::isActive(u32 now) const FL_NOEXCEPT {
45
46 bool not_started = (mFinishedRisingTime == 0) &&
47 (mFinishedPlateauTime == 0) &&
49 if (not_started) {
50 // if we have not started, we are not active
51 return false;
52 }
53
54 if (now < mStart) {
55 // if the time is before the start, we are not active
56 return false;
57 }
58
59 if (now > mFinishedFallingTime) {
60 // if the time is after the finished rising, we are not active
61 return false;
62 }
63
64 return true;
65}
66
68 if (!isActive(now)) {
70 }
71
72 if (now < mFinishedRisingTime) {
73 return RampPhase::Rising;
74 } else if (now < mFinishedPlateauTime) {
75 return RampPhase::Plateau;
76 } else if (now < mFinishedFallingTime) {
77 return RampPhase::Falling;
78 }
79
81}
82
84 RampPhase phase = getCurrentPhase(now);
85
86 switch (phase) {
88 // Not active, start fresh
89 mStart = now;
93 break;
94
96 // Continue rising, just extend the plateau end time
99 break;
100
102 // Extend the plateau by the full latch time from now
105 break;
106
108 // Reverse back to plateau
109 // Jump immediately to plateau and hold for latch time
113 mStart = now - mRisingTime; // Adjust start so timing stays consistent
114 break;
115 }
116}
117
119 if (!isActive(now)) {
120 return 0;
121 }
122 // u32 elapsed = now - mStart;
123 u8 out = 0;
124 if (now < mFinishedRisingTime) {
126 } else if (now < mFinishedPlateauTime) {
127 // plateau
128 out = 255;
129 } else if (now < mFinishedFallingTime) {
130 // ramp down
131 u8 alpha =
133 out = 255 - alpha;
134 } else {
135 // finished
136 out = 0;
137 }
138
139 mLastValue = out;
140 return out;
141}
142
143} // namespace fl
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
Centralized logging categories for FastLED hardware interfaces and subsystems.
unsigned char u8
Definition stdint.h:131
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