2020-10-13 17:00:25 +02:00
|
|
|
#include "omobidisplaybackend.h"
|
2020-10-08 20:06:57 +02:00
|
|
|
|
|
|
|
OmobiDisplayBackend::OmobiDisplayBackend(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
this->ble = new QBluetoothLeUart();
|
2020-10-11 21:01:21 +02:00
|
|
|
this->displayTextModel = new OmobiDisplayTextModel(this);
|
2020-10-08 20:06:57 +02:00
|
|
|
|
|
|
|
connect(this->ble, &QBluetoothLeUart::stateChanged, this, &OmobiDisplayBackend::handleBluetoothStateChange);
|
|
|
|
connect(this->ble, &QBluetoothLeUart::foundNewDevice, this, &OmobiDisplayBackend::handleFoundNewDevice);
|
|
|
|
connect(this->ble, &QBluetoothLeUart::dataReceived, this, &OmobiDisplayBackend::DataHandler);
|
2020-10-09 00:03:50 +02:00
|
|
|
connect(this->ble, &QBluetoothLeUart::connectedToDevice, this, &OmobiDisplayBackend::handleBluetoothDeviceConected);
|
2020-10-11 21:01:21 +02:00
|
|
|
connect(this->displayTextModel, &OmobiDisplayTextModel::dataChanged, this, &OmobiDisplayBackend::handleDisplayTextModelDataChanged);
|
2020-10-11 15:50:13 +02:00
|
|
|
|
|
|
|
this->setState(Idle);
|
|
|
|
this->ble->startScanningForDevices();
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::startScanning() {
|
|
|
|
this->ble->startScanningForDevices();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::handleBluetoothStateChange(QBluetoothLeUart::BluetoothLeUartState state){
|
|
|
|
switch(state){
|
2020-10-11 15:50:13 +02:00
|
|
|
case QBluetoothLeUart::Idle: {
|
|
|
|
this->setState(Idle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Scanning: {
|
|
|
|
this->setState(Scanning);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ScanFinished: {
|
|
|
|
this->setState(ReadyToConnect);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Connecting: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ScanningForService: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::ServiceFound: {
|
|
|
|
this->setState(Connecting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QBluetoothLeUart::Connected:
|
|
|
|
{
|
2020-10-08 20:06:57 +02:00
|
|
|
break;
|
2020-10-11 15:50:13 +02:00
|
|
|
}
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 00:03:50 +02:00
|
|
|
void OmobiDisplayBackend::handleBluetoothDeviceConected() {
|
2020-10-13 01:58:14 +02:00
|
|
|
this->ble->sendData("GET_TEXTS");
|
|
|
|
this->ble->sendData("GET_BRIGHTNESS");
|
2020-10-11 21:01:21 +02:00
|
|
|
|
|
|
|
// get the existing model data!
|
|
|
|
// TODO: implement some communication!
|
|
|
|
|
|
|
|
QString currentDisplayText = "[[],[]]";
|
|
|
|
|
|
|
|
this->setState(Connected);
|
2020-10-09 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 22:03:30 +02:00
|
|
|
void OmobiDisplayBackend::handleFoundNewDevice(QBluetoothLeUartDevice* device) {
|
2020-10-08 20:06:57 +02:00
|
|
|
qDebug() << "Found a device: name: " << device->getName() << " address: " << device->getAddress();
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:58:14 +02:00
|
|
|
void OmobiDisplayBackend::DataHandler(QString s){
|
|
|
|
qDebug() << "New data: " << s;
|
|
|
|
if(s.startsWith("GET_TEXTS:")) {
|
|
|
|
QJsonParseError parseError;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(s.replace("GET_TEXTS:", "").toUtf8(), &parseError);
|
|
|
|
|
|
|
|
if(parseError.error != QJsonParseError::NoError)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QList<QMap<int, QVariant>> texts;
|
|
|
|
|
|
|
|
for(QVariant textMap : doc.toVariant().toList()) {
|
|
|
|
QMap<int, QVariant> text;
|
|
|
|
|
|
|
|
static const QMap<QString, int> keyTranslations {
|
|
|
|
{ "text", OmobiDisplayTextModel::TextRole },
|
|
|
|
{ "active", OmobiDisplayTextModel::ActiveRole },
|
|
|
|
{ "runtime", OmobiDisplayTextModel::RuntimeRole },
|
|
|
|
{ "color", OmobiDisplayTextModel::ColorRole },
|
|
|
|
{ "alignment", OmobiDisplayTextModel::AlignmentRole },
|
|
|
|
{ "scroll", OmobiDisplayTextModel::ScrollRole },
|
|
|
|
{ "scrollSpeed", OmobiDisplayTextModel::ScrollSpeedRole },
|
|
|
|
{ "scrollCount", OmobiDisplayTextModel::ScrollCountRole },
|
|
|
|
{ "index", OmobiDisplayTextModel::IndexRole }
|
|
|
|
};
|
|
|
|
|
|
|
|
for(QString key : textMap.toMap().keys()) {
|
2020-10-13 17:00:25 +02:00
|
|
|
if(keyTranslations.contains(key)) {
|
|
|
|
if(key == "color") {
|
|
|
|
QVariantMap colorMap = textMap.toMap()[key].toMap();
|
|
|
|
QColor color(colorMap["r"].toInt(), colorMap["g"].toInt(), colorMap["b"].toInt());
|
|
|
|
text.insert(keyTranslations[key], color.name());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
text.insert(keyTranslations[key], textMap.toMap()[key]);
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
texts.append(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->displayTextModel->setTexts(texts);
|
|
|
|
}
|
|
|
|
else if(s.startsWith("GET_BRIGHTNESS:")) {
|
|
|
|
this->displayBrightness = s.replace("GET_BRIGHTNESS:", "").toInt();
|
|
|
|
emit this->displayBrightnessChanged();
|
|
|
|
}
|
2020-10-08 20:06:57 +02:00
|
|
|
}
|
|
|
|
|
2020-10-11 21:01:21 +02:00
|
|
|
|
|
|
|
void OmobiDisplayBackend::handleDisplayTextModelDataChanged() {
|
2020-10-13 01:58:14 +02:00
|
|
|
|
|
|
|
QVariantList textList;
|
|
|
|
|
|
|
|
for(QMap<int, QVariant> text : this->displayTextModel->getTexts()) {
|
|
|
|
QVariantHash textMap;
|
|
|
|
|
|
|
|
for(int key : text.keys()) {
|
2020-10-13 17:00:25 +02:00
|
|
|
if(key == OmobiDisplayTextModel::ColorRole) {
|
|
|
|
QColor color = QColor(text[key].toString());
|
|
|
|
color = color.toRgb();
|
|
|
|
QVariantMap colorMap = {{"r", color.red()}, {"g", color.green()}, {"b", color.blue()}};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
textMap.insert(this->displayTextModel->roleNames()[key], text[key]);
|
2020-10-13 01:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
textList.append(textMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(textList);
|
|
|
|
|
2020-10-13 17:00:25 +02:00
|
|
|
qDebug() << doc.toJson(QJsonDocument::Indented);
|
|
|
|
|
2020-10-13 01:58:14 +02:00
|
|
|
this->ble->sendData("SET_TEXTS:" + doc.toJson(QJsonDocument::Compact));
|
2020-10-11 21:01:21 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 00:03:50 +02:00
|
|
|
QBluetoothLeUart* OmobiDisplayBackend::getBleController() {
|
|
|
|
return this->ble;
|
|
|
|
}
|
2020-10-11 15:50:13 +02:00
|
|
|
|
2020-10-11 21:01:21 +02:00
|
|
|
|
|
|
|
OmobiDisplayTextModel* OmobiDisplayBackend::getDisplayTextModel() {
|
|
|
|
return this->displayTextModel;
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:50:13 +02:00
|
|
|
OmobiDisplayBackend::OmobiDisplayAppState OmobiDisplayBackend::getState() {
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::setState(OmobiDisplayAppState state) {
|
|
|
|
if(state == this->state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->state = state;
|
|
|
|
emit this->stateChanged();
|
|
|
|
|
|
|
|
if(this->state == Idle)
|
|
|
|
this->ble->startScanningForDevices();
|
|
|
|
}
|
2020-10-13 01:58:14 +02:00
|
|
|
|
|
|
|
int OmobiDisplayBackend::getDisplayBrightness() {
|
|
|
|
return this->displayBrightness;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OmobiDisplayBackend::setDisplayBrightness(int brightness) {
|
|
|
|
if(brightness == this->displayBrightness)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this->displayBrightness = brightness;
|
|
|
|
|
|
|
|
this->ble->sendData("SET_BRIGHTNESS:" + QString::number(this->displayBrightness));
|
|
|
|
|
|
|
|
emit this->displayBrightnessChanged();
|
|
|
|
}
|