Paul Paterson
/
CanPipe_Example
Demonstration example for CanPipe Library.
ExampleCanProtocol.h@1:7cf308c1484d, 2016-11-07 (annotated)
- Committer:
- ptpaterson
- Date:
- Mon Nov 07 02:16:45 2016 +0000
- Revision:
- 1:7cf308c1484d
Cleaned up example for updates to CanPipe.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptpaterson | 1:7cf308c1484d | 1 | #ifndef EXAMPLE_CAN_PROTOCOL_H |
ptpaterson | 1:7cf308c1484d | 2 | #define EXAMPLE_CAN_PROTOCOL_H |
ptpaterson | 1:7cf308c1484d | 3 | |
ptpaterson | 1:7cf308c1484d | 4 | #include "CanPipe.h" |
ptpaterson | 1:7cf308c1484d | 5 | |
ptpaterson | 1:7cf308c1484d | 6 | /** example protocol class */ |
ptpaterson | 1:7cf308c1484d | 7 | class ExampleCanProtocol { |
ptpaterson | 1:7cf308c1484d | 8 | int adder_; |
ptpaterson | 1:7cf308c1484d | 9 | CanPipe* p_can_pipe_; |
ptpaterson | 1:7cf308c1484d | 10 | public: |
ptpaterson | 1:7cf308c1484d | 11 | ExampleCanProtocol(): adder_(0) |
ptpaterson | 1:7cf308c1484d | 12 | {} |
ptpaterson | 1:7cf308c1484d | 13 | |
ptpaterson | 1:7cf308c1484d | 14 | int adder_cb(CANMessage &msg) { |
ptpaterson | 1:7cf308c1484d | 15 | adder_ += msg.data[0]; |
ptpaterson | 1:7cf308c1484d | 16 | printf(" ExampleCanProtocol::adder_cb - %d\r\n", adder_); |
ptpaterson | 1:7cf308c1484d | 17 | return CanPipe::kOkay; |
ptpaterson | 1:7cf308c1484d | 18 | } |
ptpaterson | 1:7cf308c1484d | 19 | |
ptpaterson | 1:7cf308c1484d | 20 | int echo_cb(CANMessage &msg) { |
ptpaterson | 1:7cf308c1484d | 21 | CANMessage response(msg.id + 0x100); |
ptpaterson | 1:7cf308c1484d | 22 | response.len = msg.len; |
ptpaterson | 1:7cf308c1484d | 23 | response.format = CANStandard; |
ptpaterson | 1:7cf308c1484d | 24 | response.type = CANData; |
ptpaterson | 1:7cf308c1484d | 25 | memcpy(response.data, msg.data, msg.len); |
ptpaterson | 1:7cf308c1484d | 26 | p_can_pipe_->PostMessage(response); |
ptpaterson | 1:7cf308c1484d | 27 | |
ptpaterson | 1:7cf308c1484d | 28 | printf(" ExampleCanProtocol::echo_cb \r\n"); |
ptpaterson | 1:7cf308c1484d | 29 | for (int i = 0; i < msg.len; ++i) { |
ptpaterson | 1:7cf308c1484d | 30 | printf(" %d - 0x%02X\r\n", i, msg.data[i]); |
ptpaterson | 1:7cf308c1484d | 31 | } |
ptpaterson | 1:7cf308c1484d | 32 | return CanPipe::kOkay; |
ptpaterson | 1:7cf308c1484d | 33 | } |
ptpaterson | 1:7cf308c1484d | 34 | |
ptpaterson | 1:7cf308c1484d | 35 | void RegisterProtocols(CanPipe &can_pipe) { |
ptpaterson | 1:7cf308c1484d | 36 | p_can_pipe_ = &can_pipe; |
ptpaterson | 1:7cf308c1484d | 37 | |
ptpaterson | 1:7cf308c1484d | 38 | int handle; |
ptpaterson | 1:7cf308c1484d | 39 | |
ptpaterson | 1:7cf308c1484d | 40 | handle = p_can_pipe_->RegisterFilter(0x400, 0x7ff, CANAny, 3); |
ptpaterson | 1:7cf308c1484d | 41 | p_can_pipe_->RegisterCallback<ExampleCanProtocol>(this, &ExampleCanProtocol::adder_cb, handle); |
ptpaterson | 1:7cf308c1484d | 42 | |
ptpaterson | 1:7cf308c1484d | 43 | handle = p_can_pipe_->RegisterFilter(0x500, 0x700, CANAny, 2); |
ptpaterson | 1:7cf308c1484d | 44 | p_can_pipe_->RegisterCallback<ExampleCanProtocol>(this, &ExampleCanProtocol::echo_cb, handle); |
ptpaterson | 1:7cf308c1484d | 45 | } |
ptpaterson | 1:7cf308c1484d | 46 | }; |
ptpaterson | 1:7cf308c1484d | 47 | |
ptpaterson | 1:7cf308c1484d | 48 | #endif /* EXAMPLE_CAN_PROTOCOL_H */ |