FastLED 3.10.8
Loading...
Searching...
No Matches
cstdio.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/int.h"
4#include "fl/stl/cstddef.h"
5#include "fl/stl/optional.h"
6#include "fl/stl/noexcept.h"
7// IWYU pragma: begin_keep
8#include "platforms/is_platform.h"
9// IWYU pragma: end_keep
10
11// FL_SERIAL_BEGIN_INLINE: 1 where fl::serial_begin() is header-inline (see
12// its declaration below and platforms/arduino/io_arduino.hpp).
13#if defined(FL_IS_AVR) && defined(ARDUINO)
14#define FL_SERIAL_BEGIN_INLINE 1
15// IWYU pragma: begin_keep
16#include "fl/system/arduino.h"
17// IWYU pragma: end_keep
18#else
19#define FL_SERIAL_BEGIN_INLINE 0
20#endif
21
22namespace fl {
23class sstream; // Forward declaration
24class string; // Forward declaration
25template <typename> class function; // Forward declaration for function template
26
27// =============================================================================
28// Global Log Level Control
29// =============================================================================
30// Runtime-configurable log level that can dynamically shut off logging.
31// This affects all FL_PRINT, FL_DBG, FL_WARN, FL_INFO, FL_ERROR macros
32// when they flow through fl::print/fl::println.
33
44
48
52void setLogLevel(u8 level) FL_NO_EXCEPT;
53
54// =============================================================================
55// RAII Scoped Log Control
56// =============================================================================
57
80public:
85
90
91 // Non-copyable (prevent accidental copies that would corrupt state)
94
95 // Move-only (allow transfer of ownership)
97 : mPreviousLevel(other.mPreviousLevel) {
98 // Mark other as "moved-from" by setting to current level (no-op restore)
99 other.mPreviousLevel = static_cast<u8>(LogLevel::FL_LOG_LEVEL_NONE);
100 }
102
103private:
105};
106
107// =============================================================================
108// Serial Initialization
109// =============================================================================
110// Initialize serial communication with specified baud rate
111// Note: On some platforms (host), this is a no-op
112#if FL_SERIAL_BEGIN_INLINE
113// AVR + Arduino (#4725): inline, so the strong reference to Arduino's
114// `Serial` lands in the caller's object file. FastLED's own I/O shims only
115// reference it weakly, so sketches that never begin a serial port do not
116// link the core's Serial object, ring buffers and USART ISRs.
117inline void serial_begin(u32 baudRate = 115200) FL_NO_EXCEPT {
118 ::Serial.begin(baudRate);
119}
120#else
121void serial_begin(u32 baudRate = 115200) FL_NO_EXCEPT;
122#endif
123
124// =============================================================================
125// Low-Level Print Functions
126// =============================================================================
127// These functions avoid printf/sprintf dependencies and use the most efficient
128// output method for each platform.
129
130// Print a string without newline
131void print(const char* str) FL_NO_EXCEPT;
132
133// Print a string with newline
134#ifndef FL_DBG_PRINTLN_DECLARED
135void println(const char* str) FL_NO_EXCEPT;
136#else
137// Declaration already exists from fl/dbg.h
138#endif
139
140// Write raw bytes (binary data)
141// Returns number of bytes written
142size_t write_bytes(const u8* buffer, size_t size) FL_NO_EXCEPT;
143
144// Low-level input functions that provide Serial-style read functionality
145// These use the most efficient input method for each platform
146
147// Returns the number of bytes available to read from input stream
148// Returns 0 if no data is available
150
151// Peeks at the next byte without removing it from input stream
152// Returns the byte value (0-255) if data is available
153// Returns -1 if no data is available
154// Note: Not all platforms support peek (may return -1 always)
155int peek() FL_NO_EXCEPT;
156
157// Reads the next byte from input stream
158// Returns the byte value (0-255) if data is available
159// Returns -1 if no data is available
160int read() FL_NO_EXCEPT;
161
162// Reads from input stream until delimiter is found, writing to sstream (blocking with optional timeout)
163// Returns true when delimiter is found, false only if timeout occurs
164// @param out Output sstream to append characters to (delimiter not included)
165// @param delimiter Character to read until (default: '\n')
166// @param skipChar Character to skip during reading (default: '\r' for cross-platform line endings)
167// @param timeoutMs Optional timeout in milliseconds (nullopt = wait forever)
168// @note Follows Arduino Serial.readStringUntil() API
169bool readStringUntil(sstream& out, char delimiter = '\n', char skipChar = '\r', fl::optional<u32> timeoutMs = fl::nullopt) FL_NO_EXCEPT;
170
171// Reads from input stream until delimiter is found, returning as string (blocking with optional timeout)
172// Returns the string (without delimiter) when delimiter is found
173// Returns nullopt only if timeout occurs (when timeout is set)
174// @param delimiter Character to read until (default: '\n')
175// @param skipChar Character to skip during reading (default: '\r' for cross-platform line endings)
176// @param timeoutMs Optional timeout in milliseconds (nullopt = wait forever)
177// @note Follows Arduino Serial.readStringUntil() API
178// @note Delegates to readStringUntil(sstream&, ...) version
179fl::optional<fl::string> readLine(char delimiter = '\n', char skipChar = '\r', fl::optional<u32> timeoutMs = fl::nullopt) FL_NO_EXCEPT;
180
181// Flush serial output buffer
182// Waits for all buffered data to be transmitted
183// Returns true if flush completed successfully, false on timeout
184// Note: On platforms without buffering, this returns immediately
185bool flush(u32 timeoutMs = 1000) FL_NO_EXCEPT;
186
187// Check if serial port is ready for I/O
188// Returns true if serial is initialized and available
190
191// =============================================================================
192// Timing Functions
193// =============================================================================
194// Note: fl::millis() is provided by fl/stl/chrono.h
195// Note: fl::delay() is provided by fl/system/delay.h
196
197#ifdef FASTLED_TESTING
198
199// Testing function handler types
200using print_handler_t = fl::function<void(const char*)>;
201using println_handler_t = fl::function<void(const char*)>;
202using available_handler_t = fl::function<int()>;
203using read_handler_t = fl::function<int()>;
204using flush_handler_t = fl::function<bool(u32)>;
205using write_bytes_handler_t = fl::function<size_t(const u8*, size_t)>;
206
207// Inject function handlers for testing
208void inject_print_handler(const print_handler_t& handler) FL_NO_EXCEPT;
209void inject_println_handler(const println_handler_t& handler) FL_NO_EXCEPT;
210void inject_available_handler(const available_handler_t& handler) FL_NO_EXCEPT;
211void inject_read_handler(const read_handler_t& handler) FL_NO_EXCEPT;
212void inject_flush_handler(const flush_handler_t& handler) FL_NO_EXCEPT;
213void inject_write_bytes_handler(const write_bytes_handler_t& handler) FL_NO_EXCEPT;
214
215// Clear all injected handlers (restores default behavior)
216void clear_io_handlers() FL_NO_EXCEPT;
217
218// Clear individual handlers
219void clear_print_handler() FL_NO_EXCEPT;
220void clear_println_handler() FL_NO_EXCEPT;
221void clear_available_handler() FL_NO_EXCEPT;
222void clear_read_handler() FL_NO_EXCEPT;
223void clear_flush_handler() FL_NO_EXCEPT;
224void clear_write_bytes_handler() FL_NO_EXCEPT;
225
226#endif // FASTLED_TESTING
227
228} // namespace fl
ScopedLogDisable(ScopedLogDisable &&other) FL_NO_EXCEPT
Definition cstdio.h:96
ScopedLogDisable(const ScopedLogDisable &) FL_NO_EXCEPT=delete
ScopedLogDisable() FL_NO_EXCEPT
Constructor - saves current log level and disables logging.
Definition cstdio.h:82
ScopedLogDisable & operator=(const ScopedLogDisable &) FL_NO_EXCEPT=delete
~ScopedLogDisable() FL_NO_EXCEPT
Destructor - restores previous log level.
Definition cstdio.h:87
ScopedLogDisable & operator=(ScopedLogDisable &&) FL_NO_EXCEPT=delete
unsigned char u8
Definition stdint.h:131
unsigned char u8
Definition stdint.h:131
u8 getLogLevel() FL_NO_EXCEPT
Get the current global log level.
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void print(const char *str) FL_NO_EXCEPT
bool readStringUntil(sstream &out, char delimiter, char skipChar, fl::optional< u32 > timeoutMs) FL_NO_EXCEPT
bool serial_ready() FL_NO_EXCEPT
void setLogLevel(u8 level) FL_NO_EXCEPT
Set the global log level.
fl::optional< fl::string > readLine(char delimiter, char skipChar, fl::optional< u32 > timeoutMs) FL_NO_EXCEPT
Optional< T > optional
Definition optional.h:16
size_t write_bytes(const u8 *buffer, size_t size) FL_NO_EXCEPT
void serial_begin(u32 baudRate) FL_NO_EXCEPT
int read() FL_NO_EXCEPT
LogLevel
Log level constants - higher values include more output Prefixed with FL_ to avoid macro collisions (...
Definition cstdio.h:37
@ FL_LOG_LEVEL_INFO
Errors, warnings, and info.
Definition cstdio.h:41
@ FL_LOG_LEVEL_DEBUG
All logging including debug (default)
Definition cstdio.h:42
@ FL_LOG_LEVEL_WARN
Errors and warnings.
Definition cstdio.h:40
@ FL_LOG_LEVEL_NONE
No logging (completely silent)
Definition cstdio.h:38
@ FL_LOG_LEVEL_ERROR
Only errors.
Definition cstdio.h:39
bool flush(u32 timeoutMs) FL_NO_EXCEPT
fl::size_t size_t
Definition s16x16x4.h:62
constexpr nullopt_t nullopt
Definition optional.h:13
void println(const char *str) FL_NO_EXCEPT
int available() FL_NO_EXCEPT
int peek() FL_NO_EXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT
#define Serial
Definition serial.h:309