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 <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
13uint8_t time_alpha8(uint32_t now, uint32_t start, uint32_t end);
14// 0 -> 65535
15uint16_t time_alpha16(uint32_t now, uint32_t start, uint32_t end);
16
17inline float time_alphaf(uint32_t now, uint32_t start, uint32_t end) {
18 if (now < start) {
19 return 0.0f;
20 }
21 uint32_t elapsed = now - start;
22 uint32_t 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(uint32_t now) = 0;
31 virtual uint8_t update8(uint32_t now) = 0;
32 virtual uint16_t update16(uint32_t now) {
33 return static_cast<uint16_t>(update8(now) << 8) + 0xFF;
34 }
35 virtual float updatef(uint32_t now) {
36 return static_cast<float>(update16(now)) / 65535.0f;
37 }
38 virtual bool isActive(uint32_t 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(uint32_t risingTime, uint32_t latchMs, uint32_t fallingTime);
61
63 void trigger(uint32_t now) override;
64
65 void trigger(uint32_t now, uint32_t risingTime, uint32_t latchMs,
66 uint32_t fallingTime);
67
69 bool isActive(uint32_t now) const override;
70
73 uint8_t update8(uint32_t now) override;
74
75 private:
76 uint32_t mLatchMs;
77 uint32_t mRisingTime;
78 uint32_t mFallingTime;
79
80 uint32_t mFinishedRisingTime = 0;
83
84 uint32_t mStart = 0;
85 uint8_t mLastValue = 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(uint32_t duration) : mDuration(duration) {}
105
106 void trigger(uint32_t now) override {
107 mStart = now;
108 mEnd = now + mDuration;
109 }
110
111 bool isActive(uint32_t 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 uint8_t update8(uint32_t 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 uint8_t out = time_alpha8(now, mStart, mEnd);
135 return out;
136 }
137
138 float updatef(uint32_t 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 uint32_t mStart = 0;
154 uint32_t mDuration = 0;
155 uint32_t mEnd = 0;
156 float mMaxClamp = -1.f; // default disabled.
157};
158
159} // namespace fl
UIButton trigger("Trigger")
virtual float updatef(uint32_t now)
Definition time_alpha.h:35
virtual uint16_t update16(uint32_t now)
Definition time_alpha.h:32
virtual uint8_t update8(uint32_t now)=0
virtual bool isActive(uint32_t now) const =0
virtual void trigger(uint32_t now)=0
virtual ~TimeAlpha()=default
TimeClampedTransition(uint32_t duration)
Definition time_alpha.h:104
uint8_t update8(uint32_t now) override
Definition time_alpha.h:128
float updatef(uint32_t now) override
Definition time_alpha.h:138
bool isActive(uint32_t now) const override
Definition time_alpha.h:111
void set_max_clamp(float max)
Definition time_alpha.h:150
void trigger(uint32_t now) override
Definition time_alpha.h:106
uint8_t mLastValue
Definition time_alpha.h:85
uint8_t update8(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:76
uint32_t mFinishedFallingTime
Definition time_alpha.h:82
TimeRamp(uint32_t risingTime, uint32_t latchMs, uint32_t fallingTime)
uint32_t mFinishedPlateauTime
Definition time_alpha.h:81
uint32_t mStart
Definition time_alpha.h:84
uint32_t mFinishedRisingTime
Definition time_alpha.h:80
uint32_t mFallingTime
Definition time_alpha.h:78
uint32_t mRisingTime
Definition time_alpha.h:77
#define MIN(a, b)
Definition math_macros.h:15
float time_alphaf(uint32_t now, uint32_t start, uint32_t end)
Definition time_alpha.h:17
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