72 qDebug() <<
'[' << _mac <<
']' <<
" Stopping NOW!";
78 if (_owner->_state == State::Created)
80 _deviceDiscoveryAgent =
new QBluetoothDeviceDiscoveryAgent;
81 connect(_deviceDiscoveryAgent,
82 SIGNAL(deviceDiscovered(
const QBluetoothDeviceInfo&)),
84 SLOT(deviceDiscovered(
const QBluetoothDeviceInfo&)));
85 connect(_deviceDiscoveryAgent,
86 SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
88 SLOT(deviceDiscoverError(QBluetoothDeviceDiscoveryAgent::Error)));
89 connect(_deviceDiscoveryAgent, SIGNAL(finished()),
this, SLOT(deviceDiscoverFinished()));
90 connect(_deviceDiscoveryAgent, SIGNAL(canceled()),
this, SLOT(deviceDiscoverFinished()));
91 qDebug() <<
'[' << _mac <<
']' <<
" State DiscoveringDevices";
92 _owner->_state = State::DiscoveringDevices;
93 _deviceDiscoveryAgent->start();
95 else if (_owner->_state == State::DiscoveringDevicesDone)
97 if (!_deviceDiscovered)
99 qDebug() <<
'[' << _mac <<
']' <<
" Device discovering failed!";
103 qDebug() <<
'[' << _mac <<
']' <<
" State Disconnected";
104 _owner->_state = State::Disconnected;
106 else if (_owner->_state == State::Disconnected)
118 _serviceDiscovered =
false;
119 _control =
new QLowEnergyController(_currentDevice);
120 _control->setRemoteAddressType(QLowEnergyController::RandomAddress);
123 SIGNAL(serviceDiscovered(QBluetoothUuid)),
125 SLOT(serviceDiscovered(QBluetoothUuid)));
126 connect(_control, SIGNAL(discoveryFinished()),
this, SLOT(serviceScanDone()));
128 SIGNAL(error(QLowEnergyController::Error)),
130 SLOT(controllerError(QLowEnergyController::Error)));
131 connect(_control, SIGNAL(connected()),
this, SLOT(deviceConnected()));
132 connect(_control, SIGNAL(disconnected()),
this, SLOT(deviceDisconnected()));
133 _control->connectToDevice();
134 qDebug() <<
'[' << _mac <<
']' <<
" State Connecting";
135 _owner->_state = State::Connecting;
137 else if (_owner->_state == State::ConnectingDone)
139 _control->discoverServices();
140 qDebug() <<
'[' << _mac <<
']' <<
" State DiscoveringServices";
141 _owner->_state = State::DiscoveringServices;
143 else if (_owner->_state == State::DiscoveringServicesDone)
145 if (!_serviceDiscovered)
147 qDebug() <<
'[' << _mac <<
']' <<
" Service discovering failed!";
151 _service = _control->createServiceObject(QBluetoothUuid(QUuid(
UARTSERVICEUUID)));
154 SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
156 SLOT(serviceStateChanged(QLowEnergyService::ServiceState)));
158 SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)),
160 SLOT(readData(QLowEnergyCharacteristic, QByteArray)));
162 SIGNAL(descriptorWritten(QLowEnergyDescriptor, QByteArray)),
164 SLOT(receiveDeviceDisconnec(QLowEnergyDescriptor, QByteArray)));
166 _service->discoverDetails();
168 qDebug() <<
'[' << _mac <<
']' <<
" State ConnectingService";
169 _owner->_state = State::ConnectingService;
171 else if (_owner->_state == State::Running)
173 std::lock_guard g(_cmdMutex);
174 if (_service && _cmd.size() != 0)
176 const QLowEnergyCharacteristic RxChar =
177 _service->characteristic(QBluetoothUuid(QUuid(
RXUUID)));
180 _service->writeCharacteristic(RxChar,
data, QLowEnergyService::WriteWithoutResponse);
181 if (_owner->_verboseSend)
183 qDebug() <<
'[' << _mac <<
']' <<
" send: " << _cmd;
343BLEProthesisInterfaceQtWorker::consumeData<
346 if (!_valueAkk.contains(
'\n'))
348 if (_owner->_verboseReceive)
350 qDebug() <<
'[' << _mac <<
']'
351 <<
" data does not contain \\n -> no new sensor values\n Buffer:\n"
356 auto listPacks = _valueAkk.split(
'\n');
358 if (_owner->_verboseReceive)
360 qDebug() <<
'[' << _mac <<
']' <<
" parsing " << listPacks.at(listPacks.size() - 2);
363 auto listVals = listPacks.at(listPacks.size() - 2).split(
' ');
364 _owner->_thumbPos = listVals.at(0).toLong();
365 _owner->_thumbPWM = listVals.at(1).toLong();
366 _owner->_fingerPos = listVals.at(2).toLong();
367 _owner->_fingerPWM = listVals.at(3).toLong();
369 _valueAkk = listPacks.back();
374BLEProthesisInterfaceQtWorker::consumeData<BLEProthesisInterface::SensorValueProtocol::mx_pos_pwm>()
376 if (!_valueAkk.contains(
'\n'))
378 if (_owner->_verboseReceive)
380 qDebug() <<
'[' << _mac <<
']'
381 <<
" data does not contain \\n -> no new sensor values\n Buffer:\n"
386 auto listPacks = _valueAkk.split(
'\n');
388 static const QRegularExpression m2(
389 R
"(^M2:[ \t]+Pos.:[ \t]+(-?[1-9][0-9]*|0)[ \t]+PWM:[ \t]+(-?[1-9][0-9]*|0)[ \t\n\r]+$)");
390 static const QRegularExpression m3(
391 R
"(^M3:[ \t]+Pos.:[ \t]+(-?[1-9][0-9]*|0)[ \t]+PWM:[ \t]+(-?[1-9][0-9]*|0)[ \t\n\r]+$)");
393 for (
int i = 0; i < listPacks.size() - 1; ++i)
395 if (listPacks.at(i).size() == 0)
399 if (_owner->_verboseReceive)
401 qDebug() <<
'[' << _mac <<
']' <<
" parsing " << listPacks.at(i);
403 if (
const auto matchM2 = m2.match(listPacks.at(i)); matchM2.hasMatch())
405 _owner->_thumbPos = matchM2.captured(1).toLong();
406 _owner->_thumbPWM = matchM2.captured(2).toLong();
407 if (_owner->_verboseReceive)
409 qDebug() <<
'[' << _mac <<
']' <<
" updated M2";
412 else if (
const auto matchM3 = m3.match(listPacks.at(i)); matchM3.hasMatch())
414 _owner->_fingerPos = matchM3.captured(1).toLong();
415 _owner->_fingerPWM = matchM3.captured(2).toLong();
416 if (_owner->_verboseReceive)
418 qDebug() <<
'[' << _mac <<
']' <<
" updated M3";
423 qWarning() <<
"unknown format for data: " << listPacks.at(i).toLocal8Bit()
427 _valueAkk = listPacks.back();