summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-01 09:22:37 +0100
committerHampusM <hampus@hampusmat.com>2022-03-01 09:22:37 +0100
commit249323fc5fa14dbc4c1a6316873cfc07f468f43b (patch)
treebd53ea18ab364e5f19993d0f7ef07a39ce10f60f /src
parent61f11389ea6296fb0c28dd7aa4d0bfa6feb919f3 (diff)
refactor: fix serial stream & unique ptr
Diffstat (limited to 'src')
-rw-r--r--src/gyronardo.cpp7
-rw-r--r--src/serial.cpp28
-rw-r--r--src/serial.hpp4
-rw-r--r--src/std/memory.tpp9
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<SerialStream> sout_ptr;
+SerialStream sout(Serial_(), BAUD_RATE);
+// SerialStream sout(Serial, BAUD_RATE);
UniquePtr<Sensor> sensor;
void setup()
{
- sout_ptr = make_unique<SerialStream>(Serial, BAUD_RATE);
-
- auto sout = *sout_ptr;
+ sout.waitReady();
sensor = make_unique<Sensor>(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<Target> &UniquePtr<Target>::operator=(UniquePtr &&unique_ptr) noexcept
template <class Target>
UniquePtr<Target>::~UniquePtr()
{
- if (this->_target != nullptr)
- {
- delete this->_target;
- }
+ delete _target;
}
template <class Target>
Target UniquePtr<Target>::operator*() const
{
- return *(this->_target);
+ return *(_target);
}
template <class Target>
Target *UniquePtr<Target>::operator->() const
{
- return this->_target;
+ return _target;
}
template <class Target, typename... Args>