📄 iomodule.cpp
字号:
/***************************************************************************/
/**
Update the locally stored value of one of the 8-bit digital output blocks
associated with this PDO.
@param id The output block ID to be updatad.
@param value The new value for the output block.
@return true if the value was updated,
false if the block isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::DigOutPDO::Update( uint8 id, uint8 value )
{
++id;
for( int i=0; i<mapCt; i++ )
{
if( out[i].GetSub() == id )
{
out[i].Write( value );
return true;
}
}
return false;
}
/***************************************************************************/
/**
Update the locally stored value of one bit in this PDO.
@param id The output ID to be updatad.
@param value The new value for the output.
@return true if the value was updated,
false if the output isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::DigOutPDO::UpdateBit( uint16 id, bool value )
{
uint8 block = 1 + (id>>3);
uint8 mask = 1 << (id&3);
for( int i=0; i<mapCt; i++ )
{
if( out[i].GetSub() == block )
{
uint8 v = out[i].Read();
if( value ) v |= mask;
else v &= ~mask;
out[i].Write( v );
return true;
}
}
return false;
}
/***************************************************************************/
/**
Transmit this PDO.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *IOModule::DigOutPDO::Transmit( void )
{
return RPDO::Transmit( io->GetCanOpen() );
}
/***************************************************************************/
/**
Initialize an analog output PDO object.
@param io Pointer to the I/O module to which this PDO is assigned.
@param cobID The CAN ID for this PDO message.
@param ct The number of outputs to be mapped (1 to 4)
@param id An array of ct output ID numbers. These will be mapped
(in order) to the PDO.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *IOModule::AlgOutPDO::Init( class IOModule *io, uint32 cobID, uint8 ct, uint8 id[] )
{
if( ct < 1 || ct > 4 )
return &IOError::BadIOCount;
this->io = io;
const Error *err = RPDO::Init( cobID );
if( !err ) err = SetType( 255 );
for( uint8 i=0; i<ct; i++ )
{
if( !err ) err = out[i].Init( IOOBJID_AOUT_16_VALUE, id[i]+1 );
if( !err ) err = AddVar( out[i] );
}
return err;
}
/***************************************************************************/
/**
Update the locally stored value of one of the 16-bit analog outputs
associated with this PDO.
@param id The output block ID to be updatad.
@param value The new value for the output block.
@return true if the value was updated,
false if the block isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::AlgOutPDO::Update( uint8 id, int16 value )
{
++id;
for( int i=0; i<mapCt; i++ )
{
if( out[i].GetSub() == id )
{
out[i].Write( value );
return true;
}
}
return false;
}
/***************************************************************************/
/**
Transmit this PDO.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *IOModule::AlgOutPDO::Transmit( void )
{
return RPDO::Transmit( io->GetCanOpen() );
}
/***************************************************************************/
/**
Initialize a digital input PDO object.
@param io Pointer to the I/O module to which this PDO is assigned.
@param cobID The CAN ID for this PDO message.
@param ct The number of input blocks to be mapped (1 to 8)
@param id An array of ct input block ID numbers. These will be mapped
(in order) to the PDO.
@param event The event bit to post when a PDO message is received.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *IOModule::DigInPDO::Init( class IOModule *io, uint32 cobID, uint8 ct,
uint8 id[], IOMODULE_EVENTS event )
{
if( ct < 1 || ct > 8 )
return &IOError::BadIOCount;
this->io = io;
eventMask = event;
const Error *err = TPDO::Init( io->GetCanOpen(), cobID );
if( !err ) err = SetType( 255 );
for( uint8 i=0; i<ct; i++ )
{
if( !err ) err = in[i].Init( IOOBJID_DIN_8_VALUE, id[i]+1 );
if( !err ) err = AddVar( in[i] );
}
if( !err ) err = EnableReceiver();
return err;
}
/***************************************************************************/
/**
Read the specified input bank from the PDO's cached data. The value returned
will be the last value received via PDO for this input bank.
@param id The input block ID to be checked.
@param value The input value for the block will be returned here.
@return true if the value was returned,
false if the block isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::DigInPDO::GetInVal( uint8 id, uint8 &value )
{
++id;
for( int i=0; i<mapCt; i++ )
{
if( in[i].GetSub() == id )
{
value = in[i].Read();
return true;
}
}
return false;
}
/***************************************************************************/
/**
Update the locally stored value of one bit in this PDO.
@param id The output ID to be updatad.
@param value The new value for the output.
@return true if the value was updated,
false if the output isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::DigInPDO::GetBitVal( uint16 id, bool &value )
{
uint8 block = 1 + (id>>3);
uint8 mask = 1 << (id&3);
for( int i=0; i<mapCt; i++ )
{
if( in[i].GetSub() == block )
{
value = (in[i].Read() & mask) == mask;
return true;
}
}
return false;
}
/***************************************************************************/
/**
New transmit PDO received. This method is called by the CANopen reader thread
when a new PDO message is received. It causes this PDO object to post it's
event to the IOModule object's event map. This will cause any waiting threads
to wake up.
*/
/***************************************************************************/
void IOModule::DigInPDO::Received( void )
{
io->PostIOEvent( eventMask );
}
/***************************************************************************/
/**
Initialize a analog input PDO object.
@param io Pointer to the I/O module to which this PDO is assigned.
@param cobID The CAN ID for this PDO message.
@param ct The number of inputs to be mapped (1 to 4)
@param id An array of ct input ID numbers. These will be mapped
(in order) to the PDO.
@param event The event bit to post when a PDO message is received.
@return A pointer to an error object, or NULL on success
*/
/***************************************************************************/
const Error *IOModule::AlgInPDO::Init( class IOModule *io, uint32 cobID, uint8 ct,
uint8 id[], IOMODULE_EVENTS event )
{
if( ct < 1 || ct > 4 )
return &IOError::BadIOCount;
this->io = io;
eventMask = event;
const Error *err = TPDO::Init( io->GetCanOpen(), cobID );
if( !err ) err = SetType( 255 );
for( uint8 i=0; i<ct; i++ )
{
if( !err ) err = in[i].Init( IOOBJID_AIN_16_VALUE, id[i]+1 );
if( !err ) err = AddVar( in[i] );
}
if( !err ) err = EnableReceiver();
return err;
}
/***************************************************************************/
/**
Read the specified input from the PDO's cached data. The value returned
will be the last value received via PDO for this input bank.
@param id The input ID to be checked.
@param value The input value will be returned here. If the input is not
mapped to this PDO, then this will not be changed.
@return true if the value was returned,
false if the input isn't mapped to this PDO.
*/
/***************************************************************************/
bool IOModule::AlgInPDO::GetInVal( uint8 id, int16 &value )
{
++id;
for( int i=0; i<mapCt; i++ )
{
if( in[i].GetSub() == id )
{
value = in[i].Read();
return true;
}
}
return false;
}
/***************************************************************************/
/**
New transmit PDO received. This method is called by the CANopen reader thread
when a new PDO message is received. It causes this PDO object to post it's
event to the IOModule object's event map. This will cause any waiting threads
to wake up.
*/
/***************************************************************************/
void IOModule::AlgInPDO::Received( void )
{
io->PostIOEvent( eventMask );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -