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