ClearCore Library
Loading...
Searching...
No Matches
SerialUsb.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Teknic, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
30#ifndef __SERIALUSB_H__
31#define __SERIALUSB_H__
32
33#include <inttypes.h>
34#include "Connector.h"
35#include "ISerial.h"
36
38#ifdef DEC
39#undef DEC
40#endif
41#define DEC 10
42
44#ifdef HEX
45#undef HEX
46#endif
47#define HEX 16
48
50#ifdef OCT
51#undef OCT
52#endif
53#define OCT 8
54
56#ifdef BIN
57#undef BIN
58#endif
59#define BIN 2
60
62#define USB_SERIAL_TIMEOUT 5000 // milliseconds
63
64namespace ClearCore {
65
75class SerialUsb : public ISerial, public Connector {
76 friend class SysManager;
77
78public:
79#ifndef HIDE_FROM_DOXYGEN
84 SerialUsb() {};
85#endif
86
88
94 void Flush() override;
95
99 void FlushInput() override;
100
104 void PortOpen() override;
105
109 void PortClose() override;
110
111#ifndef HIDE_FROM_DOXYGEN
115 bool Speed(uint32_t bitsPerSecond) override;
116
120 uint32_t Speed() override;
121#endif
122
126 int16_t CharGet() override;
127
131 int16_t CharPeek() override;
132
138 bool SendChar(uint8_t charToSend) override;
139
143 int32_t AvailableForRead() override;
144
154 int32_t AvailableForWrite() override;
155
159 void WaitForTransmitIdle() override;
160
164 bool PortIsOpen() override;
165
171 bool Parity(Parities newParity) override {
172 return newParity == Parities::PARITY_N;
173 }
174
180 Parities Parity() override {
181 return Parities::PARITY_N;
182 }
183
189 bool StopBits(uint8_t bits) override {
190 return bits == 1;
191 }
192
198 bool CharSize(uint8_t size) override {
199 return size == 8;
200 }
201
203
210 virtual ConnectorModes Mode() override {
211 return Connector::Mode();
212 }
213
221 bool Mode(ConnectorModes newMode) override {
222 return (m_mode == newMode);
223 }
224
233
239 bool IsWritable() override {
240 return PortIsOpen();
241 }
242
261 operator bool() override;
262
263#ifndef HIDE_FROM_DOXYGEN
269 int16_t State() override {
270 return PortIsOpen();
271 }
272
280 bool State(int16_t newState) override {
281 if (newState) {
282 PortOpen();
283 }
284 else {
285 PortClose();
286 }
287 return true;
288 }
289
290 bool IsInHwFault() override {
291 return false;
292 }
293#endif
294
295private:
296 // Index of this instance
297 uint16_t m_index;
298
299#ifndef HIDE_FROM_DOXYGEN
300 explicit SerialUsb(uint16_t index);
301
305 void Initialize(ClearCorePins clearCorePin) override {
306 m_index = clearCorePin;
307 m_mode = USB_CDC;
308 }
309
315 void Refresh() override {};
316#endif
317}; // SerialUsb
318
319} // ClearCore namespace
320
321#endif // __SERIALDRIVER_H__
Base class for all connector classes.
A standardized ClearCore Serial interface.
ClearCorePins
ClearCore PIN definitions.
Definition SysConnectors.h:54
Base class for interacting with all ClearCore connector objects.
Definition Connector.h:62
virtual bool IsInHwFault()=0
Get whether the connector is in a hardware fault state.
virtual ConnectorModes Mode()
Get the connector's operational mode.
Definition Connector.h:309
virtual int16_t State()=0
Get the connector's last sampled value.
ConnectorModes
All possible operational modes for a connector.
Definition Connector.h:74
@ USB_CDC
Definition Connector.h:164
ConnectorTypes
The different types of ClearCore connectors.
Definition Connector.h:172
@ SERIAL_USB_TYPE
Definition Connector.h:287
Base class for interacting with all ClearCore serial.
Definition ISerial.h:50
enum ClearCore::ISerial::_Parities Parities
virtual uint32_t Speed()=0
Gets the baud rate of the port.
ClearCore Serial USB Connector class.
Definition SerialUsb.h:75
Connector::ConnectorTypes Type() override
Get connector type.
Definition SerialUsb.h:230
bool IsWritable() override
Get R/W status of the connector.
Definition SerialUsb.h:239
int16_t CharGet() override
Attempt to read the next character from serial channel.
int32_t AvailableForWrite() override
Determines the number of characters available in the transmit buffer.
void PortOpen() override
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition SerialUsb.h:210
int32_t AvailableForRead() override
bool PortIsOpen() override
Return whether or not the port is open.
bool Parity(Parities newParity) override
Set UART transmission parity format.
Definition SerialUsb.h:171
void PortClose() override
int16_t CharPeek() override
Attempt to get the next character from the serial channel without pulling the character out of the bu...
void FlushInput() override
bool Mode(ConnectorModes newMode) override
Set the connector's operational mode.
Definition SerialUsb.h:221
Parities Parity() override
Return current port UART transmission parity.
Definition SerialUsb.h:180
bool SendChar(uint8_t charToSend) override
Send an ascii character on the serial channel.
bool StopBits(uint8_t bits) override
Change the number of stop bits used in UART communication.
Definition SerialUsb.h:189
void Flush() override
void WaitForTransmitIdle() override
bool CharSize(uint8_t size) override
Change the number of bits in a character.
Definition SerialUsb.h:198
ClearCore Board Supervisory System Manager.
Definition SysManager.h:58
Namespace to encompass the ClearCore board API.
Definition AdcManager.h:36