From bcdce9633dc351d3bc7f347a165348b8fab87cd9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 15 Feb 2022 12:33:52 +0100 Subject: refactor: reorganize files & improve classes --- src/serial.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/serial.cpp (limited to 'src/serial.cpp') diff --git a/src/serial.cpp b/src/serial.cpp new file mode 100644 index 0000000..d03f8e8 --- /dev/null +++ b/src/serial.cpp @@ -0,0 +1,68 @@ +#include "serial.hpp" + +#include "std/conversion.hpp" + +SerialStream::SerialStream(Serial_ serial, uint64_t baud_rate) : _serial(serial) +{ + this->_serial.begin(baud_rate); + + while (!this->_serial) {} +} + +SerialStream &SerialStream::operator<<(const char *str) +{ + this->write(str); + return *this; +} + +SerialStream &SerialStream::operator<<(const SmartString &str) +{ + this->write(str.c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const double &num) +{ + this->write(doubleToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const int &num) +{ + this->write(intToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const unsigned int &num) +{ + this->write(uintToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(const unsigned long &num) +{ + this->write(uintToStr(num)->c_str); + return *this; +} + +SerialStream &SerialStream::operator<<(void (*manipulator)(SerialStream *)) +{ + manipulator(this); + return *this; +} + +void SerialStream::write(const char *str) +{ + this->_serial.write(str); +} + +void SerialStream::flush() +{ + this->_serial.flush(); +} + +void endl(SerialStream *serial_stream) +{ + serial_stream->write("\n"); + serial_stream->flush(); +} -- cgit v1.2.3-18-g5258