-
Notifications
You must be signed in to change notification settings - Fork 6
/
Port_ESPEasySerial_USB_HWCDC.cpp
198 lines (169 loc) · 4.55 KB
/
Port_ESPEasySerial_USB_HWCDC.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
#include "Port_ESPEasySerial_USB_HWCDC.h"
#if USES_HWCDC
// #include <USB.h>
volatile bool usbActive = false;
volatile int32_t eventidTriggered = ESP_EVENT_ANY_ID;
static void hwcdcEventCallback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
arduino_hw_cdc_event_data_t *data = (arduino_hw_cdc_event_data_t *)event_data;
eventidTriggered = event_id;
switch (event_id) {
case ARDUINO_HW_CDC_CONNECTED_EVENT:
usbActive = true;
break;
case ARDUINO_HW_CDC_BUS_RESET_EVENT:
// Serial.println("CDC BUS RESET");
break;
case ARDUINO_HW_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_HW_CDC_TX_EVENT:
usbActive = true;
// No example provided
break;
case ARDUINO_HW_CDC_MAX_EVENT:
// No example provided
break;
default:
break;
}
}
Port_ESPEasySerial_USB_HWCDC_t::Port_ESPEasySerial_USB_HWCDC_t(const ESPEasySerialConfig& config)
:
# if ARDUINO_USB_CDC_ON_BOOT // Serial used for USB CDC
_hwcdc_serial(&Serial)
# else // if ARDUINO_USB_CDC_ON_BOOT
_hwcdc_serial(&USBSerial)
# endif // if ARDUINO_USB_CDC_ON_BOOT
{
_config.port = ESPEasySerialPort::usb_hw_cdc;
// USB.begin();
if (_hwcdc_serial != nullptr) {
_config.rxBuffSize = _hwcdc_serial->setRxBufferSize(_config.rxBuffSize);
_config.txBuffSize = _hwcdc_serial->setRxBufferSize(_config.txBuffSize);
_hwcdc_serial->begin();
// _hwcdc_serial->onEvent(hwcdcEventCallback);
}
}
Port_ESPEasySerial_USB_HWCDC_t::~Port_ESPEasySerial_USB_HWCDC_t() {}
void Port_ESPEasySerial_USB_HWCDC_t::begin(unsigned long baud)
{
_config.baud = baud;
/*
if (_hwcdc_serial != nullptr) {
_config.rxBuffSize = _hwcdc_serial->setRxBufferSize(_config.rxBuffSize);
_config.txBuffSize = _hwcdc_serial->setRxBufferSize(_config.txBuffSize);
_hwcdc_serial->begin();
delay(10);
_hwcdc_serial->onEvent(hwcdcEventCallback);
delay(1);
}
*/
}
void Port_ESPEasySerial_USB_HWCDC_t::end() {
// Disabled for now
// See: https://github.com/espressif/arduino-esp32/issues/8224
if (_hwcdc_serial != nullptr) {
// _hwcdc_serial->end();
}
}
int Port_ESPEasySerial_USB_HWCDC_t::available(void)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->available();
}
return 0;
}
int Port_ESPEasySerial_USB_HWCDC_t::availableForWrite(void)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->availableForWrite();
}
return 0;
}
int Port_ESPEasySerial_USB_HWCDC_t::peek(void)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->peek();
}
return 0;
}
int Port_ESPEasySerial_USB_HWCDC_t::read(void)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->read();
}
return 0;
}
size_t Port_ESPEasySerial_USB_HWCDC_t::read(uint8_t *buffer,
size_t size)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->read(buffer, size);
}
return 0;
}
void Port_ESPEasySerial_USB_HWCDC_t::flush(void)
{
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->flush();
}
}
void Port_ESPEasySerial_USB_HWCDC_t::flush(bool txOnly)
{
flush();
}
size_t Port_ESPEasySerial_USB_HWCDC_t::write(uint8_t value)
{
if (operator bool()) {
return _hwcdc_serial->write(value);
}
return 0;
}
size_t Port_ESPEasySerial_USB_HWCDC_t::write(const uint8_t *buffer,
size_t size)
{
if (operator bool()) {
return _hwcdc_serial->write(buffer, size);
}
return 0;
}
Port_ESPEasySerial_USB_HWCDC_t::operator bool() const
{
if (_hwcdc_serial != nullptr) {
// return usbActive;
const bool connected = (*_hwcdc_serial);
return connected;
}
return false;
}
void Port_ESPEasySerial_USB_HWCDC_t::setDebugOutput(bool enabled) {
if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->setDebugOutput(enabled);
}
}
size_t Port_ESPEasySerial_USB_HWCDC_t::setRxBufferSize(size_t new_size)
{
if (_hwcdc_serial != nullptr) {
_config.rxBuffSize = _hwcdc_serial->setRxBufferSize(new_size);
return _config.rxBuffSize;
}
return 0;
}
size_t Port_ESPEasySerial_USB_HWCDC_t::setTxBufferSize(size_t new_size)
{
if (_hwcdc_serial != nullptr) {
_config.txBuffSize = _hwcdc_serial->setTxBufferSize(new_size);
return _config.txBuffSize;
}
return 0;
}
#endif // if USES_HWCDC