Move from slow JSON to much quicker byte transfer

This commit is contained in:
Robert Marshall 2021-12-22 10:22:37 +00:00
parent abe4353475
commit 1fef7427a5
6 changed files with 64 additions and 68 deletions

View file

@ -29,5 +29,3 @@ monitor_port = /dev/ttyUSB0
; monitor_speed = 115200
; monitor_port = /dev/ttyUSB0
lib_deps =
bblanchon/ArduinoJson @ ^6.18.3

View file

@ -1,20 +1,20 @@
#include "JsonLightControl.cpp"
#include "PayloadLightControl.cpp"
#include <BLEUtils.h>
class BluetoothLEDCallback : public BLECharacteristicCallbacks {
JsonLightControl *_lightControl;
std::string _json;
PayloadLightControl *_lightControl;
std::string _payload;
void onWrite(BLECharacteristic *characteristic) {
_json = characteristic->getValue();
_lightControl->action(_json);
characteristic->setValue(_json);
_payload = characteristic->getValue();
_lightControl->action(_payload);
characteristic->setValue(_payload);
characteristic->indicate();
}
public:
BluetoothLEDCallback(JsonLightControl *lightControl){
BluetoothLEDCallback(PayloadLightControl *lightControl){
_lightControl = lightControl;
}
};

View file

@ -1,52 +0,0 @@
#include "LED.cpp"
#include "TimerManager.cpp"
#include <map>
#include <ArduinoJson.h>
class JsonLightControl{
std::map<std::string, LED*> _leds;
TimerManager *_timers;
public:
JsonLightControl(TimerManager *timers){
_timers = timers;
}
void registerLEDs(LED *leds){
_leds[leds->getName()] = leds;
}
void action(std::string input){
if (input.length() <= 0)
return;
StaticJsonDocument<1024> document;
deserializeJson(document, input.c_str());
auto lights = document.as<JsonArray>();
for (int i = 0; i < lights.size(); i++)
{
auto light = lights[i];
auto name = light["name"].as<std::string>();
if (!_leds.count(name))
continue;
auto led = _leds.find(name)->second;
auto brightness = light["brightness"].as<float>();
led->setBrightness(brightness);
auto on = light["on"].as<bool>();
if (on)
led->on();
else
led->off();
auto timer = light["timer"].as<unsigned long>();
if (timer > 0)
_timers->reset(name, timer);
}
}
};

View file

@ -21,6 +21,7 @@ public:
void attach(unsigned int pin){
ledcAttachPin(pin, _channel);
}
void writeFraction(float fraction){
auto value = fraction * LED_OUTPUT_PWM_RANGE;
if (_invert)

View file

@ -0,0 +1,49 @@
#include "LED.cpp"
#include "TimerManager.cpp"
#include <map>
class PayloadLightControl{
std::map<int, LED*> _leds;
TimerManager *_timers;
unsigned long convertBytesToUnsignedLong(byte byte1, byte byte2, byte byte3, byte byte4) {
return (unsigned long)byte1 << 24 | (unsigned long)byte2 << 16 | (unsigned long)byte3 << 8 | (unsigned long)byte4;
}
public:
PayloadLightControl(TimerManager *timers){
_timers = timers;
}
void registerLEDs(int id, LED *leds){
_leds[id] = leds;
}
void action(std::string input){
if (input.length() <= 0)
return;
auto data = input.data();
for (int i = 0; i < input.length(); i += 10) {
byte name = data[i];
bool on = data[i + 1] == 1;
float brightness = convertBytesToUnsignedLong(data[i + 2], data[i + 3], data[i + 4], data[i + 5])/100.0f;
unsigned long timer = convertBytesToUnsignedLong(data[i + 6], data[i + 7], data[i + 8], data[i + 9]);
if (!_leds.count(name)) continue;
auto led = _leds.find(name)->second;
led->setBrightness(brightness);
if (on)
led->on();
else
led->off();
if (timer > 0)
_timers->reset(led->getName(), timer);
}
}
};

View file

@ -20,8 +20,8 @@ LEDOutput _cabLedOutput(4);
LED _cabLeds("cab", &_cabLedOutput, FADE_IN_DURATION, FADE_OUT_DURATION);
TimerManager _timers;
JsonLightControl _jsonLightControl(&_timers);
BluetoothLEDCallback _btCallback(&_jsonLightControl);
PayloadLightControl _payloadLightControl(&_timers);
BluetoothLEDCallback _btCallback(&_payloadLightControl);
BluetoothService _btService("Van Lights", &_btCallback);
void setup() {
@ -46,11 +46,11 @@ void setup() {
_timers.add(_awningLeds.getName(), []() { _awningLeds.off(); });
_timers.add(_cabLeds.getName(), []() { _cabLeds.off(); });
_jsonLightControl.registerLEDs(&_seatingLeds);
_jsonLightControl.registerLEDs(&_kitchenLeds);
_jsonLightControl.registerLEDs(&_bathroomLeds);
_jsonLightControl.registerLEDs(&_awningLeds);
_jsonLightControl.registerLEDs(&_cabLeds);
_payloadLightControl.registerLEDs(1, &_seatingLeds);
_payloadLightControl.registerLEDs(2, &_kitchenLeds);
_payloadLightControl.registerLEDs(3, &_bathroomLeds);
_payloadLightControl.registerLEDs(4, &_awningLeds);
_payloadLightControl.registerLEDs(5, &_cabLeds);
_btService.init();
_btService.start();