VitroIO SDK
Software development kit for Vitro Shard.
Loading...
Searching...
No Matches
vitroio::sdk::Canbus Class Reference

Class is a wrapper for mbed::CAN class and provides interface for CanbusFrame type. More...

#include <canbus.h>

Inheritance diagram for vitroio::sdk::Canbus:
Collaboration diagram for vitroio::sdk::Canbus:

Public Types

typedef Callback< void(const CanbusFrame &)> OnFrameReceivedCallback
 Type defines signature of callback function which is to be called when a new frame will be received. More...
 

Public Member Functions

 Canbus (PinName rd, PinName td, EventQueue *targetQueue, uint32_t eventQueueSize=VITROIO_CANBUS_DEFAULT_EVENT_QUEUE_SIZE, int hz=VITROIO_CANBUS_DEFAULT_FREQUENCY)
 Constructor initializes CAN bus and chains an internal event queue to the targetQueue. More...
 
 Canbus (PinName rd, PinName td, osPriority priority=osPriorityNormal, uint32_t eventQueueSize=VITROIO_CANBUS_DEFAULT_EVENT_QUEUE_SIZE, int hz=VITROIO_CANBUS_DEFAULT_FREQUENCY)
 Constructor initializes CAN bus and creates internal event loop using thread with priority priority. More...
 
 ~Canbus ()
 Destructor. More...
 
bool isValid () const
 Function allows to check whether the bus is in valid state. More...
 
int sendFrame (const CanbusFrame &frame)
 Functions sends frame over the bus. More...
 
int setOnFrameReceivedCallback (OnFrameReceivedCallback callback, int handle=-1)
 Function allows to set callbacks which are to be called any time the frame received. More...
 

Detailed Description

Class is a wrapper for mbed::CAN class and provides interface for CanbusFrame type.

Read of incoming frames is possible only by callbacks because of thread safety - the frames are read using RX IRQ.

The class uses an event queue for reading incoming frames. The event queue can be managed by external queue specified by user or by internal queue with thread of specified priority.

Member Typedef Documentation

◆ OnFrameReceivedCallback

Type defines signature of callback function which is to be called when a new frame will be received.

Constructor & Destructor Documentation

◆ Canbus() [1/2]

vitroio::sdk::Canbus::Canbus ( PinName  rd,
PinName  td,
EventQueue *  targetQueue,
uint32_t  eventQueueSize = VITROIO_CANBUS_DEFAULT_EVENT_QUEUE_SIZE,
int  hz = VITROIO_CANBUS_DEFAULT_FREQUENCY 
)
explicit

Constructor initializes CAN bus and chains an internal event queue to the targetQueue.

There is not guarantee that the object is in valid state. User should call isValid() method to determine if the object was initialized successfully. When the object is in invalid state, the behaviour is undefined.

The contructor uses mbed::EventQueue::chain method for connection to targetQueue.

Note
Thread which handles targetQueue must be run before usage of this class (construction of the object can be done earlier).
Parameters
rdRD pin of the CAN interface
tdTD pin of the CAN interface
targetQueueExternal event queue to which the internal queue is to be chained
eventQueueSizeSize of internal event queue
hzTransmission frequency in hertz

◆ Canbus() [2/2]

vitroio::sdk::Canbus::Canbus ( PinName  rd,
PinName  td,
osPriority  priority = osPriorityNormal,
uint32_t  eventQueueSize = VITROIO_CANBUS_DEFAULT_EVENT_QUEUE_SIZE,
int  hz = VITROIO_CANBUS_DEFAULT_FREQUENCY 
)
explicit

Constructor initializes CAN bus and creates internal event loop using thread with priority priority.

There is not guarantee that the object is in valid state. User should call isValid() method to determine if the object was initialized successfully. When the object is in invalid state, the behaviour is undefined.

Parameters
rdRD pin of the CAN interface
tdTD pin of the CAN interface
priorityPriority of thread for handling internal event queue
eventQueueSizeSize of internal event queue
hzTransmission frequency in hertz

◆ ~Canbus()

vitroio::sdk::Canbus::~Canbus ( )

Destructor.

Member Function Documentation

◆ isValid()

bool vitroio::sdk::Canbus::isValid ( ) const

Function allows to check whether the bus is in valid state.

Note
This method should be called always after creation of the object.
Returns
Function returns true if the object is valid or false otherwise.

◆ sendFrame()

int vitroio::sdk::Canbus::sendFrame ( const CanbusFrame frame)

Functions sends frame over the bus.

Parameters
frameFrame to send
Returns
Function returns VITROIO_ERR_SUCCESS on success or appropriate error code otherwise.

◆ setOnFrameReceivedCallback()

int vitroio::sdk::Canbus::setOnFrameReceivedCallback ( OnFrameReceivedCallback  callback,
int  handle = -1 
)

Function allows to set callbacks which are to be called any time the frame received.

Maximum number of callbacks that can be set is 4. The callback can be unset by passing OnFrameReceivedCallback(NULL) as callback and providing handle returned by this method.

Note
When a new frame is received, all of registered callbacks will be called for the same frame. Callbacks are responsible for filtering incoming frames and handling only those that interest them.
Parameters
handleHandle of callback returned by this function - it should be used when the specified callback is to be changed
Returns
Function returns non-negative handle of callback on success or -1 otherwise.