LedDisplay/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp

140 lines
5.2 KiB
C++
Raw Normal View History

#include "OmobiLedDisplay.h"
OmobiLedDisplay::OmobiLedDisplay(String deviceName, const byte ledPin)
{
this->ledDisplayController = new LedDisplayController(ledPin);
this->ledDisplayController->setTexts(LedDisplayController::sets_t{
{// TEXT , SHOWTIME, COLOR, ALIGNMENT, SCROLL, SCROLLNR, ACTIVE
{"itsblue", 5000, ledDisplayController->Color(0, 0, 255), LedDisplayController::TEXTCENTER, false, 1, true},
{"", 0, ledDisplayController->Color(0, 0, 255), LedDisplayController::TEXTCENTER, false, 4, false}
},
"OK"});
this->bleServer = new BluetoothLeUartServer(deviceName, "6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e");
this->bleServer->setCallbacks(this);
}
void OmobiLedDisplay::onDeviceConnectedChanged(bool deviceConnected)
{
if (deviceConnected)
Serial.println("Device connected");
}
void OmobiLedDisplay::onDataReceived(String data)
{
if (data.startsWith("GET_TEXTS"))
{
const size_t capacity = JSON_ARRAY_SIZE(LedDisplayController::text_nr_sets) + LedDisplayController::text_nr_sets * (JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(8));
DynamicJsonDocument doc(capacity);
for (int i = 0; i < LedDisplayController::text_nr_sets; i++)
{
LedDisplayController::text_set_t textSet = this->ledDisplayController->getTexts().sets[i];
JsonObject doc_0 = doc.createNestedObject();
doc_0["active"] = textSet.active;
doc_0["alignment"] = textSet.align;
JsonObject doc_0_color = doc_0.createNestedObject("color");
doc_0_color["r"] = 0;
doc_0_color["g"] = 0;
doc_0_color["b"] = 0;
doc_0["runtime"] = textSet.time_ms;
doc_0["scroll"] = textSet.text_scroll;
doc_0["scrollCount"] = textSet.text_scroll_pass;
doc_0["scrollSpeed"] = 5;
doc_0["text"] = textSet.text;
}
String json;
serializeJson(doc, json);
this->bleServer->sendData("GET_TEXTS:" + json);
}
else if (data.startsWith("SET_TEXTS:"))
{
const size_t capacity = JSON_ARRAY_SIZE(LedDisplayController::text_nr_sets) + LedDisplayController::text_nr_sets * JSON_OBJECT_SIZE(8) + 270;
DynamicJsonDocument doc(capacity);
data.replace("SET_TEXTS:", "");
Serial.println("Got new SET TEXT:" + data);
// TODO: handle Error!
DeserializationError err = deserializeJson(doc, data);
Serial.println("deserialization result: " + String(err.c_str()));
if(err != DeserializationError::Ok)
return;
JsonArray textsArray = doc.as<JsonArray>();
LedDisplayController::sets_t textSets;
strncpy(textSets.valid, "OK", sizeof(textSets.valid));
for (int i = 0; i < textsArray.size(); i++)
{
Serial.println(" | Processing index " + String(i));
if (i >= LedDisplayController::text_nr_sets)
break;
JsonObject textObject = textsArray[i].as<JsonObject>();
if(
!textObject.containsKey("text") ||
!textObject.containsKey("runtime") ||
//!textObject.containsKey("color") ||
!textObject.containsKey("scroll") ||
!textObject.containsKey("scrollCount") ||
!textObject.containsKey("active") ||
!textObject.containsKey("alignment")
)
continue;
LedDisplayController::text_set_t textSet;
const char* text = textObject["text"];
strncpy(textSet.text, text, sizeof(textSet.text));
uint16_t runtime = textObject["runtime"];
textSet.time_ms = runtime;
//JsonObject colorObject = textObject["color"].as<JsonObject>();
//uint16_t color = this->ledDisplayController->Color(colorObject["r"], colorObject["color"]["g"], colorObject["color"]["b"]);
//textSet.color = color;
bool scroll = textObject["scroll"];
textSet.text_scroll = scroll;
uint16_t scrollCount = textObject["scrollCount"];
textSet.text_scroll_pass = scrollCount;
bool active = textObject["active"];
textSet.active = active;
int alignment = textObject["alignment"];
textSet.align = LedDisplayController::text_align_t(alignment);
Serial.println(
" | Got set: text: " + String(textSet.text) +
" time: " + String(textSet.time_ms) +
" color: " + String(textSet.color) +
" scroll: " + String(textSet.text_scroll) +
" scrollCount: " + String(textSet.text_scroll_pass) +
" active: " + String(textSet.active)
);
textSets.sets[i] = textSet;
}
this->ledDisplayController->setTexts(textSets);
this->bleServer->sendData("SET_TEXTS:OK");
}
else if (data.startsWith("GET_BRIGHTNESS"))
this->bleServer->sendData("GET_BRIGHTNESS:5");
else if (data.startsWith("SET_BRIGHTNESS:"))
this->bleServer->sendData("SET_BRIGHTNESS:OK");
}