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 
51  virtual size_t setRxBufferSize(size_t size) = 0;
52 
58  virtual size_t setTxBufferSize(size_t size) = 0;
59 
66  void setTxWait(bool wait)
67  {
68  bitWrite(options, UART_OPT_TXWAIT, wait);
69  }
70 
71  uint16_t readMemoryBlock(char* buf, int max_len) override
72  {
73  return readBytes(buf, max_len);
74  }
75 
76  bool seek(int len) override
77  {
78  return false;
79  }
80 
85  virtual void clear(SerialMode mode = SERIAL_FULL) = 0;
86 
91  void systemDebugOutput(bool enabled);
92 
94  {
95  receiveCallback = callback;
96  return true;
97  }
98 
100  {
101  transmitCompleteCallback = callback;
102  return true;
103  }
104 
110  unsigned getStatus();
111 
112  // Called internally
113  void handleEvent(Event event);
114 
115 protected:
116  uart_options_t options{_BV(UART_OPT_TXWAIT)};
117 
118  void queueFlush();
119 
120 private:
121  void processEvents();
122 
123  SimpleTimer flushTimer;
124  DataReceived receiveCallback;
125  TransmitComplete transmitCompleteCallback;
126  uint16_t status{0};
127  BitSet<uint8_t, Event> eventMask;
128 };
129 
130 } // namespace USB::CDC
#define _BV(bit)
Definition: BitManipulations.h:10
#define bitWrite(value, bit, bitvalue)
Definition: BitManipulations.h:21
virtual size_t readBytes(char *buffer, size_t length)
Read chars from stream into buffer.
Base class for read/write stream.
Definition: ReadWriteStream.h:20
Base class for both device and host serial port modes.
Definition: UsbSerial.h:41
void handleEvent(Event event)
unsigned getStatus()
Get status error flags and clear them.
virtual size_t setTxBufferSize(size_t size)=0
Sets transmit buffer size.
virtual void clear(SerialMode mode=SERIAL_FULL)=0
Clear the serial port transmit/receive buffers.
void setTxWait(bool wait)
Governs write behaviour when UART transmit buffers are full.
Definition: UsbSerial.h:66
uart_options_t options
Definition: UsbSerial.h:116
bool onDataReceived(DataReceived callback)
Definition: UsbSerial.h:93
uint16_t readMemoryBlock(char *buf, int max_len) override
Read a block of memory.
Definition: UsbSerial.h:71
bool seek(int len) override
Move read cursor.
Definition: UsbSerial.h:76
virtual size_t setRxBufferSize(size_t size)=0
Sets receiving buffer size.
void systemDebugOutput(bool enabled)
Configure serial port for system debug output and redirect output from debugf.
bool onTransmitComplete(TransmitComplete callback)
Definition: UsbSerial.h:99
Delegate< void(Stream &source, char arrivedChar, uint16_t availableCharsCount)> StreamDataReceivedDelegate
Delegate callback type for serial data reception.
Definition: HardwareSerial.h:46
static constexpr SerialMode SERIAL_FULL
Definition: HardwareSerial.h:76
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:70
Definition: Libraries/USB/src/USB/CDC/Device.h:26
Event
Definition: UsbSerial.h:29