diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-02-15 12:33:52 +0100 |
commit | bcdce9633dc351d3bc7f347a165348b8fab87cd9 (patch) | |
tree | 88c2f5d8f0c5fac2bca28e4b543e5209f3bc98fb /src/utils | |
parent | 917adc6a2b6b166e37fc3d4f94b41488f0c245a5 (diff) |
refactor: reorganize files & improve classes
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/general.cpp | 37 | ||||
-rw-r--r-- | src/utils/general.hpp | 36 | ||||
-rw-r--r-- | src/utils/memory.hpp | 33 | ||||
-rw-r--r-- | src/utils/memory.tpp | 93 | ||||
-rw-r--r-- | src/utils/serial.cpp | 68 | ||||
-rw-r--r-- | src/utils/serial.hpp | 29 | ||||
-rw-r--r-- | src/utils/smart_string.cpp | 65 | ||||
-rw-r--r-- | src/utils/smart_string.hpp | 20 | ||||
-rw-r--r-- | src/utils/time.cpp | 41 | ||||
-rw-r--r-- | src/utils/time.hpp | 52 |
10 files changed, 0 insertions, 474 deletions
diff --git a/src/utils/general.cpp b/src/utils/general.cpp deleted file mode 100644 index 3464c30..0000000 --- a/src/utils/general.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "general.hpp" - -void stop() -{ - while (true) {} -} - -UniquePtr<SmartString> doubleToStr(double num, unsigned int width, unsigned int precision) -{ - auto str = make_unique<SmartString>(width + precision); - - 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 deleted file mode 100644 index dbdbdfc..0000000 --- a/src/utils/general.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include "memory.hpp" -#include "smart_string.hpp" - -/** - * Stops code execution. - */ -void stop(); - -/** - * Converts a double number to 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. - */ -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 deleted file mode 100644 index e870b0b..0000000 --- a/src/utils/memory.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include <stddef.h> - -template <typename memType> -memType *malloc_s(unsigned int size); - -template <class Target> -class UniquePtr -{ -public: - 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; - -private: - Target *_target = nullptr; -}; - -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 deleted file mode 100644 index 6261c81..0000000 --- a/src/utils/memory.tpp +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#include "general.hpp" - -#include <Arduino.h> - -template <typename memType> -memType *malloc_s(unsigned int size) -{ - auto *mem = malloc(size); - - if (mem == nullptr) - { - Serial.println("Error: Memory allocation failed"); - while (true) {} - } - - return static_cast<memType *>(mem); -} - -template <class 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))) -{ -} - -template <class Target> -UniquePtr<Target>::UniquePtr(UniquePtr &&unique_ptr) noexcept - : _target(unique_ptr._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 UniquePtr<Target>::operator*() const -{ - return *(this->_target); -} - -template <class Target> -Target *UniquePtr<Target>::operator->() const -{ - return this->_target; -} - -template <class Target, typename... Args> -UniquePtr<Target> make_unique(Args... args) -{ - return UniquePtr<Target>(new Target(args...)); -} diff --git a/src/utils/serial.cpp b/src/utils/serial.cpp deleted file mode 100644 index 8e4194f..0000000 --- a/src/utils/serial.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "serial.hpp" - -#include "general.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 uint32_t &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(); -} diff --git a/src/utils/serial.hpp b/src/utils/serial.hpp deleted file mode 100644 index 0d4f929..0000000 --- a/src/utils/serial.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include "smart_string.hpp" - -#include <USBAPI.h> - -class SerialStream -{ -public: - SerialStream(Serial_ serial, uint64_t baud_rate); - - SerialStream &operator<<(const char *str); - SerialStream &operator<<(const SmartString &str); - 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 *)); - - void write(const char *str); - - void flush(); - -private: - Serial_ _serial; -}; - -void endl(SerialStream *serial_stream); diff --git a/src/utils/smart_string.cpp b/src/utils/smart_string.cpp deleted file mode 100644 index ac9a862..0000000 --- a/src/utils/smart_string.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "smart_string.hpp" - -#include "memory.hpp" - -#include <stdlib.h> - -SmartString::SmartString(char *c_string) : c_str(c_string) -{ -} - -SmartString::SmartString(unsigned int size) : 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 (c_str != nullptr) - { - free(c_str); - } -} - -SmartString::operator char *() const -{ - return c_str; -} diff --git a/src/utils/smart_string.hpp b/src/utils/smart_string.hpp deleted file mode 100644 index 6390465..0000000 --- a/src/utils/smart_string.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -class SmartString -{ -public: - 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(); - - explicit operator char *() const; - - char *c_str = nullptr; -}; diff --git a/src/utils/time.cpp b/src/utils/time.cpp deleted file mode 100644 index c6d981c..0000000 --- a/src/utils/time.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "time.hpp" - -#include <Arduino.h> - -Time::Time(uint64_t time_micros) : _time_micros(time_micros) -{ -} - -void Time::update() -{ - _time_micros = micros(); -} - -Time Time::diff(Time prev_time) -{ - return Time(_time_micros - prev_time.microsecs()); -} - -double Time::secs() -{ - const double micros_to_secs = 0.000001; - - return static_cast<double>(_time_micros) * micros_to_secs; -} - -double Time::millisecs() -{ - const double micros_to_millis = 0.001; - - return static_cast<double>(_time_micros) * micros_to_millis; -} - -uint64_t Time::microsecs() -{ - return _time_micros; -} - -Time time_now() -{ - return Time(micros()); -} diff --git a/src/utils/time.hpp b/src/utils/time.hpp deleted file mode 100644 index 1577bb7..0000000 --- a/src/utils/time.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include <stdint.h> - -/** - * A representation of time. - */ -class Time -{ -public: - /** - * A representation of time. - * - * @param time_micros Time in microseconds - */ - explicit Time(uint64_t time_micros); - - /** - * Updates the time to the current time. - */ - void update(); - - /** - * Returns the difference between two points in time. - * - * @param prev_time A previous point in time - */ - Time diff(Time prev_time); - - /** - * Returns the time in seconds. - */ - double secs(); - - /** - * Returns the time in milliseconds. - */ - double millisecs(); - - /** - * Returns the time in microseconds. - */ - uint64_t microsecs(); - -private: - uint64_t _time_micros; -}; - -/** - * Returns a time object for the time since the program started. - */ -Time time_now(); |