FastLED 3.9.15
Loading...
Searching...
No Matches
io.cpp
Go to the documentation of this file.
1#include "io.h"
2
3#include "fl/stdint.h"
4
5// Platform-specific I/O implementations
6#ifdef __EMSCRIPTEN__
7#include "platforms/wasm/io_wasm.h"
8#elif defined(FASTLED_TESTING) || defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
9#include "platforms/io_native.h"
10#elif defined(ESP32) || defined(ESP8266)
11#include "platforms/esp/io_esp.h"
12#elif defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)
13#include "platforms/avr/io_avr.h"
14#elif defined(__MKL26Z64__)
15// Teensy LC has special handling to avoid _write linker issues
16#include "platforms/io_teensy_lc.h"
17#elif defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__) || defined(__MK20DX128__)
18// All other Teensy platforms use lightweight implementation to avoid Serial library bloat
19#include "platforms/io_teensy.h"
20#else
21#include "platforms/io_arduino.h"
22#endif
23
24namespace fl {
25
26#ifdef FASTLED_TESTING
27// Static storage for injected handlers using lazy initialization to avoid global constructors
28static print_handler_t& get_print_handler() {
29 static print_handler_t handler;
30 return handler;
31}
32
33static println_handler_t& get_println_handler() {
34 static println_handler_t handler;
35 return handler;
36}
37
38static available_handler_t& get_available_handler() {
39 static available_handler_t handler;
40 return handler;
41}
42
43static read_handler_t& get_read_handler() {
44 static read_handler_t handler;
45 return handler;
46}
47#endif
48
49void print(const char* str) {
50 if (!str) return;
51
52#ifdef FASTLED_TESTING
53 // Check for injected handler first
54 if (get_print_handler()) {
55 get_print_handler()(str);
56 return;
57 }
58#endif
59
60#ifdef __EMSCRIPTEN__
61 print_wasm(str);
62#elif defined(FASTLED_TESTING) || defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
63 print_native(str);
64#elif defined(ESP32) || defined(ESP8266)
65 print_esp(str);
66#elif defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)
67 print_avr(str);
68#elif defined(__MKL26Z64__)
69 // Teensy LC uses special no-op functions to avoid _write linker issues
70 print_teensy_lc(str);
71#elif defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__) || defined(__MK20DX128__)
72 // All other Teensy platforms use lightweight implementation
73 print_teensy(str);
74#else
75 // Use generic Arduino print for all other platforms including:
76 // - STM32 (STM32F1, STM32F4, STM32H7, ARDUINO_GIGA)
77 // - NRF (NRF52, NRF52832, NRF52840, ARDUINO_NRF52_DK)
78 // - All other Arduino-compatible platforms
79 print_arduino(str);
80#endif
81}
82
83void println(const char* str) {
84 if (!str) return;
85
86#ifdef FASTLED_TESTING
87 // Check for injected handler first
88 if (get_println_handler()) {
89 get_println_handler()(str);
90 return;
91 }
92#endif
93
94#ifdef __EMSCRIPTEN__
95 println_wasm(str);
96#elif defined(FASTLED_TESTING) || defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
97 println_native(str);
98#elif defined(ESP32) || defined(ESP8266)
99 println_esp(str);
100#elif defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)
101 println_avr(str);
102#elif defined(__MKL26Z64__)
103 // Teensy LC uses special no-op functions to avoid _write linker issues
104 println_teensy_lc(str);
105#elif defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__) || defined(__MK20DX128__)
106 // All other Teensy platforms use lightweight implementation
107 println_teensy(str);
108#else
109 // Use generic Arduino print for all other platforms including:
110 // - STM32 (STM32F1, STM32F4, STM32H7, ARDUINO_GIGA)
111 // - NRF (NRF52, NRF52832, NRF52840, ARDUINO_NRF52_DK)
112 // - All other Arduino-compatible platforms
113 println_arduino(str);
114#endif
115}
116
118#ifdef FASTLED_TESTING
119 // Check for injected handler first
120 if (get_available_handler()) {
121 return get_available_handler()();
122 }
123#endif
124
125#ifdef __EMSCRIPTEN__
126 return available_wasm();
127#elif defined(FASTLED_TESTING) || defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
128 return available_native();
129#elif defined(ESP32) || defined(ESP8266)
130 return available_esp();
131#elif defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)
132 return available_avr();
133#elif defined(__MKL26Z64__)
134 // Teensy LC uses special no-op functions to avoid _write linker issues
135 return available_teensy_lc();
136#elif defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__) || defined(__MK20DX128__)
137 // All other Teensy platforms use lightweight implementation
138 return available_teensy();
139#else
140 // Use generic Arduino input for all other platforms including:
141 // - STM32 (STM32F1, STM32F4, STM32H7, ARDUINO_GIGA)
142 // - NRF (NRF52, NRF52832, NRF52840, ARDUINO_NRF52_DK)
143 // - All other Arduino-compatible platforms
144 return available_arduino();
145#endif
146}
147
148int read() {
149#ifdef FASTLED_TESTING
150 // Check for injected handler first
151 if (get_read_handler()) {
152 return get_read_handler()();
153 }
154#endif
155
156#ifdef __EMSCRIPTEN__
157 return read_wasm();
158#elif defined(FASTLED_TESTING) || defined(__linux__) || defined(__APPLE__) || defined(_WIN32)
159 return read_native();
160#elif defined(ESP32) || defined(ESP8266)
161 return read_esp();
162#elif defined(__AVR__) && !defined(ARDUINO_ARCH_MEGAAVR)
163 return read_avr();
164#elif defined(__MKL26Z64__)
165 // Teensy LC uses special no-op functions to avoid _write linker issues
166 return read_teensy_lc();
167#elif defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__) || defined(__MK20DX128__)
168 // All other Teensy platforms use lightweight implementation
169 return read_teensy();
170#else
171 // Use generic Arduino input for all other platforms including:
172 // - STM32 (STM32F1, STM32F4, STM32H7, ARDUINO_GIGA)
173 // - NRF (NRF52, NRF52832, NRF52840, ARDUINO_NRF52_DK)
174 // - All other Arduino-compatible platforms
175 return read_arduino();
176#endif
177}
178
179#ifdef FASTLED_TESTING
180
181// Inject function handlers for testing
182void inject_print_handler(const print_handler_t& handler) {
183 get_print_handler() = handler;
184}
185
186void inject_println_handler(const println_handler_t& handler) {
187 get_println_handler() = handler;
188}
189
190void inject_available_handler(const available_handler_t& handler) {
191 get_available_handler() = handler;
192}
193
194void inject_read_handler(const read_handler_t& handler) {
195 get_read_handler() = handler;
196}
197
198// Clear all injected handlers (restores default behavior)
199void clear_io_handlers() {
200 get_print_handler() = print_handler_t{};
201 get_println_handler() = println_handler_t{};
202 get_available_handler() = available_handler_t{};
203 get_read_handler() = read_handler_t{};
204}
205
206// Clear individual handlers
207void clear_print_handler() {
208 get_print_handler() = print_handler_t{};
209}
210
211void clear_println_handler() {
212 get_println_handler() = println_handler_t{};
213}
214
215void clear_available_handler() {
216 get_available_handler() = available_handler_t{};
217}
218
219void clear_read_handler() {
220 get_read_handler() = read_handler_t{};
221}
222
223#endif // FASTLED_TESTING
224
225} // namespace fl
int available()
Definition io.cpp:117
int read()
Definition io.cpp:148
void print(const char *str)
Definition io.cpp:49
void println(const char *str)
Definition io.cpp:83
IMPORTANT!
Definition crgb.h:20