-
Notifications
You must be signed in to change notification settings - Fork 22
/
CANController.cpp
216 lines (171 loc) · 3.22 KB
/
CANController.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "CANController.h"
CANControllerClass::CANControllerClass() :
_onReceive(NULL),
_packetBegun(false),
_txId(-1),
_txExtended(-1),
_txRtr(false),
_txDlc(0),
_txLength(0),
_rxId(-1),
_rxExtended(false),
_rxRtr(false),
_rxDlc(0),
_rxLength(0),
_rxIndex(0)
{
// overide Stream timeout value
setTimeout(0);
}
CANControllerClass::~CANControllerClass()
{
}
int CANControllerClass::begin(long /*baudRate*/)
{
_packetBegun = false;
_txId = -1;
_txRtr =false;
_txDlc = 0;
_txLength = 0;
_rxId = -1;
_rxRtr = false;
_rxDlc = 0;
_rxLength = 0;
_rxIndex = 0;
return 1;
}
void CANControllerClass::end()
{
}
int CANControllerClass::beginPacket(int id, int dlc, bool rtr)
{
if (id < 0 || id > 0x7FF) {
return 0;
}
if (dlc > 8) {
return 0;
}
_packetBegun = true;
_txId = id;
_txExtended = false;
_txRtr = rtr;
_txDlc = dlc;
_txLength = 0;
memset(_txData, 0x00, sizeof(_txData));
return 1;
}
int CANControllerClass::beginExtendedPacket(long id, int dlc, bool rtr)
{
if (id < 0 || id > 0x1FFFFFFF) {
return 0;
}
if (dlc > 8) {
return 0;
}
_packetBegun = true;
_txId = id;
_txExtended = true;
_txRtr = rtr;
_txDlc = dlc;
_txLength = 0;
memset(_txData, 0x00, sizeof(_txData));
return 1;
}
int CANControllerClass::endPacket()
{
if (!_packetBegun) {
return 0;
}
_packetBegun = false;
if (_txDlc >= 0) {
_txLength = _txDlc;
}
return 1;
}
int CANControllerClass::parsePacket()
{
return 0;
}
long CANControllerClass::packetId()
{
return _rxId;
}
bool CANControllerClass::packetExtended()
{
return _rxExtended;
}
bool CANControllerClass::packetRtr()
{
return _rxRtr;
}
int CANControllerClass::packetDlc()
{
return _rxDlc;
}
size_t CANControllerClass::write(uint8_t byte)
{
return write(&byte, sizeof(byte));
}
size_t CANControllerClass::write(const uint8_t *buffer, size_t size)
{
if (!_packetBegun) {
return 0;
}
if (size > (sizeof(_txData) - _txLength)) {
size = sizeof(_txData) - _txLength;
}
memcpy(&_txData[_txLength], buffer, size);
_txLength += size;
return size;
}
int CANControllerClass::available()
{
return (_rxLength - _rxIndex);
}
int CANControllerClass::read()
{
if (!available()) {
return -1;
}
return _rxData[_rxIndex++];
}
int CANControllerClass::peek()
{
if (!available()) {
return -1;
}
return _rxData[_rxIndex];
}
void CANControllerClass::flush()
{
}
void CANControllerClass::onReceive(void(*callback)(int))
{
_onReceive = callback;
}
int CANControllerClass::filter(int /*id*/, int /*mask*/)
{
return 0;
}
int CANControllerClass::filterExtended(long /*id*/, long /*mask*/)
{
return 0;
}
int CANControllerClass::observe()
{
return 0;
}
int CANControllerClass::loopback()
{
return 0;
}
int CANControllerClass::sleep()
{
return 0;
}
int CANControllerClass::wakeup()
{
return 0;
}