FastLED 3.9.7
Loading...
Searching...
No Matches
frame_interpolator.h
1#pragma once
2
3#include "fl/map.h"
4#include "fx/video/pixel_stream.h"
5#include "fx/frame.h"
6#include "fx/video/frame_tracker.h"
7#include "fl/namespace.h"
8
9namespace fl {
10
11FASTLED_SMART_PTR(FrameInterpolator);
12
13// Holds onto frames and allow interpolation. This allows
14// effects to have high effective frame rate and also
15// respond to things like sound which can modify the timing.
17 public:
18 struct Less {
19 bool operator()(uint32_t a, uint32_t b) const { return a < b; }
20 };
22 FrameInterpolator(size_t nframes, float fpsVideo);
23
24 // Will search through the array, select the two frames that are closest to
25 // the current time and then interpolate between them, storing the results
26 // in the provided frame. The destination frame will have "now" as the
27 // current timestamp if and only if there are two frames that can be
28 // interpolated. Else it's set to the timestamp of the frame that was
29 // selected. Returns true if the interpolation was successful, false
30 // otherwise. If false then the destination frame will not be modified. Note
31 // that this adjustable_time is allowed to go pause or go backward in time.
32 bool draw(uint32_t adjustable_time, Frame *dst);
33 bool draw(uint32_t adjustable_time, CRGB *leds);
34 bool insert(uint32_t frameNumber, FramePtr frame) {
35 InsertResult result;
36 mFrames.insert(frameNumber, frame, &result);
37 return result != InsertResult::kMaxSize;
38 }
39
40 // Clear all frames
41 void clear() { mFrames.clear(); }
42
43 bool empty() const { return mFrames.empty(); }
44
45 bool has(uint32_t frameNum) const { return mFrames.has(frameNum); }
46
47 FramePtr erase(uint32_t frameNum) {
48 FramePtr out;
49 auto it = mFrames.find(frameNum);
50 if (it == mFrames.end()) {
51 return out;
52 }
53 out = it->second;
54 mFrames.erase(it);
55 return out;
56 }
57
58 FramePtr get(uint32_t frameNum) const {
59 auto it = mFrames.find(frameNum);
60 if (it != mFrames.end()) {
61 return it->second;
62 }
63 return FramePtr();
64 }
65
66 bool full() const { return mFrames.full(); }
67 size_t capacity() const { return mFrames.capacity(); }
68
69 FrameBuffer *getFrames() { return &mFrames; }
70
71 bool needsFrame(uint32_t now, uint32_t *currentFrameNumber,
72 uint32_t *nextFrameNumber) const {
73 mFrameTracker.get_interval_frames(now, currentFrameNumber,
74 nextFrameNumber);
75 return !has(*currentFrameNumber) || !has(*nextFrameNumber);
76 }
77
78 bool get_newest_frame_number(uint32_t *frameNumber) const {
79 if (mFrames.empty()) {
80 return false;
81 }
82 auto &front = mFrames.back();
83 *frameNumber = front.first;
84 return true;
85 }
86
87 bool get_oldest_frame_number(uint32_t *frameNumber) const {
88 if (mFrames.empty()) {
89 return false;
90 }
91 auto &front = mFrames.front();
92 *frameNumber = front.first;
93 return true;
94 }
95
96 uint32_t get_exact_timestamp_ms(uint32_t frameNumber) const {
97 return mFrameTracker.get_exact_timestamp_ms(frameNumber);
98 }
99
100 FrameTracker &getFrameTracker() { return mFrameTracker; }
101
102 private:
103 FrameBuffer mFrames;
104 FrameTracker mFrameTracker;
105};
106
107} // namespace fl
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54