-
Notifications
You must be signed in to change notification settings - Fork 6
/
ESPeasySerial.h
173 lines (128 loc) · 4.77 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
/*
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 "ESPEasySerial_common_defines.h"
#include "ESPEasySerialConfig.h"
#include "ESPEasySerialPort.h"
#include "ESPEasySerialType.h"
#include "Port_ESPEasySerial_base.h"
#include <Stream.h>
// 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)
class ESPeasySerial : public Stream {
public:
static Port_ESPEasySerial_base* ESPEasySerial_Port_factory(const ESPEasySerialConfig& config);
// Ideal buffer size is a trade-off between bootspeed
// and not missing data when the ESP is busy processing stuff.
// Mainly HWCDC and USBCDC may appear blocking if the USB device is detected
// by your OS but no serial port has been opened.
// Flushing the buffers may then take buffSize * timeout to flush, which is blocking.
ESPeasySerial(ESPEasySerialPort port,
int receivePin,
int transmitPin,
bool inverse_logic = false,
unsigned int buffSize = SOC_UART_FIFO_LEN,
bool forceSWserial = false);
// Config may be altered by running validate()
// Thus make a deepcopy and not const reference
ESPeasySerial(ESPEasySerialConfig config);
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 = SOC_UART_FIFO_LEN,
bool forceSWserial = false);
// If baud rate is set to 0, it will perform an auto-detect on the baudrate
void begin(unsigned long baud);
#ifdef ESP8266
void begin(unsigned long baud,
SerialConfig config,
SerialMode mode = SERIAL_FULL);
#endif // ifdef ESP8266
#ifdef ESP32
void begin(unsigned long baud,
uint32_t config);
#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;
/*
// 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 getBaudRate() const;
operator bool() const;
bool connected() const;
void setDebugOutput(bool);
bool isTxEnabled(void);
bool isRxEnabled(void);
bool listen();
String getLogString() const;
using Print::write;
int getRxPin() const {
return getSerialConfig().receivePin;
}
int getTxPin() const {
return getSerialConfig().transmitPin;
}
bool usesGPIOpins() const {
return useGPIOpins(getSerialConfig().port);
}
ESPEasySerialPort getSerialPortType() const {
return getSerialConfig().port;
}
ESPEasySerialConfig getSerialConfig() const {
if (_serialPort != nullptr) {
return _serialPort->getSerialConfig();
}
ESPEasySerialConfig res;
return res;
}
String getPortDescription() const {
if (_serialPort != nullptr) {
return _serialPort->getPortDescription();
}
return String();
}
private:
bool isValid() const;
Port_ESPEasySerial_base *_serialPort = nullptr;
};
#endif // ifndef ESPeasySerial_h