aa

Committer:
hudakz
Date:
Thu Mar 29 18:56:19 2018 +0000
Revision:
2:2b8425b12d05
Parent:
1:34738eb16cf7
Child:
3:4e42fdc0459f
Updated.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:2b8425b12d05 47 MBED_ASSERT(len + sizeof(T) <= 8);
hudakz 2:2b8425b12d05 48 *reinterpret_cast<T*>(&data[len]) = val;
hudakz 2:2b8425b12d05 49 len += sizeof(T);
hudakz 2:2b8425b12d05 50 return *this;
hudakz 0:3d11ed680b6a 51 }
hudakz 0:3d11ed680b6a 52
hudakz 0:3d11ed680b6a 53 /** Extract operator: Extracts data (value) from CAN message
hudakz 0:3d11ed680b6a 54 */
hudakz 0:3d11ed680b6a 55 template<class T>
hudakz 0:3d11ed680b6a 56 CANMsg &operator>>(T& val) {
hudakz 2:2b8425b12d05 57 MBED_ASSERT(sizeof(T) <= len);
hudakz 2:2b8425b12d05 58 val = *reinterpret_cast<T*>(&data[0]);
hudakz 2:2b8425b12d05 59 len -= sizeof(T);
hudakz 2:2b8425b12d05 60 memcpy(data, data + sizeof(T), len);
hudakz 0:3d11ed680b6a 61 return *this;
hudakz 0:3d11ed680b6a 62 }
hudakz 0:3d11ed680b6a 63 };
hudakz 0:3d11ed680b6a 64
hudakz 1:34738eb16cf7 65 #endif // CANMSG_H