Libraries/USB/src/USB/CDC/Device.h
Go to the documentation of this file.
1 /****
2  * CDC/Device.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 "UsbSerial.h"
23 #include "../DeviceInterface.h"
24 
25 namespace USB::CDC
26 {
30 class Device : public DeviceInterface, public UsbSerial
31 {
32 public:
33  Device(uint8_t idx, const char* name);
34 
35  size_t setRxBufferSize(size_t size) override
36  {
37  return CFG_TUD_CDC_RX_BUFSIZE;
38  }
39 
40  virtual size_t setTxBufferSize(size_t size) override
41  {
42  return CFG_TUD_CDC_TX_BUFSIZE;
43  }
44 
45  int available() override
46  {
47  return tud_cdc_n_available(inst);
48  }
49 
50  bool isFinished() override
51  {
52  return !tud_cdc_n_connected(inst);
53  }
54 
55  int read() override
56  {
57  return tud_cdc_n_read_char(inst);
58  }
59 
60  size_t readBytes(char* buffer, size_t length) override
61  {
62  return tud_cdc_n_read(inst, buffer, length);
63  }
64 
65  int peek() override
66  {
67  uint8_t c;
68  return tud_cdc_n_peek(inst, &c) ? c : -1;
69  }
70 
71  void clear(SerialMode mode = SERIAL_FULL) override
72  {
73  if(mode != SerialMode::TxOnly) {
74  tud_cdc_n_read_flush(inst);
75  }
76  if(mode != SerialMode::RxOnly) {
77  tud_cdc_n_write_clear(inst);
78  }
79  }
80 
81  void flush() override
82  {
83  tud_cdc_n_write_flush(inst);
84  }
85 
86  using Stream::write;
87 
88  size_t write(const uint8_t* buffer, size_t size) override;
89 };
90 
91 } // namespace USB::CDC
void flush() override
Definition: Libraries/USB/src/USB/CDC/Device.h:115
Serial device implementation, in ACM mode.
Definition: Libraries/USB/src/USB/CDC/Device.h:47
int available() override
Return the total length of the stream.
Definition: Libraries/USB/src/USB/CDC/Device.h:79
Device(uint8_t idx, const char *name)
void clear(SerialMode mode=SERIAL_FULL) override
Clear the serial port transmit/receive buffers.
Definition: Libraries/USB/src/USB/CDC/Device.h:105
uint8_t inst
Definition: DeviceInterface.h:81
virtual size_t setTxBufferSize(size_t size) override
Sets transmit buffer size.
Definition: Libraries/USB/src/USB/CDC/Device.h:74
Base class for both device and host serial port modes.
Definition: UsbSerial.h:57
const char * name
Definition: DeviceInterface.h:82
virtual size_t write(uint8_t c)=0
Writes a single character to output stream.
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: Libraries/USB/src/USB/CDC/Device.h:89
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
Definition: Libraries/USB/src/USB/CDC/Device.h:94
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:70
static constexpr SerialMode SERIAL_FULL
Definition: HardwareSerial.h:76
size_t setRxBufferSize(size_t size) override
Sets receiving buffer size.
Definition: Libraries/USB/src/USB/CDC/Device.h:69
int peek() override
Read a character without advancing the stream pointer.
Definition: Libraries/USB/src/USB/CDC/Device.h:99
Base class to support a USB device interface implementation.
Definition: DeviceInterface.h:46
bool isFinished() override
Check if all data has been read.
Definition: Libraries/USB/src/USB/CDC/Device.h:84
Definition: Libraries/USB/src/USB/CDC/Device.h:25