Files
BionxControl/bctransmitter.cpp

155 lines
3.9 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 23:01:31 +01:00
if( _canDriver->getDriverState() != 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
}
}
2026-01-02 01:52:48 +01:00
void BCTransmitter::enqueueValueOp( const BCValuePtr value)
2025-12-20 16:33:00 +01:00
{
2026-01-02 01:43:49 +01:00
// Hier sind wir noch in GUI Thread
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
2026-01-01 23:01:31 +01:00
// wollen wir nicht den Value in verschiedenen Threads gleichzeitig
2025-12-20 16:33:00 +01:00
// 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);
2026-01-02 01:43:49 +01:00
QMetaObject::invokeMethod(this, [this]()
2025-12-20 16:33:00 +01:00
{
2026-01-02 01:43:49 +01:00
processValueOp();
2025-12-20 16:33:00 +01:00
}, Qt::QueuedConnection );
}
2026-01-02 01:43:49 +01:00
void BCTransmitter::processValueOp()
2025-12-20 16:33:00 +01:00
{
if (_isBusy)
return;
_isBusy = true;
while (true)
{
2026-01-02 01:52:48 +01:00
BCValuePtr value{};
2025-12-20 16:33:00 +01:00
{
QMutexLocker locker(&_mutex);
if (_valueQueue.isEmpty())
{
_isBusy = false;
break; // Schleife verlassen, warten auf neue Events
}
2026-01-02 01:52:48 +01:00
value =_valueQueue.dequeue();
2025-12-20 16:33:00 +01:00
} // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock!
2026-01-02 01:52:48 +01:00
2026-01-01 23:01:31 +01:00
// Abkürzung
2026-01-02 01:52:48 +01:00
//const BCValue& value = *currentValue;
2026-01-01 23:01:31 +01:00
// Value ist 'under construction'
//emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::Locked );
2026-01-02 01:43:49 +01:00
2026-01-02 01:52:48 +01:00
/*
2026-01-01 23:01:31 +01:00
if( opID == BCValue::OpID::ReadValue )
{
2026-01-01 23:01:31 +01:00
QString result = currentValue->readRawValueX( *this );
2026-01-02 01:43:49 +01:00
emit valueUpdated( valuel.deviceID, value.indexRow, BCValue::State::InSync, result );
}
2026-01-01 23:01:31 +01:00
else if( opID == BCValue::OpID::WriteValue )
{
2026-01-01 23:01:31 +01:00
currentValue->writeRawValueX( *this );
}
2026-01-02 01:52:48 +01:00
*/
emit valueUpdated( value->deviceID, value->indexRow, BCValue::State::InSync, "fitze!");
// __fix
2026-01-01 23:01:31 +01:00
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 23:01:31 +01:00
uint8_t BCTransmitter::readByte( uint32_t deviceID, uint8_t registerID ) const
2025-12-20 16:33:00 +01:00
{
2026-01-01 23:01:31 +01:00
BCDriver::TransmitResult result = _canDriver->readRawByte( deviceID, registerID );
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
{
2026-01-01 23:01:31 +01:00
_canDriver->writeRawByte( deviceID, registerID, value );
2025-12-20 16:33:00 +01:00
}
2025-12-21 14:40:38 +01:00