TR-mbed
1.0
Loading...
Searching...
No Matches
core
util
communications
CANMsg.h
Go to the documentation of this file.
1
#ifndef CANMSG_H
2
#define CANMSG_H
3
4
/* CAN message container.
5
* Provides "<<" (append) and ">>" (extract) operators to simplyfy
6
* adding/getting data items to/from a CAN message.
7
* Usage is similar to the C++ io-stream operators.
8
* Data length of CAN message is automatically updated when using "<<" or ">>" operators.
9
*
10
* See Wiki page <https://developer.mbed.org/users/hudakz/code/CAN_Hello/> for demo.
11
*/
12
13
#include "CAN.h"
14
15
class
CANMsg
:
public
mbed::CANMessage
16
{
17
public
:
20
CANMsg
() :
21
CANMessage(){ }
22
25
CANMsg
(
int
_id,
const
char
*_data,
char
_len = 8, CANType _type = CANData, CANFormat _format = CANStandard) :
26
CANMessage(_id, _data, _len, _type, _format){ }
27
30
CANMsg
(
int
_id, CANFormat _format = CANStandard) :
31
CANMessage(_id, _format){ }
32
35
void
clear
(
void
) {
36
len = 0;
37
type = CANData;
38
format = CANStandard;
39
id
= 0;
40
memset(data, 0, 8);
41
};
42
45
template
<
class
T>
46
CANMsg
&
operator<<
(
const
T val) {
47
MBED_ASSERT(len +
sizeof
(T) <= 8);
48
memcpy(&data[len], &val,
sizeof
(T));
49
len +=
sizeof
(T);
50
return
*
this
;
51
}
52
55
template
<
class
T>
56
CANMsg
&
operator>>
(T& val) {
57
//printf("Size:%d\tmax length:%d\n", sizeof(T), len);
58
MBED_ASSERT(
sizeof
(T) <= len);
59
if
(
sizeof
(T) > len) {
60
memcpy(&val, data, len);
61
len = 0;
62
}
63
else
{
64
memcpy(&val, data,
sizeof
(T));
65
len -=
sizeof
(T);
66
}
67
memcpy(data, data +
sizeof
(T), len);
68
return
*
this
;
69
}
70
};
71
72
#endif
// CANMSG_H
73
74
CANMsg
Definition
CANMsg.h:16
CANMsg::CANMsg
CANMsg(int _id, CANFormat _format=CANStandard)
Definition
CANMsg.h:30
CANMsg::operator>>
CANMsg & operator>>(T &val)
Definition
CANMsg.h:56
CANMsg::clear
void clear(void)
Definition
CANMsg.h:35
CANMsg::operator<<
CANMsg & operator<<(const T val)
Definition
CANMsg.h:46
CANMsg::CANMsg
CANMsg(int _id, const char *_data, char _len=8, CANType _type=CANData, CANFormat _format=CANStandard)
Definition
CANMsg.h:25
CANMsg::CANMsg
CANMsg()
Definition
CANMsg.h:20
Generated by
1.9.8