Paul Paterson
/
CanPipe_Example
Demonstration example for CanPipe Library.
Diff: main.cpp
- Revision:
- 1:7cf308c1484d
- Parent:
- 0:5abca479f387
--- a/main.cpp Fri Nov 04 13:54:35 2016 +0000 +++ b/main.cpp Mon Nov 07 02:16:45 2016 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "CanPipe.h" +#include "ExampleCanProtocol.h" Ticker ticker; DigitalOut led1(LED1); @@ -8,30 +9,49 @@ CanPipe m_can_pipe(&m_can); char counter = 0; - void send() { led1 = !led1; m_can_pipe.PostMessage(CANMessage(1337, &counter, 1)); + //m_can.write(CANMessage(1337, &counter, 1)); + ++counter; } -int callback1(CANMessage &msg) { return CanPipe::kOkay; } -int callback2(CANMessage &msg) { return CanPipe::kOkay; } -int callback3(CANMessage &msg) { return CanPipe::kOkay; } +/* Define user static callbacks */ +int user_cb1(CANMessage &msg) { printf("user_cb1\r\n"); return CanPipe::kOkay; } +int user_cb2(CANMessage &msg) { printf("user_cb2\r\n"); return CanPipe::kOkay; } +int user_cb3(CANMessage &msg) { printf("user_cb3\r\n"); return CanPipe::kOkay; } +/* main program */ int main() { + m_can.frequency(500000); + int handle; - handle = m_can_pipe.RegisterFilter(0x200, 0x780); - m_can_pipe.RegisterCallback(callback1, handle); + /* 1 is default hardware filter handle for ALL messages. override it. + * Otherwise, other filters will be skipped + */ + handle = m_can.filter(0x000, 0x7ff, CANAny, 1); + + /* react to message 0x400 */ + handle = m_can_pipe.RegisterFilter(0x400, 0x7ff, CANAny, 3); + m_can_pipe.RegisterCallback(CanMessageCallback(user_cb1), handle); - handle = m_can_pipe.RegisterFilter(0x281, 0x780); - m_can_pipe.RegisterCallback(callback2, handle); - m_can_pipe.RegisterCallback(callback3, handle); + /* react to messages 0x500 to 0x5FF */ + handle = m_can_pipe.RegisterFilter(0x500, 0x700, CANAny, 2); + m_can_pipe.RegisterCallback(CanMessageCallback(user_cb2), handle); + m_can_pipe.RegisterCallback(CanMessageCallback(user_cb3), handle); + /* CanProtocol callbacks will be called after the user callbacks */ + ExampleCanProtocol protocol; + protocol.RegisterProtocols(m_can_pipe); + + /* Schedule a message to be sent once a second */ ticker.attach(send, 1); while (1) { + /* We know there will be nothing to handle until there is an interrupt */ __WFI(); //sleep(); + /* Handle the messages as soon as we can */ m_can_pipe.HandleMessages(); } } \ No newline at end of file