Files
BionxControl/bctransmitter.cpp

138 lines
3.4 KiB
C++
Raw Normal View History

2025-12-20 16:33:00 +01:00
#include <QThread>
#include <QDebug>
#include <bctransmitter.h>
BCTransmitter::BCTransmitter(QObject *parent)
: QObject(parent), _isBusy(false)
{
}
void BCTransmitter::onToggleConnectionState( bool connect )
{
if( connect )
{
2025-12-21 18:31:16 +01:00
if( _canDriver.getState() != BCCanDriver::DriverState::Ready )
2025-12-20 16:33:00 +01:00
_canDriver.onStartDriver();
2025-12-23 14:58:54 +01:00
uint32_t hwVersion = _canDriver.readRawValue( BCDevice::ID::Console, BC::ID::Cons_Rev_Hw);
2025-12-20 16:33:00 +01:00
2025-12-21 18:31:16 +01:00
if(!hwVersion)
2025-12-20 16:33:00 +01:00
{
qDebug() << "Console not responding";
}
else
{
/*
swVersion = getValue(CONSOLE, CONSOLE_REF_SW);
printf( "Console information:" _NL
" hardware version ........: %02d" _NL
" software version ........: %02d" _NL
" assistance level ........: %d" _NL,
hwVersion, swVersion,
getValue(CONSOLE, CONSOLE_ASSIST_INITLEVEL)
);
if (!gNoSerialNumbers)
printf( " part number .............: %05d" _NL
" item number .............: %05d" _NL _NL,
((getValue(CONSOLE, CONSOLE_SN_PN_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_PN_LO)),
((getValue(CONSOLE, CONSOLE_SN_ITEM_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_ITEM_LO))
);
*/
}
qDebug() << " ---HAIL to the kings: " << hwVersion;
}
}
2025-12-23 11:09:26 +01:00
void BCTransmitter::enqueueValueOp(BC::OpID opID, const BCDataItem* value)
2025-12-20 16:33:00 +01:00
{
QMutexLocker locker(&_mutex);
_valueQueue.enqueue( value );
2025-12-20 16:33:00 +01:00
// Logging (Thread-safe via Signal)
//emit messageLogged(QString("Command %1 queued.").arg(cmd.label));
// wir wollen nicht den ganzen Value verschicken, erstrecht
// wollen wir den Value in verschiedenen Threads gleichzeitig
// in die Hand nehmen, also hantieren wir nur mit den Inidizes.
// Trigger processing im Event-Loop des Worker Threads
// invokeMethod mit QueuedConnection entkoppelt den Aufruf,
// damit enqueueValueOp sofort zurückkehrt (non-blocking für den Aufrufer).
2025-12-20 16:33:00 +01:00
//QMetaObject::invokeMethod(this, "processValueOp", Qt::QueuedConnection);
2025-12-20 16:33:00 +01:00
QMetaObject::invokeMethod(this, [this, opID]()
{
this->processValueOp(opID);
2025-12-20 16:33:00 +01:00
}, Qt::QueuedConnection );
}
void BCTransmitter::processValueOp( BC::OpID opID )
2025-12-20 16:33:00 +01:00
{
if (_isBusy)
return;
_isBusy = true;
while (true)
{
2025-12-23 11:09:26 +01:00
const BCDataItem* currentValue{};
2025-12-20 16:33:00 +01:00
{
QMutexLocker locker(&_mutex);
if (_valueQueue.isEmpty())
{
_isBusy = false;
break; // Schleife verlassen, warten auf neue Events
}
currentValue =_valueQueue.dequeue();
} // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock!
2025-12-20 16:33:00 +01:00
if( opID == BC::OpID::ReadValue )
2025-12-21 14:40:38 +01:00
currentValue->readRawValue( *this );
2025-12-20 16:33:00 +01:00
else if( opID == BC::OpID::WriteValue )
2025-12-21 14:40:38 +01:00
currentValue->writeRawValue( *this );
2025-12-20 16:33:00 +01:00
//emit commandFinished(cmd.id, true);
//emit commandFinished(0, true);
}
}
2025-12-23 14:58:54 +01:00
uint32_t BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
2025-12-20 16:33:00 +01:00
{
2025-12-21 20:46:16 +01:00
try
{
return _canDriver.readRawValue( deviceID, registerID );
}
catch ( BCException& exception )
{
qDebug() << " -- OUCH: read exception: " << exception.what();
}
2025-12-21 14:40:38 +01:00
}
2025-12-20 16:33:00 +01:00
2025-12-23 14:58:54 +01:00
void BCTransmitter::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, uint8_t value ) const
2025-12-21 14:40:38 +01:00
{
2025-12-21 20:46:16 +01:00
try
{
2025-12-21 14:40:38 +01:00
_canDriver.writeRawValue( deviceID, registerID, value );
2025-12-21 20:46:16 +01:00
}
catch ( BCException& exception )
{
qDebug() << " -- OUCH: write exception: " << exception.what();
}
2025-12-20 16:33:00 +01:00
}
2025-12-21 14:40:38 +01:00