From 249323fc5fa14dbc4c1a6316873cfc07f468f43b Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 1 Mar 2022 09:22:37 +0100 Subject: refactor: fix serial stream & unique ptr --- src/gyronardo.cpp | 7 +++---- src/serial.cpp | 28 ++++++++++++++++------------ src/serial.hpp | 4 +++- src/std/memory.tpp | 9 +++------ 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/gyronardo.cpp b/src/gyronardo.cpp index e78bb1d..57bcd18 100644 --- a/src/gyronardo.cpp +++ b/src/gyronardo.cpp @@ -15,15 +15,14 @@ constexpr unsigned int BAUD_RATE = 9600U; constexpr unsigned int SENSOR_RETRY_TIME = 2000U; -UniquePtr sout_ptr; +SerialStream sout(Serial_(), BAUD_RATE); +// SerialStream sout(Serial, BAUD_RATE); UniquePtr sensor; void setup() { - sout_ptr = make_unique(Serial, BAUD_RATE); - - auto sout = *sout_ptr; + sout.waitReady(); sensor = make_unique(SENSOR_ADDRESS, Wire, sout, SENSOR_THROTTLE_TIME); diff --git a/src/serial.cpp b/src/serial.cpp index d03f8e8..acf92fa 100644 --- a/src/serial.cpp +++ b/src/serial.cpp @@ -2,46 +2,45 @@ #include "std/conversion.hpp" -SerialStream::SerialStream(Serial_ serial, uint64_t baud_rate) : _serial(serial) +SerialStream::SerialStream(Serial_ serial, const unsigned long &baud_rate) + : _serial(serial) { - this->_serial.begin(baud_rate); - - while (!this->_serial) {} + _serial.begin(baud_rate); } SerialStream &SerialStream::operator<<(const char *str) { - this->write(str); + write(str); return *this; } SerialStream &SerialStream::operator<<(const SmartString &str) { - this->write(str.c_str); + write(str.c_str); return *this; } SerialStream &SerialStream::operator<<(const double &num) { - this->write(doubleToStr(num)->c_str); + write(doubleToStr(num)->c_str); return *this; } SerialStream &SerialStream::operator<<(const int &num) { - this->write(intToStr(num)->c_str); + write(intToStr(num)->c_str); return *this; } SerialStream &SerialStream::operator<<(const unsigned int &num) { - this->write(uintToStr(num)->c_str); + write(uintToStr(num)->c_str); return *this; } SerialStream &SerialStream::operator<<(const unsigned long &num) { - this->write(uintToStr(num)->c_str); + write(uintToStr(num)->c_str); return *this; } @@ -51,14 +50,19 @@ SerialStream &SerialStream::operator<<(void (*manipulator)(SerialStream *)) return *this; } +void SerialStream::waitReady() +{ + while (!_serial) {} +} + void SerialStream::write(const char *str) { - this->_serial.write(str); + _serial.write(str); } void SerialStream::flush() { - this->_serial.flush(); + _serial.flush(); } void endl(SerialStream *serial_stream) diff --git a/src/serial.hpp b/src/serial.hpp index 9a9488d..f32ca63 100644 --- a/src/serial.hpp +++ b/src/serial.hpp @@ -7,7 +7,7 @@ class SerialStream { public: - SerialStream(Serial_ serial, uint64_t baud_rate); + SerialStream(Serial_ serial, const unsigned long &baud_rate); SerialStream &operator<<(const char *str); SerialStream &operator<<(const SmartString &str); @@ -18,6 +18,8 @@ public: SerialStream &operator<<(void (*manipulator)(SerialStream *)); + void waitReady(); + void write(const char *str); void flush(); diff --git a/src/std/memory.tpp b/src/std/memory.tpp index 1bfa473..fdfe72b 100644 --- a/src/std/memory.tpp +++ b/src/std/memory.tpp @@ -68,22 +68,19 @@ UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept template UniquePtr::~UniquePtr() { - if (this->_target != nullptr) - { - delete this->_target; - } + delete _target; } template Target UniquePtr::operator*() const { - return *(this->_target); + return *(_target); } template Target *UniquePtr::operator->() const { - return this->_target; + return _target; } template -- cgit v1.2.3-18-g5258