-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComm_Arduino.h
243 lines (203 loc) · 4.17 KB
/
Comm_Arduino.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
////////////////////////////////////////////////////////////
// Serial port handler for the AGV
////////////////////////////////////////////////////////////
#ifndef Comm_Arduino_h
#define Comm_Arduino_h
////////////////////////////////////////////////////////////
// Arduino comm interface (serial port)
////////////////////////////////////////////////////////////
#define CComm_Arduino_SERIAL_PORT_CLOSED (-1)
class CComm_Arduino : public ICommunicationInterface
{
private:
int m_serialID;
CSlidingBuffer m_transmitBuf;
CSlidingBuffer m_receiveBuf;
public:
CComm_Arduino()
{
m_serialID = CComm_Arduino_SERIAL_PORT_CLOSED;
m_transmitBuf.setCanGrow(false);
m_receiveBuf.setCanGrow(false);
}
~CComm_Arduino()
{
if(m_serialID >= 0)
close();
}
unsigned int read(unsigned char *_buf, unsigned int _bufSize, bool _consume = true)
{
unsigned int uiAmountRead = m_receiveBuf.read(_buf, _bufSize, _consume);
return uiAmountRead;
}
unsigned int write(const unsigned char *_buf, unsigned int _bufSize)
{
return m_transmitBuf.write(_buf, _bufSize);
}
int getError()
{
return 0;
}
int bytesInReceiveBuffer()
{
return m_receiveBuf.bytesAvailable();
}
int bytesInTransmitBuffer()
{
return m_transmitBuf.bytesAvailable();
}
int gets(char *_buf, int _bufSize)
{
return m_receiveBuf.gets(_buf, _bufSize);
}
bool puts(const char * _buf)
{
return m_transmitBuf.puts(_buf);
}
bool open(int _iSerialID, unsigned long _buad)
{
if(m_serialID >= 0)
close();
switch(_iSerialID)
{
case 0:
m_serialID = 0;
Serial.begin(_buad);
break;
case 1:
m_serialID = 1;
Serial1.begin(_buad);
break;
case 2:
m_serialID = 2;
Serial2.begin(_buad);
break;
case 3:
m_serialID = 3;
Serial3.begin(_buad);
break;
default:
m_serialID = CComm_Arduino_SERIAL_PORT_CLOSED;
break;
}
if(m_serialID >= 0)
return true;
return false;
}
void close()
{
switch(m_serialID)
{
case 0:
m_serialID = 0;
Serial.end();
break;
case 1:
m_serialID = 1;
Serial1.end();
break;
case 2:
m_serialID = 2;
Serial2.end();
break;
case 3:
m_serialID = 3;
Serial3.end();
break;
default:
m_serialID = CComm_Arduino_SERIAL_PORT_CLOSED;
break;
}
m_serialID = CComm_Arduino_SERIAL_PORT_CLOSED;
}
void tick()
{
unsigned byteCount;
unsigned char data[512];
switch(m_serialID)
{
case 0:
byteCount = Serial.available();
if(byteCount)
{
Serial.readBytes(data, byteCount);
m_receiveBuf.write(data, byteCount);
}
byteCount = m_transmitBuf.bytesAvailable();
if(byteCount > sizeof(data))
byteCount = sizeof(data);
m_transmitBuf.read(data, byteCount);
Serial.write(data, byteCount);
break;
case 1:
byteCount = Serial1.available();
if(byteCount)
{
Serial1.readBytes(data, byteCount);
m_receiveBuf.write(data, byteCount);
}
byteCount = m_transmitBuf.bytesAvailable();
if(byteCount > sizeof(data))
byteCount = sizeof(data);
m_transmitBuf.read(data, byteCount);
Serial1.write(data, byteCount);
break;
case 2:
byteCount = Serial2.available();
if(byteCount)
{
Serial2.readBytes(data, byteCount);
m_receiveBuf.write(data, byteCount);
}
byteCount = m_transmitBuf.bytesAvailable();
if(byteCount > sizeof(data))
byteCount = sizeof(data);
m_transmitBuf.read(data, byteCount);
Serial2.write(data, byteCount);
break;
case 3:
byteCount = Serial3.available();
if(byteCount)
{
Serial3.readBytes(data, byteCount);
m_receiveBuf.write(data, byteCount);
}
byteCount = m_transmitBuf.bytesAvailable();
if(byteCount > sizeof(data))
byteCount = sizeof(data);
m_transmitBuf.read(data, byteCount);
Serial3.write(data, byteCount);
break;
default:
break;
}
}
bool wantsTick()
{
if(m_transmitBuf.bytesAvailable())
return true;
switch(m_serialID)
{
case 0:
if(Serial.available())
return true;
break;
case 1:
if(Serial1.available())
return true;
break;
case 2:
if(Serial2.available())
return true;
break;
case 3:
if(Serial3.available())
return true;
break;
default:
return false;
}
return false;
}
};
#endif