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) {
55 FASTLED_WARN("Frame::drawXY: in index out of range: " << in_idx);
56 continue;
57 }
58 if (out_idx >= mPixelsCount) {
59 FASTLED_WARN("Frame::drawXY: out index out of range: " << out_idx);
60 continue;
61 }
62 switch (draw_mode) {
64 leds[out_idx] = mRgb[in_idx];
65 break;
66 }
68 leds[out_idx] = CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
69 break;
70 }
71 }
72 }
73 }
74}
75
76void Frame::clear() { memset(mRgb.get(), 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 (!rgbFirst || !rgbSecond) {
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.get());
105}
106
107} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
XYMap xyMap(HEIGHT, WIDTH, SERPENTINE)
void draw(CRGB *leds, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:29
size_t size() const
Definition frame.h:34
fl::scoped_array< CRGB > mRgb
Definition frame.h:43
void interpolate(const Frame &frame1, const Frame &frame2, uint8_t amountOfFrame2)
Definition frame.cpp:98
~Frame() override
Definition frame.cpp:23
const size_t mPixelsCount
Definition frame.h:42
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:46
CRGB * rgb()
Definition frame.h:32
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:60
Implements the FastLED namespace macros.
DrawMode
Definition frame.h:17
@ DRAW_MODE_BLEND_BY_BLACK
Definition frame.h:19
@ DRAW_MODE_OVERWRITE
Definition frame.h:18
void LargeBlockDeallocate(void *ptr)
Definition allocator.cpp:50
void * LargeBlockAllocate(size_t size, bool zero)
Definition allocator.cpp:42
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:50
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower)
Definition crgb.cpp:58
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
#define FASTLED_WARN
Definition warn.h:7