Reworked data types, part II
This commit is contained in:
16
bc.cpp
16
bc.cpp
@@ -7,6 +7,22 @@
|
||||
#include <chrono>
|
||||
#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
|
||||
{
|
||||
|
||||
|
||||
14
bc.h
14
bc.h
@@ -37,6 +37,20 @@
|
||||
|
||||
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
|
||||
{
|
||||
[[maybe_unused]] constexpr static double UNLIMITED_SPEED_VALUE = 70; // Km/h
|
||||
|
||||
@@ -10,34 +10,17 @@ BCCanDriver::BCCanDriver(QObject* parent )
|
||||
|
||||
}
|
||||
|
||||
BCCanDriver::dState BCCanDriver::getState()
|
||||
BCCanDriver::DriverState BCCanDriver::getState() const
|
||||
{
|
||||
return _driverState;
|
||||
}
|
||||
|
||||
void BCCanDriver::setState( dState newState )
|
||||
void BCCanDriver::setState( 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.
|
||||
@@ -56,7 +39,7 @@ void BCCanDriver::onStartDriver()
|
||||
{
|
||||
|
||||
// erstmal die Dll Laden: State::sIdle -> State::sLoaded
|
||||
if( BCCanDriver::sIdle == _driverState )
|
||||
if( _driverState == DriverState::NotPresent)
|
||||
_driverState = loadDriver();
|
||||
|
||||
emit stateChanged( (int) _driverState );
|
||||
|
||||
@@ -58,31 +58,26 @@ class BCCanDriver : public QObject
|
||||
|
||||
public:
|
||||
|
||||
typedef enum
|
||||
enum class DriverState
|
||||
{
|
||||
sDriverError = 0,
|
||||
sIdle,
|
||||
sLoaded,
|
||||
sReady,
|
||||
dStateSize
|
||||
} dState;
|
||||
|
||||
[[maybe_unused]] constexpr static const char* stDriverError = "Error";
|
||||
[[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 );
|
||||
NotPresent,
|
||||
Error,
|
||||
Loaded,
|
||||
Initialized,
|
||||
Connected,
|
||||
Ready
|
||||
};
|
||||
Q_ENUM(DriverState)
|
||||
|
||||
explicit BCCanDriver( QObject* parent = nullptr );
|
||||
virtual ~BCCanDriver() = default;
|
||||
|
||||
dState getState();
|
||||
DriverState getState() const;
|
||||
|
||||
virtual BCCanDriver::dState loadDriver() = 0;
|
||||
virtual BCCanDriver::dState initDriver() = 0;
|
||||
virtual DriverState loadDriver() = 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;
|
||||
|
||||
signals:
|
||||
@@ -91,20 +86,18 @@ signals:
|
||||
void statusHint( const QString& msg ) const;
|
||||
|
||||
void stateChanged( int state );
|
||||
void itemReady( const QString& label );
|
||||
void itemLoaded( CBCItem& item );
|
||||
|
||||
public slots:
|
||||
|
||||
void onStartDriver();
|
||||
//void onLoadItem( CBCItem& item );
|
||||
|
||||
protected:
|
||||
|
||||
void setState( dState newState );
|
||||
void setState( DriverState newState );
|
||||
|
||||
dState _driverState = sIdle;
|
||||
DriverState _driverState{DriverState::NotPresent};
|
||||
|
||||
//??
|
||||
int _retries = 5;
|
||||
int _timeOuts = 20;
|
||||
|
||||
|
||||
@@ -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";
|
||||
@@ -31,6 +31,8 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
|
||||
if( ::LoadDriver( NULL ) < 0 )
|
||||
throw std::runtime_error( "Driver Error: 'LoadDriver'" );
|
||||
|
||||
setState(BCCanDriver::DriverState::Loaded);
|
||||
|
||||
if( ::CanInitDriver( NULL ) < 0 )
|
||||
throw std::runtime_error( "Driver Error: 'InitDriver'" );
|
||||
|
||||
@@ -41,6 +43,8 @@ BCCanDriver::dState BCCanDriverTinyCan::loadDriver()
|
||||
::CanSetMode( 0, OP_CAN_START, CAN_CMD_ALL_CLEAR );
|
||||
::CanGetDeviceStatus( 0, &status );
|
||||
|
||||
setState(BCCanDriver::DriverState::Initialized);
|
||||
|
||||
if( status.DrvStatus < DRV_STATUS_CAN_OPEN )
|
||||
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'" );
|
||||
}
|
||||
|
||||
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 ... ";
|
||||
// BCDevice::ID::Console already in slave mode. good!
|
||||
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 ... ";
|
||||
|
||||
@@ -92,7 +96,7 @@ BCCanDriver::dState BCCanDriverTinyCan::initDriver()
|
||||
do
|
||||
{
|
||||
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 );
|
||||
|
||||
} while( retry-- && !isSlave );
|
||||
@@ -100,20 +104,24 @@ BCCanDriver::dState BCCanDriverTinyCan::initDriver()
|
||||
bc::delay_millis( 500 ); // give the Console some time to settle
|
||||
//if( !isSlave )
|
||||
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
|
||||
//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);
|
||||
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 );
|
||||
|
||||
if( timeOuts == -1 )
|
||||
throw std::runtime_error( "getValue error: could not send value" );
|
||||
throw BCException( "readRawValue error: could not send value" );
|
||||
|
||||
retry:
|
||||
|
||||
@@ -178,6 +186,9 @@ retry:
|
||||
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;
|
||||
|
||||
uint32_t device = static_cast<uint32_t>(deviceID);
|
||||
|
||||
@@ -12,10 +12,10 @@ public:
|
||||
explicit BCCanDriverTinyCan( QObject* parent=nullptr );
|
||||
virtual ~BCCanDriverTinyCan() = default;
|
||||
|
||||
BCCanDriver::dState loadDriver() override;
|
||||
BCCanDriver::dState initDriver() override;
|
||||
DriverState loadDriver() 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;
|
||||
|
||||
QString getNodeName( unsigned char id );
|
||||
|
||||
@@ -99,9 +99,9 @@ void BCItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) co
|
||||
QLabel *lblUnit = editor->findChild<QLabel*>("lblUnit");
|
||||
|
||||
if (slider && lblUnit) {
|
||||
bool oldState = slider->blockSignals(true);
|
||||
bool olDriverState = slider->blockSignals(true);
|
||||
slider->setValue(bc.value.toInt());
|
||||
slider->blockSignals(oldState);
|
||||
slider->blockSignals(olDriverState);
|
||||
|
||||
lblUnit->setText(QString("%1 %2").arg(bc.value.toInt()).arg( "mm3"));
|
||||
} else {
|
||||
|
||||
@@ -54,7 +54,7 @@ BCMainWindow::BCMainWindow(QWidget *parent)
|
||||
//_valueView->setItemDelegate(_delegate);
|
||||
|
||||
// Highlight mit Fade-Out:
|
||||
_delegate->onHighlightRow(2); // 2 Sekunden Fade
|
||||
//_delegate->onHighlightRow(2); // 2 Sekunden Fade
|
||||
BCTransmitter* transmitter = _valueManager.getTransmitter();
|
||||
|
||||
// besser: model::emit dataChanged
|
||||
|
||||
@@ -14,12 +14,12 @@ void BCTransmitter::onToggleConnectionState( bool connect )
|
||||
{
|
||||
if( connect )
|
||||
{
|
||||
if( BCCanDriver::sIdle == _canDriver.getState() )
|
||||
if( _canDriver.getState() != BCCanDriver::DriverState::Ready )
|
||||
_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";
|
||||
}
|
||||
@@ -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 );
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
public slots:
|
||||
|
||||
@@ -60,7 +60,7 @@ BCValue::BCValue(const BCValueType& valueType_, BCDevice::ID deviceID_, BC::ID r
|
||||
|
||||
void BCValue::readRawValue( const BCAbstractTransmitter& transmitter ) const
|
||||
{
|
||||
std::optional<uint32_t> result = transmitter.readRawValue( deviceID, registerID );
|
||||
bcdata_t result = transmitter.readRawValue( deviceID, registerID );
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ class BCAbstractTransmitter
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user