FastLED 3.9.7
Loading...
Searching...
No Matches
bytestreammemory.cpp
1#include <string.h>
2
3#include "fl/bytestreammemory.h"
4#include "fl/math_macros.h"
5#include "fl/namespace.h"
6#include "fl/warn.h"
7
8
9
10namespace fl {
11
12ByteStreamMemory::ByteStreamMemory(uint32_t size_buffer)
13 : mReadBuffer(size_buffer) {}
14
15ByteStreamMemory::~ByteStreamMemory() = default;
16
17bool ByteStreamMemory::available(size_t n) const {
18 return mReadBuffer.size() >= n;
19}
20
21size_t ByteStreamMemory::read(uint8_t *dst, size_t bytesToRead) {
22 if (!available(bytesToRead) || dst == nullptr) {
23 FASTLED_WARN("ByteStreamMemory::read: !available(bytesToRead): " << bytesToRead << " mReadBuffer.size(): " << mReadBuffer.size());
24 return 0;
25 }
26
27 size_t actualBytesToRead = MIN(bytesToRead, mReadBuffer.size());
28 size_t bytesRead = 0;
29
30 while (bytesRead < actualBytesToRead) {
31 uint8_t& b = dst[bytesRead];
32 mReadBuffer.pop_front(&b);
33 bytesRead++;
34 }
35
36 if (bytesRead == 0) {
37 FASTLED_WARN("ByteStreamMemory::read: bytesRead == 0");
38 }
39
40 return bytesRead;
41}
42
43size_t ByteStreamMemory::write(const uint8_t* src, size_t n) {
44 if (src == nullptr || mReadBuffer.capacity() == 0) {
45 FASTLED_WARN_IF(src == nullptr, "ByteStreamMemory::write: src == nullptr");
46 FASTLED_WARN_IF(mReadBuffer.capacity() == 0, "ByteStreamMemory::write: mReadBuffer.capacity() == 0");
47 return 0;
48 }
49
50 size_t written = 0;
51 for (size_t i = 0; i < n; ++i) {
52 if (mReadBuffer.full()) {
53 FASTLED_WARN("ByteStreamMemory::write: mReadBuffer.full(): " << mReadBuffer.size());
54 break;
55 }
56 mReadBuffer.push_back(src[i]);
57 ++written;
58 }
59 return written;
60}
61
62size_t ByteStreamMemory::writeCRGB(const CRGB* src, size_t n) {
63 size_t bytes_written = write(reinterpret_cast<const uint8_t*>(src), n * 3);
64 size_t pixels_written = bytes_written / 3;
65 return pixels_written;
66}
67
68} // namespace fl
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54