summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-14 18:18:38 +0100
committerHampusM <hampus@hampusmat.com>2022-02-14 18:18:38 +0100
commit01ce0af940bd69c94a2fac8b65219262845cca98 (patch)
tree97c443782ce1cfba90b205183c8aab1e3edb0bb3 /src/utils
parentcb7a167c7dee2fa1a19bd09ede3bae8b140e79da (diff)
refactor: clean sewage
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/general.cpp27
-rw-r--r--src/utils/general.hpp33
-rw-r--r--src/utils/memory.hpp26
-rw-r--r--src/utils/memory.tpp65
-rw-r--r--src/utils/serial.cpp31
-rw-r--r--src/utils/serial.hpp8
-rw-r--r--src/utils/smart_string.cpp53
-rw-r--r--src/utils/smart_string.hpp12
-rw-r--r--src/utils/time.cpp22
-rw-r--r--src/utils/time.hpp17
10 files changed, 223 insertions, 71 deletions
diff --git a/src/utils/general.cpp b/src/utils/general.cpp
index bbcd4dd..3464c30 100644
--- a/src/utils/general.cpp
+++ b/src/utils/general.cpp
@@ -5,10 +5,33 @@ void stop()
while (true) {}
}
-unique_ptr<SmartString> floatToStr(float num, unsigned int width, unsigned int precision)
+UniquePtr<SmartString> doubleToStr(double num, unsigned int width, unsigned int precision)
{
auto str = make_unique<SmartString>(width + precision);
- dtostrf(num, width, precision, str->c_str);
+
+ dtostrf(num, static_cast<signed char>(width), precision, str->c_str);
+
+ return str;
+}
+
+UniquePtr<SmartString> intToStr(int num)
+{
+ auto width = static_cast<unsigned int>(log10(num));
+
+ auto str = make_unique<SmartString>(width + 1U);
+
+ dtostrf(num, static_cast<signed char>(width + 1U), 0, str->c_str);
+
+ return str;
+}
+
+UniquePtr<SmartString> uintToStr(unsigned int num)
+{
+ auto width = static_cast<unsigned int>(log10(num));
+
+ auto str = make_unique<SmartString>(width + 1U);
+
+ dtostrf(num, static_cast<signed char>(width + 1U), 0, str->c_str);
return str;
}
diff --git a/src/utils/general.hpp b/src/utils/general.hpp
index 9ad0655..dbdbdfc 100644
--- a/src/utils/general.hpp
+++ b/src/utils/general.hpp
@@ -9,15 +9,28 @@
void stop();
/**
- * Converts a floating point number to a string.
+ * Converts a double number to a string.
*
- * @param num A floating point number
- * @param width The desired float width
- * @param precision The desired float precision
- * @returns The float as a string.
+ * @param num A double number
+ * @param width The desired double width
+ * @param precision The desired double precision
+ * @returns The double as a string.
*/
-unique_ptr<SmartString> floatToStr(
- float num,
- unsigned int width = 3,
- unsigned int precision = 2
-);
+UniquePtr<SmartString> doubleToStr(double num, unsigned int width = 3,
+ unsigned int precision = 2);
+
+/**
+ * Converts a integer to a string.
+ *
+ * @param num A number
+ * @returns The number as a string.
+ */
+UniquePtr<SmartString> intToStr(int num);
+
+/**
+ * Converts a unsigned integer to a string.
+ *
+ * @param num A number
+ * @returns The number as a string.
+ */
+UniquePtr<SmartString> uintToStr(unsigned int num);
diff --git a/src/utils/memory.hpp b/src/utils/memory.hpp
index 81e3757..e870b0b 100644
--- a/src/utils/memory.hpp
+++ b/src/utils/memory.hpp
@@ -1,23 +1,33 @@
#pragma once
+#include <stddef.h>
+
template <typename memType>
memType *malloc_s(unsigned int size);
template <class Target>
-class unique_ptr
+class UniquePtr
{
public:
- unique_ptr(Target *target);
- ~unique_ptr();
+ explicit UniquePtr();
+ explicit UniquePtr(Target *target);
+ UniquePtr(const UniquePtr &unique_ptr);
+ UniquePtr(UniquePtr &&unique_ptr) noexcept;
+
+ UniquePtr &operator=(const UniquePtr &unique_ptr);
+
+ UniquePtr &operator=(UniquePtr &&unique_ptr) noexcept;
+
+ ~UniquePtr();
- Target operator *() const;
- Target *operator ->() const;
+ Target operator*() const;
+ Target *operator->() const;
private:
- Target *_target;
+ Target *_target = nullptr;
};
-template<class Target, typename...Args>
-unique_ptr<Target> make_unique(Args&... args);
+template <typename Target, typename... Args>
+UniquePtr<Target> make_unique(Args... args);
#include "memory.tpp"
diff --git a/src/utils/memory.tpp b/src/utils/memory.tpp
index 276b0b4..6261c81 100644
--- a/src/utils/memory.tpp
+++ b/src/utils/memory.tpp
@@ -1,8 +1,9 @@
-#include "memory.hpp"
+#pragma once
-#include "Arduino.h"
#include "general.hpp"
+#include <Arduino.h>
+
template <typename memType>
memType *malloc_s(unsigned int size)
{
@@ -18,31 +19,75 @@ memType *malloc_s(unsigned int size)
}
template <class Target>
-unique_ptr<Target>::unique_ptr(Target *target)
+UniquePtr<Target>::UniquePtr() = default;
+
+template <class Target>
+UniquePtr<Target>::UniquePtr(Target *target) : _target(target)
+{
+}
+
+template <class Target>
+UniquePtr<Target>::UniquePtr(const UniquePtr &unique_ptr)
+ : _target(new Target(*(unique_ptr._target)))
{
- this->_target = target;
}
template <class Target>
-unique_ptr<Target>::~unique_ptr()
+UniquePtr<Target>::UniquePtr(UniquePtr &&unique_ptr) noexcept
+ : _target(unique_ptr._target)
{
- delete this->_target;
+ unique_ptr._target = nullptr;
+}
+
+template <class Target>
+UniquePtr<Target> &UniquePtr<Target>::operator=(const UniquePtr &unique_ptr)
+{
+ if (&unique_ptr != this)
+ {
+ delete _target;
+ _target = nullptr;
+ _target = new Target(*(unique_ptr._target));
+ }
+
+ return *this;
+}
+
+template <class Target>
+UniquePtr<Target> &UniquePtr<Target>::operator=(UniquePtr &&unique_ptr) noexcept
+{
+ if (&unique_ptr != this)
+ {
+ delete _target;
+ _target = unique_ptr._target;
+ unique_ptr._target = nullptr;
+ }
+
+ return *this;
+}
+
+template <class Target>
+UniquePtr<Target>::~UniquePtr()
+{
+ if (this->_target != nullptr)
+ {
+ delete this->_target;
+ }
}
template <class Target>
-Target unique_ptr<Target>::operator*() const
+Target UniquePtr<Target>::operator*() const
{
return *(this->_target);
}
template <class Target>
-Target *unique_ptr<Target>::operator->() const
+Target *UniquePtr<Target>::operator->() const
{
return this->_target;
}
template <class Target, typename... Args>
-unique_ptr<Target> make_unique(Args... args)
+UniquePtr<Target> make_unique(Args... args)
{
- return unique_ptr<Target>(new Target(args...));
+ return UniquePtr<Target>(new Target(args...));
}
diff --git a/src/utils/serial.cpp b/src/utils/serial.cpp
index 11be550..8e4194f 100644
--- a/src/utils/serial.cpp
+++ b/src/utils/serial.cpp
@@ -2,20 +2,13 @@
#include "general.hpp"
-SerialStream::SerialStream(Serial_ serial, unsigned long baud_rate)
+SerialStream::SerialStream(Serial_ serial, uint64_t baud_rate) : _serial(serial)
{
- this->_serial = serial;
-
this->_serial.begin(baud_rate);
while (!this->_serial) {}
}
-SerialStream::~SerialStream()
-{
- Serial.end();
-}
-
SerialStream &SerialStream::operator<<(const char *str)
{
this->write(str);
@@ -28,9 +21,27 @@ SerialStream &SerialStream::operator<<(const SmartString &str)
return *this;
}
-SerialStream &SerialStream::operator<<(const float num)
+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 uint32_t &num)
{
- this->write(floatToStr(num)->c_str);
+ this->write(uintToStr(num)->c_str);
return *this;
}
diff --git a/src/utils/serial.hpp b/src/utils/serial.hpp
index f97e1d8..0d4f929 100644
--- a/src/utils/serial.hpp
+++ b/src/utils/serial.hpp
@@ -7,12 +7,14 @@
class SerialStream
{
public:
- SerialStream(Serial_ serial, unsigned long baud_rate);
- ~SerialStream();
+ SerialStream(Serial_ serial, uint64_t baud_rate);
SerialStream &operator<<(const char *str);
SerialStream &operator<<(const SmartString &str);
- SerialStream &operator<<(const float num);
+ SerialStream &operator<<(const double &num);
+ SerialStream &operator<<(const int &num);
+ SerialStream &operator<<(const unsigned int &num);
+ SerialStream &operator<<(const uint32_t &num);
SerialStream &operator<<(void (*manipulator)(SerialStream *));
diff --git a/src/utils/smart_string.cpp b/src/utils/smart_string.cpp
index 5258ffe..ac9a862 100644
--- a/src/utils/smart_string.cpp
+++ b/src/utils/smart_string.cpp
@@ -4,23 +4,62 @@
#include <stdlib.h>
-SmartString::SmartString(char *c_string)
+SmartString::SmartString(char *c_string) : c_str(c_string)
{
- this->c_str = c_string;
}
-SmartString::SmartString(unsigned int size)
+SmartString::SmartString(unsigned int size) : c_str(malloc_s<char>(size + 1))
{
- this->c_str = malloc_s<char>(size + 1);
+}
+
+SmartString::SmartString(const SmartString &smart_str)
+ : c_str(malloc_s<char>(strlen(smart_str.c_str) + 1))
+{
+ memcpy(c_str, smart_str.c_str, strlen(smart_str.c_str) + 1);
+}
+
+SmartString::SmartString(SmartString &&smart_str) noexcept : c_str(smart_str.c_str)
+{
+ smart_str.c_str = nullptr;
+}
+
+SmartString &SmartString::operator=(const SmartString &smart_str)
+{
+ if (&smart_str != this)
+ {
+ free(c_str);
+ c_str = nullptr;
+
+ auto str_size = strlen(smart_str.c_str) + 1;
+
+ c_str = malloc_s<char>(str_size);
+ memcpy(c_str, smart_str.c_str, str_size);
+ }
+
+ return *this;
+}
+
+SmartString &SmartString::operator=(SmartString &&smart_str) noexcept
+{
+ if (&smart_str != this)
+ {
+ free(c_str);
+ c_str = smart_str.c_str;
+ smart_str.c_str = nullptr;
+ }
+
+ return *this;
}
SmartString::~SmartString()
{
- if (this->c_str != nullptr)
- free(this->c_str);
+ if (c_str != nullptr)
+ {
+ free(c_str);
+ }
}
SmartString::operator char *() const
{
- return this->c_str;
+ return c_str;
}
diff --git a/src/utils/smart_string.hpp b/src/utils/smart_string.hpp
index fcaff98..6390465 100644
--- a/src/utils/smart_string.hpp
+++ b/src/utils/smart_string.hpp
@@ -3,12 +3,18 @@
class SmartString
{
public:
- SmartString(char *c_str);
- SmartString(unsigned int size);
+ explicit SmartString(char *c_str);
+ explicit SmartString(unsigned int size);
+ SmartString(const SmartString &smart_str);
+ SmartString(SmartString &&smart_str) noexcept;
+
+ SmartString &operator=(const SmartString &smart_str);
+
+ SmartString &operator=(SmartString &&smart_str) noexcept;
~SmartString();
- operator char *() const;
+ explicit operator char *() const;
char *c_str = nullptr;
};
diff --git a/src/utils/time.cpp b/src/utils/time.cpp
index adc33db..c6d981c 100644
--- a/src/utils/time.cpp
+++ b/src/utils/time.cpp
@@ -1,9 +1,9 @@
#include "time.hpp"
-#include "Arduino.h"
-Time::Time(unsigned long time_micros)
+#include <Arduino.h>
+
+Time::Time(uint64_t time_micros) : _time_micros(time_micros)
{
- _time_micros = time_micros;
}
void Time::update()
@@ -16,19 +16,23 @@ Time Time::diff(Time prev_time)
return Time(_time_micros - prev_time.microsecs());
}
-unsigned long Time::microsecs()
+double Time::secs()
{
- return _time_micros;
+ const double micros_to_secs = 0.000001;
+
+ return static_cast<double>(_time_micros) * micros_to_secs;
}
-unsigned long Time::millisecs()
+double Time::millisecs()
{
- return _time_micros * 0.001;
+ const double micros_to_millis = 0.001;
+
+ return static_cast<double>(_time_micros) * micros_to_millis;
}
-float Time::secs()
+uint64_t Time::microsecs()
{
- return _time_micros * 0.000001;
+ return _time_micros;
}
Time time_now()
diff --git a/src/utils/time.hpp b/src/utils/time.hpp
index e0385ef..1577bb7 100644
--- a/src/utils/time.hpp
+++ b/src/utils/time.hpp
@@ -1,5 +1,6 @@
-#ifndef TIME_HPP
-#define TIME_HPP
+#pragma once
+
+#include <stdint.h>
/**
* A representation of time.
@@ -12,7 +13,7 @@ public:
*
* @param time_micros Time in microseconds
*/
- Time(unsigned long time_micros);
+ explicit Time(uint64_t time_micros);
/**
* Updates the time to the current time.
@@ -29,25 +30,23 @@ public:
/**
* Returns the time in seconds.
*/
- float secs();
+ double secs();
/**
* Returns the time in milliseconds.
*/
- unsigned long millisecs();
+ double millisecs();
/**
* Returns the time in microseconds.
*/
- unsigned long microsecs();
+ uint64_t microsecs();
private:
- unsigned long _time_micros;
+ uint64_t _time_micros;
};
/**
* Returns a time object for the time since the program started.
*/
Time time_now();
-
-#endif