Reworked data types, part II

This commit is contained in:
2025-12-21 18:31:16 +01:00
parent cb9eb8c3fe
commit e3c26ffa34
12 changed files with 85 additions and 68 deletions

16
bc.cpp
View File

@@ -7,6 +7,22 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
//! erzeugt einen std::runtime_error mit text und optionalem parameter
BCException::BCException(const QString& what, const QString& param )
: std::runtime_error( param.isEmpty() ? what.toStdString() : QString( "%1: %2" ).arg(what,param).toStdString( ) )
{
}
BCException::BCException( const QString& what, int errCode )
: std::runtime_error( QString( "%1: %2" ).arg(what,errCode).toStdString( ) )
{
}
namespace bc namespace bc
{ {

14
bc.h
View File

@@ -37,6 +37,20 @@
using bcdata_t = uint8_t; using bcdata_t = uint8_t;
/**
* @brief Simple exception class
*/
class BCException : public std::runtime_error
{
public:
BCException( const QString& what, const QString& param="" );
BCException( const QString& what, int errCode );
};
namespace bc namespace bc
{ {
[[maybe_unused]] constexpr static double UNLIMITED_SPEED_VALUE = 70; // Km/h [[maybe_unused]] constexpr static double UNLIMITED_SPEED_VALUE = 70; // Km/h

View File

@@ -10,34 +10,17 @@ BCCanDriver::BCCanDriver(QObject* parent )
} }
BCCanDriver::dState BCCanDriver::getState() BCCanDriver::DriverState BCCanDriver::getState() const
{ {
return _driverState; return _driverState;
} }
void BCCanDriver::setState( dState newState ) void BCCanDriver::setState( DriverState newState )
{ {
_driverState = newState; _driverState = newState;
} }
QString BCCanDriver::stateLabel( dState state )
{
switch( (int) state )
{
case sDriverError:
return stDriverError;
case sIdle:
return stIdle;
case sLoaded:
return stLoaded;
case sReady:
return stReady;
};
}
/** /**
* *
* @brief Der Slot, der das Initialisieren des Treibers auslöst. * @brief Der Slot, der das Initialisieren des Treibers auslöst.
@@ -56,7 +39,7 @@ void BCCanDriver::onStartDriver()
{ {
// erstmal die Dll Laden: State::sIdle -> State::sLoaded // erstmal die Dll Laden: State::sIdle -> State::sLoaded
if( BCCanDriver::sIdle == _driverState ) if( _driverState == DriverState::NotPresent)
_driverState = loadDriver(); _driverState = loadDriver();
emit stateChanged( (int) _driverState ); emit stateChanged( (int) _driverState );

View File

@@ -58,31 +58,26 @@ class BCCanDriver : public QObject
public: public:
typedef enum enum class DriverState
{ {
sDriverError = 0, NotPresent,
sIdle, Error,
sLoaded, Loaded,
sReady, Initialized,
dStateSize Connected,
} dState; Ready
};
[[maybe_unused]] constexpr static const char* stDriverError = "Error"; Q_ENUM(DriverState)
[[maybe_unused]] constexpr static const char* stIdle = "[not loaded]";
[[maybe_unused]] constexpr static const char* stLoaded = "[not connected]";
[[maybe_unused]] constexpr static const char* stReady = "Ready";
static QString stateLabel( dState state );
explicit BCCanDriver( QObject* parent = nullptr ); explicit BCCanDriver( QObject* parent = nullptr );
virtual ~BCCanDriver() = default; virtual ~BCCanDriver() = default;
dState getState(); DriverState getState() const;
virtual BCCanDriver::dState loadDriver() = 0; virtual DriverState loadDriver() = 0;
virtual BCCanDriver::dState initDriver() = 0; virtual DriverState initDriver() = 0;
virtual std::optional<bcdata_t> readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const = 0; virtual bcdata_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const = 0;
virtual void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const = 0; virtual void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const = 0;
signals: signals:
@@ -91,20 +86,18 @@ signals:
void statusHint( const QString& msg ) const; void statusHint( const QString& msg ) const;
void stateChanged( int state ); void stateChanged( int state );
void itemReady( const QString& label );
void itemLoaded( CBCItem& item );
public slots: public slots:
void onStartDriver(); void onStartDriver();
//void onLoadItem( CBCItem& item );
protected: protected:
void setState( dState newState ); void setState( DriverState newState );
dState _driverState = sIdle; DriverState _driverState{DriverState::NotPresent};
//??
int _retries = 5; int _retries = 5;
int _timeOuts = 20; int _timeOuts = 20;

View File

@@ -21,7 +21,7 @@ BCCanDriverTinyCan::BCCanDriverTinyCan( QObject* parent )
} }
BCCanDriver::dState BCCanDriverTinyCan::loadDriver() BCCanDriver::DriverState BCCanDriverTinyCan::loadDriver()
{ {
//qDebug() << "CanBusControl " << cbc::Version << '\n' << "based on BigXionFlasher (c) 2011-2013 by Thomas Koenig <info@bigxionflasher.org> - www.bigxionflasher.org"; //qDebug() << "CanBusControl " << cbc::Version << '\n' << "based on BigXionFlasher (c) 2011-2013 by Thomas Koenig <info@bigxionflasher.org> - www.bigxionflasher.org";
@@ -31,6 +31,8 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
if( ::LoadDriver( NULL ) < 0 ) if( ::LoadDriver( NULL ) < 0 )
throw std::runtime_error( "Driver Error: 'LoadDriver'" ); throw std::runtime_error( "Driver Error: 'LoadDriver'" );
setState(BCCanDriver::DriverState::Loaded);
if( ::CanInitDriver( NULL ) < 0 ) if( ::CanInitDriver( NULL ) < 0 )
throw std::runtime_error( "Driver Error: 'InitDriver'" ); throw std::runtime_error( "Driver Error: 'InitDriver'" );
@@ -41,6 +43,8 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
::CanSetMode( 0, OP_CAN_START, CAN_CMD_ALL_CLEAR ); ::CanSetMode( 0, OP_CAN_START, CAN_CMD_ALL_CLEAR );
::CanGetDeviceStatus( 0, &status ); ::CanGetDeviceStatus( 0, &status );
setState(BCCanDriver::DriverState::Initialized);
if( status.DrvStatus < DRV_STATUS_CAN_OPEN ) if( status.DrvStatus < DRV_STATUS_CAN_OPEN )
throw std::runtime_error( "Driver Error: could not open device." ); throw std::runtime_error( "Driver Error: could not open device." );
@@ -50,9 +54,9 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
throw std::runtime_error( "Driver Error: CAN Status 'BusOff'" ); throw std::runtime_error( "Driver Error: CAN Status 'BusOff'" );
} }
setState(BCCanDriver::sLoaded); setState(BCCanDriver::DriverState::Ready);
return BCCanDriver::sLoaded; return BCCanDriver::DriverState::Ready;
} }
@@ -75,13 +79,13 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
*/ */
BCCanDriver::dState BCCanDriverTinyCan::initDriver() BCCanDriver::DriverState BCCanDriverTinyCan::initDriver()
{ {
qDebug() << "XXX BCCanDriverTinyCan::Driver Init: putting BCDevice::ID::Console in slave mode ... "; qDebug() << "XXX BCCanDriverTinyCan::Driver Init: putting BCDevice::ID::Console in slave mode ... ";
// BCDevice::ID::Console already in slave mode. good! // BCDevice::ID::Console already in slave mode. good!
if( readRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave ) ) if( readRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave ) )
return BCCanDriver::sReady; return DriverState::Ready;
qDebug() << "BCCanDriverTinyCan::BCCanDriverTinyCan::XXX Driver Init: putting BCDevice::ID::Console in slave mode ... "; qDebug() << "BCCanDriverTinyCan::BCCanDriverTinyCan::XXX Driver Init: putting BCDevice::ID::Console in slave mode ... ";
@@ -92,7 +96,7 @@ BCCanDriver::dState BCCanDriverTinyCan::initDriver()
do do
{ {
writeRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave, 1 ); writeRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave, 1 );
isSlave = *readRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave ); isSlave = readRawValue( BCDevice::ID::Console, BC::ID::Cons_Status_Slave );
bc::delay_millis( 200 ); bc::delay_millis( 200 );
} while( retry-- && !isSlave ); } while( retry-- && !isSlave );
@@ -100,20 +104,24 @@ BCCanDriver::dState BCCanDriverTinyCan::initDriver()
bc::delay_millis( 500 ); // give the Console some time to settle bc::delay_millis( 500 ); // give the Console some time to settle
//if( !isSlave ) //if( !isSlave )
emit statusHint( QString("putting BCDevice::ID::Console in slave mode ") + (isSlave ? "done" : "failed") ); emit statusHint( QString("putting BCDevice::ID::Console in slave mode ") + (isSlave ? "done" : "failed") );
// ist das jetzt irgendwie schlimm, wenn wir keine slave BCDevice::ID::Console haben // ist das jetzt irgendwie schlimm, wenn wir keine slave BCDevice::ID::Console haben
//return isSlave ? BCCanDriver::connected //return isSlave ? BCCanDriver::connected
return BCCanDriver::sReady; return DriverState::Ready;
} }
std::optional<bcdata_t> BCCanDriverTinyCan::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const bcdata_t BCCanDriverTinyCan::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
{ {
struct TCanMsg msg; if( getState() != DriverState::Ready)
throw BCException( "readRawValue error: driver not loaded." );
TCanMsg msg;
uint32_t device = static_cast<uint32_t>(deviceID); uint32_t device = static_cast<uint32_t>(deviceID);
uint8_t reg = static_cast<uint8_t> (registerID); uint8_t reg = static_cast<uint8_t> (registerID);
@@ -137,7 +145,7 @@ std::optional<bcdata_t> BCCanDriverTinyCan::readRawValue( BCDevice::ID deviceID,
bc::delay_millis( cTIMEOUT_MS ); bc::delay_millis( cTIMEOUT_MS );
if( timeOuts == -1 ) if( timeOuts == -1 )
throw std::runtime_error( "getValue error: could not send value" ); throw BCException( "readRawValue error: could not send value" );
retry: retry:
@@ -178,6 +186,9 @@ retry:
void BCCanDriverTinyCan::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const void BCCanDriverTinyCan::writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const
{ {
if( getState() != DriverState::Ready)
throw BCException( "writeRawValue error: driver not loaded." );
qDebug() << " --- BCCanDriverTinyCan writeRawValue: " << value; qDebug() << " --- BCCanDriverTinyCan writeRawValue: " << value;
uint32_t device = static_cast<uint32_t>(deviceID); uint32_t device = static_cast<uint32_t>(deviceID);

View File

@@ -12,10 +12,10 @@ public:
explicit BCCanDriverTinyCan( QObject* parent=nullptr ); explicit BCCanDriverTinyCan( QObject* parent=nullptr );
virtual ~BCCanDriverTinyCan() = default; virtual ~BCCanDriverTinyCan() = default;
BCCanDriver::dState loadDriver() override; DriverState loadDriver() override;
BCCanDriver::dState initDriver() override; DriverState initDriver() override;
std::optional<bcdata_t> readRawValue ( BCDevice::ID deviceID, BC::ID registerID ) const override; bcdata_t readRawValue ( BCDevice::ID deviceID, BC::ID registerID ) const override;
void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override; void writeRawValue( BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override;
QString getNodeName( unsigned char id ); QString getNodeName( unsigned char id );

View File

@@ -99,9 +99,9 @@ void BCItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) co
QLabel *lblUnit = editor->findChild<QLabel*>("lblUnit"); QLabel *lblUnit = editor->findChild<QLabel*>("lblUnit");
if (slider && lblUnit) { if (slider && lblUnit) {
bool oldState = slider->blockSignals(true); bool olDriverState = slider->blockSignals(true);
slider->setValue(bc.value.toInt()); slider->setValue(bc.value.toInt());
slider->blockSignals(oldState); slider->blockSignals(olDriverState);
lblUnit->setText(QString("%1 %2").arg(bc.value.toInt()).arg( "mm3")); lblUnit->setText(QString("%1 %2").arg(bc.value.toInt()).arg( "mm3"));
} else { } else {

View File

@@ -54,7 +54,7 @@ BCMainWindow::BCMainWindow(QWidget *parent)
//_valueView->setItemDelegate(_delegate); //_valueView->setItemDelegate(_delegate);
// Highlight mit Fade-Out: // Highlight mit Fade-Out:
_delegate->onHighlightRow(2); // 2 Sekunden Fade //_delegate->onHighlightRow(2); // 2 Sekunden Fade
BCTransmitter* transmitter = _valueManager.getTransmitter(); BCTransmitter* transmitter = _valueManager.getTransmitter();
// besser: model::emit dataChanged // besser: model::emit dataChanged

View File

@@ -14,12 +14,12 @@ void BCTransmitter::onToggleConnectionState( bool connect )
{ {
if( connect ) if( connect )
{ {
if( BCCanDriver::sIdle == _canDriver.getState() ) if( _canDriver.getState() != BCCanDriver::DriverState::Ready )
_canDriver.onStartDriver(); _canDriver.onStartDriver();
std::optional<bcdata_t> hwVersion = _canDriver.readRawValue( BCDevice::ID::Console, BC::ID::Cons_Rev_Hw); bcdata_t hwVersion = _canDriver.readRawValue( BCDevice::ID::Console, BC::ID::Cons_Rev_Hw);
if(!hwVersion.has_value()) if(!hwVersion)
{ {
qDebug() << "Console not responding"; qDebug() << "Console not responding";
} }
@@ -106,7 +106,7 @@ void BCTransmitter::processValueOp( BC::OpID opID )
} }
} }
std::optional<bcdata_t> BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const bcdata_t BCTransmitter::readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const
{ {
return _canDriver.readRawValue( deviceID, registerID ); return _canDriver.readRawValue( deviceID, registerID );
} }

View File

@@ -17,7 +17,7 @@ public:
explicit BCTransmitter(QObject *parent = nullptr); explicit BCTransmitter(QObject *parent = nullptr);
std::optional<bcdata_t> readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const override; bcdata_t readRawValue( BCDevice::ID deviceID, BC::ID registerID ) const override;
void writeRawValue(BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override; void writeRawValue(BCDevice::ID deviceID, BC::ID registerID, bcdata_t value ) const override;
public slots: public slots:

View File

@@ -60,7 +60,7 @@ BCValue::BCValue(const BCValueType& valueType_, BCDevice::ID deviceID_, BC::ID r
void BCValue::readRawValue( const BCAbstractTransmitter& transmitter ) const void BCValue::readRawValue( const BCAbstractTransmitter& transmitter ) const
{ {
std::optional<uint32_t> result = transmitter.readRawValue( deviceID, registerID ); bcdata_t result = transmitter.readRawValue( deviceID, registerID );
} }

View File

@@ -61,8 +61,8 @@ class BCAbstractTransmitter
public: public:
// //
virtual std::optional<bcdata_t> readRawValue( BCDevice::ID deviceID_, BC::ID registerID_ ) const = 0; virtual bcdata_t readRawValue( BCDevice::ID deviceID_, BC::ID registerID_ ) const = 0;
virtual void writeRawValue( BCDevice::ID deviceID_, BC::ID registerID_, uint8_t value_ ) const = 0; virtual void writeRawValue( BCDevice::ID deviceID_, BC::ID registerID_, uint8_t value_ ) const = 0;
}; };