mergCanBus
 All Classes Functions Typedefs
MergCBUS Class Reference

#include <MergCBUS.h>

Public Types

typedef void(* userHandlerType )(Message *, MergCBUS *)
 

Public Member Functions

 MergCBUS (byte num_node_vars, byte num_events, byte num_events_vars, byte max_device_numbers)
 Constructor Create the internal buffers. Initiate the memory management and transport object. More...
 
virtual ~MergCBUS ()
 Destructor Not used.
 
void skipMessage (message_type msg)
 Set to skip processing certain message. More...
 
void processMessage (message_type msg)
 Set to process certain message. More...
 
unsigned int run ()
 Method that deals with the majority of messages and behavior. Auto enummeration, query requests and config messages. If a custom function is set it calls it for every non automatic message. More...
 
bool hasMessageToHandle ()
 
MergNodeIdentificationgetNodeId ()
 Get a reference to node identification. More...
 
bool initCanBus (unsigned int port, unsigned int rate, unsigned int retries, unsigned int retryIntervalMilliseconds)
 Set the port number for SPI communication. Set the CBUS rate and initiate the transport layer. More...
 
void setUserHandlerFunction (userHandlerType f)
 Set the user function to handle other messages.
 
void setDebug (bool debug)
 Put in debug mode.
 
void sendERRMessage (byte code)
 Send the error message with the code. More...
 
bool hasThisEvent ()
 Check it the received event is a learned event. For the messages ACONs,ACOFs,ASONs,ASOFs. Should be called after reading the can bus.
 
bool readCanBus ()
 Read the can bus from the circular buffer and load the data in the message object. More...
 
bool readCanBus (byte buf_num)
 Read the can bus and load the data in the message object. More...
 
bool readCanBus (byte *msg, byte *header, byte *length, byte buf_num)
 Read the can bus and return the buffer. More...
 
void printSentMessage ()
 Print the message to be sent to serial. Used for debug.
 
void printReceivedMessage ()
 Print the received message buffer to serial. Used for debug.
 
void setSlimMode ()
 Set the node to slim mode.
 
void setFlimMode ()
 Set the node to flim mode.
 
byte getNodeMode ()
 Get the node mode.
 
void setUpNewMemory ()
 Reset EEPROM. More...
 
void dumpMemory ()
 Print all EEPROM values.
 
void setLeds (byte green, byte yellow)
 Set the CBUS modules stardard leds. More...
 
void setPushButton (byte pb)
 Set the CBUS modules stardard push button.
 
void setStdNN (unsigned int val)
 Set the standard node number for slim mode. The user of this library has to create its own way of letting a customer set a node number in SLIM mode. If a standard value is not set and a push button is set then the library will use the value 0 if it is a consumer and 4444 if it is a producer. More...
 
unsigned int getStdNN ()
 Get the standard node number. More...
 
void saveNodeFlags ()
 
void sendMessage (Message *msg)
 
bool isAccOn ()
 
bool isAccOff ()
 
bool eventMatch ()
 
unsigned int getEventIndex (Message *msg)
 Get the Index in memory of an event. More...
 
byte getNodeVar (byte varIndex)
 Get node variable by index. More...
 
byte getEventVar (Message *msg, byte varIndex)
 Get the variable of a learned event. More...
 
void setDeviceNumber (unsigned int val, byte port)
 Set the device number for a specific port. More...
 
unsigned int getDeviceNumber (byte port)
 Get the device number for a specific port. More...
 
byte getDeviceNumberIndex ()
 
state getNodeState ()
 
byte sendOnEvent (bool longEvent, unsigned int event)
 
byte sendOffEvent (bool longEvent, unsigned int event)
 
byte sendOnEvent1 (bool longEvent, unsigned int event, byte var1)
 
byte sendOffEvent1 (bool longEvent, unsigned int event, byte var1)
 
byte sendOnEvent2 (bool longEvent, unsigned int event, byte var1, byte var2)
 
byte sendOffEvent2 (bool longEvent, unsigned int event, byte var1, byte var2)
 
byte sendOnEvent3 (bool longEvent, unsigned int event, byte var1, byte var2, byte var3)
 
byte sendOffEvent3 (bool longEvent, unsigned int event, byte var1, byte var2, byte var3)
 
void setTimerInterval (long value)
 
long getTimerInterval ()
 
void cbusRead ()
 Read the can bus message and put in circular buffer. Used by the timer the buffer is 1 byte for the message size.1 byte for RTR, 4 bytes for header. 8 bytes to max message. More...
 

Detailed Description

A general class that support the MergCBUS protocol. The class is used to all operations regarding the protocol, but is flexible enough to allow you to use general can messages. It uses a modified version of mcp_can.h, that included the CAN header manipulation and RTR messages. When using the CBUS the user has to set the node information: -The manufacturer ID as a HEX numeric (If the manufacturer has a NMRA number this can be used) -Minor code version as an alphabetic character (ASCII) -Manufacturer’s module identifier as a HEX numeric -Number of supported events as a HEX numeric -Number of Event Variables per event as a HEX numeric -Number of supported Node Variables as a HEX numeric -Major version as a HEX numeric. (can be 0 if no major version allocated) -Node Flags You has to start the CAN by calling initCanBus(...); If your board follows the Merg boards with on push button and red and yellow leds, you can specify then with setLeds() and setPushButton() If you want the library to take care of a lot of stuff, you need to define your user function that implements the node logic and tell the lib about it calling setUserHandlerFunction() and on the arduino loop() call the run() method. See the examples. If you want to take care of all messages by yourself just read the messages using readCanBus() or readCanBus(header[],data[]) and use the getMessage() to get a wrapper over the can message.

