Skip to content

Commit 2ae50f2

Browse files
committed
add begun guard, rename example to match others
1 parent 0ef6be1 commit 2ae50f2

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

examples/CDC/multiCDC/multiCDC.ino renamed to examples/CDC/cdc_multi/cdc_multi.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This example demonstrates the use of multiple USB CDC/ACM "Virtual
3-
Serial" ports, using Ha Thach's tinyUSB library, and the port of
3+
Serial" ports, using Ha Thach's TinyUSB library, and the port of
44
that library to the Arduino environment
55
66
https://github.com/hathach/tinyusb

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,62 @@ void Adafruit_USBD_CDC::end(void) {
9292
}
9393

9494
uint32_t Adafruit_USBD_CDC::baud(void) {
95+
if (!_begun) {
96+
return 0;
97+
}
98+
9599
cdc_line_coding_t coding;
96100
tud_cdc_n_get_line_coding(_itf, &coding);
97101

98102
return coding.bit_rate;
99103
}
100104

101105
uint8_t Adafruit_USBD_CDC::stopbits(void) {
106+
if (!_begun) {
107+
return 0;
108+
}
109+
102110
cdc_line_coding_t coding;
103111
tud_cdc_n_get_line_coding(_itf, &coding);
104112

105113
return coding.stop_bits;
106114
}
107115

108116
uint8_t Adafruit_USBD_CDC::paritytype(void) {
117+
if (!_begun) {
118+
return 0;
119+
}
120+
109121
cdc_line_coding_t coding;
110122
tud_cdc_n_get_line_coding(_itf, &coding);
111123

112124
return coding.parity;
113125
}
114126

115127
uint8_t Adafruit_USBD_CDC::numbits(void) {
128+
if (!_begun) {
129+
return 0;
130+
}
131+
116132
cdc_line_coding_t coding;
117133
tud_cdc_n_get_line_coding(_itf, &coding);
118134

119135
return coding.data_bits;
120136
}
121137

122-
int Adafruit_USBD_CDC::dtr(void) { return tud_cdc_n_connected(_itf); }
138+
int Adafruit_USBD_CDC::dtr(void) {
139+
if (!_begun) {
140+
return 0;
141+
}
142+
143+
return tud_cdc_n_connected(_itf);
144+
}
123145

124146
Adafruit_USBD_CDC::operator bool() {
147+
if (!_begun) {
148+
return false;
149+
}
150+
125151
bool ret = tud_cdc_n_connected(_itf);
126152

127153
// Add an yield to run usb background in case sketch block wait as follows
@@ -133,6 +159,10 @@ Adafruit_USBD_CDC::operator bool() {
133159
}
134160

135161
int Adafruit_USBD_CDC::available(void) {
162+
if (!_begun) {
163+
return 0;
164+
}
165+
136166
uint32_t count = tud_cdc_n_available(_itf);
137167

138168
// Add an yield to run usb background in case sketch block wait as follows
@@ -145,17 +175,36 @@ int Adafruit_USBD_CDC::available(void) {
145175
}
146176

147177
int Adafruit_USBD_CDC::peek(void) {
178+
if (!_begun) {
179+
return -1;
180+
}
181+
148182
uint8_t ch;
149183
return tud_cdc_n_peek(_itf, &ch) ? (int)ch : -1;
150184
}
151185

152-
int Adafruit_USBD_CDC::read(void) { return (int)tud_cdc_n_read_char(_itf); }
186+
int Adafruit_USBD_CDC::read(void) {
187+
if (!_begun) {
188+
return -1;
189+
}
190+
return (int)tud_cdc_n_read_char(_itf);
191+
}
153192

154-
void Adafruit_USBD_CDC::flush(void) { tud_cdc_n_write_flush(_itf); }
193+
void Adafruit_USBD_CDC::flush(void) {
194+
if (!_begun) {
195+
return;
196+
}
197+
198+
tud_cdc_n_write_flush(_itf);
199+
}
155200

156201
size_t Adafruit_USBD_CDC::write(uint8_t ch) { return write(&ch, 1); }
157202

158203
size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
204+
if (!_begun) {
205+
return 0;
206+
}
207+
159208
size_t remain = size;
160209
while (remain && tud_cdc_n_connected(_itf)) {
161210
size_t wrcount = tud_cdc_n_write(_itf, buffer, remain);
@@ -172,6 +221,9 @@ size_t Adafruit_USBD_CDC::write(const uint8_t *buffer, size_t size) {
172221
}
173222

174223
int Adafruit_USBD_CDC::availableForWrite(void) {
224+
if (!_begun) {
225+
return 0;
226+
}
175227
return tud_cdc_n_write_available(_itf);
176228
}
177229

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
7676
uint8_t _itf;
7777

7878
/* TODO when closed to BSP release cycle (SAMD, nRF, rp2040)
79-
* rename _itf to _instance and add
79+
* rename _itf to _instance, _begun can be removed
8080
*
8181
* static uint8_t getInstanceCount(void);
8282
* static uint8_t _instance_count;

0 commit comments

Comments
 (0)