FastLED 3.9.12
Loading...
Searching...
No Matches
rectangular_draw_buffer.cpp
1
2#include "fl/rectangular_draw_buffer.h"
3#include "fl/namespace.h"
4#include "rgbw.h"
5#include "fl/allocator.h"
6
7namespace fl {
8
9DrawItem::DrawItem(uint8_t pin, uint16_t numLeds, bool is_rgbw)
10 : mPin(pin), mIsRgbw(is_rgbw) {
11 if (is_rgbw) {
12 numLeds = Rgbw::size_as_rgb(numLeds);
13 }
14 mNumBytes = numLeds * 3;
15}
16
17Slice<uint8_t> RectangularDrawBuffer::getLedsBufferBytesForPin(uint8_t pin, bool clear_first) {
18 auto it = mPinToLedSegment.find(pin);
19 if (it == mPinToLedSegment.end()) {
20 FASTLED_ASSERT(false, "Pin not found in RectangularDrawBuffer");
21 return fl::Slice<uint8_t>();
22 }
23 fl::Slice<uint8_t> slice = it->second;
24 if (clear_first) {
25 memset(slice.data(), 0, slice.size() * sizeof(slice[0]));
26 }
27 return slice;
28}
29
30bool RectangularDrawBuffer::onQueuingStart() {
31 if (mQueueState == QUEUEING) {
32 return false;
33 }
34 mQueueState = QUEUEING;
35 mPinToLedSegment.clear();
36 mDrawList.swap(mPrevDrawList);
37 mDrawList.clear();
38 if (mAllLedsBufferUint8Size > 0) {
39 memset(mAllLedsBufferUint8.get(), 0, mAllLedsBufferUint8Size);
40 }
41 return true;
42}
43
44void RectangularDrawBuffer::queue(const DrawItem &item) {
45 mDrawList.push_back(item);
46}
47
48bool RectangularDrawBuffer::onQueuingDone() {
49 if (mQueueState == QUEUE_DONE) {
50 return false;
51 }
52 mQueueState = QUEUE_DONE;
53 mDrawListChangedThisFrame = mDrawList != mPrevDrawList;
54 // iterator through the current draw objects and calculate the total
55 // number of bytes (representing RGB or RGBW) that will be drawn this frame.
56 uint32_t total_bytes = 0;
57 uint32_t max_bytes_in_strip = 0;
58 uint32_t num_strips = 0;
59 getBlockInfo(&num_strips, &max_bytes_in_strip, &total_bytes);
60 if (total_bytes > mAllLedsBufferUint8Size) {
61 uint8_t* old_ptr = mAllLedsBufferUint8.release();
63 uint8_t* ptr = fl::LargeBlockAllocator<uint8_t>::Alloc(total_bytes);
64 mAllLedsBufferUint8.reset(ptr);
65 }
66 mAllLedsBufferUint8Size = total_bytes;
67 uint32_t offset = 0;
68 for (auto it = mDrawList.begin(); it != mDrawList.end(); ++it) {
69 uint8_t pin = it->mPin;
70 Slice<uint8_t> slice(mAllLedsBufferUint8.get() + offset,
71 max_bytes_in_strip);
72 mPinToLedSegment[pin] = slice;
73 offset += max_bytes_in_strip;
74 }
75 return true;
76}
77
78uint32_t RectangularDrawBuffer::getMaxBytesInStrip() const {
79 uint32_t max_bytes = 0;
80 for (auto it = mDrawList.begin(); it != mDrawList.end(); ++it) {
81 max_bytes = MAX(max_bytes, it->mNumBytes);
82 }
83 return max_bytes;
84}
85
86uint32_t RectangularDrawBuffer::getTotalBytes() const {
87 uint32_t num_strips = mDrawList.size();
88 uint32_t max_bytes = getMaxBytesInStrip();
89 return num_strips * max_bytes;
90}
91
92void RectangularDrawBuffer::getBlockInfo(uint32_t *num_strips,
93 uint32_t *bytes_per_strip,
94 uint32_t *total_bytes) const {
95 *num_strips = mDrawList.size();
96 *bytes_per_strip = getMaxBytesInStrip();
97 *total_bytes = (*num_strips) * (*bytes_per_strip);
98}
99
100} // namespace fl
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Functions for red, green, blue, white (RGBW) output.