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