FastLED 3.9.15
Loading...
Searching...
No Matches
serial.h
Go to the documentation of this file.
1// Arduino-compatible Serial API for FastLED
2// Provides a familiar interface for serial I/O across all platforms
3
4#pragma once
5
6#include "fl/stl/has_include.h" // IWYU pragma: keep
7
8#include "fl/stl/stdint.h"
9#include "fl/stl/cstddef.h"
10#include "fl/stl/stdio.h" // for fl::snprintf
11#include "fl/stl/type_traits.h" // for enable_if and is_multi_byte_integer
12
13namespace fl {
14
39public:
47 void begin(u32 baudRate = 115200);
48
54 void end();
55
60 int available();
61
68 int read();
69
76 int peek();
77
83 size_t write(u8 byte);
84
91 size_t write(const u8* buffer, size_t size);
92
98 size_t print(const char* str);
99
105 size_t print(int value);
106
112 size_t print(long value);
113
120 template<typename T>
123 size_t>::type
124 print(T value);
125
131 size_t println(const char* str);
132
137 size_t println();
138
144 size_t println(int value);
145
151 size_t println(long value);
152
159 template<typename T>
162 size_t>::type
163 println(T value);
164
179 template<typename... Args>
180 size_t printf(const char* format, Args... args); // ok snprintf — method routes through fl::snprintf at line 284
181
190 bool flush(u32 timeoutMs = 1000);
191
198 explicit operator bool() const;
199
206 void setTimeout(u32 timeoutMs);
207
215
224 fl::string readStringUntil(char delimiter);
225
234 size_t readBytes(u8* buffer, size_t length);
235
246 size_t readBytesUntil(char delimiter, u8* buffer, size_t length);
247
255 long parseInt();
256
264 float parseFloat();
265
266private:
267 u32 mTimeoutMs = 1000; // Default 1 second timeout
268};
269
270// ============================================================================
271// Template Implementations
272// ============================================================================
273
274
275
276template<typename... Args>
277inline size_t SerialPort::printf(const char* format, Args... args) {
278 if (!format) {
279 return 0;
280 }
281
282 // Format into a fixed-size buffer using fl::snprintf
283 char buffer[256]; // Maximum formatted string length
284 int len = fl::snprintf(buffer, sizeof(buffer), format, args...);
285
286 if (len < 0) {
287 return 0; // Formatting error
288 }
289
290 // Delegate to print() method
291 return print(buffer);
292}
293
294// Global Serial object (Arduino-compatible)
295// FL_MAYBE_UNUSED allows compiler to elide if not used
297
298} // namespace fl
299
300// Define Serial as fl::fl_serial ONLY when Arduino is not available
301// When Arduino.h exists, Serial remains the Arduino Serial object
302// When Arduino.h doesn't exist, Serial is defined as fl::fl_serial for convenience
303#if !FL_HAS_INCLUDE(<Arduino.h>)
304#define Serial fl::fl_serial
305#endif
int peek()
Peek at next byte without removing it from buffer.
bool flush(u32 timeoutMs=1000)
Wait for serial output to complete.
void begin(u32 baudRate=115200)
Initialize serial communication.
fl::string readString()
Read all available bytes into a string.
size_t print(const char *str)
Print string to serial output.
float parseFloat()
Parse floating-point number from serial input.
size_t println()
Print newline only.
fl::string readStringUntil(char delimiter)
Read characters until delimiter is found.
void setTimeout(u32 timeoutMs)
Set timeout for read operations.
size_t write(u8 byte)
Write single byte to serial output.
size_t readBytesUntil(char delimiter, u8 *buffer, size_t length)
Read bytes until delimiter is found.
long parseInt()
Parse integer from serial input.
size_t printf(const char *format, Args... args)
Print formatted string to serial output (printf-style)
Definition serial.h:277
int available()
Check how many bytes are available to read.
void end()
Close serial communication.
int read()
Read next byte from serial input.
size_t readBytes(u8 *buffer, size_t length)
Read fixed number of bytes into buffer.
Arduino-compatible Serial class for cross-platform serial I/O.
Definition serial.h:38
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
unsigned char u8
Definition stdint.h:131
constexpr int type_rank< T >::value
SerialPort fl_serial
int snprintf(char *buffer, fl::size size, const char *format, const Args &... args) FL_NOEXCEPT
Snprintf-like formatting function that writes to a buffer.
Definition stdio.h:666
fl::string format(const char *fmt)
Format with no arguments.
Definition format.h:439
Base definition for an LED controller.
Definition crgb.hpp:179
corkscrew_args args
Definition old.h:149
#define FL_MAYBE_UNUSED