FastLED 3.10.8
Loading...
Searching...
No Matches
Remote.ino
Go to the documentation of this file.
1// @filter: (memory is large)
2
23
24#include <FastLED.h>
25#include "fl/remote/remote.h" // opt-in: no longer pulled in by FastLED.h
26
27#define NUM_LEDS 10
28#define DATA_PIN 3
29
31
32// Request/Response queues for callback-based I/O
35
36// Remote with callback-based I/O
38 // RequestSource: pull from queue
40 if (requestQueue.empty()) {
41 return fl::nullopt;
42 }
43 auto req = fl::move(requestQueue[0]);
44 requestQueue.erase(requestQueue.begin());
45 return req;
46 },
47 // ResponseSink: push to queue
48 [](const fl::json& response) {
49 responseQueue.push_back(response);
50 }
51);
52
53// Forward declaration
55
56void setup() {
57 Serial.begin(115200);
58 while (!Serial) {
59 ; // Wait for serial port to connect (needed for some boards)
60 }
61
62 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
63
64 // Register RPC functions (commands - no return value)
65 remote.bind("setLed", [](int index, int r, int g, int b) {
66 if (index >= 0 && index < NUM_LEDS) {
67 leds[index] = CRGB(r, g, b);
68 FL_DBG("Set LED " << index << " to RGB(" << r << ", " << g << ", " << b << ")");
69 }
70 });
71
72 remote.bind("fill", [](int r, int g, int b) {
73 fill_solid(leds, NUM_LEDS, CRGB(r, g, b));
74 FL_DBG("Filled all LEDs with RGB(" << r << ", " << g << ", " << b << ")");
75 });
76
77 remote.bind("setBrightness", [](int brightness) {
78 FastLED.setBrightness(brightness);
79 FL_DBG("Set brightness to " << brightness);
80 });
81
82 // Register RPC query functions (with return values)
83 // Match Arduino function names for consistency
84 remote.bind("millis", []() -> int64_t {
85 return static_cast<int64_t>(millis());
86 });
87
88 remote.bind("micros", []() -> int64_t {
89 return static_cast<int64_t>(micros());
90 });
91
92 remote.bind("getStatus", []() -> fl::json {
93 fl::json result = fl::json::object();
94 result.set("numLeds", NUM_LEDS);
95 result.set("brightness", FastLED.getBrightness());
96 result.set("millis", static_cast<int64_t>(millis()));
97 return result;
98 });
99
100 remote.bind("getLed", [](int index) -> fl::json {
101 fl::json result = fl::json::object();
102 if (index >= 0 && index < NUM_LEDS) {
103 result.set("r", leds[index].r);
104 result.set("g", leds[index].g);
105 result.set("b", leds[index].b);
106 } else {
107 result.set("error", "Index out of range");
108 }
109 return result;
110 });
111
112 Serial.println("Remote RPC example ready");
113 Serial.println();
114
115 // Show schema (flat tuple format optimized for low-memory devices)
116 Serial.println("=== RPC Schema ===");
117 fl::json schema = remote.schema();
118 fl::string schemaStr = schema.to_string();
119
120 Serial.print("Schema size: ");
121 Serial.print(schemaStr.size());
122 Serial.println(" bytes (flat tuple format)");
123 Serial.println();
124
125 Serial.println("Send JSON over serial, e.g.:");
126 Serial.println(" Commands (no return):");
127 Serial.println(R"( {"function":"fill","args":[255,0,0]})");
128 Serial.println(R"( {"timestamp":5000,"function":"setBrightness","args":[64]})");
129 Serial.println(" Queries (with return):");
130 Serial.println(R"( {"function":"millis","args":[]})");
131 Serial.println(R"( {"function":"getStatus","args":[]})");
132 Serial.println(R"( {"function":"getLed","args":[0]})");
133 Serial.println(" Schema:");
134 Serial.println(R"raw( {"method":"rpc.discover","id":1})raw" " // Get RPC schema");
135}
136
137void loop() {
138 // Check for incoming JSON RPC via serial and queue it
139 if (Serial.available()) {
140 fl::string jsonRpc = readSerialJson();
141
142 // Parse JSON and queue the request
143 fl::json doc = fl::json::parse(jsonRpc);
144 requestQueue.push_back(doc);
145 }
146
147 // Process all queued requests: pull + tick + push
148 remote.update(millis());
149
150 // Drain response queue and output to serial
151 while (!responseQueue.empty()) {
152 const auto& response = responseQueue[0];
153 Serial.println(response.to_string().c_str());
154 responseQueue.erase(responseQueue.begin());
155 }
156
157 FastLED.show();
158 delay(10);
159}
160
162 fl::string result;
163 while (Serial.available()) {
164 char c = Serial.read();
165 if (c == '\n' || c == '\r') break;
166 result += c;
167 }
168 return result;
169}
CRGB leds[1]
#define NUM_LEDS
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
#define DATA_PIN
Definition ClientReal.h:82
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
void setup()
Definition Remote.ino:56
fl::string readSerialJson()
Definition Remote.ino:161
fl::vector< fl::json > requestQueue
Definition Remote.ino:33
fl::Remote remote([]() -> fl::optional< fl::json > { if(requestQueue.empty()) { return fl::nullopt;} auto req=fl::move(requestQueue[0]);requestQueue.erase(requestQueue.begin());return req;}, [](const fl::json &response) { responseQueue.push_back(response);})
fl::vector< fl::json > responseQueue
Definition Remote.ino:34
void loop()
Definition Remote.ino:137
fl::unique_ptr< fl::Remote > remote
Definition RpcClient.ino:43
JSON-RPC server with scheduling support.
Definition remote.h:42
fl::size size() const FL_NO_EXCEPT
fl::string to_string() const FL_NO_EXCEPT
Definition json.h:836
static json parse(const fl::string &txt) FL_NO_EXCEPT
Definition json.h:842
static json object() FL_NO_EXCEPT
Definition json.h:857
void fill_solid(CRGB *targetArray, int numToFill, const CRGB &color) FL_NO_EXCEPT
Fill a range of LEDs with a solid color.
Definition fill.cpp.hpp:10
constexpr EOrder GRB
Definition eorder.h:19
fl::CRGB CRGB
Definition crgb.h:25
#define FL_DBG
Definition log.h:553
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:30
Optional< T > optional
Definition optional.h:16
constexpr nullopt_t nullopt
Definition optional.h:13
#define Serial
Definition serial.h:309