nana
CANMsg.h@1:34738eb16cf7, 2017-03-18 (annotated)
- Committer:
- hudakz
- Date:
- Sat Mar 18 23:09:30 2017 +0000
- Revision:
- 1:34738eb16cf7
- Parent:
- 0:3d11ed680b6a
- Child:
- 2:2b8425b12d05
Updated.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 1:34738eb16cf7 | 1 | #ifndef CANMSG_H |
hudakz | 1:34738eb16cf7 | 2 | #define CANMSG_H |
hudakz | 0:3d11ed680b6a | 3 | |
hudakz | 0:3d11ed680b6a | 4 | /* CAN message container. |
hudakz | 0:3d11ed680b6a | 5 | * Provides "<<" (append) and ">>" (extract) operators to simplyfy |
hudakz | 0:3d11ed680b6a | 6 | * adding/getting data items to/from a CAN message. |
hudakz | 0:3d11ed680b6a | 7 | * Usage is similar to the C++ io-stream operators. |
hudakz | 0:3d11ed680b6a | 8 | * Data length of CAN message is automatically updated when using "<<" or ">>" operators. |
hudakz | 0:3d11ed680b6a | 9 | * |
hudakz | 0:3d11ed680b6a | 10 | * See Wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> for demo. |
hudakz | 0:3d11ed680b6a | 11 | */ |
hudakz | 0:3d11ed680b6a | 12 | |
hudakz | 0:3d11ed680b6a | 13 | #include "CAN.h" |
hudakz | 0:3d11ed680b6a | 14 | |
hudakz | 0:3d11ed680b6a | 15 | class CANMsg : public CANMessage |
hudakz | 0:3d11ed680b6a | 16 | { |
hudakz | 0:3d11ed680b6a | 17 | public: |
hudakz | 0:3d11ed680b6a | 18 | /** Creates empty CAN message. |
hudakz | 0:3d11ed680b6a | 19 | */ |
hudakz | 0:3d11ed680b6a | 20 | CANMsg() : |
hudakz | 0:3d11ed680b6a | 21 | CANMessage(){ } |
hudakz | 0:3d11ed680b6a | 22 | |
hudakz | 0:3d11ed680b6a | 23 | /** Creates CAN message with specific content. |
hudakz | 0:3d11ed680b6a | 24 | */ |
hudakz | 0:3d11ed680b6a | 25 | CANMsg(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) : |
hudakz | 0:3d11ed680b6a | 26 | CANMessage(_id, _data, _len, _type, _format){ } |
hudakz | 0:3d11ed680b6a | 27 | |
hudakz | 0:3d11ed680b6a | 28 | /** Creates CAN remote message. |
hudakz | 0:3d11ed680b6a | 29 | */ |
hudakz | 0:3d11ed680b6a | 30 | CANMsg(int _id, CANFormat _format = CANStandard) : |
hudakz | 0:3d11ed680b6a | 31 | CANMessage(_id, _format){ } |
hudakz | 0:3d11ed680b6a | 32 | |
hudakz | 0:3d11ed680b6a | 33 | /** Clears CAN message content |
hudakz | 0:3d11ed680b6a | 34 | */ |
hudakz | 0:3d11ed680b6a | 35 | void clear(void) { |
hudakz | 0:3d11ed680b6a | 36 | len = 0; |
hudakz | 0:3d11ed680b6a | 37 | type = CANData; |
hudakz | 0:3d11ed680b6a | 38 | format = CANStandard; |
hudakz | 0:3d11ed680b6a | 39 | id = 0; |
hudakz | 0:3d11ed680b6a | 40 | memset(data, 0, 8); |
hudakz | 0:3d11ed680b6a | 41 | }; |
hudakz | 0:3d11ed680b6a | 42 | |
hudakz | 0:3d11ed680b6a | 43 | /** Append operator: Appends data (value) to CAN message |
hudakz | 0:3d11ed680b6a | 44 | */ |
hudakz | 0:3d11ed680b6a | 45 | template<class T> |
hudakz | 0:3d11ed680b6a | 46 | CANMsg &operator<<(const T val) { |
hudakz | 0:3d11ed680b6a | 47 | if(len + sizeof(T) <= 8) { |
hudakz | 0:3d11ed680b6a | 48 | *reinterpret_cast < T * > (&data[len]) = val; |
hudakz | 0:3d11ed680b6a | 49 | len += sizeof(T); |
hudakz | 0:3d11ed680b6a | 50 | } |
hudakz | 0:3d11ed680b6a | 51 | #if DEBUG |
hudakz | 0:3d11ed680b6a | 52 | else { |
hudakz | 0:3d11ed680b6a | 53 | printf("Error: Cannot append data - exceeding CAN data length!\r\n"); |
hudakz | 0:3d11ed680b6a | 54 | } |
hudakz | 0:3d11ed680b6a | 55 | #endif |
hudakz | 0:3d11ed680b6a | 56 | return *this; |
hudakz | 0:3d11ed680b6a | 57 | } |
hudakz | 0:3d11ed680b6a | 58 | |
hudakz | 0:3d11ed680b6a | 59 | /** Extract operator: Extracts data (value) from CAN message |
hudakz | 0:3d11ed680b6a | 60 | */ |
hudakz | 0:3d11ed680b6a | 61 | template<class T> |
hudakz | 0:3d11ed680b6a | 62 | CANMsg &operator>>(T& val) { |
hudakz | 0:3d11ed680b6a | 63 | if(sizeof(T) <= len) { |
hudakz | 0:3d11ed680b6a | 64 | val = *reinterpret_cast < T * > (&data[0]); |
hudakz | 0:3d11ed680b6a | 65 | len -= sizeof(T); |
hudakz | 0:3d11ed680b6a | 66 | memcpy(data, data + sizeof(T), len); |
hudakz | 0:3d11ed680b6a | 67 | } |
hudakz | 0:3d11ed680b6a | 68 | #if DEBUG |
hudakz | 0:3d11ed680b6a | 69 | else { |
hudakz | 0:3d11ed680b6a | 70 | printf("Error: Cannot extract data - exceeding CAN data length!\r\n"); |
hudakz | 0:3d11ed680b6a | 71 | } |
hudakz | 0:3d11ed680b6a | 72 | #endif |
hudakz | 0:3d11ed680b6a | 73 | return *this; |
hudakz | 0:3d11ed680b6a | 74 | } |
hudakz | 0:3d11ed680b6a | 75 | }; |
hudakz | 0:3d11ed680b6a | 76 | |
hudakz | 1:34738eb16cf7 | 77 | #endif // CANMSG_H |