FastLED 3.9.7
Loading...
Searching...
No Matches
frame_tracker.cpp
1#include "frame_tracker.h"
2
3namespace fl {
4
5namespace { // anonymous namespace
6long map(long x, long in_min, long in_max, long out_min, long out_max) {
7 const long run = in_max - in_min;
8 if (run == 0) {
9 return 0; // AVR returns -1, SAM returns 0
10 }
11 const long rise = out_max - out_min;
12 const long delta = x - in_min;
13 return (delta * rise) / run + out_min;
14}
15} // anonymous namespace
16
17FrameTracker::FrameTracker(float fps) {
18 // Convert fps to microseconds per frame interval
19 mMicrosSecondsPerInterval = static_cast<uint32_t>(1000000.0f / fps + .5f);
20}
21
22void FrameTracker::get_interval_frames(uint32_t now, uint32_t *frameNumber,
23 uint32_t *nextFrameNumber,
24 uint8_t *amountOfNextFrame) const {
25 // Account for any pause time
26 uint32_t effectiveTime = now;
27
28 // Convert milliseconds to microseconds for precise calculation
29 uint64_t microseconds = static_cast<uint64_t>(effectiveTime) * 1000ULL;
30
31 // Calculate frame number with proper rounding
32 *frameNumber = microseconds / mMicrosSecondsPerInterval;
33 *nextFrameNumber = *frameNumber + 1;
34
35 // Calculate interpolation amount if requested
36 if (amountOfNextFrame != nullptr) {
37 uint64_t frame1_start = (*frameNumber * mMicrosSecondsPerInterval);
38 uint64_t frame2_start = (*nextFrameNumber * mMicrosSecondsPerInterval);
39 uint32_t rel_time = microseconds - frame1_start;
40 uint32_t frame_duration = frame2_start - frame1_start;
41 uint8_t progress = uint8_t(map(rel_time, 0, frame_duration, 0, 255));
42 *amountOfNextFrame = progress;
43 }
44}
45
46uint32_t FrameTracker::get_exact_timestamp_ms(uint32_t frameNumber) const {
47 uint64_t microseconds = frameNumber * mMicrosSecondsPerInterval;
48 return static_cast<uint32_t>(microseconds / 1000) + mStartTime;
49}
50
51} // namespace fl
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16