FastLED 3.9.3
Loading...
Searching...
No Matches
data_stream.cpp
1
2#include "fx/detail/data_stream.h"
3#include "namespace.h"
4#include "fx/storage/filebuffer.h"
5
6#ifndef INT32_MAX
7#define INT32_MAX 0x7fffffff
8#endif
9
10
11
12FASTLED_NAMESPACE_BEGIN
13
14DataStream::DataStream(int bytes_per_frame) : mbytesPerFrame(bytes_per_frame), mUsingByteStream(false) {
15}
16
17DataStream::~DataStream() {
18 close();
19}
20
21bool DataStream::begin(FileHandleRef h) {
22 close();
23 mFileHandle = h;
24 mFileBuffer = FileBufferRef::New(h);
25 mUsingByteStream = false;
26 return mFileBuffer->available();
27}
28
29bool DataStream::beginStream(ByteStreamRef s) {
30 close();
31 mByteStream = s;
32 mUsingByteStream = true;
33 return mByteStream->available(mbytesPerFrame);
34}
35
36void DataStream::close() {
37 if (!mUsingByteStream && mFileBuffer) {
38 mFileBuffer.reset();
39 }
40 mByteStream.reset();
41 mFileHandle.reset();
42}
43
44int32_t DataStream::bytesPerFrame() {
45 return mbytesPerFrame;
46}
47
48bool DataStream::readPixel(CRGB* dst) {
49 if (mUsingByteStream) {
50 return mByteStream->read(&dst->r, 1) && mByteStream->read(&dst->g, 1) && mByteStream->read(&dst->b, 1);
51 } else {
52 return mFileBuffer->read(&dst->r, 1) && mFileBuffer->read(&dst->g, 1) && mFileBuffer->read(&dst->b, 1);
53 }
54}
55
56bool DataStream::available() const {
57 if (mUsingByteStream) {
58 return mByteStream->available(mbytesPerFrame);
59 } else {
60 return mFileBuffer->available();
61 }
62}
63
64bool DataStream::readFrame(Frame* frame) {
65 // returns true if a frame was read.
66 if (!framesRemaining() || !frame) {
67 return false;
68 }
69 if (mUsingByteStream) {
70 mByteStream->read(frame->rgb(), mbytesPerFrame);
71 } else {
72 mFileBuffer->read(frame->rgb(), mbytesPerFrame);
73 }
74 return true;
75}
76
77int32_t DataStream::framesRemaining() const {
78 if (mbytesPerFrame == 0) return 0;
79 int32_t bytes_left = bytesRemaining();
80 if (bytes_left <= 0) {
81 return 0;
82 }
83 return bytes_left / mbytesPerFrame;
84}
85
86int32_t DataStream::framesDisplayed() const {
87 if (mUsingByteStream) {
88 // ByteStream doesn't have a concept of total size, so we can't calculate this
89 return -1;
90 } else {
91 int32_t bytes_played = mFileBuffer->FileSize() - mFileBuffer->BytesLeft();
92 return bytes_played / mbytesPerFrame;
93 }
94}
95
96int32_t DataStream::bytesRemaining() const {
97 if (mUsingByteStream) {
98 return INT32_MAX;
99 } else {
100 return mFileBuffer->BytesLeft();
101 }
102}
103
104int32_t DataStream::bytesRemainingInFrame() const {
105 return bytesRemaining() % mbytesPerFrame;
106}
107
108bool DataStream::rewind() {
109 if (mUsingByteStream) {
110 // ByteStream doesn't support rewinding
111 return false;
112 } else {
113 mFileBuffer->rewindToStart();
114 return true;
115 }
116}
117
118DataStream::Type DataStream::getType() const {
119 return mUsingByteStream ? Type::kStreaming : Type::kFile;
120}
121
122size_t DataStream::readBytes(uint8_t* dst, size_t len) {
123 uint16_t bytesRead = 0;
124 if (mUsingByteStream) {
125 while (bytesRead < len && mByteStream->available(len)) {
126 // use pop_front()
127 if (mByteStream->read(dst + bytesRead, 1)) {
128 bytesRead++;
129 } else {
130 break;
131 }
132 }
133 } else {
134 while (bytesRead < len && mFileBuffer->available()) {
135 if (mFileBuffer->read(dst + bytesRead, 1)) {
136 bytesRead++;
137 } else {
138 break;
139 }
140 }
141 }
142 return bytesRead;
143}
144
145FASTLED_NAMESPACE_END
Definition frame.h:18
uint8_t r
Red channel value.
Definition crgb.h:43
uint8_t g
Green channel value.
Definition crgb.h:47
uint8_t b
Blue channel value.
Definition crgb.h:51
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39