diff --git a/bcdriver.cpp b/bcdriver.cpp index d99f458..33cbfd1 100644 --- a/bcdriver.cpp +++ b/bcdriver.cpp @@ -43,7 +43,7 @@ BCDriver::BCDriver(QObject* parent ) } -BCDriver::DriverState BCDriver::getState() const +BCDriver::DriverState BCDriver::getDriverState() const { return _driverState; } @@ -64,6 +64,7 @@ void BCDriverDummy::onStartDriver() _driverState = DriverState::DeviceReady; emit driverStateChanged( DriverState::DeviceReady, "Driver Ready." ); } +// __Fix /* try { @@ -100,12 +101,11 @@ BCDriverDummy::BCDriverDummy( QObject* parent ) { } - BCDriver::TransmitResult BCDriverDummy::readRawByte( uint32_t deviceID, uint8_t registerID ) const { Q_UNUSED(deviceID) Q_UNUSED(registerID) - qDebug() << " --- DriverState: " << _driverState; + qDebug() << " --- Dummy: readRawByte:DriverState: " << getDriverState(); uint8_t myRandomByte = static_cast(QRandomGenerator::global()->bounded(256)); return myRandomByte; } diff --git a/bcdriver.h b/bcdriver.h index 1406989..77339c0 100644 --- a/bcdriver.h +++ b/bcdriver.h @@ -100,7 +100,7 @@ public: Loaded, Initialized, Opened, // bis hierher: dll vorhanden, Treiber geladen - DeviceReady // hier: devices connectable + DeviceReady // hier: devices connectable, wir können arbeiten. }; Q_ENUM(DriverState) @@ -112,10 +112,17 @@ public: explicit BCDriver( QObject* parent = nullptr ); virtual ~BCDriver() = default; - DriverState getState() const; + // Gibt den aktuelle Zustand des Treibers zurück. Der DriverState + // muss auf DeviceReady stehen, um Werte lesen & schreiben zu können. + // Dazu muss das Bionx-System eingeschaltet sein. - virtual TransmitResult readRawByte( uint32_t deviceID, uint8_t registerID ) const = 0; - virtual TransmitResult writeRawByte(uint32_t deviceID, uint8_t registerID, uint8_t value ) const = 0; + DriverState getDriverState() const; + + // Abstrakte Methoden für das tatsächliche Lesen & Schreiben über den CAN-Bus. In der Implementierungs- + // Klasse 'BCDriverTinyCan' werden die C-Funktionen der Treiber DLL gekapselt. + + virtual TransmitResult readRawByte ( uint32_t deviceID, uint8_t registerID ) const = 0; + virtual TransmitResult writeRawByte( uint32_t deviceID, uint8_t registerID, uint8_t value ) const = 0; public slots: diff --git a/bcdrivertinycan.cpp b/bcdrivertinycan.cpp index 490de1c..4b1af22 100644 --- a/bcdrivertinycan.cpp +++ b/bcdrivertinycan.cpp @@ -127,14 +127,14 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver() unsigned int retry = cTimeOuts; emit driverStateChanged( _driverState, "Driver Init: putting Console in slave mode ... " ); - TransmitResult isSlave = 0; + TransmitResult isSlave = 0; do - { - writeRawByte( console, slaveFlag, 1 ); - isSlave = readRawByte( console, slaveFlag ); - bc::delay_millis( 200 ); + { + writeRawByte( console, slaveFlag, 1 ); + isSlave = readRawByte( console, slaveFlag ); + bc::delay_millis( 200 ); - } while( retry-- && !(*isSlave) ); + } while( retry-- && !(*isSlave) ); bc::delay_millis( 500 ); // give the Console some time to settle //if( !isSlave ) @@ -156,9 +156,6 @@ BCDriver::DriverState BCDriverTinyCan::connectDriver() { ::CanDownDriver(); ::UnloadDriver(); - - // re-throw - throw BCException( except.what() ); } */ @@ -168,14 +165,11 @@ BCDriver::TransmitResult BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_ { //TransmitResult - qDebug() << " --- DriverState: " << _driverState; + qDebug() << " --- BCDriverTinyCan::readRawByte DriverState: " << getDriverState(); + + if( getDriverState() != DriverState::DeviceReady) + return std::unexpected(QString("readRawValue error: driver not loaded." ) ); - /* - Nicht hier! - if( getState() != DriverState::DeviceReady) - //throw BCException( "readRawValue error: driver not loaded." ); - return std::nullopt; - */ ::TCanMsg msg; // msg verpacken @@ -197,7 +191,7 @@ BCDriver::TransmitResult BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_ bc::delay_millis( cTIMEOUT_MS ); if( timeOuts == -1 ) - return std::unexpected(QString("readRawValue error: could not send value" )); + return std::unexpected(QString("readRawValue error: could not send value" )); retry: @@ -238,11 +232,9 @@ retry: BCDriver::TransmitResult BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8_t value ) const { - /* - Nicht hier! - if( getState() != DriverState::DeviceReady) - throw BCException( "writeRawValue error: driver not loaded." ); - */ + if( getDriverState() != DriverState::DeviceReady) + return std::unexpected(QString("writeRawValue error: driver not loaded." ) ); + qDebug() << " --- BCDriverTinyCan writeRawValue: " << value; ::TCanMsg msg; diff --git a/bctransmitter.cpp b/bctransmitter.cpp index 588e935..6c9276c 100644 --- a/bctransmitter.cpp +++ b/bctransmitter.cpp @@ -47,7 +47,7 @@ void BCTransmitter::onToggleConnectionState( bool connect ) { if( connect ) { - if( _canDriver->getState() != BCDriver::DriverState::DeviceReady ) + if( _canDriver->getDriverState() != BCDriver::DriverState::DeviceReady ) _canDriver->onStartDriver(); // __fix! @@ -73,7 +73,7 @@ void BCTransmitter::enqueueValueOp(BCValue::OpID opID, const BCValue* value) _valueQueue.enqueue( value ); // wir wollen nicht den ganzen Value verschicken, erstrecht - // wollen wir den Value in verschiedenen Threads gleichzeitig + // wollen wir nicht 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 @@ -109,63 +109,39 @@ void BCTransmitter::processValueOp( BCValue::OpID opID ) } // Mutex wird hier freigegeben! WICHTIG: Execute ohne Lock! - try + // Abkürzung + const BCValue& val = *currentValue; + // Value ist 'under construction' + //emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::Locked ); + if( opID == BCValue::OpID::ReadValue ) { - // 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 ); - } - + QString result = currentValue->readRawValueX( *this ); + emit valueUpdated( val.deviceID, val.indexRow, BCValue::State::InSync, result ); } - catch (...) + else if( opID == BCValue::OpID::WriteValue ) { - qDebug() << " --- OUCH!"; + currentValue->writeRawValueX( *this ); } // __fix - bc::processEventsFor(50); + bc::processEventsFor(50); //emit valueStateChanged(cmd.id, true); //emit valueStateChanged(0, true); } } -uint8_t BCTransmitter::readByte( uint32_t deviceID, uint8_t registerID ) const +uint8_t BCTransmitter::readByte( uint32_t deviceID, uint8_t registerID ) const { - BCDriver::TransmitResult result; - try - { - result = _canDriver->readRawByte( deviceID, registerID ); - } - catch ( BCException& exception ) - { - qDebug() << " -- OUCH: read exception: " << exception.what(); - } + BCDriver::TransmitResult result = _canDriver->readRawByte( deviceID, registerID ); + return result.value(); } void BCTransmitter::writeByte( uint32_t deviceID, uint8_t registerID , uint8_t value ) const { - try - { - _canDriver->writeRawByte( deviceID, registerID, value ); - } - catch ( BCException& exception ) - { - qDebug() << " -- OUCH: write exception: " << exception.what(); - } - + _canDriver->writeRawByte( deviceID, registerID, value ); } diff --git a/bctransmitter.h b/bctransmitter.h index 258122f..947f191 100644 --- a/bctransmitter.h +++ b/bctransmitter.h @@ -41,7 +41,7 @@ #include #include -// template ... ? + class BCTransmitter : public QObject, public BCAbstractTransmitter { Q_OBJECT @@ -68,8 +68,8 @@ private: using BCDataQueue = QQueue; - BCDataQueue _valueQueue; - QMutex _mutex; + BCDataQueue _valueQueue; + QMutex _mutex; std::atomic _isBusy{ false }; BCDriver* _canDriver{}; diff --git a/bcvalue.cpp b/bcvalue.cpp index 28181c6..6507205 100644 --- a/bcvalue.cpp +++ b/bcvalue.cpp @@ -51,7 +51,7 @@ QString BCValue::readRawValueX( const BCAbstractTransmitter& transmitter ) const uint32_t devID = static_cast(deviceID); uint8_t regID = static_cast (registerID); - // wir sind hier im anderen thread! nicht einfach so reinschreiben + // wir sind hier im anderen thread! nicht einfach so reinschreiben, nur lesen if( valueType->readValueFunc ) { uint32_t result = valueType->readValueFunc( transmitter, devID, regID ); diff --git a/bcvalue.h b/bcvalue.h index 495fe6d..414b483 100644 --- a/bcvalue.h +++ b/bcvalue.h @@ -99,29 +99,17 @@ public: QString readRawValueX( const BCAbstractTransmitter& transmitter ) const; void writeRawValueX( const BCAbstractTransmitter& transmitter ) const; - // void reset() - -// später vielleicht -//protected: mutable States state{BCValue::State::NoState}; - //const BCValueType& valueType; - //BCValueTypeCRef valueType; const BCValueType* valueType{}; BCDevice::ID deviceID{BCDevice::ID::Invalid}; BC::ID registerID{BC::ID::Invalid}; int indexRow{-1}; QString label; - // ?? mutable QString visibleValue; - QVariant defaultValue; - //?? - bool inSync{false}; - bool readOnly{false}; - - //mutable std::optional rawValue; }; + Q_DECLARE_OPERATORS_FOR_FLAGS(BCValue::States) Q_DECLARE_METATYPE(const BCValue*) //Q_DECLARE_METATYPE(const BCValue&)