Files
BionxControl/bctransmitter.cpp

174 lines
4.1 KiB
C++
Raw Normal View History

2025-12-26 14:07:55 +01:00
/***************************************************************************
BionxControl
Copyright © 2025 christoph holzheuer
christoph.holzheuer@gmail.com
Using:
mhs_can_drv.c
© 2011 - 2023 by MHS-Elektronik GmbH & Co. KG, Germany
Klaus Demlehner, klaus@mhs-elektronik.de
@see www.mhs-elektronik.de
Based on Bionx data type descriptions from:
BigXionFlasher USB V 0.2.4 rev. 97
© 2011-2013 by Thomas Koenig <info@bigxionflasher.org>
@see www.bigxionflasher.org
Bionx Bike Info
© 2018 Thorsten Schmidt (tschmidt@ts-soft.de)
@see www.ts-soft.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
@see https://github.com/bikemike/bionx-bikeinfo
***************************************************************************/
2025-12-20 16:33:00 +01:00
#include <QThread>
#include <QDebug>
#include <bctransmitter.h>
BCTransmitter::BCTransmitter(QObject *parent)
: QObject(parent), _isBusy(false)
{
2025-12-31 17:27:50 +01:00
_canDriver = new BCDriverTinyCan{this};
//_canDriver = new BCDriverDummy{this};
2025-12-20 16:33:00 +01:00
}
void BCTransmitter::onToggleConnectionState( bool connect )
{
if( connect )
{
2026-01-01 13:28:17 +01:00
if( _canDriver->getState() != BCDriver::DriverState::DeviceReady )
2025-12-30 14:42:49 +01:00
_canDriver->onStartDriver();
2025-12-20 16:33:00 +01:00
2026-01-01 01:58:54 +01:00
// __fix!
2026-01-01 13:28:17 +01:00
BCDriver::TransmitResult hwVersion = _canDriver->readRawByte( (uint32_t)BCDevice::ID::Console, (uint8_t)BC::ID::Cons_Rev_Hw);
2025-12-21 18:31:16 +01:00
if(!hwVersion)
2025-12-20 16:33:00 +01:00
{
qDebug() << "Console not responding";
}
else
{
2026-01-01 01:58:54 +01:00
2025-12-20 16:33:00 +01:00
}
2026-01-01 13:28:17 +01:00
qDebug() << " ---HAIL to the kings: " << hwVersion.value();
2025-12-20 16:33:00 +01:00
}
}
void BCTransmitter::enqueueValueOp(BCValue::OpID opID, const BCValue* value)
2025-12-20 16:33:00 +01:00
{
QMutexLocker locker(&_mutex);
_valueQueue.enqueue( value );
2025-12-20 16:33:00 +01:00
// 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]()
{
processValueOp(opID);
2025-12-20 16:33:00 +01:00
}, Qt::QueuedConnection );
}
void BCTransmitter::processValueOp( BCValue::OpID opID )
2025-12-20 16:33:00 +01:00
{
if (_isBusy)
return;
_isBusy = true;
while (true)
{
2025-12-28 22:48:18 +01:00
const BCValue* 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!
try
{
2025-12-28 22:48:18 +01:00
// Abkürzung
const BCValue& val = *currentValue;
// Value ist 'under construction'
//emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::Locked );
if( opID == BCValue::OpID::ReadValue )
{
QString result = currentValue->readRawValueX( *this );
emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::InSync, result );
}
else if( opID == BCValue::OpID::WriteValue )
{
currentValue->writeRawValueX( *this );
}
}
catch (...)
{
qDebug() << " --- OUCH!";
}
// __fix
bc::processEventsFor(50);
2025-12-28 22:48:18 +01:00
//emit valueStateChanged(cmd.id, true);
//emit valueStateChanged(0, true);
2025-12-20 16:33:00 +01:00
}
}
2026-01-01 13:28:17 +01:00
uint8_t BCTransmitter::readByte( uint32_t deviceID, uint8_t registerID ) const
2025-12-20 16:33:00 +01:00
{
2026-01-01 13:28:17 +01:00
BCDriver::TransmitResult result;
2025-12-21 20:46:16 +01:00
try
{
2025-12-30 14:42:49 +01:00
result = _canDriver->readRawByte( deviceID, registerID );
2025-12-21 20:46:16 +01:00
}
catch ( BCException& exception )
2025-12-28 20:18:28 +01:00
{
2025-12-21 20:46:16 +01:00
qDebug() << " -- OUCH: read exception: " << exception.what();
}
2026-01-01 13:28:17 +01:00
return result.value();
2025-12-21 20:46:16 +01:00
2025-12-21 14:40:38 +01:00
}
2025-12-20 16:33:00 +01:00
2026-01-01 13:28:17 +01:00
void BCTransmitter::writeByte( uint32_t deviceID, uint8_t registerID , uint8_t value ) const
2025-12-21 14:40:38 +01:00
{
2025-12-21 20:46:16 +01:00
try
{
2025-12-30 14:42:49 +01:00
_canDriver->writeRawByte( 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