Browse Source

switch to my own FastLED fork

main
Hendrik Langer 7 years ago
parent
commit
21d9dbc130
  1. 4
      software/platformio.ini
  2. 41
      software/src/MyMQTT.cpp
  3. 13
      software/src/led.cpp
  4. 3
      software/src/led.h

4
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

41
software/src/MyMQTT.cpp

@ -1,15 +1,18 @@
#include "MyMQTT.h"
#include <AsyncMqttClient.h>
#include <ArduinoJson.h>
#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,20 +62,28 @@ 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);
}
}
}
@ -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");
}

13
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<LED_TYPE, LED_PIN, COLOR_ORDER>(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--;

3
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 */

Loading…
Cancel
Save