Member Typedef Documentation

typedef void(* MergCBUS::userHandlerType)(Message *, MergCBUS *)

This is the user function for processing other messages. It receives a reference to a Message and a reference to MergCBUS.

Constructor & Destructor Documentation

MergCBUS::MergCBUS ( byte  num_node_vars,
byte  num_events,
byte  num_events_var,
byte  max_device_numbers 
)

Constructor Create the internal buffers. Initiate the memory management and transport object.

Member Function Documentation

void MergCBUS::cbusRead ( )

Read the can bus message and put in circular buffer. Used by the timer the buffer is 1 byte for the message size.1 byte for RTR, 4 bytes for header. 8 bytes to max message.

Returns
true if a message in the can bus.
unsigned int MergCBUS::getDeviceNumber ( byte  port)

Get the device number for a specific port.

Parameters
portPort assigned to the device number
Returns
The device number for the port or 0 if the index is out of bounds
unsigned int MergCBUS::getEventIndex ( Message msg)

Get the Index in memory of an event.

Parameters
msgPointer to a received message. It will use the node number and the event number from the msg *
Returns
Returns the index of the event in memory
byte MergCBUS::getEventVar ( Message msg,
byte  varIndex 
)

Get the variable of a learned event.

Parameters
msgPointer to a received message. It will use the node number and the event number from the msg *
varIndexthe index in the variable to be retrieved.
Returns
Returns the variable value.
MergNodeIdentification* MergCBUS::getNodeId ( )
inline

Get a reference to node identification.

Returns
Pointer to a
See Also
MergNodeIdentification
byte MergCBUS::getNodeVar ( byte  varIndex)

Get node variable by index.

Parameters
varIndexindex of the variable
Returns
Returns the variable. One byte
unsigned int MergCBUS::getStdNN ( )
inline

Get the standard node number.

Returns
node number
bool MergCBUS::initCanBus ( unsigned int  port,
unsigned int  rate,
unsigned int  retries,
unsigned int  retryIntervalMilliseconds 
)

Set the port number for SPI communication. Set the CBUS rate and initiate the transport layer.

Parameters
portis the the SPI port number.
rateis the can bus rate. The values defined in the can transport layer are CAN_5KBPS 1 CAN_10KBPS 2 CAN_20KBPS 3 CAN_31K25BPS 4 CAN_40KBPS 5 CAN_50KBPS 6 CAN_80KBPS 7 CAN_100KBPS 8 CAN_125KBPS 9 CAN_200KBPS 10 CAN_250KBPS 11 CAN_500KBPS 12 CAN_1000KBPS 13
retriesis the number of retries to configure the can bus
retryIntervalMillisecondsis the delay in milliseconds between each retry.
bool MergCBUS::isAccOff ( )

Check the if it is an OFF message. Major event in CBUS.

Returns
True if is and OFF event, false if not
bool MergCBUS::isAccOn ( )

Check the if it is an ON message. Major event in CBUS.

Returns
True if is and On event, false if not
void MergCBUS::processMessage ( message_type  msg)
inline

Set to process certain message.

Parameters
msgMessage type to process.
bool MergCBUS::readCanBus ( )

Read the can bus from the circular buffer and load the data in the message object.

Returns
true if a message in the can bus.
bool MergCBUS::readCanBus ( byte  buf_num)

Read the can bus and load the data in the message object.

Returns
true if a message in the can bus.
bool MergCBUS::readCanBus ( byte *  data,
byte *  header,
byte *  length,
byte  buf_num 
)

Read the can bus and return the buffer.

Returns
number of bytes read;
unsigned int MergCBUS::run ( )

Method that deals with the majority of messages and behavior. Auto enummeration, query requests and config messages. If a custom function is set it calls it for every non automatic message.

See Also
setUserHandlerFunction If the green and yellow leds are set ,it also control the standard their behaviour based on node state.
void MergCBUS::sendERRMessage ( byte  code)

Send the error message with the code.

Parameters
codecan be: 1 Command Not Supported - see note 1. 2 Not In Learn Mode 3 Not in Setup Mode - see note 1 4 Too Many Events 5 Reserved 6 Invalid Event variable index 7 Invalid Event 8 Reserved - see note 2 9 Invalid Parameter Index 10 Invalid Node Variable Index 11 Invalid Event Variable Value 12 Invalid Node Variable Value

Note 1: Accessory modules do not return this error Note 2: Currently used by code that processes OPC REVAL 0x9C but this code should be updated to use codes 6 & 7.

void MergCBUS::setDeviceNumber ( unsigned int  val,
byte  port 
)

Set the device number for a specific port.

Parameters
valThe device number
portPort assigned to the device number
void MergCBUS::setLeds ( byte  green,
byte  yellow 
)

Set the CBUS modules stardard leds.

Set the pins for green and yello leds.

Parameters
greenPin for the green led.
yellowPin for the yellow led.
void MergCBUS::setStdNN ( unsigned int  val)
inline

Set the standard node number for slim mode. The user of this library has to create its own way of letting a customer set a node number in SLIM mode. If a standard value is not set and a push button is set then the library will use the value 0 if it is a consumer and 4444 if it is a producer.

Parameters
newnode number
void MergCBUS::setUpNewMemory ( )

Reset EEPROM.

Create a new memory set up. Writes the EEPROM with null values. Equivalent to reset the memory. Should be done once before setting a new node.

void MergCBUS::skipMessage ( message_type  msg)
inline

Set to skip processing certain message.

Parameters
msgMessage type not to process.

The documentation for this class was generated from the following files: