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