2020-10-13 17:01:29 +02:00
|
|
|
#include "OmobiLedDisplay.h"
|
2020-10-15 16:58:06 +02:00
|
|
|
#include "esp_gatts_api.h"
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
OmobiLedDisplay::OmobiLedDisplay(String deviceName, Adafruit_NeoMatrix *ledDisplayMatrix)
|
2020-10-13 17:01:29 +02:00
|
|
|
{
|
2020-10-15 16:58:06 +02:00
|
|
|
this->lastKeepAlive = -1;
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
// init led display controller
|
|
|
|
this->ledDisplayController = new LedDisplayController(ledDisplayMatrix);
|
|
|
|
this->ledDisplayController->setTextSetParameter(0, LedDisplayController::ActiveParameter, "true");
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
// init ble server
|
2020-10-15 16:58:06 +02:00
|
|
|
this->bleServer = new BluetoothLeUartServer(deviceName, "92fecb20-1406-426a-afa5-cd5c1f306462", "92fecb21-1406-426a-afa5-cd5c1f306462", "92fecb22-1406-426a-afa5-cd5c1f306462");
|
2020-10-13 17:01:29 +02:00
|
|
|
this->bleServer->setCallbacks(this);
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:58:06 +02:00
|
|
|
void OmobiLedDisplay::loop() {
|
|
|
|
if(millis() - lastKeepAlive > this->maximumKeepAliveDelay)
|
|
|
|
this->bleServer->disconnectCurrentDevice();
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:01:29 +02:00
|
|
|
void OmobiLedDisplay::onDeviceConnectedChanged(bool deviceConnected)
|
|
|
|
{
|
2020-10-15 16:58:06 +02:00
|
|
|
if (deviceConnected) {
|
2020-10-13 17:01:29 +02:00
|
|
|
Serial.println("Device connected");
|
2020-10-15 16:58:06 +02:00
|
|
|
this->lastKeepAlive = millis();
|
|
|
|
}
|
|
|
|
else {
|
2020-10-14 23:54:54 +02:00
|
|
|
Serial.println("Device disconnected");
|
2020-10-15 16:58:06 +02:00
|
|
|
this->lastKeepAlive = -1;
|
|
|
|
}
|
2020-10-13 17:01:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
void OmobiLedDisplay::onDataReceived(String dataString)
|
2020-10-13 17:01:29 +02:00
|
|
|
{
|
2020-10-14 23:54:54 +02:00
|
|
|
// 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)
|
2020-10-13 17:01:29 +02:00
|
|
|
{
|
2020-10-15 16:58:06 +02:00
|
|
|
case KeepAliveCommand:
|
|
|
|
{
|
|
|
|
replyStatus = Success;
|
|
|
|
this->lastKeepAlive = millis();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
case GetAllTextSetsCommand:
|
2020-10-13 17:01:29 +02:00
|
|
|
{
|
2020-10-14 23:54:54 +02:00
|
|
|
// cycle through all text sets
|
2020-10-15 00:02:48 +02:00
|
|
|
for (int textSetIndex = 0; textSetIndex < LedDisplayController::maximumTextSets; textSetIndex++)
|
2020-10-13 17:01:29 +02:00
|
|
|
{
|
2020-10-14 23:54:54 +02:00
|
|
|
if (this->ledDisplayController->getTextSetParameter(textSetIndex, LedDisplayController::TextParameter) == "")
|
2020-10-13 17:01:29 +02:00
|
|
|
continue;
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
//Serial.println("Adding index " + String(textSetIndex) + " with text: " + this->ledDisplayController->getTextSetParameter(textSetIndex, LedDisplayController::TextParameter));
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
// 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);
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
doc["header"] = GetTextSetParameterCommand;
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
JsonObject data = doc.createNestedObject("data");
|
|
|
|
data["index"] = textSetIndex;
|
|
|
|
data["parameter"] = textSetParameter;
|
|
|
|
data["value"] = this->ledDisplayController->getTextSetParameter(
|
|
|
|
textSetIndex,
|
|
|
|
LedDisplayController::DisplayTextSetParameter(textSetParameter));
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
String json;
|
|
|
|
serializeJson(doc, json);
|
|
|
|
this->bleServer->sendData(json);
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
replyStatus = Success;
|
2020-10-15 00:02:48 +02:00
|
|
|
replyData["maximumTextSets"] = LedDisplayController::maximumTextSets;
|
|
|
|
replyData["maximumTextLength"] = LedDisplayController::maximumTextLength;
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GetDisplayBrightnessCommand:
|
|
|
|
{
|
|
|
|
replyData["displayBrightness"] = this->ledDisplayController->getBrightness();
|
|
|
|
replyStatus = Success;
|
|
|
|
break;
|
|
|
|
}
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
case SetTextSetParameterCommand:
|
|
|
|
{
|
|
|
|
int index = requestData["index"];
|
|
|
|
int parameter = requestData["parameter"];
|
|
|
|
String value = requestData["value"];
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
LedDisplayController::GetSetTextSetParameterExitCode res = this->ledDisplayController->setTextSetParameter(index, LedDisplayController::DisplayTextSetParameter(parameter), value);
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
if (res == LedDisplayController::Success)
|
|
|
|
replyStatus = Success;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
replyStatus = DisplayControllerError;
|
|
|
|
replyData["displayControllerError"] = res;
|
2020-10-13 17:01:29 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SetDisplayBrightnessCommand:
|
|
|
|
{
|
|
|
|
this->ledDisplayController->setBrightness(requestData["displayBrightness"]);
|
|
|
|
replyStatus = Success;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2020-10-13 17:01:29 +02:00
|
|
|
}
|
2020-10-14 23:54:54 +02:00
|
|
|
|
|
|
|
// reply to the client
|
|
|
|
replyDoc["status"] = replyStatus;
|
|
|
|
String json;
|
|
|
|
serializeJson(replyDoc, json);
|
|
|
|
this->bleServer->sendData(json);
|
2020-10-13 17:01:29 +02:00
|
|
|
}
|