Paul Paterson / Mbed 2 deprecated CanPipe_Example

Dependencies:   CanPipe mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "CanPipe.h"
00003 #include "ExampleCanProtocol.h"
00004 
00005 Ticker ticker;
00006 DigitalOut led1(LED1);
00007 
00008 CAN m_can(P0_11, P0_31);
00009 CanPipe m_can_pipe(&m_can);
00010 
00011 char counter = 0;
00012 void send() {
00013     led1 = !led1;
00014     m_can_pipe.PostMessage(CANMessage(1337, &counter, 1));
00015     //m_can.write(CANMessage(1337, &counter, 1));
00016     ++counter;
00017 }
00018 
00019 /* Define user static callbacks */
00020 int user_cb1(CANMessage &msg) { printf("user_cb1\r\n"); return CanPipe::kOkay; }
00021 int user_cb2(CANMessage &msg) { printf("user_cb2\r\n"); return CanPipe::kOkay; }
00022 int user_cb3(CANMessage &msg) { printf("user_cb3\r\n"); return CanPipe::kOkay; }
00023 
00024 /* main program */
00025 int main() {
00026     m_can.frequency(500000);
00027     
00028     int handle;
00029     
00030     /* 1 is default hardware filter handle for ALL messages.  override it.  
00031      * Otherwise, other filters will be skipped 
00032      */
00033     handle = m_can.filter(0x000, 0x7ff, CANAny, 1);
00034     
00035     /* react to message 0x400 */
00036     handle = m_can_pipe.RegisterFilter(0x400, 0x7ff, CANAny, 3);
00037     m_can_pipe.RegisterCallback(CanMessageCallback(user_cb1), handle);
00038     
00039     /* react to messages 0x500 to 0x5FF */
00040     handle = m_can_pipe.RegisterFilter(0x500, 0x700, CANAny, 2);
00041     m_can_pipe.RegisterCallback(CanMessageCallback(user_cb2), handle);
00042     m_can_pipe.RegisterCallback(CanMessageCallback(user_cb3), handle);
00043     
00044     /* CanProtocol callbacks will be called after the user callbacks */
00045     ExampleCanProtocol protocol;
00046     protocol.RegisterProtocols(m_can_pipe);
00047     
00048     /* Schedule a message to be sent once a second */
00049     ticker.attach(send, 1);
00050     
00051     while (1) {
00052         /* We know there will be nothing to handle until there is an interrupt */
00053         __WFI(); //sleep();
00054         /* Handle the messages as soon as we can */
00055         m_can_pipe.HandleMessages();
00056     }
00057 }