LedDisplay/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp

144 lines
4.8 KiB
C++
Raw Normal View History

#include "OmobiLedDisplay.h"
#include "esp_gatts_api.h"
OmobiLedDisplay::OmobiLedDisplay(String deviceName, Adafruit_NeoMatrix *ledDisplayMatrix)
{
this->lastKeepAlive = -1;
// init led display controller
this->ledDisplayController = new LedDisplayController(ledDisplayMatrix);
this->ledDisplayController->setTextSetParameter(0, LedDisplayController::ActiveParameter, "true");
// init ble server
this->bleServer = new BluetoothLeUartServer(deviceName, "92fecb20-1406-426a-afa5-cd5c1f306462", "92fecb21-1406-426a-afa5-cd5c1f306462", "92fecb22-1406-426a-afa5-cd5c1f306462");
this->bleServer->setCallbacks(this);
}
void OmobiLedDisplay::loop() {
2020-10-15 17:21:44 +02:00
if(millis() - lastKeepAlive > this->maximumKeepAliveDelay) {
this->lastKeepAlive = millis();
this->bleServer->disconnectCurrentDevice();
2020-10-15 17:21:44 +02:00
}
}
void OmobiLedDisplay::onDeviceConnectedChanged(bool deviceConnected)
{
if (deviceConnected) {
Serial.println("Device connected");
this->lastKeepAlive = millis();
}
else {
Serial.println("Device disconnected");
this->lastKeepAlive = -1;
}
}
void OmobiLedDisplay::onDataReceived(String dataString)
{
// process JSON
const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 200;
DynamicJsonDocument requestDoc(capacity);
DeserializationError error = deserializeJson(requestDoc, dataString);
// return on error
if(error != DeserializationError::Ok)
return;
// get reuqest data
OmobiDisplayCommand requestHeader = requestDoc["header"];
JsonObject requestData = requestDoc["data"];
// prepare reply data
DynamicJsonDocument replyDoc(capacity);
replyDoc["header"] = requestHeader;
OmobiDisplayStatusCode replyStatus = InternalError;
JsonObject replyData = replyDoc.createNestedObject("data");
switch (requestHeader)
{
case KeepAliveCommand:
{
replyStatus = Success;
this->lastKeepAlive = millis();
break;
}
case GetAllTextSetsCommand:
{
// cycle through all text sets
2020-10-15 00:02:48 +02:00
for (int textSetIndex = 0; textSetIndex < LedDisplayController::maximumTextSets; textSetIndex++)
{
if (this->ledDisplayController->getTextSetParameter(textSetIndex, LedDisplayController::TextParameter) == "")
continue;
//Serial.println("Adding index " + String(textSetIndex) + " with text: " + this->ledDisplayController->getTextSetParameter(textSetIndex, LedDisplayController::TextParameter));
// if a set isn't empty, go through all parameters
for (int textSetParameter = 0; textSetParameter < LedDisplayController::DisplayTextSetParameterCount; textSetParameter++)
{
// send each parameter to the client
const size_t capacity = JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3) + 200;
DynamicJsonDocument doc(capacity);
doc["header"] = GetTextSetParameterCommand;
JsonObject data = doc.createNestedObject("data");
data["index"] = textSetIndex;
data["parameter"] = textSetParameter;
data["value"] = this->ledDisplayController->getTextSetParameter(
textSetIndex,
LedDisplayController::DisplayTextSetParameter(textSetParameter));
String json;
serializeJson(doc, json);
this->bleServer->sendData(json);
}
}
replyStatus = Success;
2020-10-15 00:02:48 +02:00
replyData["maximumTextSets"] = LedDisplayController::maximumTextSets;
replyData["maximumTextLength"] = LedDisplayController::maximumTextLength;
break;
}
case GetDisplayBrightnessCommand:
{
replyData["displayBrightness"] = this->ledDisplayController->getBrightness();
replyStatus = Success;
break;
}
case SetTextSetParameterCommand:
{
int index = requestData["index"];
int parameter = requestData["parameter"];
String value = requestData["value"];
LedDisplayController::GetSetTextSetParameterExitCode res = this->ledDisplayController->setTextSetParameter(index, LedDisplayController::DisplayTextSetParameter(parameter), value);
if (res == LedDisplayController::Success)
replyStatus = Success;
else
{
replyStatus = DisplayControllerError;
replyData["displayControllerError"] = res;
}
break;
}
case SetDisplayBrightnessCommand:
{
this->ledDisplayController->setBrightness(requestData["displayBrightness"]);
replyStatus = Success;
break;
}
default:
break;
}
// reply to the client
replyDoc["status"] = replyStatus;
String json;
serializeJson(replyDoc, json);
this->bleServer->sendData(json);
}