diff --git a/bcdriver.cpp b/bcdriver.cpp index 7d93634..c9a1b4a 100644 --- a/bcdriver.cpp +++ b/bcdriver.cpp @@ -100,20 +100,14 @@ BCDriverDummy::BCDriverDummy( QObject* parent ) { } -BCDriver::DriverState BCDriverDummy::loadAndInitDriver() -{ - return BCDriver::DriverState::Ready; -} - uint32_t BCDriverDummy::readRawByte( uint32_t deviceID, uint8_t registerID ) const { - //if( getState() != DriverState::Ready) - // throw BCException( "readRawValue error: driver not loaded." ); + if( getState() != DriverState::Ready) + throw BCException( "readRawValue error: driver not loaded." ); uint32_t myRandomByte = static_cast(QRandomGenerator::global()->bounded(256)); return myRandomByte; - } void BCDriverDummy::writeRawByte( uint32_t deviceID, uint8_t registerID, uint8_t value ) const diff --git a/bcdriver.h b/bcdriver.h index 479a1c4..b696bdc 100644 --- a/bcdriver.h +++ b/bcdriver.h @@ -96,7 +96,7 @@ public: Error, Loaded, Initialized, - Opended, // bis hierher: dll vorhanden, Treiber geladen + Opened, // bis hierher: dll vorhanden, Treiber geladen Ready // hier: devices connectable }; Q_ENUM(DriverState) @@ -115,7 +115,7 @@ public slots: signals: - void driverStateChanged( DriverState state, const QString& message="" ); + void driverStateChanged( DriverState state, const QString& message="" ) const; protected: diff --git a/bcdrivertinycan.cpp b/bcdrivertinycan.cpp index ee4ac7d..7f2d0bf 100644 --- a/bcdrivertinycan.cpp +++ b/bcdrivertinycan.cpp @@ -46,6 +46,14 @@ BCDriverTinyCan::BCDriverTinyCan( QObject* parent ) } +void BCDriverTinyCan::onStartDriver() +{ + if( _driverState < DriverState::Opened) + loadDriver(); + if( _driverState == DriverState::Opened) + _driverState = connectDriver(); +} + void BCDriverTinyCan::loadDriver() { @@ -71,12 +79,7 @@ void BCDriverTinyCan::loadDriver() Q_UNUSED(state) if( ::CanDeviceOpen( 0, NULL ) < 0 ) return std::unexpected(QString("Driver Error: 'DeviceOpen'")); - return DriverState::Opended; - }; - auto callSetDeviceMode = [](DriverState state) -> Result - { - Q_UNUSED(state) ::TDeviceStatus deviceStatus; ::CanSetSpeed( 0, CAN_125K_BIT ); @@ -84,20 +87,19 @@ void BCDriverTinyCan::loadDriver() ::CanGetDeviceStatus( 0, &deviceStatus ); if( deviceStatus.DrvStatus < DRV_STATUS_CAN_OPEN ) - std::unexpected(QString("Driver Error: could not open device." )); + return std::unexpected(QString("Driver Error: could not open device." )); if( deviceStatus.CanStatus == CAN_STATUS_BUS_OFF ) { ::CanSetMode( 0, OP_CAN_RESET, CAN_CMD_NONE ); - std::unexpected(QString("Driver Error: CAN Status 'BusOff'" )); + return std::unexpected(QString("Driver Error: CAN Status 'BusOff'" )); } - return BCDriver::DriverState::Ready; + return DriverState::Opened; }; auto newDriverState = callLoadDriver() .and_then( callInitDriver ) - .and_then( callOpenDevice ) - .and_then( callSetDeviceMode ); + .and_then( callOpenDevice ); _driverState = DriverState::Error; QString message("Driver Ready."); @@ -110,67 +112,58 @@ void BCDriverTinyCan::loadDriver() emit driverStateChanged( _driverState, message ); } -void BCDriverTinyCan::initDriver() +// __fix +BCDriver::DriverState BCDriverTinyCan::connectDriver() { + uint32_t console = static_cast(BCDevice::ID::Console); + uint8_t slaveFlag = static_cast(BC::ID::Cons_Status_Slave); + + qDebug() << "XXX BCDriverTinyCan::Driver Init: putting Console in slave mode ... "; + + // Console already in slave mode. good! + if( readRawByte( console, slaveFlag ) ) + return DriverState::Ready; + + qDebug() << "BCDriverTinyCan::BCDriverTinyCan::XXX Driver Init: putting Console in slave mode ... "; + + unsigned int retry = cTimeOuts; + emit driverStateChanged( _driverState, "Driver Init: putting Console in slave mode ... " ); + uint32_t isSlave = 0; + do + { + writeRawByte( console, slaveFlag, 1 ); + isSlave = readRawByte( console, slaveFlag ); + bc::delay_millis( 200 ); + + } while( retry-- && !isSlave ); + + bc::delay_millis( 500 ); // give the Console some time to settle + //if( !isSlave ) + //emit statusHint( QString("putting Console in slave mode ") + (isSlave ? "done" : "failed") ); + + // ist das jetzt irgendwie schlimm, wenn wir keine slave Console haben + return isSlave ? DriverState::Ready : DriverState::Opened; } /* try - { - loadAndInitDriver(); + { initBCDevice::ID::Console(); } catch( std::exception& except ) { ::CanDownDriver(); - ::UnloadAndInitDriver(); + ::UnloadDriver(); // re-throw throw BCException( except.what() ); } */ -/* -BCDriver::DriverState BCDriverTinyCan::initDriver() -{ - uint32_t console = static_cast(BCDevice::ID::Console); - uint8_t slaveFlag = static_cast(BC::ID::Cons_Status_Slave); - - qDebug() << "XXX BCDriverTinyCan::Driver Init: putting BCDevice::ID::Console in slave mode ... "; - // BCDevice::ID::Console already in slave mode. good! - if( readRawByte( console, slaveFlag ) ) - return DriverState::Ready; - - qDebug() << "BCDriverTinyCan::BCDriverTinyCan::XXX Driver Init: putting BCDevice::ID::Console in slave mode ... "; - - unsigned int retry = cTimeOuts; - emit statusHint( "Driver Init: putting BCDevice::ID::Console in slave mode ... " ); - - uint32_t isSlave = 0; - do - { - writeRawByte( console, slaveFlag, 1 ); - isSlave = readRawByte( console, slaveFlag ); - bc::delay_millis( 200 ); - - } while( retry-- && !isSlave ); - - 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 ? BCDriver::connected - - return DriverState::Ready; - - -} -*/ uint32_t BCDriverTinyCan::readRawByte( uint32_t deviceID, uint8_t registerID ) const { @@ -263,7 +256,7 @@ void BCDriverTinyCan::writeRawByte( uint32_t deviceID, uint8_t registerID ,uint8 bc::delay_millis( cTIMEOUT_MS ); if( timeout_count == -1 ) - emit statusHint( QString( "error: could not send value to %1" ).arg( deviceID ) ); + emit driverStateChanged( _driverState, QString( "error: could not send value to %1" ).arg( deviceID ) ); } diff --git a/bcdrivertinycan.h b/bcdrivertinycan.h index 5374d4f..05c85ae 100644 --- a/bcdrivertinycan.h +++ b/bcdrivertinycan.h @@ -49,26 +49,23 @@ public: QString getNodeName( unsigned char id ); +public slots: + + void onStartDriver() override; + protected: void loadDriver(); - void initDriver(); - - // typen? - //void setValue( unsigned char receipient, unsigned char reg, unsigned char value ); - //unsigned int getValue( unsigned char receipient, unsigned char reg ); - + DriverState connectDriver(); //const char* CBCDLL_LIN = "libmhstcan.so"; //const char* CBCDLL_WIN = "mhstcan.dll"; - static constexpr int cRetries = 5; static constexpr int cTimeOuts = 20; static constexpr int cTIMEOUT_MS = 10; // 10ms static constexpr int cTIMEOUT_COUNT = 10; - //const unsigned int BATTERY = 0x010; //const unsigned int MOTOR = 0x020; const unsigned int BIB = 0x048; diff --git a/bctransmitter.cpp b/bctransmitter.cpp index 9ed82b6..e1d850f 100644 --- a/bctransmitter.cpp +++ b/bctransmitter.cpp @@ -50,7 +50,7 @@ void BCTransmitter::onToggleConnectionState( bool connect ) if( _canDriver->getState() != BCDriver::DriverState::Ready ) _canDriver->onStartDriver(); - // fix! + // __fix! uint32_t hwVersion = _canDriver->readRawByte( (uint32_t)BCDevice::ID::Console, (uint8_t)BC::ID::Cons_Rev_Hw); if(!hwVersion) @@ -59,23 +59,7 @@ void BCTransmitter::onToggleConnectionState( bool connect ) } else { - /* - swVersion = getValue(CONSOLE, CONSOLE_REF_SW); - printf( "Console information:" _NL - " hardware version ........: %02d" _NL - " software version ........: %02d" _NL - " assistance level ........: %d" _NL, - hwVersion, swVersion, - getValue(CONSOLE, CONSOLE_ASSIST_INITLEVEL) - ); - if (!gNoSerialNumbers) - printf( " part number .............: %05d" _NL - " item number .............: %05d" _NL _NL, - ((getValue(CONSOLE, CONSOLE_SN_PN_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_PN_LO)), - ((getValue(CONSOLE, CONSOLE_SN_ITEM_HI) << 8) + getValue(CONSOLE, CONSOLE_SN_ITEM_LO)) - ); - */ }