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
6namespace fl {
7
8// Use this function to compute the alpha value based on the time elapsed
9// 0 -> 255
10uint8_t time_alpha8(uint32_t now, uint32_t start, uint32_t end);
11// 0 -> 65535
12uint16_t time_alpha16(uint32_t now, uint32_t start, uint32_t end);
13
14
15class TimeAlpha {
16 public:
17 virtual ~TimeAlpha() = default;
18 virtual void trigger(uint32_t now) = 0;
19 virtual uint8_t update(uint32_t now) = 0;
20 virtual uint16_t update16(uint32_t now) {
21 return static_cast<uint16_t>(update(now) << 8);
22 }
23 virtual bool isActive(uint32_t now) const = 0;
24};
25
26/*
27 * amplitude
28 * ^
29 * 255 ───────────────────────
30 * / \
31 * / \
32 * / \
33 * / \
34 * 0 ────────────┴ ┴──────────────────> time (ms)
35 * t0 t1 t2 t4
36 *
37 *
38 *
39 */
40class TimeRamp: public TimeAlpha {
41 public:
42
43
47 TimeRamp(uint32_t risingTime, uint32_t latchMs, uint32_t fallingTime);
48
50 void trigger(uint32_t now) override;
51
52 void trigger(uint32_t now, uint32_t risingTime, uint32_t latchMs,
53 uint32_t fallingTime);
54
56 bool isActive(uint32_t now) const override;
57
60 uint8_t update(uint32_t now) override;
61
62 private:
63 uint32_t mLatchMs;
64 uint32_t mRisingTime;
65 uint32_t mFallingTime;
66
67 uint32_t mFinishedRisingTime = 0;
70
71 uint32_t mStart = 0;
72 uint8_t mLastValue = 0;
73};
74
75
76/*
77 * amplitude
78 * ^
79 * 255 ──────────────────────────────────────
80 * /
81 * /
82 * /
83 * /
84 * 0 ────────────┴ --> time (ms)
85 * t0 t1
86 *
87 *
88 *
89 */
91 public:
92 TimeLinear(uint32_t duration) : mDuration(duration) {}
93
94 void trigger(uint32_t now) override {
95 mStart = now;
96 mEnd = now + mDuration;
97 }
98
99 bool isActive(uint32_t now) const override {
100 bool not_started = (mEnd == 0) && (mStart == 0);
101 if (not_started) {
102 // if we have not started, we are not active
103 return false;
104 }
105 if (now < mStart) {
106 // if the time is before the start, we are not active
107 return false;
108 }
109 if (now > mEnd) {
110 // if the time is after the finished rising, we are not active
111 return false;
112 }
113 return true;
114 }
115
116 uint8_t update(uint32_t now) override {
117 bool not_started = (mEnd == 0) && (mStart == 0);
118 if (not_started) {
119 // if we have not started, we are not active
120 return 0;
121 }
122 uint8_t out = time_alpha8(now, mStart, mEnd);
123 return out;
124 }
125
126 private:
127 uint32_t mStart = 0;
128 uint32_t mDuration = 0;
129 uint32_t mEnd = 0;
130};
131
132} // namespace fl
virtual uint16_t update16(uint32_t now)
Definition time_alpha.h:20
virtual bool isActive(uint32_t now) const =0
virtual void trigger(uint32_t now)=0
virtual ~TimeAlpha()=default
virtual uint8_t update(uint32_t now)=0
void trigger(uint32_t now) override
Definition time_alpha.h:94
TimeLinear(uint32_t duration)
Definition time_alpha.h:92
bool isActive(uint32_t now) const override
Definition time_alpha.h:99
uint32_t mDuration
Definition time_alpha.h:128
uint32_t mEnd
Definition time_alpha.h:129
uint8_t update(uint32_t now) override
Definition time_alpha.h:116
uint32_t mStart
Definition time_alpha.h:127
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