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

◆ setup()

void setup ( )

Definition at line 55 of file Remote.ino.

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

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: