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/memory.h"
9#include "fl/warn.h"
10#include "fl/xymap.h"
11#include "frame.h"
12
13#include "fl/memfill.h"
14namespace fl {
15
16Frame::Frame(int pixels_count) : mPixelsCount(pixels_count), mRgb() {
17 mRgb.resize(pixels_count);
18 fl::memfill((uint8_t*)mRgb.data(), 0, pixels_count * sizeof(CRGB));
19}
20
22 // Vector will handle memory cleanup automatically
23}
24
25void Frame::draw(CRGB *leds, DrawMode draw_mode) const {
26 if (!mRgb.empty()) {
27 switch (draw_mode) {
29 memcpy(leds, mRgb.data(), mPixelsCount * sizeof(CRGB));
30 break;
31 }
33 for (size_t i = 0; i < mPixelsCount; ++i) {
35 }
36 break;
37 }
38 }
39 }
40}
41
42void Frame::drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode) const {
43 const uint16_t width = xyMap.getWidth();
44 const uint16_t height = xyMap.getHeight();
45 fl::u32 count = 0;
46 for (uint16_t h = 0; h < height; ++h) {
47 for (uint16_t w = 0; w < width; ++w) {
48 fl::u32 in_idx = xyMap(w, h);
49 fl::u32 out_idx = count++;
50 if (in_idx >= mPixelsCount) {
52 "Frame::drawXY: in index out of range: " << in_idx);
53 continue;
54 }
55 if (out_idx >= mPixelsCount) {
57 "Frame::drawXY: out index out of range: " << out_idx);
58 continue;
59 }
60 switch (draw_mode) {
62 leds[out_idx] = mRgb[in_idx];
63 break;
64 }
66 leds[out_idx] =
67 CRGB::blendAlphaMaxChannel(mRgb[in_idx], leds[in_idx]);
68 break;
69 }
70 }
71 }
72 }
73}
74
75void Frame::clear() { fl::memfill((uint8_t*)mRgb.data(), 0, mPixelsCount * sizeof(CRGB)); }
76
77void Frame::interpolate(const Frame &frame1, const Frame &frame2,
78 uint8_t amountofFrame2, CRGB *pixels) {
79 if (frame1.size() != frame2.size()) {
80 return; // Frames must have the same size
81 }
82
83 const CRGB *rgbFirst = frame1.rgb();
84 const CRGB *rgbSecond = frame2.rgb();
85
86 if (frame1.mRgb.empty() || frame2.mRgb.empty()) {
87 // Error, why are we getting null pointers?
88 return;
89 }
90
91 for (size_t i = 0; i < frame2.size(); ++i) {
92 pixels[i] = CRGB::blend(rgbFirst[i], rgbSecond[i], amountofFrame2);
93 }
94 // We will eventually do something with alpha.
95}
96
97void Frame::interpolate(const Frame &frame1, const Frame &frame2,
98 uint8_t amountOfFrame2) {
99 if (frame1.size() != frame2.size() || frame1.size() != mPixelsCount) {
100 FASTLED_DBG("Frames must have the same size");
101 return; // Frames must have the same size
102 }
103 interpolate(frame1, frame2, amountOfFrame2, mRgb.data());
104}
105
106} // namespace fl
CRGB leds[NUM_LEDS]
fl::XYMap xyMap
Definition ColorBoost.h:61
void draw(CRGB *leds, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:25
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:97
const size_t mPixelsCount
Definition frame.h:44
Frame(int pixels_per_frame)
Definition frame.cpp:16
void clear()
Definition frame.cpp:75
void drawXY(CRGB *leds, const XYMap &xyMap, DrawMode draw_mode=DRAW_MODE_OVERWRITE) const
Definition frame.cpp:42
CRGB * rgb()
Definition frame.h:30
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:61
Implements the FastLED namespace macros.
void * memfill(void *ptr, int value, fl::size num)
Definition memfill.h:11
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
IMPORTANT!
Definition crgb.h:20
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Definition crgb.cpp:55
static CRGB blendAlphaMaxChannel(const CRGB &upper, const CRGB &lower)
Definition crgb.cpp:60
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
#define FASTLED_WARN
Definition warn.h:7