Demonstration example for CanPipe Library.

Dependencies:   CanPipe mbed

Files at this revision

API Documentation at this revision

Comitter:
ptpaterson
Date:
Mon Nov 07 02:16:45 2016 +0000
Parent:
0:5abca479f387
Commit message:
Cleaned up example for updates to CanPipe.

Changed in this revision

CanPipe.lib Show annotated file Show diff for this revision Revisions of this file
ExampleCanProtocol.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CanPipe.lib	Fri Nov 04 13:54:35 2016 +0000
+++ b/CanPipe.lib	Mon Nov 07 02:16:45 2016 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/ptpaterson/code/CanPipe/#07d73bfb6bea
+http://developer.mbed.org/users/ptpaterson/code/CanPipe/#2ee06a07e10e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ExampleCanProtocol.h	Mon Nov 07 02:16:45 2016 +0000
@@ -0,0 +1,48 @@
+#ifndef EXAMPLE_CAN_PROTOCOL_H
+#define EXAMPLE_CAN_PROTOCOL_H
+
+#include "CanPipe.h"
+
+/** example protocol class */
+class ExampleCanProtocol {
+    int adder_;
+    CanPipe* p_can_pipe_;
+public:
+    ExampleCanProtocol(): adder_(0)
+    {}
+
+    int adder_cb(CANMessage &msg) { 
+        adder_ += msg.data[0];
+        printf("   ExampleCanProtocol::adder_cb - %d\r\n", adder_);
+        return CanPipe::kOkay; 
+    }
+    
+    int echo_cb(CANMessage &msg) {        
+        CANMessage response(msg.id + 0x100);
+        response.len = msg.len;
+        response.format = CANStandard;
+        response.type = CANData;
+        memcpy(response.data, msg.data, msg.len);
+        p_can_pipe_->PostMessage(response);
+        
+        printf("   ExampleCanProtocol::echo_cb \r\n");
+        for (int i = 0; i < msg.len; ++i) {
+            printf("      %d - 0x%02X\r\n", i, msg.data[i]);
+        }
+        return CanPipe::kOkay; 
+    }
+    
+    void RegisterProtocols(CanPipe &can_pipe) {
+        p_can_pipe_ = &can_pipe;
+        
+        int handle;
+        
+        handle = p_can_pipe_->RegisterFilter(0x400, 0x7ff, CANAny, 3);
+        p_can_pipe_->RegisterCallback<ExampleCanProtocol>(this, &ExampleCanProtocol::adder_cb, handle);
+        
+        handle = p_can_pipe_->RegisterFilter(0x500, 0x700, CANAny, 2);
+        p_can_pipe_->RegisterCallback<ExampleCanProtocol>(this, &ExampleCanProtocol::echo_cb, handle);
+    }
+};
+
+#endif /* EXAMPLE_CAN_PROTOCOL_H */
--- 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