mergCanBus

- Main Page
- Classes
- Files

- File List

All Classes Functions Typedefs
MergCBUS.h
1 #ifndef BYTE_TYPE
2 #define BYTE_TYPE
3 //typedef char byte;
4 #endif // BYTE_TYPE
5
6 #ifndef MESSAGEPARSER_H
7 #define MESSAGEPARSER_H
8
9 #include <Arduino.h>
10 #include <EEPROM.h>
11 #include <SPI.h>
12 #include <avr/wdt.h>
13 //#include <TimerOne.h>
14
15 //#include "CANMessage.h"
16 #include "Message.h"
17 #include "MergNodeIdentification.h"
18 #include "mcp_can.h"
19 #include "MergMemoryManagement.h"
20 #include "CircularBuffer.h"
21
22
23
24 //#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {} //reset
25 #define Reset_AVR() asm volatile (" jmp 0");
26
27
28
29 #define SELF_ENUM_TIME 1000
30 #define TEMP_BUFFER_SIZE 128
31 #define SELF_ENUM_BUFFER_SIZE 99
32 #define BLINK_RATE 100
33
34 //#define lowByte(w) ((uint8_t) ((w) & 0xff))
35 //#define highByte(w) ((uint8_t) ((w) >> 8))
36
40 //enum process_mode{AUTOMATIC,MANUAL};
44 enum state {LEARN=0,
45 UNLEARN=1,
46 BOOT=2,
47 NORMAL=3,
48 SELF_ENUMERATION=4,
49 SETUP=5
50 };
51
55 enum can_error {OK=0,
56 UNKNOWN_MSG_TYPE=1,
57 NO_MESSAGE=2
58 };
59
80 class MergCBUS
81 {
82 public:
83
84 typedef void (*userHandlerType)(Message*,MergCBUS*);
86 MergCBUS(byte num_node_vars,byte num_events,byte num_events_vars,byte max_device_numbers);
87 virtual ~MergCBUS();
91 void skipMessage(message_type msg){setBitMessage (msg,true);};
95 void processMessage(message_type msg){setBitMessage (msg,false);};
96 //bool run(process_mode mode);
97 unsigned int run();
98 bool hasMessageToHandle();
102 MergNodeIdentification *getNodeId(){return &nodeId;};
103
104 bool initCanBus(unsigned int port,unsigned int rate, unsigned int retries,unsigned int retryIntervalMilliseconds);
106 void setUserHandlerFunction(userHandlerType f) {userHandler=f;};
107
108 void setDebug(bool debug);
109 void sendERRMessage(byte code);
110 bool hasThisEvent();
111 bool readCanBus();
112 bool readCanBus(byte buf_num);
113 bool readCanBus(byte *msg,byte *header,byte *length,byte buf_num);
114 void printSentMessage();
115 void printReceivedMessage();
117 void setSlimMode();
119 void setFlimMode();
121 byte getNodeMode(){return node_mode;};
123 void setUpNewMemory();
125 void dumpMemory(){memory.dumpMemory();};
127 void setLeds(byte green,byte yellow);
129 void setPushButton(byte pb) {push_button=pb;pinMode(pb,INPUT_PULLUP);};
130
136 void setStdNN(unsigned int val){std_nn=val;};
140 unsigned int getStdNN(){return std_nn;};
141 void saveNodeFlags();
142 void sendMessage(Message *msg);
143 //methods for getthing parameters
144 bool isAccOn();
145 bool isAccOff();
146 bool eventMatch(){return eventmatch;};
147 unsigned int getEventIndex(Message *msg);
148 byte getNodeVar(byte varIndex);
149 byte getEventVar(Message *msg,byte varIndex);
150
151
152 void setDeviceNumber(unsigned int val,byte port); //2 bytes representation for dn
153 unsigned int getDeviceNumber(byte port); //2 bytes representation
154 byte getDeviceNumberIndex(){return deviceNumberIdx;};
155 state getNodeState(){return state_mode;};
156 //send events
157 byte sendOnEvent(bool longEvent,unsigned int event);
158 byte sendOffEvent(bool longEvent,unsigned int event);
159 byte sendOnEvent1(bool longEvent,unsigned int event,byte var1);
160 byte sendOffEvent1(bool longEvent,unsigned int event,byte var1);
161 byte sendOnEvent2(bool longEvent,unsigned int event,byte var1,byte var2);
162 byte sendOffEvent2(bool longEvent,unsigned int event,byte var1,byte var2);
163 byte sendOnEvent3(bool longEvent,unsigned int event,byte var1,byte var2,byte var3);
164 byte sendOffEvent3(bool longEvent,unsigned int event,byte var1,byte var2,byte var3);
165
166 //set timmer interval
167 void setTimerInterval(long value){timerInterval=value;};
168 long getTimerInterval(){return timerInterval;}
169 //void startTimer();
170 //void stopTimer();
171 void cbusRead();
172
173 protected:
174 private:
175 //let the bus level lib private
176 MCP_CAN Can;
177 byte node_mode;
178 byte mergCanData[CANDATA_SIZE]; //can data . CANDATA_SIZE defined in message.h
179 Message message; //canbus message representation
180 MergNodeIdentification nodeId; //node identification:name,manufacuter , ...
181 byte messageFilter; //bit filter about each message to handle. by default avoid reserved messages
182 MergMemoryManagement memory; //organize the eeprom memory and maintain a copy in RAM
183 bool softwareEnum; //true if the node is doing self ennumeration
184 bool DEBUG; //true if debug mode
185 bool eventmatch; //true if the received message is found on learned events
186 unsigned long std_nn; //standard node number for slim
187 bool typeEventMatch; //true is long event, false is short event
188 byte deviceNumberIdx; //in case of match set the devicenumber index starting on 0
189 CircularBuffer msgBuffer;
190
191 state state_mode; //actual state of the node
192 unsigned long startTime; //used for self ennumeration
193 byte buffer[TEMP_BUFFER_SIZE]; //buffer to store can ids for self enum
194 byte bufferIndex; //index that indicates the buffer size
195 //unsigned int (*userHandler)(message*); //pointer to function
196 userHandlerType userHandler;
197 bool messageToHandle; //true if the message was not automatically handled
198
199 void setBitMessage(byte pos,bool val);
200 //void sendCanMessage();
201 void getStoredEvents(); //events that were learned
202 void getStoredIDs(); //node number,canId,device Number
203 bool matchEvent(); //
204 unsigned int runAutomatic(); //process the majority of events automatic
205
206 byte handleConfigMessages(); //process config messages
207 byte handleDCCMessages(); //process dcc messages
208 byte handleACCMessages(); //process accessory messages
209 byte handleGeneralMessages(); //process general messages
210
211
212 void finishSelfEnumeration(); //finish the self ennumeration procedures
213 void clearMsgToSend(); //clear internal buffer
214 byte sendCanMessage(); //send the message in the buffer
215 void loadMemory(); //load the saved data to ram. not the events
216
217 void sortArray(byte *a, byte n); //used in selfennum
218 void prepareMessage(byte opc); //fill the buffer with specific messages
219 void prepareMessageBuff(byte data0=0,byte data1=0,byte data2=0,byte data3=0,byte data4=0,byte data5=0,byte data6=0,byte data7=0);//fill the buffer byte by byte
220 byte getMessageSize(byte opc); //return the
221 //leds and the push button controls
222 void controlLeds();
223 byte greenLed;
224 byte yellowLed;
225 byte ledGreenState;
226 byte ledYellowState;
227 long ledtimer;
228 void controlPushButton();
229 byte push_button;
230 byte pb_state;
231
232 void(* resetFunc) (void); //declare reset function @ address 0. resets the arduino
233 void learnEvent(); //put the node in the learn event mode
234 unsigned int mainProcess();
235 void doSelfEnnumeration(bool soft); //put the node in the self enummeration mode
236 void doSetup(); // put the node in the setup mode
237 void doOutOfService(); //put the node in the out of service node
238 void initMemory(); //load the eprom memory
239 bool isSelfEnumMode(); //check if the node is in the self enum mode
240
244 void setNodeState(state newstate){ state_mode=newstate;};
245 byte accExtraData();
246 byte getAccExtraData(byte idx);//idx starts at 1
247
248 //timer functions for reading messages
249 long timerInterval;
250
251 };
252
253 #endif // MESSAGEPARSER_H
MergCBUS::getEventVar
byte getEventVar(Message *msg, byte varIndex)
Get the variable of a learned event.
Definition: MergCBUS.cpp:1492
MergCBUS::getNodeMode
byte getNodeMode()
Get the node mode.
Definition: MergCBUS.h:121
MergCBUS::getDeviceNumber
unsigned int getDeviceNumber(byte port)
Get the device number for a specific port.
Definition: MergCBUS.cpp:1524
MergCBUS::setUserHandlerFunction
void setUserHandlerFunction(userHandlerType f)
Set the user function to handle other messages.
Definition: MergCBUS.h:106
MergCBUS::setDebug
void setDebug(bool debug)
Put in debug mode.
Definition: MergCBUS.cpp:980
MergCBUS::isAccOff
bool isAccOff()
Definition: MergCBUS.cpp:1232
MergCBUS::processMessage
void processMessage(message_type msg)
Set to process certain message.
Definition: MergCBUS.h:95
MergCBUS::~MergCBUS
virtual ~MergCBUS()
Destructor Not used.
Definition: MergCBUS.cpp:82
MergCBUS::setPushButton
void setPushButton(byte pb)
Set the CBUS modules stardard push button.
Definition: MergCBUS.h:129
MergCBUS::setLeds
void setLeds(byte green, byte yellow)
Set the CBUS modules stardard leds.
Definition: MergCBUS.cpp:1168
MergCBUS::setSlimMode
void setSlimMode()
Set the node to slim mode.
Definition: MergCBUS.cpp:1251
MergCBUS::readCanBus
bool readCanBus()
Read the can bus from the circular buffer and load the data in the message object.
Definition: MergCBUS.cpp:333
MergCBUS::cbusRead
void cbusRead()
Read the can bus message and put in circular buffer. Used by the timer the buffer is 1 byte for the m...
Definition: MergCBUS.cpp:1621
MergCBUS::setDeviceNumber
void setDeviceNumber(unsigned int val, byte port)
Set the device number for a specific port.
Definition: MergCBUS.cpp:1515
MergCBUS::MergCBUS
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...
Definition: MergCBUS.cpp:11
MergMemoryManagement::dumpMemory
void dumpMemory()
Print all memory to serial. Used for debug.
Definition: MergMemoryManagement.cpp:500
MergCBUS::initCanBus
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...
Definition: MergCBUS.cpp:108
MergCBUS::getNodeId
MergNodeIdentification * getNodeId()
Get a reference to node identification.
Definition: MergCBUS.h:102
MergCBUS::setStdNN
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 let...
Definition: MergCBUS.h:136
MergCBUS::setUpNewMemory
void setUpNewMemory()
Reset EEPROM.
Definition: MergCBUS.cpp:1159
MergCBUS::dumpMemory
void dumpMemory()
Print all EEPROM values.
Definition: MergCBUS.h:125
MergCBUS::getNodeVar
byte getNodeVar(byte varIndex)
Get node variable by index.
Definition: MergCBUS.cpp:1481
MergCBUS
Definition: MergCBUS.h:80
MergCBUS::isAccOn
bool isAccOn()
Definition: MergCBUS.cpp:1225
MergCBUS::setFlimMode
void setFlimMode()
Set the node to flim mode.
Definition: MergCBUS.cpp:1261
MergCBUS::printReceivedMessage
void printReceivedMessage()
Print the received message buffer to serial. Used for debug.
Definition: MergCBUS.cpp:1141
MergCBUS::getEventIndex
unsigned int getEventIndex(Message *msg)
Get the Index in memory of an event.
Definition: MergCBUS.cpp:1470
MergCBUS::hasThisEvent
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.
Definition: MergCBUS.cpp:1093
Message
Definition: Message.h:53
MergCBUS::getStdNN
unsigned int getStdNN()
Get the standard node number.
Definition: MergCBUS.h:140
MCP_CAN
Definition: mcp_can.h:30
MergCBUS::run
unsigned int run()
Method that deals with the majority of messages and behavior. Auto enummeration, query requests and c...
Definition: MergCBUS.cpp:156
MergCBUS::userHandlerType
void(* userHandlerType)(Message *, MergCBUS *)
Definition: MergCBUS.h:84
MergCBUS::sendERRMessage
void sendERRMessage(byte code)
Send the error message with the code.
Definition: MergCBUS.cpp:1083
MergMemoryManagement
Definition: MergMemoryManagement.h:42
MergCBUS::skipMessage
void skipMessage(message_type msg)
Set to skip processing certain message.
Definition: MergCBUS.h:91
MergCBUS::printSentMessage
void printSentMessage()
Print the message to be sent to serial. Used for debug.
Definition: MergCBUS.cpp:1127