FastLED 3.9.15
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
8namespace fl {
9class sstream; // Forward declaration
10class string; // Forward declaration
11template <typename> class function; // Forward declaration for function template
12
13// =============================================================================
14// Global Log Level Control
15// =============================================================================
16// Runtime-configurable log level that can dynamically shut off logging.
17// This affects all FL_PRINT, FL_DBG, FL_WARN, FL_INFO, FL_ERROR macros
18// when they flow through fl::print/fl::println.
19
30
34
38void setLogLevel(u8 level) FL_NOEXCEPT;
39
40// =============================================================================
41// RAII Scoped Log Control
42// =============================================================================
43
66public:
71
76
77 // Non-copyable (prevent accidental copies that would corrupt state)
80
81 // Move-only (allow transfer of ownership)
83 : mPreviousLevel(other.mPreviousLevel) {
84 // Mark other as "moved-from" by setting to current level (no-op restore)
85 other.mPreviousLevel = static_cast<u8>(LogLevel::FL_LOG_LEVEL_NONE);
86 }
88
89private:
91};
92
93// =============================================================================
94// Serial Initialization
95// =============================================================================
96// Initialize serial communication with specified baud rate
97// Note: On some platforms (host), this is a no-op
98void serial_begin(u32 baudRate = 115200) FL_NOEXCEPT;
99
100// =============================================================================
101// Low-Level Print Functions
102// =============================================================================
103// These functions avoid printf/sprintf dependencies and use the most efficient
104// output method for each platform.
105
106// Print a string without newline
107void print(const char* str) FL_NOEXCEPT;
108
109// Print a string with newline
110#ifndef FL_DBG_PRINTLN_DECLARED
111void println(const char* str) FL_NOEXCEPT;
112#else
113// Declaration already exists from fl/dbg.h
114#endif
115
116// Write raw bytes (binary data)
117// Returns number of bytes written
118size_t write_bytes(const u8* buffer, size_t size) FL_NOEXCEPT;
119
120// Low-level input functions that provide Serial-style read functionality
121// These use the most efficient input method for each platform
122
123// Returns the number of bytes available to read from input stream
124// Returns 0 if no data is available
126
127// Peeks at the next byte without removing it from input stream
128// Returns the byte value (0-255) if data is available
129// Returns -1 if no data is available
130// Note: Not all platforms support peek (may return -1 always)
131int peek() FL_NOEXCEPT;
132
133// Reads the next byte from input stream
134// Returns the byte value (0-255) if data is available
135// Returns -1 if no data is available
136int read() FL_NOEXCEPT;
137
138// Reads from input stream until delimiter is found, writing to sstream (blocking with optional timeout)
139// Returns true when delimiter is found, false only if timeout occurs
140// @param out Output sstream to append characters to (delimiter not included)
141// @param delimiter Character to read until (default: '\n')
142// @param skipChar Character to skip during reading (default: '\r' for cross-platform line endings)
143// @param timeoutMs Optional timeout in milliseconds (nullopt = wait forever)
144// @note Follows Arduino Serial.readStringUntil() API
145bool readStringUntil(sstream& out, char delimiter = '\n', char skipChar = '\r', fl::optional<u32> timeoutMs = fl::nullopt) FL_NOEXCEPT;
146
147// Reads from input stream until delimiter is found, returning as string (blocking with optional timeout)
148// Returns the string (without delimiter) when delimiter is found
149// Returns nullopt only if timeout occurs (when timeout is set)
150// @param delimiter Character to read until (default: '\n')
151// @param skipChar Character to skip during reading (default: '\r' for cross-platform line endings)
152// @param timeoutMs Optional timeout in milliseconds (nullopt = wait forever)
153// @note Follows Arduino Serial.readStringUntil() API
154// @note Delegates to readStringUntil(sstream&, ...) version
155fl::optional<fl::string> readLine(char delimiter = '\n', char skipChar = '\r', fl::optional<u32> timeoutMs = fl::nullopt) FL_NOEXCEPT;
156
157// Flush serial output buffer
158// Waits for all buffered data to be transmitted
159// Returns true if flush completed successfully, false on timeout
160// Note: On platforms without buffering, this returns immediately
161bool flush(u32 timeoutMs = 1000) FL_NOEXCEPT;
162
163// Check if serial port is ready for I/O
164// Returns true if serial is initialized and available
166
167// =============================================================================
168// Timing Functions
169// =============================================================================
170// Note: fl::millis() is provided by fl/stl/chrono.h
171// Note: fl::delay() is provided by fl/system/delay.h
172
173#ifdef FASTLED_TESTING
174
175// Testing function handler types
176using print_handler_t = fl::function<void(const char*)>;
177using println_handler_t = fl::function<void(const char*)>;
178using available_handler_t = fl::function<int()>;
179using read_handler_t = fl::function<int()>;
180using flush_handler_t = fl::function<bool(u32)>;
181using write_bytes_handler_t = fl::function<size_t(const u8*, size_t)>;
182
183// Inject function handlers for testing
184void inject_print_handler(const print_handler_t& handler) FL_NOEXCEPT;
185void inject_println_handler(const println_handler_t& handler) FL_NOEXCEPT;
186void inject_available_handler(const available_handler_t& handler) FL_NOEXCEPT;
187void inject_read_handler(const read_handler_t& handler) FL_NOEXCEPT;
188void inject_flush_handler(const flush_handler_t& handler) FL_NOEXCEPT;
189void inject_write_bytes_handler(const write_bytes_handler_t& handler) FL_NOEXCEPT;
190
191// Clear all injected handlers (restores default behavior)
192void clear_io_handlers() FL_NOEXCEPT;
193
194// Clear individual handlers
195void clear_print_handler() FL_NOEXCEPT;
196void clear_println_handler() FL_NOEXCEPT;
197void clear_available_handler() FL_NOEXCEPT;
198void clear_read_handler() FL_NOEXCEPT;
199void clear_flush_handler() FL_NOEXCEPT;
200void clear_write_bytes_handler() FL_NOEXCEPT;
201
202#endif // FASTLED_TESTING
203
204} // namespace fl
ScopedLogDisable() FL_NOEXCEPT
Constructor - saves current log level and disables logging.
Definition cstdio.h:68
ScopedLogDisable(const ScopedLogDisable &) FL_NOEXCEPT=delete
ScopedLogDisable & operator=(const ScopedLogDisable &) FL_NOEXCEPT=delete
~ScopedLogDisable() FL_NOEXCEPT
Destructor - restores previous log level.
Definition cstdio.h:73
ScopedLogDisable(ScopedLogDisable &&other) FL_NOEXCEPT
Definition cstdio.h:82
ScopedLogDisable & operator=(ScopedLogDisable &&) FL_NOEXCEPT=delete
unsigned char u8
Definition stdint.h:131
int available()
unsigned char u8
Definition stdint.h:131
int read()
int peek()
void print(const char *str)
Optional< T > optional
Definition optional.h:16
void serial_begin(u32 baudRate)
LogLevel
Log level constants - higher values include more output Prefixed with FL_ to avoid macro collisions (...
Definition cstdio.h:23
@ FL_LOG_LEVEL_INFO
Errors, warnings, and info.
Definition cstdio.h:27
@ FL_LOG_LEVEL_DEBUG
All logging including debug (default)
Definition cstdio.h:28
@ FL_LOG_LEVEL_WARN
Errors and warnings.
Definition cstdio.h:26
@ FL_LOG_LEVEL_NONE
No logging (completely silent)
Definition cstdio.h:24
@ FL_LOG_LEVEL_ERROR
Only errors.
Definition cstdio.h:25
fl::size size_t
Definition s16x16x4.h:223
constexpr nullopt_t nullopt
Definition optional.h:13
bool serial_ready()
bool readStringUntil(sstream &out, char delimiter, char skipChar, fl::optional< u32 > timeoutMs)
size_t write_bytes(const u8 *buffer, size_t size)
u8 getLogLevel()
Get the current global log level.
void println(const char *str) FL_NOEXCEPT
bool flush(u32 timeoutMs)
void setLogLevel(u8 level)
Set the global log level.
fl::optional< fl::string > readLine(char delimiter, char skipChar, fl::optional< u32 > timeoutMs)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT