FastLED 3.10.8
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 56 of file Remote.ino.

56 {
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 {
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 {
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}
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.
fl::unique_ptr< fl::Remote > remote
Definition RpcClient.ino:43
fl::size size() const FL_NO_EXCEPT
fl::string to_string() const FL_NO_EXCEPT
Definition json.h:836
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
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
#define Serial
Definition serial.h:309

References brightness, DATA_PIN, FastLED, fill_solid(), FL_DBG, GRB, leds, NUM_LEDS, fl::json::object(), remote, Serial, fl::basic_string::size(), and fl::json::to_string().

+ Here is the call graph for this function: