Reworked driver code, part II

This commit is contained in:
2026-01-01 01:58:54 +01:00
parent 3ed3511f50
commit 550382207e
5 changed files with 55 additions and 87 deletions

View File

@@ -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<uint32_t>(BCDevice::ID::Console);
uint8_t slaveFlag = static_cast<uint8_t>(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<uint32_t>(BCDevice::ID::Console);
uint8_t slaveFlag = static_cast<uint8_t>(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 ) );
}