FastLED 3.9.15
Loading...
Searching...
No Matches

◆ createSerialRequestSource()

fl::function< fl::optional< fl::json >()> fl::createSerialRequestSource ( const char * prefix = "")
inline

Create a JSON-RPC RequestSource that reads from fl:: serial input.

Parameters
prefixOptional prefix to strip from input lines (default: "")
Returns
RequestSource callback suitable for fl::Remote constructor
Note
Composes transport layer (serial I/O) with protocol layer (JSON-RPC normalization)

Example:

auto requestSource = fl::createSerialRequestSource();
auto responseSink = fl::createSerialResponseSink("REMOTE: ");
fl::Remote remote(requestSource, responseSink);
fl::unique_ptr< fl::Remote > remote
Definition RpcClient.ino:43
JSON-RPC server with scheduling support.
Definition remote.h:40
fl::function< fl::optional< fl::json >()> createSerialRequestSource(const char *prefix="")
Create a JSON-RPC RequestSource that reads from fl:: serial input.
Definition serial.h:109
fl::function< void(const fl::json &)> createSerialResponseSink(const char *prefix="REMOTE: ")
Create a JSON-RPC ResponseSink that writes to fl:: serial output.
Definition serial.h:162

Definition at line 109 of file serial.h.

109 {
110 return [prefix]() -> fl::optional<fl::json> {
111 // Non-blocking check - any data available?
112 int avail = fl::available();
113 if (avail <= 0) {
114 return fl::nullopt;
115 }
116
117 // Data available - read a complete line.
118 // On Arduino platforms, fl::readLine() delegates to Serial.readStringUntil()
119 // which uses yield() (immediate context switch) for fast USB CDC multi-packet
120 // assembly. On other platforms, falls back to character-by-character reading.
121 auto line = fl::readLine('\n', '\r', fl::optional<u32>(1000));
122 if (!line.has_value() || line->empty()) {
123 return fl::nullopt;
124 }
125
126 // Use string_view for zero-copy prefix stripping and trimming
127 fl::string_view view = *line;
128
129 // Strip prefix if present
130 if (prefix && prefix[0] != '\0') {
131 if (view.starts_with(prefix)) {
132 view.remove_prefix(fl::strlen(prefix));
133 }
134 }
135
136 // Trim leading whitespace
137 while (!view.empty() && fl::isspace(view.front())) {
138 view.remove_prefix(1);
139 }
140
141 // Trim trailing whitespace
142 while (!view.empty() && fl::isspace(view.back())) {
143 view.remove_suffix(1);
144 }
145
146 // Only parse if input starts with '{'
147 if (view.empty() || view[0] != '{') {
148 return fl::nullopt;
149 }
150
151 // Single copy when parsing JSON (unavoidable - JSON needs owned string)
152 fl::string input(view);
153 return fl::json::parse(input);
154 };
155}
static json parse(const fl::string &txt) FL_NOEXCEPT
Definition json.h:677
constexpr bool empty() const FL_NOEXCEPT
void remove_suffix(fl::size n) FL_NOEXCEPT
constexpr const char & front() const FL_NOEXCEPT
Definition string_view.h:86
bool starts_with(string_view sv) const FL_NOEXCEPT
constexpr const char & back() const FL_NOEXCEPT
Definition string_view.h:90
void remove_prefix(fl::size n) FL_NOEXCEPT
int available()
size_t strlen(const char *s) FL_NOEXCEPT
Optional< T > optional
Definition optional.h:16
constexpr nullopt_t nullopt
Definition optional.h:13
bool isspace(char c) FL_NOEXCEPT
Check if character is whitespace (space, tab, newline, carriage return)
Definition cctype.h:18
fl::optional< fl::string > readLine(char delimiter, char skipChar, fl::optional< u32 > timeoutMs)

References available(), fl::string_view::back(), fl::string_view::empty(), fl::string_view::front(), isspace(), nullopt, fl::json::parse(), readLine(), fl::string_view::remove_prefix(), fl::string_view::remove_suffix(), fl::string_view::starts_with(), and strlen().

Referenced by autoResearchLowMemorySetup(), and createSerialTransport().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: