-
Notifications
You must be signed in to change notification settings - Fork 6
/
Port_ESPEasySerial_USBCDC.cpp
272 lines (229 loc) · 6.24 KB
/
Port_ESPEasySerial_USBCDC.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
265
266
267
268
269
270
271
272
#include "Port_ESPEasySerial_USBCDC.h"
#if USES_USBCDC
# if !ARDUINO_USB_CDC_ON_BOOT
USBCDC ESPEasySerial_USBCDC_port0(0);
// USBCDC ESPEasySerial_USBCDC_port1(1);
# endif // if !ARDUINO_USB_CDC_ON_BOOT
volatile bool usbActive = false;
volatile int32_t eventidTriggered = ESP_EVENT_ANY_ID;
// See:
// - https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/usb_cdc.html
// - https://docs.espressif.com/projects/esp-idf/en/v4.3/esp32s2/api-guides/usb-console.html
static void usbcdcEventCallback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
if (event_base == ARDUINO_USB_EVENTS) {
arduino_usb_event_data_t *data = (arduino_usb_event_data_t *)event_data;
switch (event_id) {
case ARDUINO_USB_STARTED_EVENT:
// HWSerial.println("USB PLUGGED");
break;
case ARDUINO_USB_STOPPED_EVENT:
// HWSerial.println("USB UNPLUGGED");
break;
case ARDUINO_USB_SUSPEND_EVENT:
// HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
break;
case ARDUINO_USB_RESUME_EVENT:
// HWSerial.println("USB RESUMED");
break;
default:
break;
}
} else if (event_base == ARDUINO_USB_CDC_EVENTS) {
arduino_usb_cdc_event_t *data = (arduino_usb_cdc_event_t *)event_data;
eventidTriggered = event_id;
switch (event_id) {
case ARDUINO_USB_CDC_CONNECTED_EVENT:
usbActive = true;
break;
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
// Sent when serial port disconnects
usbActive = true;
break;
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
usbActive = true;
break;
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
// Sent when USB is initialized by USB host (e.g. PC)
usbActive = true;
break;
case ARDUINO_USB_CDC_RX_EVENT:
/*
Serial.printf("CDC RX EVENT [%u]: ", data->rx.len);
{
uint8_t buf[data->rx.len];
size_t len = Serial.read(buf, data->rx.len);
Serial.write(buf, len);
}
Serial.println();
*/
usbActive = true;
break;
case ARDUINO_USB_CDC_TX_EVENT:
usbActive = true;
// No example provided
break;
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
// No example provided
break;
default:
break;
}
}
}
Port_ESPEasySerial_USBCDC_t::Port_ESPEasySerial_USBCDC_t(const ESPEasySerialConfig& config)
{
# if ARDUINO_USB_CDC_ON_BOOT
# else // if ARDUINO_USB_CDC_ON_BOOT
_serial = nullptr;
if (config.port == ESPEasySerialPort::usb_cdc_0) {
_serial = &ESPEasySerial_USBCDC_port0;
}
/*
else if (config.port == ESPEasySerialPort::usb_cdc_1) {
_serial = &ESPEasySerial_USBCDC_port1;
}
*/
# endif // if ARDUINO_USB_CDC_ON_BOOT
if (_serial != nullptr) {
_config.port = config.port;
// USB.onEvent(usbcdcEventCallback);
// _serial->onEvent(usbcdcEventCallback);
delay(10);
_config.rxBuffSize = _serial->setRxBufferSize(_config.rxBuffSize);
// TD-er: No setTxBufferSize()
// _config.txBuffSize = _serial->setTxBufferSize(_config.txBuffSize);
_serial->begin();
// _serial->onEvent(usbcdcEventCallback);
// USB.begin();
delay(1);
}
}
Port_ESPEasySerial_USBCDC_t::~Port_ESPEasySerial_USBCDC_t()
{
if (_serial != nullptr) {
_serial->end();
}
}
void Port_ESPEasySerial_USBCDC_t::begin(unsigned long baud)
{
if (_serial != nullptr) {
_config.baud = baud;
// USB.onEvent(usbcdcEventCallback);
// _serial->onEvent(usbcdcEventCallback);
delay(10);
_config.rxBuffSize = _serial->setRxBufferSize(_config.rxBuffSize);
// TD-er: No setTxBufferSize()
// _config.txBuffSize = _serial->setTxBufferSize(_config.txBuffSize);
_serial->begin();
// _serial->onEvent(usbcdcEventCallback);
// USB.begin();
delay(1);
}
}
void Port_ESPEasySerial_USBCDC_t::end() {
if (_serial != nullptr) {
_serial->end();
}
}
int Port_ESPEasySerial_USBCDC_t::available(void)
{
if (_serial != nullptr) {
return _serial->available();
}
return 0;
}
int Port_ESPEasySerial_USBCDC_t::availableForWrite(void)
{
if (_serial != nullptr) {
return _serial->availableForWrite();
}
return 0;
}
int Port_ESPEasySerial_USBCDC_t::peek(void)
{
if (_serial != nullptr) {
return _serial->peek();
}
return 0;
}
int Port_ESPEasySerial_USBCDC_t::read(void)
{
if (_serial != nullptr) {
return _serial->read();
}
return 0;
}
size_t Port_ESPEasySerial_USBCDC_t::read(uint8_t *buffer,
size_t size)
{
if (_serial != nullptr) {
return _serial->read(buffer, size);
}
return 0;
}
void Port_ESPEasySerial_USBCDC_t::flush(void)
{
if (_serial != nullptr) {
return _serial->flush();
}
}
void Port_ESPEasySerial_USBCDC_t::flush(bool txOnly)
{
flush();
}
size_t Port_ESPEasySerial_USBCDC_t::write(uint8_t value)
{
if (_serial != nullptr) {
return _serial->write(value);
}
return 0;
}
size_t Port_ESPEasySerial_USBCDC_t::write(const uint8_t *buffer,
size_t size)
{
if (_serial != nullptr) {
return _serial->write(buffer, size);
}
return 0;
}
Port_ESPEasySerial_USBCDC_t::operator bool() const
{
if (_serial != nullptr) {
const bool res = (*_serial);
return res;
// return usbActive;
}
return false;
}
void Port_ESPEasySerial_USBCDC_t::setDebugOutput(bool enabled) {
if (_serial != nullptr) {
return _serial->setDebugOutput(enabled);
}
}
size_t Port_ESPEasySerial_USBCDC_t::setRxBufferSize(size_t new_size)
{
if (_serial != nullptr) {
_config.rxBuffSize = _serial->setRxBufferSize(new_size);
return _config.rxBuffSize;
}
return 0;
}
size_t Port_ESPEasySerial_USBCDC_t::setTxBufferSize(size_t new_size)
{
// TD-er: No setTxBufferSize()
/*
if (_serial != nullptr) {
_config.txBuffSize = _serial->setTxBufferSize(new_size);
return _config.txBuffSize;
}
*/
return 0;
}
int Port_ESPEasySerial_USBCDC_t::getBaudRate() const
{
if (_serial != nullptr) {
return _serial->baudRate();
}
return 0;
}
#endif // if USES_USBCDC