forked from arduino/ArduinoCore-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanMsg.h
96 lines (74 loc) · 2.49 KB
/
CanMsg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#ifndef ARDUINOCORE_API_CAN_MSG_H_
#define ARDUINOCORE_API_CAN_MSG_H_
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include <inttypes.h>
#include <string.h>
#include <Arduino.h>
/**************************************************************************************
* NAMESPACE
**************************************************************************************/
namespace arduino
{
/**************************************************************************************
* CLASS DECLARATION
**************************************************************************************/
class CanMsg : public Printable
{
public:
static size_t constexpr MAX_DATA_LENGTH = 8;
CanMsg(uint32_t const can_id, uint8_t const can_data_len, uint8_t const * can_data_ptr)
: id{can_id}
, data_length{can_data_len}
, data{0}
{
memcpy(data, can_data_ptr, min(can_data_len, MAX_DATA_LENGTH));
}
CanMsg() : CanMsg(0, 0, nullptr) { }
CanMsg(CanMsg const & other)
{
this->id = other.id;
this->data_length = other.data_length;
memcpy(this->data, other.data, this->data_length);
}
virtual ~CanMsg() { }
void operator = (CanMsg const & other)
{
if (this == &other)
return;
this->id = other.id;
this->data_length = other.data_length;
memcpy(this->data, other.data, this->data_length);
}
virtual size_t printTo(Print & p) const override
{
char buf[20] = {0};
size_t len = 0;
/* Print the header. */
len = snprintf(buf, sizeof(buf), "[%08" PRIX32 "] (%d) : ", id, data_length);
size_t n = p.write(buf, len);
/* Print the data. */
for (size_t d = 0; d < data_length; d++)
{
len = snprintf(buf, sizeof(buf), "%02X", data[d]);
n += p.write(buf, len);
}
/* Wrap up. */
return n;
}
uint32_t id;
uint8_t data_length;
uint8_t data[MAX_DATA_LENGTH];
};
/**************************************************************************************
* NAMESPACE
**************************************************************************************/
} /* arduino */
#endif /* ARDUINOCORE_API_CAN_MSG_H_ */