From f55e743e4524e412c67fcaf7de43d768d17a04be Mon Sep 17 00:00:00 2001 From: Javier Balloffet Date: Sun, 11 Feb 2024 20:24:00 +0100 Subject: [PATCH] Add serial stream interface and the Arduino implementation Signed-off-by: Javier Balloffet --- andino_firmware/src/serial_stream.h | 177 ++++++++++++++++++ andino_firmware/src/serial_stream_arduino.cpp | 90 +++++++++ andino_firmware/src/serial_stream_arduino.h | 83 ++++++++ 3 files changed, 350 insertions(+) create mode 100644 andino_firmware/src/serial_stream.h create mode 100644 andino_firmware/src/serial_stream_arduino.cpp create mode 100644 andino_firmware/src/serial_stream_arduino.h diff --git a/andino_firmware/src/serial_stream.h b/andino_firmware/src/serial_stream.h new file mode 100644 index 00000000..4f58c899 --- /dev/null +++ b/andino_firmware/src/serial_stream.h @@ -0,0 +1,177 @@ +// BSD 3-Clause License +// +// Copyright (c) 2024, Ekumen Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#pragma once + +#include + +namespace andino { + +/// @brief This class defines an interface for serial streams. +class SerialStream { + public: + /// @brief Supported numeral systems. + enum Base { + kBin = 2, + kOct = 8, + kDec = 10, + kHex = 16, + }; + + /// @brief Constructs a SerialStream. + explicit SerialStream() = default; + + /// @brief Destructs the serial stream. + virtual ~SerialStream() = default; + + /// @brief Initializes the serial stream. + /// + /// @param baud Data rate in bits per second (baud). + virtual void begin(unsigned long baud) const = 0; + + /// @brief Gets the number of bytes available for reading. + /// + /// @return Number of bytes available for reading. + virtual int available() const = 0; + + /// @brief Gets the incoming data. + /// + /// @return First byte of incoming data, or -1 if no data is available. + virtual int read() const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param c String to send. + /// @return Number of bytes sent. + virtual size_t print(const char* c) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param c Character to send. + /// @return Number of bytes sent. + virtual size_t print(char c) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param b Byte to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t print(unsigned char b, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t print(int num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t print(unsigned int num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t print(long num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t print(unsigned long num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param digits Number of decimal places to use. + /// @return Number of bytes sent. + virtual size_t print(double num, int digits = 2) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param c String to send. + /// @return Number of bytes sent. + virtual size_t println(const char* c) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param c Character to send. + /// @return Number of bytes sent. + virtual size_t println(char c) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param b Byte to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t println(unsigned char b, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t println(int num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t println(unsigned int num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t println(long num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param base Numeral system to use for the representation. + /// @return Number of bytes sent. + virtual size_t println(unsigned long num, int base = kDec) const = 0; + + /// @brief Sends data as human-readable ASCII text. + /// + /// @param num Number to send. + /// @param digits Number of decimal places to use. + /// @return Number of bytes sent. + virtual size_t println(double num, int digits = 2) const = 0; +}; + +} // namespace andino diff --git a/andino_firmware/src/serial_stream_arduino.cpp b/andino_firmware/src/serial_stream_arduino.cpp new file mode 100644 index 00000000..1e32be3f --- /dev/null +++ b/andino_firmware/src/serial_stream_arduino.cpp @@ -0,0 +1,90 @@ +// BSD 3-Clause License +// +// Copyright (c) 2024, Ekumen Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#include "serial_stream_arduino.h" + +#include + +#include + +namespace andino { + +void SerialStreamArduino::begin(unsigned long baud) const { Serial.begin(baud); } + +int SerialStreamArduino::available() const { return Serial.available(); } + +int SerialStreamArduino::read() const { return Serial.read(); } + +size_t SerialStreamArduino::print(const char* c) const { return Serial.print(c); } + +size_t SerialStreamArduino::print(char c) const { return Serial.print(c); } + +size_t SerialStreamArduino::print(unsigned char b, int base) const { return Serial.print(b, base); } + +size_t SerialStreamArduino::print(int num, int base) const { return Serial.print(num, base); } + +size_t SerialStreamArduino::print(unsigned int num, int base) const { + return Serial.print(num, base); +} + +size_t SerialStreamArduino::print(long num, int base) const { return Serial.print(num, base); } + +size_t SerialStreamArduino::print(unsigned long num, int base) const { + return Serial.print(num, base); +} + +size_t SerialStreamArduino::print(double num, int digits) const { + return Serial.print(num, digits); +} + +size_t SerialStreamArduino::println(const char* c) const { return Serial.println(c); } + +size_t SerialStreamArduino::println(char c) const { return Serial.println(c); } + +size_t SerialStreamArduino::println(unsigned char b, int base) const { + return Serial.println(b, base); +} + +size_t SerialStreamArduino::println(int num, int base) const { return Serial.println(num, base); } + +size_t SerialStreamArduino::println(unsigned int num, int base) const { + return Serial.println(num, base); +} + +size_t SerialStreamArduino::println(long num, int base) const { return Serial.println(num, base); } + +size_t SerialStreamArduino::println(unsigned long num, int base) const { + return Serial.println(num, base); +} + +size_t SerialStreamArduino::println(double num, int digits) const { + return Serial.println(num, digits); +} + +} // namespace andino diff --git a/andino_firmware/src/serial_stream_arduino.h b/andino_firmware/src/serial_stream_arduino.h new file mode 100644 index 00000000..c34ff855 --- /dev/null +++ b/andino_firmware/src/serial_stream_arduino.h @@ -0,0 +1,83 @@ +// BSD 3-Clause License +// +// Copyright (c) 2024, Ekumen Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#pragma once + +#include + +#include "serial_stream.h" + +namespace andino { + +/// @brief This class provides an Arduino implementation of the serial stream interface. +class SerialStreamArduino : public SerialStream { + public: + /// @brief Constructs a SerialArduino. + explicit SerialStreamArduino() : SerialStream() {} + + void begin(unsigned long baud) const override; + + int available() const override; + + int read() const override; + + size_t print(const char* c) const override; + + size_t print(char c) const override; + + size_t print(unsigned char b, int base) const override; + + size_t print(int num, int base) const override; + + size_t print(unsigned int num, int base) const override; + + size_t print(long num, int base) const override; + + size_t print(unsigned long num, int base) const override; + + size_t print(double num, int digits) const override; + + size_t println(const char* c) const override; + + size_t println(char c) const override; + + size_t println(unsigned char b, int base) const override; + + size_t println(int num, int base) const override; + + size_t println(unsigned int num, int base) const override; + + size_t println(long num, int base) const override; + + size_t println(unsigned long num, int base) const override; + + size_t println(double num, int digits) const override; +}; + +} // namespace andino