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.reset(reinterpret_cast<CRGB *>(
19 LargeBlockAllocate(pixels_count * sizeof(CRGB))));
20 memset(mRgb.get(), 0, pixels_count * sizeof(CRGB));
21}
22
24 if (mRgb) {
25 LargeBlockDeallocate(mRgb.release());
26 }
27}
28
29void Frame::draw(CRGB *leds, DrawMode draw_mode) const {
30 if (mRgb) {
31 switch (draw_mode) {
33 memcpy(leds, mRgb.get(), mPixelsCount * sizeof(CRGB));
34 break;
35 }
37 for (size_t i = 0; i < mPixelsCount; ++i) {
39 }
40 break;
41 }
42 }
43 }
44}
45
46void Frame::drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode) const {
47 const uint16_t width = xyMap.getWidth();
48 const uint16_t height = xyMap.getHeight();
49 uint32_t count = 0;
50 for (uint16_t h = 0; h < height; ++h) {
51 for (uint16_t w = 0; w < width; ++w) {
52 uint32_t in_idx = xyMap(w, h);
53 uint32_t out_idx = count++;
54 if (in_idx >= mPixelsCount) {
56 "Frame::drawXY: in index out of range: " << in_idx);
57 continue;
58 }
59 if (out_idx >= mPixelsCount) {
61 "Frame::drawXY: out index out of range: " << out_idx);
62 continue;
63 }
64 switch (draw_mode) {
66 leds[out_idx] = mRgb[in_idx];
67 break;
68 }
70 leds[out_idx] =
71 CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
72 break;
73 }
74 }
75 }
76 }
77}
78
79void Frame::clear() { memset(mRgb.get(), 0, mPixelsCount * sizeof(CRGB)); }
80
81void Frame::interpolate(const Frame &frame1, const Frame &frame2,
82 uint8_t amountofFrame2, CRGB *pixels) {
83 if (frame1.size() != frame2.size()) {
84 return; // Frames must have the same size
85 }
86
87 const CRGB *rgbFirst = frame1.rgb();
88 const CRGB *rgbSecond = frame2.rgb();
89
90 if (!rgbFirst || !rgbSecond) {
91 // Error, why are we getting null pointers?
92 return;
93 }
94
95 for (size_t i = 0; i < frame2.size(); ++i) {
96 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
97 }
98 // We will eventually do something with alpha.
99}
100
101void Frame::interpolate(const Frame &frame1, const Frame &frame2,
102 uint8_t amountOfFrame2) {
103 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
104 FASTLED_DBG("Frames must have the same size");
105 return; // Frames must have the same size
106 }
107 interpolate(frame1, frame2, amountOfFrame2, mRgb.get());
108}
109
110} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
XYMap xyMap(WIDTH, HEIGHT, false)
void draw(CRGB *leds, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:29
size_t size() const
Definition frame.h:31
fl::scoped_array< CRGB > mRgb
Definition frame.h:44
void interpolate(const Frame &frame1, const Frame &frame2, uint8_t amountOfFrame2)
Definition frame.cpp:101
~Frame() override
Definition frame.cpp:23
const size_t mPixelsCount
Definition frame.h:43
Frame(int pixels_per_frame)
Definition frame.cpp:17
void clear()
Definition frame.cpp:79
void drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:46
CRGB * rgb()
Definition frame.h:29
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:57
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
void LargeBlockDeallocate(void *ptr)
Definition allocator.cpp:49
void * LargeBlockAllocate(size_t size, bool zero)
Definition allocator.cpp:41
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