FastLED 3.9.15
Loading...
Searching...
No Matches
codec_processor.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <FastLED.h>
3
4#include "codec_processor.h"
5#include "inlined_data.h"
6
7namespace CodecProcessor {
8
9// Configuration constants
10const int TARGET_FPS = 30;
11
12// LED array pointers - set from main sketch
13CRGB* leds = nullptr;
14int numLeds = 0;
15int ledWidth = 64;
16int ledHeight = 64;
17
18void processCodecWithTiming(const char* codecName, fl::function<void()> codecFunc) {
19 FL_WARN("Starting format " << codecName);
20
21 fl::u32 start_time = millis();
22
23 // Execute the codec processing function
24 codecFunc();
25
26 fl::u32 diff_time = millis() - start_time;
27
28 FL_WARN("Format took " << diff_time << "ms to process " << codecName);
29
30 // Now build up the message to display the current leds
31 fl::sstream message;
32 message << "Format: " << codecName << "\n";
33 message << "LEDs: " << numLeds << " (" << ledWidth << "x" << ledHeight << ")\n";
34
35 // For 32x32, just show a summary rather than all pixels
36 if (numLeds > 16) {
37 // Show first few pixels as a sample
38 message << "First 4 pixels: ";
39 for (int i = 0; i < 4 && i < numLeds; i++) {
40 message << "RGB(" << (int)leds[i].r << "," << (int)leds[i].g << "," << (int)leds[i].b << ") ";
41 }
42 message << "\n";
43 } else {
44 // For small displays, show all LEDs
45 for (int i = 0; i < numLeds; i++) {
46 message << "LED " << i << ": " << leds[i] << "\n";
47 }
48 }
49 FL_WARN(message.str().c_str());
50 FastLED.show();
51}
52
54 Serial.println("\n=== Processing JPEG ===");
55
56 if (!fl::Jpeg::isSupported()) {
57 Serial.println("JPEG decoding not supported on this platform");
58 return;
59 }
60
61 // Copy data from PROGMEM to RAM
64
65 // Configure JPEG decoder
66 fl::JpegConfig config;
69
70 // Create data span
71 fl::span<const fl::u8> data(jpegData.data(), jpegData.size());
72
73 // Decode the JPEG
74 fl::string error_msg;
75 fl::FramePtr framePtr = fl::Jpeg::decode(config, data, &error_msg);
76
77 if (framePtr && framePtr->isValid()) {
78 displayFrameOnLEDs(*framePtr);
79 showDecodedMessage("JPEG decoded successfully!");
80 } else {
81 Serial.print("Failed to decode JPEG: ");
82 Serial.println(error_msg.c_str());
83 }
84}
85
86
87void processGif() {
88 Serial.println("\n=== Processing GIF ===");
89
90 if (!fl::Gif::isSupported()) {
91 Serial.println("GIF decoding not supported on this platform");
92 return;
93 }
94
95 // Copy data from PROGMEM to RAM
98
99 // Configure GIF decoder
100 fl::GifConfig config;
103
104 // Create decoder
105 fl::string error_msg;
106 fl::shared_ptr<fl::IDecoder> decoder = fl::Gif::createDecoder(config, &error_msg);
107
108 if (!decoder) {
109 Serial.print("Failed to create GIF decoder: ");
110 Serial.println(error_msg.c_str());
111 return;
112 }
113
114 // Create byte stream from data
115 auto stream = fl::make_shared<fl::memorybuf>(gifData.size());
116 stream->write(gifData);
117
118 // Begin decoding
119 if (!decoder->begin(stream)) {
120 fl::string error;
121 decoder->hasError(&error);
122 Serial.print("Failed to begin GIF decoding: ");
123 Serial.println(error.c_str());
124 return;
125 }
126
127 // Decode first frame
128 fl::DecodeResult result = decoder->decode();
129
130 if (result == fl::DecodeResult::Success) {
131 fl::Frame frame = decoder->getCurrentFrame();
132 if (frame.isValid()) {
133 displayFrameOnLEDs(frame);
134 showDecodedMessage("GIF decoded successfully!");
135 } else {
136 Serial.println("Invalid GIF frame received");
137 }
138 } else {
139 fl::string error;
140 decoder->hasError(&error);
141 Serial.print("GIF frame decode error: ");
142 Serial.println(error.c_str());
143 }
144
145 decoder->end();
146}
147
149 Serial.println("\n=== Processing MPEG1 ===");
150
151 if (!fl::Mpeg1::isSupported()) {
152 Serial.println("MPEG1 decoding not supported on this platform");
153 return;
154 }
155
156 // Copy data from PROGMEM to RAM
159
160 // Configure MPEG1 decoder
161 fl::Mpeg1Config config;
163 config.targetFps = TARGET_FPS;
164 config.looping = false;
165 config.skipAudio = true;
166
167 // Create decoder
168 fl::string error_msg;
169 fl::shared_ptr<fl::IDecoder> decoder = fl::Mpeg1::createDecoder(config, &error_msg);
170
171 if (!decoder) {
172 Serial.print("Failed to create MPEG1 decoder: ");
173 Serial.println(error_msg.c_str());
174 return;
175 }
176
177 // Create byte stream from data
178 auto stream = fl::make_shared<fl::memorybuf>(mpegData.size());
179 stream->write(mpegData);
180
181 // Begin decoding
182 if (!decoder->begin(stream)) {
183 fl::string error;
184 decoder->hasError(&error);
185 Serial.print("Failed to begin MPEG1 decoding: ");
186 Serial.println(error.c_str());
187 return;
188 }
189
190 // Decode first frame
191 fl::DecodeResult result = decoder->decode();
192
193 if (result == fl::DecodeResult::Success) {
194 fl::Frame frame = decoder->getCurrentFrame();
195 if (frame.isValid()) {
196 displayFrameOnLEDs(frame);
197 showDecodedMessage("MPEG1 decoded successfully!");
198 } else {
199 Serial.println("Invalid MPEG1 frame received");
200 }
201 } else {
202 fl::string error;
203 decoder->hasError(&error);
204 Serial.print("MPEG1 frame decode error: ");
205 Serial.println(error.c_str());
206 }
207
208 decoder->end();
209}
210
211void displayFrameOnLEDs(const fl::Frame& frame) {
212 for (int y = 0; y < ledHeight; y++) {
213 for (int x = 0; x < ledWidth; x++) {
214 // Calculate source pixel coordinates (with scaling)
215 int srcX = (x * (int)frame.getWidth()) / ledWidth;
216 int srcY = (y * (int)frame.getHeight()) / ledHeight;
217
218 // Get pixel from frame
219 CRGB color = getPixelFromFrame(frame, srcX, srcY);
220
221 // Set LED color
222 int ledIndex = y * ledWidth + x;
223 if (ledIndex < numLeds) {
224 leds[ledIndex] = color;
225 }
226 }
227 }
228}
229
230CRGB getPixelFromFrame(const fl::Frame& frame, int x, int y) {
231 if (x >= (int)frame.getWidth() || y >= (int)frame.getHeight() || !frame.isValid()) {
232 return CRGB::Black;
233 }
234
235 int pixelIndex = y * (int)frame.getWidth() + x;
236
237 // Frame stores data as CRGB
238 if (pixelIndex < (int)frame.size()) {
239 return frame.rgb()[pixelIndex];
240 }
241
242 return CRGB::Black;
243}
244
245void showDecodedMessage(const char* format) {
246 Serial.println(format);
247}
248
249} // namespace CodecProcessor
int y
Definition simple.h:93
int x
Definition simple.h:92
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
size_t size() const
Definition frame.h:44
fl::u16 getWidth() const
Definition frame.h:59
fl::span< CRGB > rgb()
Definition frame.h:42
fl::u16 getHeight() const
Definition frame.h:60
bool isValid() const
static IDecoderPtr createDecoder(const GifConfig &config, fl::string *error_message=nullptr)
Definition gif.cpp.hpp:9
static bool isSupported()
Definition gif.cpp.hpp:23
static bool decode(const JpegConfig &config, fl::span< const fl::u8 > data, Frame *frame, fl::string *error_message=nullptr)
Definition jpeg.cpp.hpp:296
static bool isSupported()
Definition jpeg.cpp.hpp:376
static bool isSupported()
Definition mpeg1.cpp.hpp:14
static IDecoderPtr createDecoder(const Mpeg1Config &config, fl::string *error_message=nullptr)
Definition mpeg1.cpp.hpp:8
const char * c_str() const FL_NOEXCEPT
string str() const FL_NOEXCEPT
Definition strstream.h:43
fl::size size() const FL_NOEXCEPT
T * data() FL_NOEXCEPT
Definition vector.h:619
fl::CRGB CRGB
Definition crgb.h:25
#define FL_WARN(X)
Definition log.h:276
const size_t sampleJpegDataLength
const size_t sampleGifDataLength
const uint8_t sampleMpeg1Data[]
const uint8_t sampleJpegData[]
const uint8_t sampleGifData[]
const size_t sampleMpeg1DataLength
void displayFrameOnLEDs(const fl::Frame &frame)
CRGB getPixelFromFrame(const fl::Frame &frame, int x, int y)
void showDecodedMessage(const char *format)
void processCodecWithTiming(const char *codecName, fl::function< void()> codecFunc)
void * memcopy(void *dest, const void *src, size_t n) FL_NOEXCEPT
Definition cstring.h:103
shared_ptr< T > make_shared(Args &&... args) FL_NOEXCEPT
Definition shared_ptr.h:414
third_party::Mpeg1Config Mpeg1Config
Definition mpeg1.h:29
DecodeResult
Definition idecoder.h:18
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition crgb.h:510
@ SingleFrame
Definition gif.h:30
PixelFormat format
Definition gif.h:33
FrameMode mode
Definition gif.h:32
PixelFormat format
Definition jpeg.h:31
Quality quality
Definition jpeg.h:30
#define Serial
Definition serial.h:304