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:
45 
46  UsbSerial();
47  ~UsbSerial();
48 
54  virtual size_t setRxBufferSize(size_t size) = 0;
55 
61  virtual size_t setTxBufferSize(size_t size) = 0;
62 
69  void setTxWait(bool wait)
70  {
71  bitWrite(options, UART_OPT_TXWAIT, wait);
72  }
73 
74  uint16_t readMemoryBlock(char* buf, int max_len) override
75  {
76  return readBytes(buf, max_len);
77  }
78 
79  bool seek(int len) override
80  {
81  return false;
82  }
83 
88  virtual void clear(SerialMode mode = SERIAL_FULL) = 0;
89 
94  void systemDebugOutput(bool enabled);
95 
101  void commandProcessing(bool reqEnable);
102 
104  {
105  receiveCallback = callback;
106  return true;
107  }
108 
110  {
111  transmitCompleteCallback = callback;
112  return true;
113  }
114 
120  unsigned getStatus();
121 
122  // Called internally
123  void handleEvent(Event event);
124 
125 protected:
126  uart_options_t options{_BV(UART_OPT_TXWAIT)};
128 
129 private:
130  void processEvents();
131 
132  DataReceived receiveCallback;
133  TransmitComplete transmitCompleteCallback;
134  std::unique_ptr<CommandExecutor> commandExecutor;
135  uint16_t status{0};
136  BitSet<uint8_t, Event> eventMask;
137 };
138 
139 } // namespace USB::CDC
Definition: Libraries/USB/src/USB/CDC/Device.h:25
Delegate< void(Stream &source, char arrivedChar, uint16_t availableCharsCount)> StreamDataReceivedDelegate
Delegate callback type for serial data reception.
Definition: HardwareSerial.h:46
Event
Definition: UsbSerial.h:29
uint16_t readMemoryBlock(char *buf, int max_len) override
Read a block of memory.
Definition: UsbSerial.h:74
bool seek(int len) override
Move read cursor.
Definition: UsbSerial.h:79
SerialMode
values equivalent to uart_mode_t
Definition: HardwareSerial.h:72
void setTxWait(bool wait)
Governs write behaviour when UART transmit buffers are full.
Definition: UsbSerial.h:69
#define _BV(bit)
Definition: BitManipulations.h:10
static constexpr SerialMode SERIAL_FULL
Definition: HardwareSerial.h:78
bool onDataReceived(DataReceived callback)
Definition: UsbSerial.h:103
#define bitWrite(value, bit, bitvalue)
Definition: BitManipulations.h:21
Base class for both device and host serial port modes.
Definition: UsbSerial.h:40
Base class for read/write stream.
Definition: ReadWriteStream.h:19
SimpleTimer flushTimer
Definition: UsbSerial.h:127
bool onTransmitComplete(TransmitComplete callback)
Definition: UsbSerial.h:109