FastLED 3.9.3
Loading...
Searching...
No Matches
high_precision_interval.h
1#pragma once
2
3#include "namespace.h"
4
5FASTLED_NAMESPACE_BEGIN
6
8public:
9 HighPrecisionInterval(uint64_t microsSecondsPerInterval)
10 : mMicrosSecondsPerInterval(microsSecondsPerInterval), mIntervalCounter(0), mStartTime(0), mPauseOffset(0), mPauseTime(0), mIsPaused(false) {}
11
12 void reset(uint32_t startTime) {
13 mStartTime = startTime;
14 mIntervalCounter = 0;
15 mPauseOffset = 0;
16 mIsPaused = false;
17 }
18
19 void incrementIntervalCounter() { mIntervalCounter++; }
20
21 void pause(uint32_t now) {
22 if (!mIsPaused) {
23 mPauseTime = now;
24 mIsPaused = true;
25 }
26 }
27
28 void resume(uint32_t now) {
29 if (mIsPaused) {
30 mPauseOffset += now - mPauseTime;
31 mIsPaused = false;
32 }
33 }
34
35 bool needsFrame(uint32_t now, uint32_t* precise_timestamp) const {
36 if (mIsPaused) {
37 return false;
38 }
39
40 uint32_t adjustedNow = now - mPauseOffset;
41 uint32_t elapsed = adjustedNow - mStartTime;
42 uint32_t elapsedMicros = elapsed * 1000;
43 uint32_t intervalNumber = elapsedMicros / mMicrosSecondsPerInterval;
44 bool needs_update = intervalNumber > mIntervalCounter;
45 if (needs_update) {
46 *precise_timestamp = mStartTime + ((mIntervalCounter+1) * mMicrosSecondsPerInterval) / 1000 + mPauseOffset;
47 }
48 return needs_update;
49 }
50
51 bool isPaused() const { return mIsPaused; }
52
53private:
54 uint64_t mMicrosSecondsPerInterval;
55 uint32_t mIntervalCounter;
56 uint32_t mStartTime;
57 uint32_t mPauseOffset;
58 uint32_t mPauseTime;
59 bool mIsPaused;
60};
61
62FASTLED_NAMESPACE_END