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