1#include "frame_tracker.h"
6long map(
long x,
long in_min,
long in_max,
long out_min,
long out_max) {
7 const long run = in_max - in_min;
11 const long rise = out_max - out_min;
12 const long delta = x - in_min;
13 return (delta * rise) / run + out_min;
17FrameTracker::FrameTracker(
float fps) {
19 mMicrosSecondsPerInterval =
static_cast<uint32_t
>(1000000.0f / fps + .5f);
22void FrameTracker::get_interval_frames(uint32_t now, uint32_t *frameNumber,
23 uint32_t *nextFrameNumber,
24 uint8_t *amountOfNextFrame)
const {
26 uint32_t effectiveTime = now;
29 uint64_t microseconds =
static_cast<uint64_t
>(effectiveTime) * 1000ULL;
32 *frameNumber = microseconds / mMicrosSecondsPerInterval;
33 *nextFrameNumber = *frameNumber + 1;
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;
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;
Implements a simple red square effect for 2D LED grids.