FastLED 3.9.3
Loading...
Searching...
No Matches
bytestreammemory.cpp
1#include <string.h>
2
3#include "bytestreammemory.h"
4
5#include "math_macros.h"
6
7FASTLED_NAMESPACE_BEGIN
8
9ByteStreamMemory::ByteStreamMemory(uint32_t size_buffer)
10 : mBuffer(size_buffer) {}
11
12ByteStreamMemory::~ByteStreamMemory() = default;
13
14bool ByteStreamMemory::available(size_t n) const {
15 return mBuffer.size() >= n;
16}
17
18size_t ByteStreamMemory::read(uint8_t *dst, size_t bytesToRead) {
19 if (!available(bytesToRead) || dst == nullptr) {
20 return 0;
21 }
22
23 size_t actualBytesToRead = MIN(bytesToRead, mBuffer.size());
24 size_t bytesRead = 0;
25
26 while (bytesRead < actualBytesToRead) {
27 mBuffer.pop_front(&dst[bytesRead]);
28 bytesRead++;
29 }
30
31 return bytesRead;
32}
33
34size_t ByteStreamMemory::write(const uint8_t* src, size_t n) {
35 if (src == nullptr || mBuffer.capacity() == 0) {
36 return 0;
37 }
38
39 size_t written = 0;
40 for (size_t i = 0; i < n; ++i) {
41 if (mBuffer.full()) {
42 break;
43 }
44 mBuffer.push_back(src[i]);
45 ++written;
46 }
47 return written;
48}
49
50size_t ByteStreamMemory::write(const CRGB* src, size_t n) {
51 return write(reinterpret_cast<const uint8_t*>(src), n * 3);
52}
53
54FASTLED_NAMESPACE_END
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39