-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialLine_ArduinoUSB.cpp
215 lines (172 loc) · 5.51 KB
/
SerialLine_ArduinoUSB.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
//
// (c)2018 by Lucky Resistor. See LICENSE for details.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "SerialLine_ArduinoUSB.hpp"
#include "hal-common/Timer.hpp"
#include <Arduino.h>
namespace lr {
namespace {
/// Use a local reference to access the serial line.
/// This allows a simple change of the used variable.
///
auto &serialAccess = Serial;
/// The default baud rate to use. It is ignored.
///
const uint32_t cDefaultBaudRate = 115200;
/// Initial delay waiting for a connection.
///
const auto cInitialWait = 2000_ms;
/// Retry time after the the initial delay.
///
const auto cRetryWait = 5000_ms;
}
SerialLine_ArduinoUSB::SerialLine_ArduinoUSB()
: _state(State::Initialized), _connectDeadline(2000_ms)
{
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::initialize()
{
serialAccess.begin(cDefaultBaudRate); // The baud rate is ignored.
_state = State::Initialized;
_connectDeadline.restart(cInitialWait); // Maximum wat 3 seconds to initialization.
return Status::Success;
}
SerialLine_ArduinoUSB::DataSize SerialLine_ArduinoUSB::sendBytesAvailable() noexcept
{
if (!isReady()) {
return 0;
}
return serialAccess.availableForWrite();
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::send(uint8_t value) noexcept
{
if (!isReady()) {
return Status::Error;
}
auto writtenBytes = serialAccess.write(value);
return ((writtenBytes == 1) ? Status::Success : Status::Partial);
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::send(const uint8_t *data, DataSize dataSize, DataSize *dataSent) noexcept
{
if (!isReady()) {
return Status::Error;
}
auto writtenBytes = serialAccess.write(data, dataSize);
if (dataSent != nullptr) {
*dataSent = writtenBytes;
}
return ((writtenBytes == dataSize) ? Status::Success : Status::Partial);
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::sendReset() noexcept
{
if (!isReady()) {
return Status::Error;
}
serialAccess.flush(); // Best we ca do.
return Status::Success;
}
SerialLine_ArduinoUSB::DataSize SerialLine_ArduinoUSB::receiveBytesAvailable() noexcept
{
if (!isReady()) {
return 0;
}
return serialAccess.available();
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::receive(uint8_t &value) noexcept
{
if (!isReady()) {
return Status::Error;
}
const auto received = serialAccess.read();
if (received >= 0) {
value = static_cast<uint8_t>(received);
return Status::Success;
} else {
return Status::Partial;
}
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::receive(uint8_t *data, DataSize dataSize, DataSize *dataReceived) noexcept
{
if (!isReady()) {
return Status::Error;
}
const auto received = serialAccess.readBytes(reinterpret_cast<char*>(data), dataSize);
if (dataReceived != nullptr) {
*dataReceived = received;
}
return ((received == dataSize) ? Status::Success : Status::Partial);
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::receiveBlock(uint8_t *data, DataSize dataSize, uint8_t blockEndMark, DataSize *dataReceived) noexcept
{
if (!isReady()) {
return Status::Error;
}
DataSize readCount = 0;
int readByte;
do {
readByte = serialAccess.read();
if (readByte >= 0) {
data[readCount] = static_cast<uint8_t>(readByte);
readCount += 1;
}
} while (readByte > 0 && static_cast<uint8_t>(readByte) != blockEndMark && readCount < dataSize);
if (dataReceived != nullptr) {
*dataReceived = readCount;
}
return ((readCount == dataSize) ? Status::Success : Status::Partial);
}
SerialLine_ArduinoUSB::Status SerialLine_ArduinoUSB::receiveReset() noexcept
{
if (!isReady()) {
return Status::Error;
}
serialAccess.flush(); // Best we ca do.
return Status::Success;
}
bool SerialLine_ArduinoUSB::isReady()
{
if (_state == State::Open) {
return true;
} else if (_state == State::NoConnection) {
if (_connectDeadline.hasTimeout()) {
if (serialAccess) {
_state = State::Open;
return true;
}
_connectDeadline.restart(cRetryWait);
}
return false;
} else if (_state == State::Initialized) {
if (serialAccess) {
_state = State::Open;
return true;
}
if (_connectDeadline.hasTimeout()) {
// Give up for now.
_state = State::NoConnection;
_connectDeadline.restart(cRetryWait);
}
}
return false;
}
void SerialLine_ArduinoUSB::waitUntilReady()
{
while (!isReady() && _state == State::Initialized) {
Timer::delay(100_ms);
}
}
}