forked from TD-er/ESPEasySerial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPeasySerial.h
290 lines (230 loc) · 8.99 KB
/
ESPeasySerial.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
ESPeasySerial.h
ESPeasySerial.cpp - Wrapper for Arduino SoftwareSerial and HardwareSerial for ESP8266 and ESP32
Copyright (c) 2018 Gijs Noorlander. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ESPeasySerial_h
#define ESPeasySerial_h
#include <Arduino.h>
#include <HardwareSerial.h>
#include <inttypes.h>
#include <Stream.h>
#ifndef DISABLE_SC16IS752_Serial
#include <ESPEasySC16IS752_Serial.h>
#endif
#if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ESP32)
# ifndef DISABLE_SOFTWARE_SERIAL
# define DISABLE_SOFTWARE_SERIAL
# endif // ifndef DISABLE_SOFTWARE_SERIAL
#endif // if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ESP32)
#if !defined(DISABLE_SOFTWARE_SERIAL) && defined(ESP8266)
# include <ESPEasySoftwareSerial.h>
# include "ESPEasySerialConfig.h"
#endif
#ifdef ESP32
# define NR_ESPEASY_SERIAL_TYPES 4 // Serial 0, 1, 2, sc16is752
#endif // ifdef ESP32
#ifdef ESP8266
#if !defined(DISABLE_SOFTWARE_SERIAL)
# define NR_ESPEASY_SERIAL_TYPES 5 // Serial 0, 1, 0_swap, software, sc16is752
#else // if !defined(DISABLE_SOFTWARE_SERIAL)
# define NR_ESPEASY_SERIAL_TYPES 3 // Serial 0, 1, 0_swap
#endif // if !defined(DISABLE_SOFTWARE_SERIAL)
#endif
#ifndef ESP32
# if defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
# ifndef CORE_2_4_X
# define CORE_2_4_X
# endif // ifndef CORE_2_4_X
# endif // if defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1) || defined(ARDUINO_ESP8266_RELEASE_2_4_2)
# if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1)
# ifndef CORE_PRE_2_4_2
# define CORE_PRE_2_4_2
# endif // ifndef CORE_PRE_2_4_2
# endif // if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_0) || defined(ARDUINO_ESP8266_RELEASE_2_4_1)
# if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(CORE_2_4_X)
# ifndef CORE_PRE_2_5_0
# define CORE_PRE_2_5_0
# endif // ifndef CORE_PRE_2_5_0
# else // if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(CORE_2_4_X)
# ifndef CORE_POST_2_5_0
# define CORE_POST_2_5_0
# endif // ifndef CORE_POST_2_5_0
# endif // if defined(ARDUINO_ESP8266_RELEASE_2_3_0) || defined(CORE_2_4_X)
#endif // ESP32
#include "ESPEasySerialPort.h"
#include "ESPEasySerialType.h"
class ESPeasySerial : public Stream {
public:
#ifdef ESP8266
// ESP82xx has 2 HW serial ports and option for several software serial ports.
// Serial0: RX: 3 TX: 1
// Serial0 swapped RX: 13 TX: 15
// Serial1: RX: -- TX: 2 (TX only)
// SC16IS752: Rx: I2C addr TX: channel (A = 0, B = 1)
ESPeasySerial(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic = false,
unsigned int buffSize = 64,
bool forceSWserial = false);
virtual ~ESPeasySerial();
// Same parameters as the constructor, to allow to reconfigure the ESPEasySerial object to be another type of port.
void resetConfig(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic = false,
unsigned int buffSize = 64,
bool forceSWserial = false);
// If baud rate is set to 0, it will perform an auto-detect on the baudrate
void begin(unsigned long baud,
SerialConfig config = SERIAL_8N1,
SerialMode mode = SERIAL_FULL);
#endif // ifdef ESP8266
#ifdef ESP32
// ESP32 has 3 HW serial ports.
// Serial0: RX: 3 TX: 1
// Serial1: RX: 9 TX: 10 Defaults will never work, share pins with flash
// Serial2: RX: 16 TX: 17
// Pins set in the constructor will be used as override when not given when calling begin()
// @param inverse_logic can be used to set the logic in the constructor which will then be used in the call to begin.
// This makes the call to the constructor more in line with the constructor of SoftwareSerial.
// buffsize is for compatibility reasons. ESP32 cannot set the buffer size.
ESPeasySerial(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic = false,
unsigned int buffSize = 64);
virtual ~ESPeasySerial();
// Same parameters as the constructor, to allow to reconfigure the ESPEasySerial object to be another type of port.
void resetConfig(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic = false,
unsigned int buffSize = 64);
// If baud rate is set to 0, it will perform an auto-detect on the baudrate
void begin(unsigned long baud,
uint32_t config = SERIAL_8N1,
int8_t rxPin = -1,
int8_t txPin = -1,
bool invert = false,
unsigned long timeout_ms = 20000UL);
#endif // ifdef ESP32
void end();
int peek(void);
size_t write(uint8_t val) override;
int read(void) override;
int available(void) override;
int availableForWrite(void);
void flush(void) override;
#if defined(ESP8266)
bool overflow(); // SoftwareSerial ESP8266
bool hasOverrun(void); // HardwareSerial ESP8266
#endif // if defined(ESP8266)
/*
// FIXME TD-er: See https://www.artima.com/cppsource/safebool.html
operator bool() {
if (!isValid()) {
return false;
}
if (isSWserial()) {
return _swserial->bool();
} else {
return getHW()->bool();
}
}
*/
// HardwareSerial specific:
size_t write(const uint8_t *buffer,
size_t size);
size_t write(const char *buffer);
int baudRate(void);
#if defined(ESP8266)
void swap() {
swap(_transmitPin);
}
void swap(uint8_t tx_pin);
size_t readBytes(char *buffer,
size_t size) override;
size_t readBytes(uint8_t *buffer,
size_t size) override;
#endif
void setDebugOutput(bool);
bool isTxEnabled(void);
bool isRxEnabled(void);
#if defined(ESP8266)
bool hasRxError(void);
void startDetectBaudrate();
unsigned long testBaudrate();
unsigned long detectBaudrate(time_t timeoutMillis);
// SoftwareSerial specific
void setTransmitEnablePin(uint8_t transmitEnablePin);
// AVR compatibility methods
bool isListening();
bool stopListening();
bool serial0_swap_active() const {
return _serial0_swap_active;
}
#endif // if defined(ESP8266)
bool listen();
String getLogString() const;
using Print::write;
int getRxPin() const {
return _receivePin;
}
int getTxPin() const {
return _transmitPin;
}
unsigned long getBaudRate() const {
return _baud;
}
bool useGPIOpins() const {
return _serialtype != ESPEasySerialPort::sc16is752;
}
private:
const HardwareSerial* getHW() const;
HardwareSerial * getHW();
bool isValid() const;
#ifdef ESP8266
bool doHWbegin(unsigned long baud,
SerialConfig config,
SerialMode mode);
#endif // ifdef ESP8266
bool isI2Cserial() const {
return _serialtype == ESPEasySerialPort::sc16is752;
}
#ifndef DISABLE_SC16IS752_Serial
ESPEasySC16IS752_Serial *_i2cserial = nullptr;
#endif
#if !defined(DISABLE_SOFTWARE_SERIAL) && defined(ESP8266)
bool isSWserial() const {
return _serialtype == ESPEasySerialPort::software;
}
ESPeasySoftwareSerial *_swserial = nullptr;
#else // if !defined(DISABLE_SOFTWARE_SERIAL) && defined(ESP8266)
bool isSWserial() const {
return false;
}
#endif // if !defined(DISABLE_SOFTWARE_SERIAL) && defined(ESP8266)
#ifdef ESP8266
static bool _serial0_swap_active;
#endif // ESP8266
ESPEasySerialPort _serialtype = ESPEasySerialPort::MAX_SERIAL_TYPE;
int _receivePin;
int _transmitPin;
unsigned long _baud = 0;
bool _inverse_logic = false;
unsigned int _buffSize = 64;
};
#endif // ifndef ESPeasySerial_h