From 21d9dbc130d8da2ce57233ac657c127bc8795508 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sun, 7 Jan 2018 20:28:49 +0100 Subject: [PATCH] switch to my own FastLED fork --- software/platformio.ini | 4 +++- software/src/MyMQTT.cpp | 43 +++++++++++++++++++++++++++-------------- software/src/led.cpp | 13 ++++++++++++- software/src/led.h | 3 +++ 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/software/platformio.ini b/software/platformio.ini index 0f82361..390992e 100644 --- a/software/platformio.ini +++ b/software/platformio.ini @@ -22,7 +22,9 @@ lib_deps = ; AsyncTCP FS ; FastLED - https://github.com/samguyer/FastLED.git +; https://github.com/samguyer/FastLED.git + https://github.com/h3ndrik/FastLED.git ; AsyncMqttClient https://github.com/marvinroger/async-mqtt-client.git + ArduinoJson lib_ignore = ESPAsyncTCP diff --git a/software/src/MyMQTT.cpp b/software/src/MyMQTT.cpp index e7c81b2..79ec67d 100644 --- a/software/src/MyMQTT.cpp +++ b/software/src/MyMQTT.cpp @@ -1,15 +1,18 @@ #include "MyMQTT.h" #include +#include #include "hardware.h" #include "shelf.h" #include "wifi.h" +#include "led.h" using namespace std; extern Shelf* shelf; extern Wifi wifi; +extern Led led; MyMQTT::MyMQTT(void) : host {mqtt_server}, @@ -59,21 +62,29 @@ void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessagePro Serial.print("incoming: "); Serial.println(topic); Serial.println(payload); - if (String(payload).equals("{'num':'0'}")) { - shelf->dispense(0); - } else if (String(payload).equals("{'num':'1'}")) { - shelf->dispense(1); - } else if (String(payload).equals("{'num':'2'}")) { - shelf->dispense(2); - } else if (String(payload).equals("{'num':'3'}")) { - shelf->dispense(3); - } else if (String(payload).equals("{'num':'4'}")) { - shelf->dispense(4); - } else if (String(payload).equals("{'num':'5'}")) { - shelf->dispense(5); - } else if (String(payload).equals("{'num':'6'}")) { - shelf->dispense(6); - } + + StaticJsonBuffer<200> jsonBuffer; + JsonObject& root = jsonBuffer.parseObject(payload); + if (!root.success()) { + Serial.println("parseObject() failed"); + return; + } + if (String(topic).equals("sensor/vendingmachine/dispense")) { + if (root.containsKey("num")) { + shelf->dispense(root["num"]); + } + } else if (String(topic).equals("sensor/vendingmachine/color") || String(topic).equals("sensor/esp100/set")) { + if (root.containsKey("color")) { + uint8_t r = root["color"][0]; + uint8_t g = root["color"][1]; + uint8_t b = root["color"][2]; + led.changeColor(r,g,b); + } + if (root.containsKey("brightness")) { + uint8_t brightness = root["brightness"]; + led.changeBrightness(brightness); + } + } } void MyMQTT::onMqttConnect(bool sessionPresent) { @@ -126,6 +137,8 @@ bool MyMQTT::connected(void) { void MyMQTT::subscribe(void) { uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0); + uint16_t colorIdSub = mqttClient.subscribe("sensor/vendingmachine/color", 0); + uint16_t color2IdSub = mqttClient.subscribe("sensor/esp100/set", 0); mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test"); } diff --git a/software/src/led.cpp b/software/src/led.cpp index 386be35..ef32c77 100644 --- a/software/src/led.cpp +++ b/software/src/led.cpp @@ -11,6 +11,7 @@ Led::Led(void) { index = 0; duration = 0; change = true; + color = CRGB::Orange; } void Led::loop_rainbow(void) { @@ -41,7 +42,7 @@ void Led::loop_dispense(void) { } void Led::loop_steady(void) { - fill_solid(leds, NUM_LEDS, CRGB::Orange); + fill_solid(leds, NUM_LEDS, color); } void Led::changeAnimation(uint8_t num, uint16_t duration) { @@ -54,6 +55,15 @@ void Led::changeAnimation(uint8_t num, uint16_t duration) { else if (num == 3) refresh = &Led::loop_rainbow; } +void Led::changeColor(uint8_t r, uint8_t g, uint8_t b) { + color = CRGB(r, g, b); + change = true; +} +void Led::changeBrightness(uint8_t brightness) { + FastLED.setBrightness(brightness); + change = true; +} + void Led::setup(void) { pinMode(LED_PIN, OUTPUT); FastLED.addLeds(leds, NUM_LEDS); @@ -87,6 +97,7 @@ void Led::animationTask(void* parameters) { index++; if (duration == 0) { refresh = &Led::loop_steady; + change = true; } else if (duration == UINT16_MAX) { } else { duration--; diff --git a/software/src/led.h b/software/src/led.h index 3089103..4e64d6e 100644 --- a/software/src/led.h +++ b/software/src/led.h @@ -13,6 +13,8 @@ class Led { Led(void); void setup(void); void changeAnimation(uint8_t num, uint16_t duration); + void changeColor(uint8_t, uint8_t, uint8_t); + void changeBrightness(uint8_t); private: CRGB leds[NUM_LEDS]; void animationTask(void*); @@ -26,6 +28,7 @@ class Led { uint16_t duration; uint8_t index; bool change; + CRGB color; }; #endif /* _LED_H */