Skip to content

Commit a8c727b

Browse files
committed
Add the "mqtt" effect, where individual leds can be set via MQTT
For example, this message set LED 0 to a dim red and LED 1 to a bright blue. The other LEDs will retain their color. {effect:"mqtt",pixels:[{n:0,r:128,g:0,b:0},{n:1,r:0,g:0,b:255]} Due to limitations in the MQTT library, you can only change a few LEDs at a time. Due to the speed limits of MQTT this method is not appropriate to create dynamic patterns, but it can be used for status displays or to try out a patterns before coding them in the firmware.
1 parent 8eaac3f commit a8c727b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

anavi-miracle-controller-sw/anavi-miracle-controller-sw.ino

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,32 @@ void convertBrightness(StaticJsonDocument<200> data, uint8_t& val)
862862
val = brightness;
863863
}
864864

865+
void convertPixels(JsonArray data, CRGB *leds, int numToFill)
866+
{
867+
for (JsonObject pixel : data)
868+
{
869+
int n = pixel["n"];
870+
CRGB color;
871+
color.r = pixel["r"];
872+
color.g = pixel["g"];
873+
color.b = pixel["b"];
874+
if (n < numToFill)
875+
{
876+
#if 0
877+
Serial.print("Setting pixel ");
878+
Serial.print(n);
879+
Serial.print(" r:");
880+
Serial.print(color.r);
881+
Serial.print(" g:");
882+
Serial.print(color.g);
883+
Serial.print(" b:");
884+
Serial.println(color.b);
885+
#endif
886+
leds[n] = color;
887+
}
888+
}
889+
}
890+
865891
void setColors(uint8_t r, uint8_t g, uint8_t b, CRGB& color, uint8_t& hue)
866892
{
867893
color.setRGB(r, g, b);
@@ -943,6 +969,12 @@ void mqttCallback(char* topic, byte* payload, unsigned int length)
943969
printLedStatus();
944970
}
945971
}
972+
973+
if (data.containsKey("pixels"))
974+
{
975+
convertPixels(data["pixels"].as<JsonArray>(), leds1, numberLed1);
976+
}
977+
946978
// Update the content on the display
947979
need_redraw = true;
948980

@@ -988,6 +1020,12 @@ void mqttCallback(char* topic, byte* payload, unsigned int length)
9881020
printLedStatus();
9891021
}
9901022
}
1023+
1024+
if (data.containsKey("pixels"))
1025+
{
1026+
convertPixels(data["pixels"].as<JsonArray>(), leds2, numberLed2);
1027+
}
1028+
9911029
// Update the content on the display
9921030
need_redraw = true;
9931031

@@ -1191,6 +1229,7 @@ bool publishLightDiscovery(int ledId)
11911229
effects.add("confetti");
11921230
effects.add("bpm");
11931231
effects.add("juggle");
1232+
effects.add("mqtt");
11941233

11951234
String deviceSuffix = machineId + String("-led") + String(ledId);
11961235

@@ -1610,6 +1649,10 @@ void processEffects(CRGB *leds, bool power, const char* effect, uint8_t hue, uin
16101649
{
16111650
juggle(leds, hue, val, numToFill);
16121651
}
1652+
else if (0 == strcmp(effect, "mqtt"))
1653+
{
1654+
// Do nothing here.
1655+
}
16131656
else
16141657
{
16151658
// Turn off LEDs if the effect is not supported

0 commit comments

Comments
 (0)