FastLED 3.9.15
Loading...
Searching...
No Matches
Loopback.ino
Go to the documentation of this file.
1
28
29#include <FastLED.h>
31#include "LoopbackTestRunner.h"
32
33// LED Configuration
34#define NUM_LEDS 10
35#define DATA_PIN 2
36
40
41// Main state machine
49
51uint32_t startup_time = 0;
52
53// LED visual feedback based on current state
54void updateLEDs() {
55 switch (state) {
56 case SERVER_STARTING:
57 // Blue pulse while server initializes
59 break;
60
62 case TESTS_RUNNING:
63 // Yellow during test execution
64 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 64, 0));
65 break;
66
67 case ALL_PASSED:
68 // Green when all tests pass
69 fill_solid(leds, NUM_LEDS, fl::CRGB(0, 64, 0));
70 break;
71
72 case FAILED:
73 // Red when tests fail
74 fill_solid(leds, NUM_LEDS, fl::CRGB(64, 0, 0));
75 break;
76 }
77}
78
79void setup() {
80 Serial.begin(115200);
81 Serial.println("HTTP Server Loopback Test");
82
83 // Initialize LED strip
84 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
85 FastLED.setBrightness(64);
86
87 // Register HTTP server routes inline (clear and simple)
88 server.get("/", [](const fl::http_request& req) {
89 return fl::http_response::ok("Hello from loopback test!\n");
90 });
91
92 server.get("/ping", [](const fl::http_request& req) {
93 return fl::http_response::ok("pong\n");
94 });
95
96 server.get("/test", [](const fl::http_request& req) {
97 return fl::http_response::ok("test response\n");
98 });
99
100 // Start HTTP server
101 if (server.start(8080)) {
102 Serial.println("Server started on http://localhost:8080/");
105 } else {
106 Serial.println("ERROR: Failed to start server");
107 Serial.print("Error: ");
108 Serial.println(server.last_error().c_str());
109 state = FAILED;
110 }
111
112 updateLEDs();
113 FastLED.show();
114}
115
116void loop() {
117 // Pump async task queue (async fetch tasks + server)
119
120 // State: WAITING_FOR_TESTS -> wait 1 second after startup -> start tests
121 if (state == WAITING_FOR_TESTS && (fl::millis() - startup_time > 1000)) {
122 // Start test sequence with completion callback
123 testRunner.startTests([](bool success, int passed, int total) {
124 // Called when all tests complete
125 state = success ? ALL_PASSED : FAILED;
126 server.stop();
127 });
129 }
130
131 // Update test runner state machine (advances tests, processes callbacks)
132 if (state == TESTS_RUNNING) {
133 testRunner.update();
134 }
135
136
137 if (state == ALL_PASSED || state == FAILED) {
138 updateLEDs();
139 FastLED.show();
140 delay(100);
141 return; // Stay in terminal state, let the framework break from this test.
142 }
143
144 // Update LED feedback and show
145 updateLEDs();
146 FastLED.show();
147
148 // Small delay allows async promise callbacks to process
149 // On WASM, delay() automatically pumps async tasks every 1ms
150 delay(10);
151}
#define NUM_LEDS
fl::CRGB leds[NUM_LEDS]
#define DATA_PIN
Definition ClientReal.h:82
fl::HttpServer server
@ SERVER_STARTING
TestState state
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
LoopbackTestRunner testRunner
Definition Loopback.ino:39
void updateLEDs()
Definition Loopback.ino:54
void setup()
Definition Loopback.ino:79
AppState
Definition Loopback.ino:42
@ WAITING_FOR_TESTS
Definition Loopback.ino:44
@ TESTS_RUNNING
Definition Loopback.ino:45
uint32_t startup_time
Definition Loopback.ino:51
void loop()
Definition Loopback.ino:116
Non-blocking HTTP test runner for FastLED loopback testing.
static Response ok(const string &body="")
Factory method for 200 OK response.
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
LIB8STATIC u8 beatsin8(accum88 beats_per_minute, u8 lowest=0, u8 highest=255, u32 timebase=0, u8 phase_offset=0) FL_NOEXCEPT
Generates an 8-bit sine wave at a given BPM that oscillates within a given range.
Definition beat.h:105
void run(fl::u32 microseconds, ExecFlags flags)
Run selected task subsystems.
asio::http::Request http_request
Definition server.h:280
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
asio::http::Server HttpServer
Definition server.h:279
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
#define Serial
Definition serial.h:304