Skip to content

Commit 9f1ccb9

Browse files
committed
Some CDC and BTSerial compatibility fixes
1 parent ea04207 commit 9f1ccb9

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

libraries/BluetoothSerial/src/BluetoothSerial.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ static bool _init_bt(const char *deviceName)
477477
}
478478

479479
if(!_spp_task_handle){
480-
xTaskCreatePinnedToCore(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle, 0);
480+
xTaskCreatePinnedToCore(_spp_tx_task, "spp_tx", 4096, NULL, 10, &_spp_task_handle, 0);
481481
if(!_spp_task_handle){
482482
log_e("Network Event Task Start Failed!");
483483
return false;
@@ -815,4 +815,9 @@ bool BluetoothSerial::isReady(bool checkMaster, int timeout) {
815815
TickType_t xTicksToWait = timeout / portTICK_PERIOD_MS;
816816
return (xEventGroupWaitBits(_spp_event_group, SPP_RUNNING, pdFALSE, pdTRUE, xTicksToWait) & SPP_RUNNING) != 0;
817817
}
818+
819+
BluetoothSerial::operator bool() const
820+
{
821+
return true;
822+
}
818823
#endif

libraries/BluetoothSerial/src/BluetoothSerial.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class BluetoothSerial: public Stream
3434
~BluetoothSerial(void);
3535

3636
bool begin(String localName=String(), bool isMaster=false);
37+
bool begin(unsigned long baud){//compatibility
38+
return begin();
39+
}
3740
int available(void);
3841
int peek(void);
3942
bool hasClient(void);
@@ -54,7 +57,8 @@ class BluetoothSerial: public Stream
5457
bool isReady(bool checkMaster=false, int timeout=0);
5558
bool disconnect();
5659
bool unpairDevice(uint8_t remoteAddress[]);
57-
60+
61+
operator bool() const;
5862
private:
5963
String local_name;
6064

libraries/USB/src/USB.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,9 @@ ESPUSB::ESPUSB(size_t task_stack_size, uint8_t event_task_priority)
112112
,webusb_enabled(false)
113113
,webusb_url(http://github.com/%3Cspan%20class=%22pl-s%22%3E%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3Eespressif.github.io/arduino-esp32/webusb.html%3Cspan%20class=%22pl-pds%22%3E"%3C/span%3E%3C/span%3E)
114114
,_started(false)
115+
,_task_stack_size(task_stack_size)
116+
,_event_task_priority(event_task_priority)
115117
{
116-
esp_event_loop_args_t event_task_args = {
117-
.queue_size = 5,
118-
.task_name = "arduino_usb_events",
119-
.task_priority = event_task_priority,
120-
.task_stack_size = task_stack_size,
121-
.task_core_id = tskNO_AFFINITY
122-
};
123-
if (esp_event_loop_create(&event_task_args, &arduino_usb_event_loop_handle) != ESP_OK) {
124-
log_e("esp_event_loop_create failed");
125-
}
126118
}
127119

128120
ESPUSB::~ESPUSB(){
@@ -133,6 +125,18 @@ ESPUSB::~ESPUSB(){
133125
}
134126

135127
bool ESPUSB::begin(){
128+
if (!arduino_usb_event_loop_handle) {
129+
esp_event_loop_args_t event_task_args = {
130+
.queue_size = 5,
131+
.task_name = "arduino_usb_events",
132+
.task_priority = _event_task_priority,
133+
.task_stack_size = _task_stack_size,
134+
.task_core_id = tskNO_AFFINITY
135+
};
136+
if (esp_event_loop_create(&event_task_args, &arduino_usb_event_loop_handle) != ESP_OK) {
137+
log_e("esp_event_loop_create failed");
138+
}
139+
}
136140
if(!_started){
137141
tinyusb_device_config_t tinyusb_device_config = {
138142
.vid = vid,

libraries/USB/src/USB.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class ESPUSB {
109109
String webusb_url;
110110

111111
bool _started;
112+
size_t _task_stack_size;
113+
uint8_t _event_task_priority;
112114
};
113115

114116
extern ESPUSB USB;

libraries/USB/src/USBCDC.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,20 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
119119
arduino_usb_event_handler_register_with(ARDUINO_USB_CDC_EVENTS, event, callback, this);
120120
}
121121

122-
void USBCDC::begin(size_t rx_queue_len)
123-
{
122+
size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
124123
if(rx_queue){
125-
return;
124+
return 0;
126125
}
127126
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
128127
if(!rx_queue){
129-
return;
128+
return 0;
130129
}
130+
return rx_queue_len;
131+
}
132+
133+
void USBCDC::begin(unsigned long baud)
134+
{
135+
setRxBufferSize(256);//default if not preset
131136
}
132137

133138
void USBCDC::end()
@@ -292,15 +297,13 @@ int USBCDC::availableForWrite(void)
292297
return tud_cdc_n_write_available(itf);
293298
}
294299

295-
size_t USBCDC::write(const uint8_t *buffer, size_t size){
300+
size_t USBCDC::write(const uint8_t *buffer, size_t size)
301+
{
296302
return tinyusb_cdc_write(itf, buffer, size);
297303
}
298304

299305
size_t USBCDC::write(uint8_t c)
300306
{
301-
if(itf >= MAX_USB_CDC_DEVICES){
302-
return 0;
303-
}
304307
return write(&c, 1);
305308
}
306309

libraries/USB/src/USBCDC.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ class USBCDC: public Stream
5656

5757
void onEvent(esp_event_handler_t callback);
5858
void onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback);
59-
60-
void begin(size_t rx_queue_len=256);
59+
60+
size_t setRxBufferSize(size_t);
61+
void begin(unsigned long baud=0);
6162
void end();
6263

6364
int available(void);

0 commit comments

Comments
 (0)