Handling MQTT commands formatted like HTTP requests.

This commit is contained in:
Fabian Schlenz 2021-01-08 16:20:05 +01:00
parent 25b5891f98
commit 31157453cb
3 changed files with 68 additions and 16 deletions

View File

@ -13,16 +13,20 @@ extern std::list<Edge*> edges;
extern std::vector<Corner*> corners; extern std::vector<Corner*> corners;
extern CRGB leds[LED_COUNT]; extern CRGB leds[LED_COUNT];
extern CRGB color;
enum AnimationMode { enum AnimationMode {
AM_CORNERS, AM_CORNERS,
AM_FIRST_CORNER, AM_FIRST_CORNER,
AM_NODES, AM_NODES,
AM_FIRST_NODE, AM_FIRST_NODE,
AM_FLASH AM_FLASH,
AM_OFF,
AM_ERROR,
AM_NONE
}; };
extern AnimationMode mode; extern AnimationMode mode;
extern AnimationMode temp_mode; extern AnimationMode temp_mode;
extern unsigned long temp_mode_until; extern unsigned long temp_mode_until;
extern int return_to_brightness;

View File

@ -14,10 +14,12 @@ std::list<Edge*> edges;
std::vector<Corner*> corners; std::vector<Corner*> corners;
CRGB leds[LED_COUNT]; CRGB leds[LED_COUNT];
CRGB color = CRGB::Pink;
AnimationMode mode = AM_CORNERS; AnimationMode mode = AM_CORNERS;
AnimationMode temp_mode; AnimationMode temp_mode;
unsigned long temp_mode_until; unsigned long temp_mode_until;
int return_to_brightness = -1;
unsigned long last_loop = 0; unsigned long last_loop = 0;
@ -136,6 +138,10 @@ void loop() {
active_mode = temp_mode; active_mode = temp_mode;
} else { } else {
temp_mode_until = 0; temp_mode_until = 0;
if (return_to_brightness != -1) {
FastLED.setBrightness(return_to_brightness);
return_to_brightness = -1;
}
} }
} }
@ -180,8 +186,18 @@ void loop() {
} }
if (millis() / 1000 > last_loop / 1000) { if (millis() / 1000 > last_loop / 1000) {
nodes[0]->blend_to(((millis() / 1000) % 2 == 0) ? CRGB::Black : CRGB::Orange, 0, 64); nodes[0]->blend_to(((millis() / 1000) % 2 == 0) ? CRGB::Black : color, 0, 64);
} }
} else if (active_mode == AM_OFF) {
for(Node* node : nodes) {
node->set_color(CRGB::Black);
}
} else { // This includes AM_ERROR
for(Node* node : nodes) {
node->set_color(CRGB::Black);
}
nodes[0]->set_color(CRGB::Red);
} }
last_loop = millis(); last_loop = millis();

View File

@ -20,21 +20,53 @@ LOGln("Connecting to MQTT broker...");
void callback(char* topic, byte* pl, unsigned int length) { void callback(char* topic, byte* pl, unsigned int length) {
pl[length] = 0; pl[length] = 0;
String payload((char*)pl); String payload((char*)pl);
// Syntax: key=value&key2=value2...
uint16_t duration = 0; uint16_t duration = 0;
int cp = payload.indexOf(","); AnimationMode new_mode = AM_NONE;
if (cp != -1) { String current_part;
duration = payload.substring(cp+1).toInt(); while (payload.length() > 0) {
payload = payload.substring(0, cp); int offset = payload.indexOf("&");
if (offset != -1) {
current_part = payload.substring(0, offset);
payload = payload.substring(offset + 1);
} }
AnimationMode am; offset = current_part.indexOf("=");
if (payload.equals("corners")) am = AM_CORNERS; if (offset==-1) continue;
else if (payload.equals("nodes")) am = AM_NODES; String key = current_part.substring(0, offset);
String value = current_part.substring(offset+1);
if (key.equals("mode")) {
if (value.equals("nodes")) { new_mode = AM_NODES; }
else if (value.equals("first_node")) { new_mode = AM_FIRST_NODE; }
else if (value.equals("corners")) { new_mode = AM_CORNERS; }
else if (value.equals("first_corner")) { new_mode = AM_FIRST_CORNER; }
else if (value.equals("off")) { new_mode = AM_OFF; }
else if (value.equals("flash")) { new_mode = AM_FLASH; }
else { LOGln("Unknown mode '%s'.", value.c_str()); }
} else if (key.equals("duration")) {
duration = value.toInt();
} else if (key.equals("brightness")) {
if (temp_mode_until == 0) {
return_to_brightness = FastLED.getBrightness();
}
FastLED.setBrightness(value.toInt());
} else if (key.equals("color")) {
if (value.equals("red")) { color = CRGB::Red; }
else if (value.equals("green")) { color = CRGB::Green; }
else if (value.equals("blue")) { color = CRGB::Blue; }
else { LOGln("Unknown color name %s.", value.c_str());}
} else {
LOGln("Unknown key '%s'. (For reference: Value is '%s'.)", key.c_str(), value.c_str());
}
}
if (new_mode != AM_NONE) {
if (duration > 0) { if (duration > 0) {
temp_mode = am; temp_mode = new_mode;
temp_mode_until = millis() + duration*1000; temp_mode_until = millis() + duration*1000;
} else { } else {
mode = am; mode = new_mode;
}
} }
} }