Mangue Baja team's code to frontal ECU

Committer:
einsteingustavo
Date:
Wed Jul 24 20:03:52 2019 +0000
Revision:
0:12fb9cbcabcc
Mangue Baja team's code to frontal ECU

Who changed what in which revision?

UserRevisionLine numberNew contents of line
einsteingustavo 0:12fb9cbcabcc 1 #ifndef CANMSG_H
einsteingustavo 0:12fb9cbcabcc 2 #define CANMSG_H
einsteingustavo 0:12fb9cbcabcc 3
einsteingustavo 0:12fb9cbcabcc 4 /* CAN message container.
einsteingustavo 0:12fb9cbcabcc 5 * Provides "<<" (append) and ">>" (extract) operators to simplyfy
einsteingustavo 0:12fb9cbcabcc 6 * adding/getting data items to/from a CAN message.
einsteingustavo 0:12fb9cbcabcc 7 * Usage is similar to the C++ io-stream operators.
einsteingustavo 0:12fb9cbcabcc 8 * Data length of CAN message is automatically updated when using "<<" or ">>" operators.
einsteingustavo 0:12fb9cbcabcc 9 *
einsteingustavo 0:12fb9cbcabcc 10 * See Wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> for demo.
einsteingustavo 0:12fb9cbcabcc 11 */
einsteingustavo 0:12fb9cbcabcc 12 #include "CAN.h"
einsteingustavo 0:12fb9cbcabcc 13
einsteingustavo 0:12fb9cbcabcc 14 class CANMsg : public CANMessage
einsteingustavo 0:12fb9cbcabcc 15 {
einsteingustavo 0:12fb9cbcabcc 16 public:
einsteingustavo 0:12fb9cbcabcc 17 /** Creates empty CAN message.
einsteingustavo 0:12fb9cbcabcc 18 */
einsteingustavo 0:12fb9cbcabcc 19 CANMsg() :
einsteingustavo 0:12fb9cbcabcc 20 CANMessage(){ }
einsteingustavo 0:12fb9cbcabcc 21
einsteingustavo 0:12fb9cbcabcc 22 /** Creates CAN message with specific content.
einsteingustavo 0:12fb9cbcabcc 23 */
einsteingustavo 0:12fb9cbcabcc 24 CANMsg(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) :
einsteingustavo 0:12fb9cbcabcc 25 CANMessage(_id, _data, _len, _type, _format){ }
einsteingustavo 0:12fb9cbcabcc 26
einsteingustavo 0:12fb9cbcabcc 27 /** Creates CAN remote message.
einsteingustavo 0:12fb9cbcabcc 28 */
einsteingustavo 0:12fb9cbcabcc 29 CANMsg(int _id, CANFormat _format = CANStandard) :
einsteingustavo 0:12fb9cbcabcc 30 CANMessage(_id, _format){ }
einsteingustavo 0:12fb9cbcabcc 31
einsteingustavo 0:12fb9cbcabcc 32 /** Clears CAN message content
einsteingustavo 0:12fb9cbcabcc 33 */
einsteingustavo 0:12fb9cbcabcc 34 void clear(int new_id) {
einsteingustavo 0:12fb9cbcabcc 35 len = 0;
einsteingustavo 0:12fb9cbcabcc 36 type = CANData;
einsteingustavo 0:12fb9cbcabcc 37 format = CANStandard;
einsteingustavo 0:12fb9cbcabcc 38 id = new_id;
einsteingustavo 0:12fb9cbcabcc 39 memset(data, 0, 8);
einsteingustavo 0:12fb9cbcabcc 40 };
einsteingustavo 0:12fb9cbcabcc 41
einsteingustavo 0:12fb9cbcabcc 42 /** Append operator: Appends data (value) to CAN message
einsteingustavo 0:12fb9cbcabcc 43 */
einsteingustavo 0:12fb9cbcabcc 44 template<class T>
einsteingustavo 0:12fb9cbcabcc 45 CANMsg &operator<<(const T val) {
einsteingustavo 0:12fb9cbcabcc 46 MBED_ASSERT(len + sizeof(T) <= 8);
einsteingustavo 0:12fb9cbcabcc 47 //*reinterpret_cast<T*>(&data[len]) = val; // This used to work but now it hangs at run time.
einsteingustavo 0:12fb9cbcabcc 48 memcpy(&data[len], &val, sizeof(T));
einsteingustavo 0:12fb9cbcabcc 49 len += sizeof(T);
einsteingustavo 0:12fb9cbcabcc 50 return *this;
einsteingustavo 0:12fb9cbcabcc 51 }
einsteingustavo 0:12fb9cbcabcc 52
einsteingustavo 0:12fb9cbcabcc 53 /** Extract operator: Extracts data (value) from CAN message
einsteingustavo 0:12fb9cbcabcc 54 */
einsteingustavo 0:12fb9cbcabcc 55 template<class T>
einsteingustavo 0:12fb9cbcabcc 56 CANMsg &operator>>(T& val) {
einsteingustavo 0:12fb9cbcabcc 57 MBED_ASSERT(sizeof(T) <= len);
einsteingustavo 0:12fb9cbcabcc 58 val = *reinterpret_cast<T*>(&data[0]);
einsteingustavo 0:12fb9cbcabcc 59 len -= sizeof(T);
einsteingustavo 0:12fb9cbcabcc 60 memcpy(data, data + sizeof(T), len);
einsteingustavo 0:12fb9cbcabcc 61 return *this;
einsteingustavo 0:12fb9cbcabcc 62 }
einsteingustavo 0:12fb9cbcabcc 63 };
einsteingustavo 0:12fb9cbcabcc 64
einsteingustavo 0:12fb9cbcabcc 65 #endif // CANMSG_H
einsteingustavo 0:12fb9cbcabcc 66
einsteingustavo 0:12fb9cbcabcc 67