CAN message container. Defines "<<" and ">>" operators to simplify adding/getting data to/from a CAN message.

Dependents:   grove_stream_jpa_sd2 grove_stream_jpa_sd2 grove_stream_jpa_sd2-2 grove_stream_jpa_sd2-3

Committer:
38domo
Date:
Mon Aug 31 17:49:15 2020 +0000
Revision:
5:26efbc3fd451
Parent:
4:5017a65ff423
discof746 avec can, ethernet, lcd, ts

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 4:5017a65ff423 15 class CANMsg : public mbed::CANMessage
hudakz 0:3d11ed680b6a 16 {
hudakz 0:3d11ed680b6a 17 public:
38domo 5:26efbc3fd451 18 /** Creates empty CAN message. */
hudakz 0:3d11ed680b6a 19 CANMsg() :
hudakz 0:3d11ed680b6a 20 CANMessage(){ }
38domo 5:26efbc3fd451 21 /** Creates CAN message with specific content. */
hudakz 0:3d11ed680b6a 22 CANMsg(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) :
hudakz 0:3d11ed680b6a 23 CANMessage(_id, _data, _len, _type, _format){ }
38domo 5:26efbc3fd451 24 /** Creates CAN remote message. */
hudakz 0:3d11ed680b6a 25 CANMsg(int _id, CANFormat _format = CANStandard) :
hudakz 0:3d11ed680b6a 26 CANMessage(_id, _format){ }
38domo 5:26efbc3fd451 27 /** Clears CAN message content */
hudakz 0:3d11ed680b6a 28 void clear(void) {
hudakz 0:3d11ed680b6a 29 len = 0;
hudakz 0:3d11ed680b6a 30 type = CANData;
hudakz 0:3d11ed680b6a 31 format = CANStandard;
hudakz 0:3d11ed680b6a 32 id = 0;
hudakz 0:3d11ed680b6a 33 memset(data, 0, 8);
hudakz 0:3d11ed680b6a 34 };
38domo 5:26efbc3fd451 35 /** Append operator: Appends data (value) to CAN message */
hudakz 0:3d11ed680b6a 36 template<class T>
hudakz 0:3d11ed680b6a 37 CANMsg &operator<<(const T val) {
hudakz 2:2b8425b12d05 38 MBED_ASSERT(len + sizeof(T) <= 8);
hudakz 3:4e42fdc0459f 39 memcpy(&data[len], &val, sizeof(T));
hudakz 2:2b8425b12d05 40 len += sizeof(T);
hudakz 2:2b8425b12d05 41 return *this;
hudakz 0:3d11ed680b6a 42 }
38domo 5:26efbc3fd451 43 /** Extract operator: Extracts data (value) from CAN message */
hudakz 0:3d11ed680b6a 44 template<class T>
hudakz 0:3d11ed680b6a 45 CANMsg &operator>>(T& val) {
hudakz 2:2b8425b12d05 46 MBED_ASSERT(sizeof(T) <= len);
hudakz 4:5017a65ff423 47 if (sizeof(T) > len) {
hudakz 4:5017a65ff423 48 memcpy(&val, data, len);
hudakz 4:5017a65ff423 49 len = 0;
hudakz 4:5017a65ff423 50 }
hudakz 4:5017a65ff423 51 else {
hudakz 4:5017a65ff423 52 memcpy(&val, data, sizeof(T));
hudakz 4:5017a65ff423 53 len -= sizeof(T);
hudakz 4:5017a65ff423 54 }
hudakz 2:2b8425b12d05 55 memcpy(data, data + sizeof(T), len);
hudakz 0:3d11ed680b6a 56 return *this;
hudakz 0:3d11ed680b6a 57 }
hudakz 0:3d11ed680b6a 58 };
hudakz 0:3d11ed680b6a 59
hudakz 1:34738eb16cf7 60 #endif // CANMSG_H