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
8uint8_t time_alpha8(uint32_t now, uint32_t start, uint32_t end) {
9 if (now < start) {
10 return 0;
11 }
12 if (now > end) {
13 return 255;
14 }
15 uint32_t elapsed = now - start;
16 uint32_t total = end - start;
17 uint32_t out = (elapsed * 255) / total;
18 if (out > 255) {
19 out = 255;
20 }
21 return static_cast<uint8_t>(out);
22}
23
24uint16_t time_alpha16(uint32_t now, uint32_t start, uint32_t end) {
25 if (now < start) {
26 return 0;
27 }
28 if (now > end) {
29 return 65535;
30 }
31 uint32_t elapsed = now - start;
32 uint32_t total = end - start;
33 uint32_t out = (elapsed * 65535) / total;
34 if (out > 65535) {
35 out = 65535;
36 }
37 return static_cast<uint16_t>(out);
38}
39
40TimeRamp::TimeRamp(uint32_t risingTime, uint32_t latchMs,
41 uint32_t fallingTime)
42 : mLatchMs(latchMs), mRisingTime(risingTime), mFallingTime(fallingTime) {}
43
52
53void TimeRamp::trigger(uint32_t now, uint32_t risingTime,
54 uint32_t latchMs, uint32_t fallingTime) {
55 mRisingTime = risingTime;
56 mLatchMs = latchMs;
57 mFallingTime = fallingTime;
58 trigger(now);
59}
60
61bool TimeRamp::isActive(uint32_t now) const {
62
63 bool not_started = (mFinishedRisingTime == 0) && (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
83uint8_t TimeRamp::update(uint32_t now) {
84 if (!isActive(now)) {
85 return 0;
86 }
87 // uint32_t elapsed = now - mStart;
88 uint8_t 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 uint8_t 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
uint8_t mLastValue
Definition time_alpha.h:72
uint8_t update(uint32_t now) override
Compute current 0–255 output based on how much time has elapsed since trigger().
bool isActive(uint32_t now) const override
uint32_t mLatchMs
Definition time_alpha.h:63
void trigger(uint32_t now) override
Call this when you want to (re)start the ramp cycle.
uint32_t mFinishedFallingTime
Definition time_alpha.h:69
TimeRamp(uint32_t risingTime, uint32_t latchMs, uint32_t fallingTime)
uint32_t mFinishedPlateauTime
Definition time_alpha.h:68
uint32_t mStart
Definition time_alpha.h:71
uint32_t mFinishedRisingTime
Definition time_alpha.h:67
uint32_t mFallingTime
Definition time_alpha.h:65
uint32_t mRisingTime
Definition time_alpha.h:64
uint8_t time_alpha8(uint32_t now, uint32_t start, uint32_t end)
Definition time_alpha.cpp:8
uint16_t time_alpha16(uint32_t now, uint32_t start, uint32_t end)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16