Handling MQTT commands formatted like HTTP requests.
This commit is contained in:
parent
25b5891f98
commit
31157453cb
@ -13,16 +13,20 @@ extern std::list<Edge*> edges;
|
||||
extern std::vector<Corner*> corners;
|
||||
|
||||
extern CRGB leds[LED_COUNT];
|
||||
extern CRGB color;
|
||||
|
||||
enum AnimationMode {
|
||||
AM_CORNERS,
|
||||
AM_FIRST_CORNER,
|
||||
AM_NODES,
|
||||
AM_FIRST_NODE,
|
||||
AM_FLASH
|
||||
AM_FLASH,
|
||||
AM_OFF,
|
||||
AM_ERROR,
|
||||
AM_NONE
|
||||
};
|
||||
|
||||
extern AnimationMode mode;
|
||||
extern AnimationMode temp_mode;
|
||||
extern unsigned long temp_mode_until;
|
||||
|
||||
extern int return_to_brightness;
|
||||
|
18
src/main.cpp
18
src/main.cpp
@ -14,10 +14,12 @@ std::list<Edge*> edges;
|
||||
std::vector<Corner*> corners;
|
||||
|
||||
CRGB leds[LED_COUNT];
|
||||
CRGB color = CRGB::Pink;
|
||||
|
||||
AnimationMode mode = AM_CORNERS;
|
||||
AnimationMode temp_mode;
|
||||
unsigned long temp_mode_until;
|
||||
int return_to_brightness = -1;
|
||||
|
||||
unsigned long last_loop = 0;
|
||||
|
||||
@ -136,6 +138,10 @@ void loop() {
|
||||
active_mode = temp_mode;
|
||||
} else {
|
||||
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) {
|
||||
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();
|
||||
|
58
src/mqtt.cpp
58
src/mqtt.cpp
@ -20,21 +20,53 @@ LOGln("Connecting to MQTT broker...");
|
||||
void callback(char* topic, byte* pl, unsigned int length) {
|
||||
pl[length] = 0;
|
||||
String payload((char*)pl);
|
||||
// Syntax: key=value&key2=value2...
|
||||
uint16_t duration = 0;
|
||||
int cp = payload.indexOf(",");
|
||||
if (cp != -1) {
|
||||
duration = payload.substring(cp+1).toInt();
|
||||
payload = payload.substring(0, cp);
|
||||
}
|
||||
AnimationMode am;
|
||||
if (payload.equals("corners")) am = AM_CORNERS;
|
||||
else if (payload.equals("nodes")) am = AM_NODES;
|
||||
AnimationMode new_mode = AM_NONE;
|
||||
String current_part;
|
||||
while (payload.length() > 0) {
|
||||
int offset = payload.indexOf("&");
|
||||
if (offset != -1) {
|
||||
current_part = payload.substring(0, offset);
|
||||
payload = payload.substring(offset + 1);
|
||||
}
|
||||
offset = current_part.indexOf("=");
|
||||
if (offset==-1) continue;
|
||||
String key = current_part.substring(0, offset);
|
||||
String value = current_part.substring(offset+1);
|
||||
|
||||
if (duration > 0) {
|
||||
temp_mode = am;
|
||||
temp_mode_until = millis() + duration*1000;
|
||||
} else {
|
||||
mode = am;
|
||||
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) {
|
||||
temp_mode = new_mode;
|
||||
temp_mode_until = millis() + duration*1000;
|
||||
} else {
|
||||
mode = new_mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user