-
Notifications
You must be signed in to change notification settings - Fork 6
/
Port_ESPEasySerial_SW_Serial.cpp
172 lines (148 loc) · 4.09 KB
/
Port_ESPEasySerial_SW_Serial.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
#include "Port_ESPEasySerial_SW_Serial.h"
#if USES_SW_SERIAL
Port_ESPEasySerial_SW_Serial_t::Port_ESPEasySerial_SW_Serial_t(const ESPEasySerialConfig& config)
{
if (config.port == ESPEasySerialPort::software) {
_config = config;
# if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
_swserial = new (std::nothrow) SoftwareSerial(
config.receivePin,
config.transmitPin,
config.inverse_logic);
# else // if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
_swserial = new (std::nothrow) Driver_ESPEasySoftwareSerial_t(
config.receivePin,
config.transmitPin,
config.inverse_logic);
# endif // if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
}
}
Port_ESPEasySerial_SW_Serial_t::~Port_ESPEasySerial_SW_Serial_t()
{
if (_swserial != nullptr) {
delete _swserial;
_swserial = nullptr;
}
}
void Port_ESPEasySerial_SW_Serial_t::begin(unsigned long baud)
{
if (_swserial != nullptr) {
_config.baud = baud;
# if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
// Except at high bitrates, depending on other ongoing activity, interrupts in particular, this software serial adapter supports full
// duplex receive and send. At high bitrates (115200bps) send bit timing can be improved at the expense of blocking concurrent full
// duplex receives, with the EspSoftwareSerial::UART::enableIntTx(false) function call.
_swserial->enableIntTx(baud < 57600);
# endif // if USES_LATEST_SOFTWARE_SERIAL_LIBRARY
_swserial->begin(
_config.baud); /*,
3, // SoftwareSerialConfig::SWSERIAL_8N1 // static_cast<SoftwareSerialConfig>(_config.config),
_config.receivePin,
_config.transmitPin,
_config.inverse_logic,
_config.rxBuffSize);
*/
}
}
void Port_ESPEasySerial_SW_Serial_t::end() {
if (_swserial != nullptr) {
_swserial->end();
}
}
int Port_ESPEasySerial_SW_Serial_t::available(void)
{
if (_swserial != nullptr) {
return _swserial->available();
}
return 0;
}
int Port_ESPEasySerial_SW_Serial_t::availableForWrite(void)
{
if (_swserial != nullptr) {
// FIXME TD-er: Implement availableForWrite
// return _swserial->availableForWrite();
if (_config.baud >= 9600) {
// Sending data is blocking
// Allow to send as many bytes as can be sent in roughly 1 msec.
return _config.baud / 9600;
}
return 1;
}
return 0;
}
int Port_ESPEasySerial_SW_Serial_t::peek(void)
{
if (_swserial != nullptr) {
return _swserial->peek();
}
return 0;
}
int Port_ESPEasySerial_SW_Serial_t::read(void)
{
if (_swserial != nullptr) {
return _swserial->read();
}
return 0;
}
size_t Port_ESPEasySerial_SW_Serial_t::read(uint8_t *buffer,
size_t size)
{
if (_swserial != nullptr) {
return _swserial->readBytes((char *)buffer, size);
}
return 0;
}
void Port_ESPEasySerial_SW_Serial_t::flush(void)
{
if (_swserial != nullptr) {
return _swserial->flush();
}
}
void Port_ESPEasySerial_SW_Serial_t::flush(bool txOnly)
{
flush();
}
size_t Port_ESPEasySerial_SW_Serial_t::write(uint8_t value)
{
if (_swserial != nullptr) {
return _swserial->write(value);
}
return 0;
}
size_t Port_ESPEasySerial_SW_Serial_t::write(const uint8_t *buffer,
size_t size)
{
if (_swserial != nullptr) {
return _swserial->write(buffer, size);
}
return 0;
}
int Port_ESPEasySerial_SW_Serial_t::getBaudRate() const
{
if (_swserial != nullptr) {
return _swserial->baudRate();
}
return 0;
}
Port_ESPEasySerial_SW_Serial_t::operator bool() const
{
return _swserial != nullptr;
}
void Port_ESPEasySerial_SW_Serial_t::setDebugOutput(bool) {}
size_t Port_ESPEasySerial_SW_Serial_t::setRxBufferSize(size_t new_size)
{
if (_swserial != nullptr) {
_config.rxBuffSize = new_size;
return new_size;
}
return 0;
}
size_t Port_ESPEasySerial_SW_Serial_t::setTxBufferSize(size_t new_size)
{
if (_swserial != nullptr) {
_config.txBuffSize = new_size;
return new_size;
}
return 0;
}
#endif // if USES_SW_SERIAL