FastLED 3.9.15
Loading...
Searching...
No Matches
frame.cpp
Go to the documentation of this file.
1
2#include <string.h>
3
4#include "crgb.h"
5#include "fl/allocator.h"
6#include "fl/dbg.h"
7#include "fl/namespace.h"
8#include "fl/ptr.h"
9#include "fl/warn.h"
10#include "fl/xymap.h"
11#include "frame.h"
12
13using namespace fl;
14
15namespace fl {
16
17Frame::Frame(int pixels_count) : mPixelsCount(pixels_count), mRgb() {
18 mRgb.resize(pixels_count);
19 memset(mRgb.data(), 0, pixels_count * sizeof(CRGB));
20}
21
23 // Vector will handle memory cleanup automatically
24}
25
26void Frame::draw(CRGB *leds, DrawMode draw_mode) const {
27 if (!mRgb.empty()) {
28 switch (draw_mode) {
30 memcpy(leds, mRgb.data(), mPixelsCount * sizeof(CRGB));
31 break;
32 }
34 for (size_t i = 0; i < mPixelsCount; ++i) {
36 }
37 break;
38 }
39 }
40 }
41}
42
43void Frame::drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode) const {
44 const uint16_t width = xyMap.getWidth();
45 const uint16_t height = xyMap.getHeight();
46 uint32_t count = 0;
47 for (uint16_t h = 0; h < height; ++h) {
48 for (uint16_t w = 0; w < width; ++w) {
49 uint32_t in_idx = xyMap(w, h);
50 uint32_t out_idx = count++;
51 if (in_idx >= mPixelsCount) {
53 "Frame::drawXY: in index out of range: " << in_idx);
54 continue;
55 }
56 if (out_idx >= mPixelsCount) {
58 "Frame::drawXY: out index out of range: " << out_idx);
59 continue;
60 }
61 switch (draw_mode) {
63 leds[out_idx] = mRgb[in_idx];
64 break;
65 }
67 leds[out_idx] =
68 CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
69 break;
70 }
71 }
72 }
73 }
74}
75
76void Frame::clear() { memset(mRgb.data(), 0, mPixelsCount * sizeof(CRGB)); }
77
78void Frame::interpolate(const Frame &frame1, const Frame &frame2,
79 uint8_t amountofFrame2, CRGB *pixels) {
80 if (frame1.size() != frame2.size()) {
81 return; // Frames must have the same size
82 }
83
84 const CRGB *rgbFirst = frame1.rgb();
85 const CRGB *rgbSecond = frame2.rgb();
86
87 if (frame1.mRgb.empty() || frame2.mRgb.empty()) {
88 // Error, why are we getting null pointers?
89 return;
90 }
91
92 for (size_t i = 0; i < frame2.size(); ++i) {
93 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
94 }
95 // We will eventually do something with alpha.
96}
97
98void Frame::interpolate(const Frame &frame1, const Frame &frame2,
99 uint8_t amountOfFrame2) {
100 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
101 FASTLED_DBG("Frames must have the same size");
102 return; // Frames must have the same size
103 }
104 interpolate(frame1, frame2, amountOfFrame2, mRgb.data());
105}
106
107} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
void draw(CRGB *leds, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:26
size_t size() const
Definition frame.h:32
fl::vector< CRGB, fl::allocator_psram< CRGB > > mRgb
Definition frame.h:45
void interpolate(const Frame &frame1, const Frame &frame2, uint8_t amountOfFrame2)
Definition frame.cpp:98
~Frame() override
Definition frame.cpp:22
const size_t mPixelsCount
Definition frame.h:44
Frame(int pixels_per_frame)
Definition frame.cpp:17
void clear()
Definition frame.cpp:76
void drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:43
CRGB * rgb()
Definition frame.h:30
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:57
XYMap xyMap
Definition gfx.cpp:8
Implements the FastLED namespace macros.
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_BLEND_BY_MAX_BRIGHTNESS
Definition draw_mode.h:5
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Definition crgb.cpp:54
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower)
Definition crgb.cpp:59
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55
#define FASTLED_WARN
Definition warn.h:7