CDC/HostDevice.h
Go to the documentation of this file.
1 /****
2  * CDC/HostDevice.h
3  *
4  * Copyright 2023 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming USB Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include "../HostInterface.h"
23 #include "UsbSerial.h"
24 
25 namespace USB::CDC
26 {
30 class HostDevice : public HostInterface, public UsbSerial
31 {
32 public:
33  size_t setRxBufferSize(size_t size) override
34  {
35  return CFG_TUH_CDC_RX_BUFSIZE;
36  }
37 
38  virtual size_t setTxBufferSize(size_t size) override
39  {
40  return CFG_TUH_CDC_TX_BUFSIZE;
41  }
42 
43  int available() override
44  {
45  return tuh_cdc_read_available(inst.idx);
46  }
47 
48  bool isFinished() override
49  {
50  return !tuh_cdc_mounted(inst.idx);
51  }
52 
53  int read() override
54  {
55  char c;
56  return tuh_cdc_read(inst.idx, &c, 1) ? c : -1;
57  }
58 
59  size_t readBytes(char* buffer, size_t length) override
60  {
61  return tuh_cdc_read(inst.idx, buffer, length);
62  }
63 
64  int peek() override
65  {
66  uint8_t c;
67  return tuh_cdc_peek(inst.idx, &c) ? c : -1;
68  }
69 
70  void clear(SerialMode mode = SERIAL_FULL) override
71  {
72  if(mode != SerialMode::TxOnly) {
73  tuh_cdc_read_clear(inst.idx);
74  }
75  if(mode != SerialMode::RxOnly) {
76  tuh_cdc_write_clear(inst.idx);
77  }
78  }
79 
80  void flush() override
81  {
82  tuh_cdc_write_flush(inst.idx);
83  }
84 
85  using Stream::write;
86 
87  size_t write(const uint8_t* buffer, size_t size) override;
88 };
89 
96 
101 using UnmountCallback = Delegate<void(HostDevice& dev)>;
102 
107 void onMount(MountCallback callback);
108 
113 void onUnmount(UnmountCallback callback);
114 
115 } // namespace USB::CDC
int available() override
Return the total length of the stream.
Definition: CDC/HostDevice.h:77
Identifies a TinyUSB host interface.
Definition: HostInterface.h:69
void onMount(MountCallback callback)
Application should call this method to receive device connection notifications.
Instance inst
Definition: HostInterface.h:118
virtual size_t setTxBufferSize(size_t size) override
Sets transmit buffer size.
Definition: CDC/HostDevice.h:72
size_t setRxBufferSize(size_t size) override
Sets receiving buffer size.
Definition: CDC/HostDevice.h:67
Implements CDC interface for a connected serial device.
Definition: CDC/HostDevice.h:47
int peek() override
Read a character without advancing the stream pointer.
Definition: CDC/HostDevice.h:98
bool isFinished() override
Check if all data has been read.
Definition: CDC/HostDevice.h:82
void onUnmount(UnmountCallback callback)
Application should call this method to receive device disconnection notifications.
size_t write(const uint8_t *buffer, size_t size) override
Write chars to stream.
int read() override
Read one character and moves the stream pointer.
Definition: CDC/HostDevice.h:87
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
uint8_t idx
Index or instance value specific to class.
Definition: HostInterface.h:88
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:70
static constexpr SerialMode SERIAL_FULL
Definition: HardwareSerial.h:76
void clear(SerialMode mode=SERIAL_FULL) override
Clear the serial port transmit/receive buffers.
Definition: CDC/HostDevice.h:104
void flush() override
Definition: CDC/HostDevice.h:114
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Definition: CDC/HostDevice.h:93
Definition: Delegate.h:20
Definition: Libraries/USB/src/USB/CDC/Device.h:25