UsbSerial.h
Go to the documentation of this file.
1 /****
2  * CDC/UsbSerial.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 <HardwareSerial.h>
23 #include <SimpleTimer.h>
24 #include <Data/BitSet.h>
25 #include <memory>
26 
27 namespace USB::CDC
28 {
29 enum class Event {
30  rx_data,
31  tx_done,
32  line_break,
33 };
34 
40 class UsbSerial : public ReadWriteStream
41 {
42 public:
44  using TransmitComplete = Delegate<void(UsbSerial& device)>;
45 
47 
53  virtual size_t setRxBufferSize(size_t size) = 0;
54 
60  virtual size_t setTxBufferSize(size_t size) = 0;
61 
68  void setTxWait(bool wait)
69  {
70  bitWrite(options, UART_OPT_TXWAIT, wait);
71  }
72 
73  uint16_t readMemoryBlock(char* buf, int max_len) override
74  {
75  return readBytes(buf, max_len);
76  }
77 
78  bool seek(int len) override
79  {
80  return false;
81  }
82 
87  virtual void clear(SerialMode mode = SERIAL_FULL) = 0;
88 
93  void systemDebugOutput(bool enabled);
94 
96  {
97  receiveCallback = callback;
98  return true;
99  }
100 
101  bool onTransmitComplete(TransmitComplete callback)
102  {
103  transmitCompleteCallback = callback;
104  return true;
105  }
106 
112  unsigned getStatus();
113 
114  // Called internally
115  void handleEvent(Event event);
116 
117 protected:
118  uart_options_t options{_BV(UART_OPT_TXWAIT)};
120 
121 private:
122  void processEvents();
123 
124  DataReceived receiveCallback;
125  TransmitComplete transmitCompleteCallback;
126  uint16_t status{0};
127  BitSet<uint8_t, Event> eventMask;
128 };
129 
130 } // namespace USB::CDC
uint16_t readMemoryBlock(char *buf, int max_len) override
Read a block of memory.
Definition: UsbSerial.h:90
#define _BV(bit)
Definition: BitManipulations.h:10
bool seek(int len) override
Move read cursor.
Definition: UsbSerial.h:95
StreamDataReceivedDelegate DataReceived
Definition: UsbSerial.h:60
virtual void clear(SerialMode mode=SERIAL_FULL)=0
Clear the serial port transmit/receive buffers.
size_t readBytes(char *buffer, size_t length) override
Read chars from stream into buffer.
virtual size_t setRxBufferSize(size_t size)=0
Sets receiving buffer size.
Delegate< void(Stream &source, char arrivedChar, uint16_t availableCharsCount)> StreamDataReceivedDelegate
Delegate callback type for serial data reception.
Definition: HardwareSerial.h:46
void setTxWait(bool wait)
Governs write behaviour when UART transmit buffers are full.
Definition: UsbSerial.h:85
void systemDebugOutput(bool enabled)
Configure serial port for system debug output and redirect output from debugf.
bool onDataReceived(DataReceived callback)
Definition: UsbSerial.h:112
void handleEvent(Event event)
#define bitWrite(value, bit, bitvalue)
Definition: BitManipulations.h:21
Delegate< void(UsbSerial &device)> TransmitComplete
Definition: UsbSerial.h:61
uart_options_t options
Definition: UsbSerial.h:135
Event
Definition: UsbSerial.h:46
SimpleTimer flushTimer
Definition: UsbSerial.h:136
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:70
bool onTransmitComplete(TransmitComplete callback)
Definition: UsbSerial.h:118
unsigned getStatus()
Get status error flags and clear them.
static constexpr SerialMode SERIAL_FULL
Definition: HardwareSerial.h:76
Definition: Delegate.h:20
Base class for read/write stream.
Definition: ReadWriteStream.h:19
virtual size_t setTxBufferSize(size_t size)=0
Sets transmit buffer size.
Definition: Libraries/USB/src/USB/CDC/Device.h:25