ClearCore Library
ISerial.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 
31 #ifndef ISERIAL_H_
32 #define ISERIAL_H_
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 namespace ClearCore {
50 class ISerial {
51 public:
52 
56  typedef enum _Parities {
58  PARITY_E = 0,
63  } Parities;
64 
73  virtual void Flush() = 0;
82  virtual void FlushInput() = 0;
83 
92  virtual void PortOpen() = 0;
93 
102  virtual void PortClose() = 0;
103 
116  virtual bool Speed(uint32_t bitsPerSecond) = 0;
117 
128  virtual uint32_t Speed() = 0;
129 
137  virtual int16_t CharGet() = 0;
138 
147  virtual int16_t CharPeek() = 0;
148 
155  virtual bool SendChar(uint8_t charToSend) = 0;
156 
162  bool SendLine() {
163  return SendChar('\r') && SendChar('\n');
164  }
165 
173  bool Send(const char *buffer, size_t bufferSize) {
174  for (size_t iChar = 0; iChar < bufferSize; iChar++) {
175  if (!SendChar(buffer[iChar])) {
176  return false;
177  }
178  }
179  return true;
180  }
181 
190  bool SendLine(const char *buffer, size_t bufferSize) {
191  return Send(buffer, bufferSize) && SendLine();
192  }
193 
200  bool Send(const char *nullTermStr) {
201  return Send(nullTermStr, strlen(nullTermStr));
202  }
203 
211  bool SendLine(const char *nullTermStr) {
212  return Send(nullTermStr) && SendLine();
213  }
214 
221  bool Send(char theChar) {
222  return SendChar(theChar);
223  }
224 
232  bool SendLine(char theChar) {
233  return Send(theChar) && SendLine();
234  }
235 
245  bool Send(double number, uint8_t precision = 2) {
246  char buffer[20];
247  snprintf(buffer, sizeof(buffer), "%.*f", precision, number);
248  return Send(buffer);
249  }
250 
261  bool SendLine(double number, uint8_t precision = 2) {
262  return Send(number, precision) && SendLine();
263  }
264 
273  bool Send(int8_t number, uint8_t radix = 10) {
274  return Send(static_cast<int32_t>(number), radix);
275  }
276 
286  bool SendLine(int8_t number, uint8_t radix = 10) {
287  return SendLine(static_cast<int32_t>(number), radix);
288  }
289 
298  bool Send(uint8_t number, uint8_t radix = 10) {
299  return Send(static_cast<uint32_t>(number), radix);
300  }
301 
311  bool SendLine(uint8_t number, uint8_t radix = 10) {
312  return SendLine(static_cast<uint32_t>(number), radix);
313  }
314 
323  bool Send(int16_t number, uint8_t radix = 10) {
324  return Send(static_cast<int32_t>(number), radix);
325  }
326 
336  bool SendLine(int16_t number, uint8_t radix = 10) {
337  return SendLine(static_cast<int32_t>(number), radix);
338  }
339 
348  bool Send(uint16_t number, uint8_t radix = 10) {
349  return Send(static_cast<uint32_t>(number), radix);
350  }
351 
361  bool SendLine(uint16_t number, uint8_t radix = 10) {
362  return SendLine(static_cast<uint32_t>(number), radix);
363  }
364 
373  bool Send(int32_t number, uint8_t radix = 10) {
374  if (radix < 2 || radix > 16) {
375  // Only support bases 2 through 16.
376  return false;
377  }
378  char strRep[2 + 8 * sizeof(number)];
379  itoa(number, strRep, radix);
380  Send((const char *)strRep);
381  return true;
382  }
383 
393  bool SendLine(int32_t number, uint8_t radix = 10) {
394  return Send(number, radix) && SendLine();
395  }
396 
405  bool Send(uint32_t number, uint8_t radix = 10) {
406  if (radix < 2 || radix > 16) {
407  // Only support bases 2 through 16.
408  return false;
409  }
410  char strRep[1 + 8 * sizeof(number)];
411  utoa(number, strRep, radix);
412  Send((const char *)strRep);
413  return true;
414  }
415 
425  bool SendLine(uint32_t number, uint8_t radix = 10) {
426  return Send(number, radix) && SendLine();
427  }
428 
437  bool Send(int number, uint8_t radix = 10) {
438  return Send(static_cast<int32_t>(number), radix);
439  }
440 
449  bool SendLine(int number, uint8_t radix = 10) {
450  return SendLine(static_cast<int32_t>(number), radix);
451  }
452 
456  virtual int32_t AvailableForRead() = 0;
457 
470  virtual int32_t AvailableForWrite() = 0;
471 
475  virtual void WaitForTransmitIdle() = 0;
476 
488  virtual bool PortIsOpen() = 0;
489 
493  virtual operator bool() = 0;
494 
507  virtual bool Parity(Parities newParity) = 0;
508 
520  virtual Parities Parity() = 0;
521 
531  virtual bool StopBits(uint8_t bits) = 0;
532 
546  virtual bool CharSize(uint8_t size) = 0;
547 
548 };
549 
550 } // ClearCore namespace
551 
552 #endif /* ISERIAL_H_ */
virtual void PortOpen()=0
Base class for interacting with all ClearCore serial.
Definition: ISerial.h:50
enum ClearCore::ISerial::_Parities Parities
bool Send(const char *nullTermStr)
Send a string of characters out the port.
Definition: ISerial.h:200
bool Send(char theChar)
Send a character to be printed to the serial port.
Definition: ISerial.h:221
bool SendLine(int number, uint8_t radix=10)
Send an integer to be printed to the serial port.
Definition: ISerial.h:449
bool SendLine(uint32_t number, uint8_t radix=10)
Send a 32-bit unsigned number to be printed to the serial port. Terminate the line with carriage retu...
Definition: ISerial.h:425
Odd Parity.
Definition: ISerial.h:60
virtual bool SendChar(uint8_t charToSend)=0
Send an ascii character on the serial channel.
bool Send(int32_t number, uint8_t radix=10)
Send a 32-bit signed number to be printed to the serial port.
Definition: ISerial.h:373
bool SendLine(int8_t number, uint8_t radix=10)
Send an 8-bit signed number to be printed to the serial port. Terminate the line with carriage return...
Definition: ISerial.h:286
virtual int16_t CharGet()=0
Attempt to read the next character from serial channel.
bool Send(uint8_t number, uint8_t radix=10)
Send an 8-bit unsigned number to be printed to the serial port.
Definition: ISerial.h:298
bool Send(uint32_t number, uint8_t radix=10)
Send a 32-bit unsigned number to be printed to the serial port.
Definition: ISerial.h:405
virtual void PortClose()=0
virtual int32_t AvailableForWrite()=0
Determines the number of characters available in the transmit buffer.
bool Send(int8_t number, uint8_t radix=10)
Send an 8-bit signed number to be printed to the serial port.
Definition: ISerial.h:273
bool SendLine(int16_t number, uint8_t radix=10)
Send a 16-bit signed number to be printed to the serial port. Terminate the line with carriage return...
Definition: ISerial.h:336
virtual int16_t CharPeek()=0
Attempt to get the next character from the serial channel without pulling the character out of the bu...
bool Send(uint16_t number, uint8_t radix=10)
Send a 16-bit unsigned number to be printed to the serial port.
Definition: ISerial.h:348
virtual int32_t AvailableForRead()=0
bool Send(int16_t number, uint8_t radix=10)
Send a 16-bit signed number to be printed to the serial port.
Definition: ISerial.h:323
bool SendLine(char theChar)
Send a character to be printed to the serial port. Terminate the line with carriage return and newlin...
Definition: ISerial.h:232
virtual void WaitForTransmitIdle()=0
virtual void FlushInput()=0
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
bool SendLine(double number, uint8_t precision=2)
Send a floating point number to the serial port. Terminate the line with carriage return and newline ...
Definition: ISerial.h:261
bool Send(int number, uint8_t radix=10)
Send an integer to be printed to the serial port.
Definition: ISerial.h:437
virtual Parities Parity()=0
Return current port UART transmission format.
bool Send(const char *buffer, size_t bufferSize)
Send the array of characters out the port.
Definition: ISerial.h:173
virtual uint32_t Speed()=0
Gets the baud rate of the port.
bool SendLine(const char *buffer, size_t bufferSize)
Send the array of characters out the port. Terminate the line with carriage return and newline charac...
Definition: ISerial.h:190
bool Send(double number, uint8_t precision=2)
Send a floating point number to the serial port.
Definition: ISerial.h:245
virtual bool PortIsOpen()=0
Return whether or not the port is open.
virtual void Flush()=0
bool SendLine(uint16_t number, uint8_t radix=10)
Send a 16-bit unsigned number to be printed to the serial port. Terminate the line with carriage retu...
Definition: ISerial.h:361
virtual bool CharSize(uint8_t size)=0
Change the number of bits in a character.
No Parity.
Definition: ISerial.h:62
virtual bool StopBits(uint8_t bits)=0
Even Parity.
Definition: ISerial.h:58
_Parities
Definition: ISerial.h:56
bool SendLine(uint8_t number, uint8_t radix=10)
Send an 8-bit unsigned number to be printed to the serial port. Terminate the line with carriage retu...
Definition: ISerial.h:311
bool SendLine(const char *nullTermStr)
Send a string of characters out the port. Terminate the line with carriage return and newline charact...
Definition: ISerial.h:211
bool SendLine()
Send carriage return and newline characters.
Definition: ISerial.h:162
bool SendLine(int32_t number, uint8_t radix=10)
Send a 32-bit signed number to be printed to the serial port. Terminate the line with carriage return...
Definition: ISerial.h:393