Skip to content

Commit

Permalink
Merge pull request #251 from adafruit/improve-cdc-host
Browse files Browse the repository at this point in the history
change Adafruit_USBH_CDC to inherit HardwareSerial (instead of Stream)
  • Loading branch information
hathach authored Feb 16, 2023
2 parents cdfc439 + a8c1e56 commit ce61007
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
5 changes: 3 additions & 2 deletions examples/DualRole/serial_host_bridge/serial_host_bridge.ino
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ void loop1()
// idx is index of cdc interface in the internal pool.
void tuh_cdc_mount_cb(uint8_t idx) {
// bind SerialHost object to this interface index
SerialHost.begin(idx);
SerialHost.setInterfaceIndex(idx);
SerialHost.begin(115200);

Serial.println("SerialHost is connected to a new CDC device");
}

// Invoked when a device with CDC interface is unmounted
void tuh_cdc_umount_cb(uint8_t idx) {
if (idx == SerialHost.getIndex()) {
if (idx == SerialHost.getInterfaceIndex()) {
// unbind SerialHost if this interface is unmounted
SerialHost.end();

Expand Down
18 changes: 17 additions & 1 deletion src/arduino/cdc/Adafruit_USBH_CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@

Adafruit_USBH_CDC::Adafruit_USBH_CDC(void) { _idx = TUSB_INDEX_INVALID; }

void Adafruit_USBH_CDC::begin(uint8_t idx) { _idx = idx; }
void Adafruit_USBH_CDC::begin(unsigned long baud) {

// default to index 0 when begin
if (_idx == TUSB_INDEX_INVALID) {
_idx = 0;
}

_baud = baud;
if (_baud == 0) {
_baud = 115200; // default, backward compatible with previous API begin(0)
}
}

void Adafruit_USBH_CDC::begin(unsigned long baudrate, uint16_t config) {
(void)config; // TODO support line coding later
begin(115200);
}

void Adafruit_USBH_CDC::end(void) { _idx = TUSB_INDEX_INVALID; }

Expand Down
16 changes: 9 additions & 7 deletions src/arduino/cdc/Adafruit_USBH_CDC.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@
#ifndef ADAFRUIT_USBH_CDC_H_
#define ADAFRUIT_USBH_CDC_H_

#include "Stream.h"
#include "HardwareSerial.h"

class Adafruit_USBH_CDC : public Stream {
class Adafruit_USBH_CDC : public HardwareSerial {
public:
Adafruit_USBH_CDC(void);

// Init/Bind to an specific cdc interface
void begin(uint8_t idx = 0);
// Set/Get index of cdc interface
void setInterfaceIndex(uint8_t idx) { _idx = idx; }
uint8_t getInterfaceIndex(void) { return _idx; }

void begin(unsigned long baudrate);
void begin(unsigned long baudrate, uint16_t config);

// unbind cdc interface
void end(void);

// Get index of cdc interface
uint8_t getIndex(void) { return _idx; }

// If cdc is mounted
bool mounted(void);
operator bool() { return mounted(); }
Expand Down Expand Up @@ -67,6 +68,7 @@ class Adafruit_USBH_CDC : public Stream {

private:
uint8_t _idx; // TinyUSB CDC Interface Index
uint32_t _baud;
};

#endif

0 comments on commit ce61007

Please sign in to comment.