-
Notifications
You must be signed in to change notification settings - Fork 6
/
ESPEasySerial.cpp
264 lines (224 loc) · 5.25 KB
/
ESPEasySerial.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "ESPeasySerial.h"
#include "Port_ESPEasySerial_HardwareSerial.h"
#if USES_I2C_SC16IS752
# include "Port_ESPEasySerial_I2C_SC16IS752.h"
#endif // if USES_I2C_SC16IS752
#if USES_SW_SERIAL
# include "Port_ESPEasySerial_SW_Serial.h"
#endif // if USES_SW_SERIAL
#if USES_HWCDC
# include "Port_ESPEasySerial_USB_HWCDC.h"
#endif // if USES_HWCDC
#if USES_USBCDC
# include "Port_ESPEasySerial_USBCDC.h"
#endif // if USES_USBCDC
Port_ESPEasySerial_base * ESPeasySerial::ESPEasySerial_Port_factory(const ESPEasySerialConfig& config)
{
switch (config.port) {
#if USES_SW_SERIAL
case ESPEasySerialPort::software:
{
return new (std::nothrow) Port_ESPEasySerial_SW_Serial_t(config);
}
#endif // if USES_SW_SERIAL
#if USES_I2C_SC16IS752
case ESPEasySerialPort::sc16is752:
{
return new (std::nothrow) Port_ESPEasySerial_I2C_SC16IS752_t(config);
}
#endif // if USES_I2C_SC16IS752
#if USES_HWCDC
case ESPEasySerialPort::usb_hw_cdc:
{
return new (std::nothrow) Port_ESPEasySerial_USB_HWCDC_t(config);
}
#endif // if USES_HWCDC
#if USES_USBCDC
case ESPEasySerialPort::usb_cdc_0:
// case ESPEasySerialPort::usb_cdc_1:
{
return new (std::nothrow) Port_ESPEasySerial_USBCDC_t(config);
}
#endif // if USES_USBCDC
default:
if (isHWserial(config.port)) {
Port_ESPEasySerial_HardwareSerial_t *hw = new (std::nothrow) Port_ESPEasySerial_HardwareSerial_t();
if (hw != nullptr) {
hw->resetConfig(config);
return hw;
}
}
break;
}
return nullptr;
}
ESPeasySerial::ESPeasySerial(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic,
unsigned int buffSize,
bool forceSWserial)
: Stream()
{
ESPEasySerialConfig config;
config.port = port;
config.receivePin = receivePin;
config.transmitPin = transmitPin;
config.inverse_logic = inverse_logic;
config.rxBuffSize = buffSize;
config.txBuffSize = buffSize;
config.forceSWserial = forceSWserial;
config.validate();
_serialPort = ESPEasySerial_Port_factory(config);
// FIXME TD-er: Should it call begin() ???
if (_serialPort != nullptr) {
// _serialPort->begin(_baud);
}
}
ESPeasySerial::ESPeasySerial(ESPEasySerialConfig config)
: Stream()
{
config.validate();
_serialPort = ESPEasySerial_Port_factory(config);
}
ESPeasySerial::~ESPeasySerial() {
flush();
end();
if (_serialPort != nullptr) {
delete _serialPort;
}
}
void ESPeasySerial::begin(unsigned long baud)
{
if (isValid()) {
_serialPort->begin(baud);
}
}
#ifdef ESP8266
void ESPeasySerial::begin(unsigned long baud,
SerialConfig config,
SerialMode mode)
{
if (isValid()) {
_serialPort->setPortConfig(baud, config, mode);
_serialPort->begin(baud);
}
}
#endif // ifdef ESP8266
#ifdef ESP32
void ESPeasySerial::begin(unsigned long baud, uint32_t config)
{
if (isValid()) {
_serialPort->setPortConfig(baud, config);
_serialPort->begin(baud);
}
}
#endif // ifdef ESP32
void ESPeasySerial::end() {
if (isValid()) {
flush();
_serialPort->end();
}
}
int ESPeasySerial::peek(void)
{
if (!isValid()) {
return -1;
}
return _serialPort->peek();
}
size_t ESPeasySerial::write(uint8_t val)
{
if (!isValid()) {
return 0;
}
return _serialPort->write(val);
}
size_t ESPeasySerial::write(const uint8_t *buffer, size_t size)
{
if (!isValid() || !buffer) {
return 0;
}
return _serialPort->write(buffer, size);
}
size_t ESPeasySerial::write(const char *buffer)
{
if (!buffer) { return 0; }
return write(buffer, strlen_P(buffer));
}
int ESPeasySerial::read(void)
{
if (!isValid()) {
return -1;
}
return _serialPort->read();
}
int ESPeasySerial::available(void)
{
if (!isValid()) {
return 0;
}
return _serialPort->available();
}
int ESPeasySerial::availableForWrite(void)
{
if (!isValid()) {
return 0;
}
return _serialPort->availableForWrite();
}
void ESPeasySerial::flush(void)
{
if (isValid()) {
_serialPort->flush();
}
}
int ESPeasySerial::getBaudRate() const
{
if (!isValid()) {
return 0;
}
return _serialPort->getBaudRate();
}
ESPeasySerial::operator bool() const
{
if (isValid()) {
const bool res = (*_serialPort);
return res;
}
return false;
}
bool ESPeasySerial::connected() const
{
if (isValid()) {
const bool res = (*_serialPort);
return res;
}
return false;
}
void ESPeasySerial::setDebugOutput(bool enable)
{
if (isValid()) {
_serialPort->setDebugOutput(enable);
}
}
bool ESPeasySerial::isTxEnabled(void)
{
return getTxPin() != -1;
}
bool ESPeasySerial::isRxEnabled(void)
{
return getRxPin() != -1;
}
// Not supported in ESP32, since only HW serial is used.
// Function included since it is used in some libraries.
bool ESPeasySerial::listen() {
return true;
}
String ESPeasySerial::getLogString() const {
return getSerialConfig().getLogString();
}
bool ESPeasySerial::isValid() const {
// FIXME TD-er: Must call isValid() on the individual _serialPort types
return _serialPort != nullptr;
}