FastLED 3.9.7
Loading...
Searching...
No Matches
time.cpp
1#include "time.h"
2
3#include "fl/namespace.h"
4
5#include "fl/dbg.h"
6#include "fl/warn.h"
7
8#define DBG FASTLED_DBG
9
10namespace fl {
11
12TimeScale::TimeScale(uint32_t realTimeNow, float initialTimeScale)
13 : mLastRealTime(realTimeNow),
14 mStartTime(realTimeNow),
15 mTimeScale(initialTimeScale) {}
16
17TimeScale::~TimeScale() {}
18
19void TimeScale::setScale(float timeScale) {
20 mTimeScale = timeScale;
21}
22
23float TimeScale::scale() const {
24 return mTimeScale;
25}
26
27void TimeScale::pause(uint32_t now) {
28 if (mPauseTime) {
29 FASTLED_WARN("TimeScale::pause: already paused");
30 return;
31 }
32 mPauseTime = now;
33}
34void TimeScale::resume(uint32_t now) {
35 if (mLastRealTime == 0) {
36 reset(now);
37 return;
38 }
39 uint32_t diff = now - mPauseTime;
40 mStartTime += diff;
41 mLastRealTime += diff;
42 mPauseTime = 0;
43}
44
45uint32_t TimeScale::update(uint32_t timeNow) {
46
47 //DBG("TimeScale::update: timeNow: " << timeNow << " mLastRealTime: " << mLastRealTime
48 //<< " mRelativeTime: " << mRelativeTime << " mTimeScale: " << mTimeScale);
49
50 if (mLastRealTime > timeNow) {
51 DBG("TimeScale::applyExact: mLastRealTime > timeNow: " << mLastRealTime << " > " << timeNow);
52 }
53
54 applyExact(timeNow);
55 return time();
56}
57
58uint32_t TimeScale::time() const {
59 return mRelativeTime;
60}
61
62void TimeScale::reset(uint32_t realTimeNow) {
63 mLastRealTime = realTimeNow;
64 mStartTime = realTimeNow;
65 mRelativeTime = 0;
66}
67
68void TimeScale::applyExact(uint32_t timeNow) {
69 uint32_t elapsedRealTime = timeNow - mLastRealTime;
70 mLastRealTime = timeNow;
71 int32_t diff = static_cast<int32_t>(elapsedRealTime * mTimeScale);
72 if (diff == 0) {
73 return;
74 }
75 if (diff >= 0) {
76 mRelativeTime += diff;
77 return;
78 }
79
80 // diff < 0
81 uint32_t abs_diff = -diff;
82 if (abs_diff > mRelativeTime) {
83 // Protection against rollover.
84 mRelativeTime = 0;
85 mLastRealTime = timeNow;
86 return;
87 }
88 mLastRealTime = timeNow;
89 mRelativeTime -= abs_diff;
90}
91
92} // namespace fl
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16