diff --git a/platformio.ini b/platformio.ini index c286675..a78dd55 100644 --- a/platformio.ini +++ b/platformio.ini @@ -28,3 +28,6 @@ monitor_port = /dev/ttyUSB0 ; upload_port = /dev/ttyUSB0 ; monitor_speed = 115200 ; monitor_port = /dev/ttyUSB0 + +lib_deps = + bblanchon/ArduinoJson @ ^6.18.3 diff --git a/src/BluetoothLEDCallback.cpp b/src/BluetoothLEDCallback.cpp index 0b8a495..7cfc8e7 100644 --- a/src/BluetoothLEDCallback.cpp +++ b/src/BluetoothLEDCallback.cpp @@ -1,36 +1,17 @@ -#include "LED.cpp" +#include "JsonLightControl.cpp" #include class BluetoothLEDCallback : public BLECharacteristicCallbacks { - LED *_seatingLeds, *_kitchenLeds; + JsonLightControl *_lightControl; void onWrite(BLECharacteristic *characteristic) { std::string value = characteristic->getValue(); - if (value.length() > 0) { - Serial.println(value.c_str()); - if (value == "all on"){ - _seatingLeds->on(); - _kitchenLeds->on(); - } - if (value == "all off"){ - _seatingLeds->off(); - _kitchenLeds->off(); - } - if (value == "seating on") - _seatingLeds->on(); - if (value == "kitchen on") - _kitchenLeds->on(); - if (value == "seating off") - _seatingLeds->off(); - if (value == "kitchen off") - _kitchenLeds->off(); - } + _lightControl->action(value); } public: - BluetoothLEDCallback(LED *seatingLeds, LED *kitchenLeds){ - _seatingLeds = seatingLeds; - _kitchenLeds = kitchenLeds; + BluetoothLEDCallback(JsonLightControl *lightControl){ + _lightControl = lightControl; } }; diff --git a/src/JsonLightControl.cpp b/src/JsonLightControl.cpp new file mode 100644 index 0000000..d744aa9 --- /dev/null +++ b/src/JsonLightControl.cpp @@ -0,0 +1,35 @@ +#include "LED.cpp" +#include +#include + +class JsonLightControl{ + std::map _leds; + +public: + void registerLEDs(const char *name, LED *leds){ + _leds[name] = leds; + } + + void action(std::string input){ + if (input.length() <= 0) + return; + + Serial.println(input.c_str()); + + DynamicJsonDocument document(256); + deserializeJson(document, input.c_str()); + + auto lights = document["lights"]; + Serial.println(lights.size()); + auto on = document["on"].as(); + for (int i = 0; i < lights.size(); i++) + { + auto name = lights[i].as(); + auto led = _leds.find(name)->second; + if (on) + led->on(); + else + led->off(); + } + } +}; diff --git a/src/main.cpp b/src/main.cpp index 414a552..4eadebf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,8 @@ LED _seatingLeds(&_seatingLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION); LEDOutput _kitchenLedOutput(1 , true); LED _kitchenLeds(&_kitchenLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION); -BluetoothLEDCallback _btCallback(&_seatingLeds, &_kitchenLeds); +JsonLightControl _jsonLightControl; +BluetoothLEDCallback _btCallback(&_jsonLightControl); BluetoothService _btService("Van Lights", &_btCallback); void setup() { @@ -23,6 +24,9 @@ void setup() { _kitchenLedOutput.attach(18); _kitchenLedOutput.attach(23); + _jsonLightControl.registerLEDs("seating", &_seatingLeds); + _jsonLightControl.registerLEDs("kitchen", &_kitchenLeds); + _btService.init(); _btService.start(); }