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/calibration.cpp | 107 --------------------------------------------- src/calibration.hpp | 73 ------------------------------- src/gyronardo.cpp | 6 +-- src/sensor/calibration.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++ src/sensor/calibration.hpp | 73 +++++++++++++++++++++++++++++++ src/sensor/sensor.cpp | 28 ++++++------ src/sensor/sensor.hpp | 27 ++++++------ src/serial.cpp | 68 ++++++++++++++++++++++++++++ src/serial.hpp | 29 ++++++++++++ src/std/conversion.cpp | 32 ++++++++++++++ src/std/conversion.hpp | 31 +++++++++++++ src/std/memory.hpp | 33 ++++++++++++++ src/std/memory.tpp | 94 +++++++++++++++++++++++++++++++++++++++ src/std/smart_string.cpp | 65 +++++++++++++++++++++++++++ src/std/smart_string.hpp | 20 +++++++++ src/std/time.cpp | 37 ++++++++++++++++ src/std/time.hpp | 55 +++++++++++++++++++++++ src/utils.cpp | 6 +++ src/utils.hpp | 6 +++ src/utils/general.cpp | 37 ---------------- src/utils/general.hpp | 36 --------------- src/utils/memory.hpp | 33 -------------- src/utils/memory.tpp | 93 --------------------------------------- src/utils/serial.cpp | 68 ---------------------------- src/utils/serial.hpp | 29 ------------ src/utils/smart_string.cpp | 65 --------------------------- src/utils/smart_string.hpp | 20 --------- src/utils/time.cpp | 41 ----------------- src/utils/time.hpp | 52 ---------------------- 29 files changed, 685 insertions(+), 686 deletions(-) delete mode 100644 src/calibration.cpp delete mode 100644 src/calibration.hpp create mode 100644 src/sensor/calibration.cpp create mode 100644 src/sensor/calibration.hpp create mode 100644 src/serial.cpp create mode 100644 src/serial.hpp create mode 100644 src/std/conversion.cpp create mode 100644 src/std/conversion.hpp create mode 100644 src/std/memory.hpp create mode 100644 src/std/memory.tpp create mode 100644 src/std/smart_string.cpp create mode 100644 src/std/smart_string.hpp create mode 100644 src/std/time.cpp create mode 100644 src/std/time.hpp create mode 100644 src/utils.cpp create mode 100644 src/utils.hpp delete mode 100644 src/utils/general.cpp delete mode 100644 src/utils/general.hpp delete mode 100644 src/utils/memory.hpp delete mode 100644 src/utils/memory.tpp delete mode 100644 src/utils/serial.cpp delete mode 100644 src/utils/serial.hpp delete mode 100644 src/utils/smart_string.cpp delete mode 100644 src/utils/smart_string.hpp delete mode 100644 src/utils/time.cpp delete mode 100644 src/utils/time.hpp diff --git a/src/calibration.cpp b/src/calibration.cpp deleted file mode 100644 index 66a52bf..0000000 --- a/src/calibration.cpp +++ /dev/null @@ -1,107 +0,0 @@ -#include "calibration.hpp" - -#include "utils/general.hpp" -#include "utils/time.hpp" - -SensorCalibrator::SensorCalibrator(UniquePtr &sensor, SerialStream sout) - : _sensor(sensor), _sout(sout) -{ - _resetValues(); -} - -bool SensorCalibrator::calibrate(unsigned int throttle_time) -{ - bool done = false; - Time start_time = time_now(); - - while (!done) - { - if (time_now().diff(start_time).millisecs() >= CALIBRATION_TIMEOUT) - { - return false; - } - - delay(throttle_time); - - _resetValues(); - - for (unsigned int i = 0U; i < SENSOR_READ_CNT; i++) - { - _updateValues(); - } - - _adjustValues(); - - _sout << "Accel X: " << _accel_x << " " - << "Accel Y: " << _accel_y << " " - << "Accel Z: " << _accel_z << " " - << "Gyro X: " << _gyro_x << " " - << "Gyro Y: " << _gyro_y << " " - << "Gyro Z: " << _gyro_z << endl; - - if (_isValuesInRange()) - { - done = true; - } - - _adjustCalibration(); - } - - return true; -} - -void SensorCalibrator::_resetValues() -{ - _accel_x = 0; - _accel_y = 0; - _accel_z = 0; - - _gyro_x = 0; - _gyro_y = 0; - _gyro_z = 0; -} - -void SensorCalibrator::_updateValues() -{ - _sensor->read(); - - _accel_x -= _sensor->getAccelX(); - _accel_y -= _sensor->getAccelY(); - _accel_z -= _sensor->getAccelZ(); - - _gyro_x -= _sensor->getGyroX(); - _gyro_y -= _sensor->getGyroY(); - _gyro_z -= _sensor->getGyroZ(); -} - -void SensorCalibrator::_adjustValues() -{ - _accel_x *= SENSOR_VAL_ADJUST; - _accel_y *= SENSOR_VAL_ADJUST; - _accel_z *= SENSOR_VAL_ADJUST; - - _gyro_x *= SENSOR_VAL_ADJUST; - _gyro_y *= SENSOR_VAL_ADJUST; - _gyro_z *= SENSOR_VAL_ADJUST; -} - -bool SensorCalibrator::_isValuesInRange() -{ - return (_accel_x < ACCEL_CAL_X_MAX && _accel_x > ACCEL_CAL_X_MIN && - _accel_y < ACCEL_CAL_Y_MAX && _accel_y > ACCEL_CAL_Y_MIN && - _accel_z < ACCEL_CAL_Z_MAX && _accel_z > ACCEL_CAL_Z_MIN && - _gyro_x < GYRO_CAL_X_MAX && _gyro_x > GYRO_CAL_X_MIN && - _gyro_y < GYRO_CAL_Y_MAX && _gyro_y > GYRO_CAL_Y_MIN && - _gyro_z < GYRO_CAL_Z_MAX && _gyro_z > GYRO_CAL_Z_MIN); -} - -void SensorCalibrator::_adjustCalibration() -{ - _sensor->accel_cal_x += _accel_x; - _sensor->accel_cal_y += _accel_y; - _sensor->accel_cal_z += _accel_z; - - _sensor->gyro_cal_x += _gyro_x; - _sensor->gyro_cal_y += _gyro_y; - _sensor->gyro_cal_z += _gyro_z; -} diff --git a/src/calibration.hpp b/src/calibration.hpp deleted file mode 100644 index e29602a..0000000 --- a/src/calibration.hpp +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include "sensor/sensor.hpp" -#include "utils/memory.hpp" -#include "utils/serial.hpp" - -// Calibration precision -constexpr float ACCEL_CAL_X_MAX = 0.006; -constexpr float ACCEL_CAL_X_MIN = -0.006; - -constexpr float ACCEL_CAL_Y_MAX = 0.006; -constexpr float ACCEL_CAL_Y_MIN = -0.006; - -constexpr float ACCEL_CAL_Z_MAX = 0.006; -constexpr float ACCEL_CAL_Z_MIN = -0.006; - -constexpr float GYRO_CAL_X_MAX = 0.06; -constexpr float GYRO_CAL_X_MIN = -0.06; - -constexpr float GYRO_CAL_Y_MAX = 0.06; -constexpr float GYRO_CAL_Y_MIN = -0.06; - -constexpr float GYRO_CAL_Z_MAX = 0.06; -constexpr float GYRO_CAL_Z_MIN = -0.06; - -constexpr uint32_t CALIBRATION_TIMEOUT = 120000; // Milliseconds - -constexpr unsigned int SENSOR_READ_CNT = 20; -constexpr float SENSOR_VAL_ADJUST = 0.05; - -/** - * Sensor calibrator. - */ -class SensorCalibrator -{ -public: - /** - * Sensor calibrator. - * - * @param sensor A sensor to calibrate - * @param sout A Serial output stream - */ - SensorCalibrator(UniquePtr &sensor, SerialStream sout); - - /** - * Calibrates the sensor. - * - * @param throttle_time The sensor's throttle time - * @returns Whether or not the calibration succeeded. Will return false on - * timeout. - */ - bool calibrate(unsigned int throttle_time); - -private: - void _resetValues(); - void _updateValues(); - void _adjustValues(); - bool _isValuesInRange(); - - void _adjustCalibration(); - - UniquePtr &_sensor; - - SerialStream _sout; - - double _accel_x = 0; - double _accel_y = 0; - double _accel_z = 0; - - double _gyro_x = 0; - double _gyro_y = 0; - double _gyro_z = 0; -}; diff --git a/src/gyronardo.cpp b/src/gyronardo.cpp index 1133f3f..e78bb1d 100644 --- a/src/gyronardo.cpp +++ b/src/gyronardo.cpp @@ -1,7 +1,7 @@ -#include "calibration.hpp" +#include "sensor/calibration.hpp" #include "sensor/sensor.hpp" -#include "utils/memory.hpp" -#include "utils/serial.hpp" +#include "serial.hpp" +#include "std/memory.hpp" #include #include diff --git a/src/sensor/calibration.cpp b/src/sensor/calibration.cpp new file mode 100644 index 0000000..40bb52b --- /dev/null +++ b/src/sensor/calibration.cpp @@ -0,0 +1,107 @@ +#include "calibration.hpp" + +#include "std/time.hpp" +#include "utils.hpp" + +SensorCalibrator::SensorCalibrator(UniquePtr &sensor, SerialStream sout) + : _sensor(sensor), _sout(sout) +{ + _resetValues(); +} + +bool SensorCalibrator::calibrate(unsigned int throttle_time) +{ + bool done = false; + Time start_time = time_now(); + + while (!done) + { + if (time_now().diff(start_time).millisecs() >= CALIBRATION_TIMEOUT) + { + return false; + } + + delay(throttle_time); + + _resetValues(); + + for (unsigned int i = 0U; i < SENSOR_READ_CNT; i++) + { + _updateValues(); + } + + _adjustValues(); + + _sout << "Accel X: " << _accel_x << " " + << "Accel Y: " << _accel_y << " " + << "Accel Z: " << _accel_z << " " + << "Gyro X: " << _gyro_x << " " + << "Gyro Y: " << _gyro_y << " " + << "Gyro Z: " << _gyro_z << endl; + + if (_isValuesInRange()) + { + done = true; + } + + _adjustCalibration(); + } + + return true; +} + +void SensorCalibrator::_resetValues() +{ + _accel_x = 0; + _accel_y = 0; + _accel_z = 0; + + _gyro_x = 0; + _gyro_y = 0; + _gyro_z = 0; +} + +void SensorCalibrator::_updateValues() +{ + _sensor->read(); + + _accel_x -= _sensor->getAccelX(); + _accel_y -= _sensor->getAccelY(); + _accel_z -= _sensor->getAccelZ(); + + _gyro_x -= _sensor->getGyroX(); + _gyro_y -= _sensor->getGyroY(); + _gyro_z -= _sensor->getGyroZ(); +} + +void SensorCalibrator::_adjustValues() +{ + _accel_x *= SENSOR_VAL_ADJUST; + _accel_y *= SENSOR_VAL_ADJUST; + _accel_z *= SENSOR_VAL_ADJUST; + + _gyro_x *= SENSOR_VAL_ADJUST; + _gyro_y *= SENSOR_VAL_ADJUST; + _gyro_z *= SENSOR_VAL_ADJUST; +} + +bool SensorCalibrator::_isValuesInRange() const +{ + return (_accel_x < ACCEL_CAL_X_MAX && _accel_x > ACCEL_CAL_X_MIN && + _accel_y < ACCEL_CAL_Y_MAX && _accel_y > ACCEL_CAL_Y_MIN && + _accel_z < ACCEL_CAL_Z_MAX && _accel_z > ACCEL_CAL_Z_MIN && + _gyro_x < GYRO_CAL_X_MAX && _gyro_x > GYRO_CAL_X_MIN && + _gyro_y < GYRO_CAL_Y_MAX && _gyro_y > GYRO_CAL_Y_MIN && + _gyro_z < GYRO_CAL_Z_MAX && _gyro_z > GYRO_CAL_Z_MIN); +} + +void SensorCalibrator::_adjustCalibration() +{ + _sensor->accel_cal_x += _accel_x; + _sensor->accel_cal_y += _accel_y; + _sensor->accel_cal_z += _accel_z; + + _sensor->gyro_cal_x += _gyro_x; + _sensor->gyro_cal_y += _gyro_y; + _sensor->gyro_cal_z += _gyro_z; +} diff --git a/src/sensor/calibration.hpp b/src/sensor/calibration.hpp new file mode 100644 index 0000000..03e2e9c --- /dev/null +++ b/src/sensor/calibration.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include "sensor/sensor.hpp" +#include "serial.hpp" +#include "std/memory.hpp" + +// Calibration precision +constexpr float ACCEL_CAL_X_MAX = 0.006; +constexpr float ACCEL_CAL_X_MIN = -0.006; + +constexpr float ACCEL_CAL_Y_MAX = 0.006; +constexpr float ACCEL_CAL_Y_MIN = -0.006; + +constexpr float ACCEL_CAL_Z_MAX = 0.006; +constexpr float ACCEL_CAL_Z_MIN = -0.006; + +constexpr float GYRO_CAL_X_MAX = 0.06; +constexpr float GYRO_CAL_X_MIN = -0.06; + +constexpr float GYRO_CAL_Y_MAX = 0.06; +constexpr float GYRO_CAL_Y_MIN = -0.06; + +constexpr float GYRO_CAL_Z_MAX = 0.06; +constexpr float GYRO_CAL_Z_MIN = -0.06; + +constexpr uint32_t CALIBRATION_TIMEOUT = 120000; // Milliseconds + +constexpr unsigned int SENSOR_READ_CNT = 20; +constexpr float SENSOR_VAL_ADJUST = 0.05; + +/** + * Sensor calibrator. + */ +class SensorCalibrator +{ +public: + /** + * Sensor calibrator. + * + * @param sensor A sensor to calibrate + * @param sout A Serial output stream + */ + SensorCalibrator(UniquePtr &sensor, SerialStream sout); + + /** + * Calibrates the sensor. + * + * @param throttle_time The sensor's throttle time + * @returns Whether or not the calibration succeeded. Will return false on + * timeout. + */ + bool calibrate(unsigned int throttle_time); + +private: + void _resetValues(); + void _updateValues(); + void _adjustValues(); + bool _isValuesInRange() const; + + void _adjustCalibration(); + + UniquePtr &_sensor; + + SerialStream _sout; + + double _accel_x = 0; + double _accel_y = 0; + double _accel_z = 0; + + double _gyro_x = 0; + double _gyro_y = 0; + double _gyro_z = 0; +}; diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp index a3b9a5c..5ca5dc3 100644 --- a/src/sensor/sensor.cpp +++ b/src/sensor/sensor.cpp @@ -1,20 +1,18 @@ #include "sensor.hpp" -#include "registers.hpp" -#include "utils/serial.hpp" -#include "utils/time.hpp" +#include "sensor/registers.hpp" Sensor::Sensor(uint8_t address, TwoWire wire, SerialStream sout, unsigned int throttle_time) : _wire(wire), + _sout(sout), _address(address), _throttle_enabled(true), _throttle_time(throttle_time), _last_time(0), _status(SensorStatus::OK), _accel_to_g_force(RAW_TO_G_FACTOR), - _ang_rate_to_dps(RAW_TO_DPS_FACTOR), - _sout(sout) + _ang_rate_to_dps(RAW_TO_DPS_FACTOR) { } @@ -241,52 +239,52 @@ bool Sensor::setGyroSensitivity(uint8_t sensitivity) return true; } -double Sensor::getAccelX() +double Sensor::getAccelX() const { return _accel_raw_x; } -double Sensor::getAccelY() +double Sensor::getAccelY() const { return _accel_raw_y; } -double Sensor::getAccelZ() +double Sensor::getAccelZ() const { return _accel_raw_z; } -double Sensor::getAngleX() +double Sensor::getAngleX() const { return _accel_x; } -double Sensor::getAngleY() +double Sensor::getAngleY() const { return _accel_y; } -double Sensor::getGyroX() +double Sensor::getGyroX() const { return _gyro_raw_x; } -double Sensor::getGyroY() +double Sensor::getGyroY() const { return _gyro_raw_y; } -double Sensor::getGyroZ() +double Sensor::getGyroZ() const { return _gyro_raw_z; } -double Sensor::getPitch() +double Sensor::getPitch() const { return _pitch; } -double Sensor::getRoll() +double Sensor::getRoll() const { return _roll; } diff --git a/src/sensor/sensor.hpp b/src/sensor/sensor.hpp index 3e56627..7b76ae2 100644 --- a/src/sensor/sensor.hpp +++ b/src/sensor/sensor.hpp @@ -1,7 +1,7 @@ #pragma once -#include "utils/serial.hpp" -#include "utils/time.hpp" +#include "serial.hpp" +#include "std/time.hpp" #include #include @@ -77,52 +77,52 @@ public: /** * Returns the current X axis acceleration in g:s (g-force). */ - double getAccelX(); + double getAccelX() const; /** * Returns the current Y axis acceleration in g:s (g-force). */ - double getAccelY(); + double getAccelY() const; /** * Returns the current Z axis acceleration in g:s (g-force). */ - double getAccelZ(); + double getAccelZ() const; /** * Returns the current X angle. */ - double getAngleX(); + double getAngleX() const; /** * Returns the current Y angle. */ - double getAngleY(); + double getAngleY() const; /** * Returns the current X axis in degrees/s. */ - double getGyroX(); + double getGyroX() const; /** * Returns the current Y axis in degrees/s. */ - double getGyroY(); + double getGyroY() const; /** * Returns the current Z axis in degrees/s. */ - double getGyroZ(); + double getGyroZ() const; /** * Returns the current pitch angle. */ - double getPitch(); + double getPitch() const; /** * Returns the current roll angle. */ - double getRoll(); + double getRoll() const; /** * Sets the value of a key in the sensor's register. @@ -157,6 +157,7 @@ public: private: TwoWire _wire; + SerialStream _sout; uint8_t _address; @@ -193,6 +194,4 @@ private: double _yaw = 0; int16_t _readHighLow(); - - SerialStream _sout; }; 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(); +} diff --git a/src/serial.hpp b/src/serial.hpp new file mode 100644 index 0000000..9a9488d --- /dev/null +++ b/src/serial.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include "std/smart_string.hpp" + +#include + +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 unsigned long &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/std/conversion.cpp b/src/std/conversion.cpp new file mode 100644 index 0000000..a2a624d --- /dev/null +++ b/src/std/conversion.cpp @@ -0,0 +1,32 @@ +#include "conversion.hpp" + +UniquePtr doubleToStr(double num, unsigned int width, unsigned int precision) +{ + auto str = make_unique(width + precision); + + dtostrf(num, static_cast(width), precision, str->c_str); + + return str; +} + +UniquePtr intToStr(int num) +{ + auto width = static_cast(log10(num)); + + auto str = make_unique(width + 1U); + + dtostrf(num, static_cast(width + 1U), 0, str->c_str); + + return str; +} + +UniquePtr uintToStr(unsigned int num) +{ + auto width = static_cast(log10(num)); + + auto str = make_unique(width + 1U); + + dtostrf(num, static_cast(width + 1U), 0, str->c_str); + + return str; +} diff --git a/src/std/conversion.hpp b/src/std/conversion.hpp new file mode 100644 index 0000000..726ce88 --- /dev/null +++ b/src/std/conversion.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "std/memory.hpp" +#include "std/smart_string.hpp" + +/** + * 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 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 intToStr(int num); + +/** + * Converts a unsigned integer to a string. + * + * @param num A number + * @returns The number as a string. + */ +UniquePtr uintToStr(unsigned int num); diff --git a/src/std/memory.hpp b/src/std/memory.hpp new file mode 100644 index 0000000..e870b0b --- /dev/null +++ b/src/std/memory.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +template +memType *malloc_s(unsigned int size); + +template +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 +UniquePtr make_unique(Args... args); + +#include "memory.tpp" diff --git a/src/std/memory.tpp b/src/std/memory.tpp new file mode 100644 index 0000000..fbfcd32 --- /dev/null +++ b/src/std/memory.tpp @@ -0,0 +1,94 @@ +#pragma once + +#include "utils.hpp" + +#include +#include + +template +memType *malloc_s(unsigned int size) +{ + auto *mem = malloc(size); + + if (mem == nullptr) + { + Serial.println("Error: Memory allocation failed"); + while (true) {} + } + + return static_cast(mem); +} + +template +UniquePtr::UniquePtr() = default; + +template +UniquePtr::UniquePtr(Target *target) : _target(target) +{ +} + +template +UniquePtr::UniquePtr(const UniquePtr &unique_ptr) + : _target(new Target(*(unique_ptr._target))) +{ +} + +template +UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept + : _target(unique_ptr._target) +{ + unique_ptr._target = nullptr; +} + +template +UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) +{ + if (&unique_ptr != this) + { + delete _target; + _target = nullptr; + _target = new Target(*(unique_ptr._target)); + } + + return *this; +} + +template +UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept +{ + if (&unique_ptr != this) + { + delete _target; + _target = unique_ptr._target; + unique_ptr._target = nullptr; + } + + return *this; +} + +template +UniquePtr::~UniquePtr() +{ + if (this->_target != nullptr) + { + delete this->_target; + } +} + +template +Target UniquePtr::operator*() const +{ + return *(this->_target); +} + +template +Target *UniquePtr::operator->() const +{ + return this->_target; +} + +template +UniquePtr make_unique(Args... args) +{ + return UniquePtr(new Target(args...)); +} diff --git a/src/std/smart_string.cpp b/src/std/smart_string.cpp new file mode 100644 index 0000000..b24a1a5 --- /dev/null +++ b/src/std/smart_string.cpp @@ -0,0 +1,65 @@ +#include "smart_string.hpp" + +#include "std/memory.hpp" + +#include + +SmartString::SmartString(char *c_string) : c_str(c_string) +{ +} + +SmartString::SmartString(unsigned int size) : c_str(malloc_s(size + 1)) +{ +} + +SmartString::SmartString(const SmartString &smart_str) + : c_str(malloc_s(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(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/std/smart_string.hpp b/src/std/smart_string.hpp new file mode 100644 index 0000000..6390465 --- /dev/null +++ b/src/std/smart_string.hpp @@ -0,0 +1,20 @@ +#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/std/time.cpp b/src/std/time.cpp new file mode 100644 index 0000000..cca8955 --- /dev/null +++ b/src/std/time.cpp @@ -0,0 +1,37 @@ +#include "time.hpp" + +#include + +Time::Time(uint64_t time_micros) : _time_micros(time_micros) +{ +} + +void Time::update() +{ + _time_micros = micros(); +} + +Time Time::diff(Time prev_time) const +{ + return Time(_time_micros - prev_time.microsecs()); +} + +double Time::secs() const +{ + return static_cast(_time_micros) * MICROS_TO_SECS; +} + +double Time::millisecs() const +{ + return static_cast(_time_micros) * MICROS_TO_MILLIS; +} + +uint64_t Time::microsecs() const +{ + return _time_micros; +} + +Time time_now() +{ + return Time(micros()); +} diff --git a/src/std/time.hpp b/src/std/time.hpp new file mode 100644 index 0000000..a544a5a --- /dev/null +++ b/src/std/time.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include + +constexpr double MICROS_TO_SECS = 0.000001; +constexpr double MICROS_TO_MILLIS = 0.001; + +/** + * 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) const; + + /** + * Returns the time in seconds. + */ + double secs() const; + + /** + * Returns the time in milliseconds. + */ + double millisecs() const; + + /** + * Returns the time in microseconds. + */ + uint64_t microsecs() const; + +private: + uint64_t _time_micros; +}; + +/** + * Returns a time object for the time since the program started. + */ +Time time_now(); diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..cb25471 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,6 @@ +#include "utils.hpp" + +void stop() +{ + while (true) {} +} diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..33f3379 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,6 @@ +#pragma once + +/** + * Stops code execution. + */ +void stop(); 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 doubleToStr(double num, unsigned int width, unsigned int precision) -{ - auto str = make_unique(width + precision); - - dtostrf(num, static_cast(width), precision, str->c_str); - - return str; -} - -UniquePtr intToStr(int num) -{ - auto width = static_cast(log10(num)); - - auto str = make_unique(width + 1U); - - dtostrf(num, static_cast(width + 1U), 0, str->c_str); - - return str; -} - -UniquePtr uintToStr(unsigned int num) -{ - auto width = static_cast(log10(num)); - - auto str = make_unique(width + 1U); - - dtostrf(num, static_cast(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 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 intToStr(int num); - -/** - * Converts a unsigned integer to a string. - * - * @param num A number - * @returns The number as a string. - */ -UniquePtr 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 - -template -memType *malloc_s(unsigned int size); - -template -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 -UniquePtr 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 - -template -memType *malloc_s(unsigned int size) -{ - auto *mem = malloc(size); - - if (mem == nullptr) - { - Serial.println("Error: Memory allocation failed"); - while (true) {} - } - - return static_cast(mem); -} - -template -UniquePtr::UniquePtr() = default; - -template -UniquePtr::UniquePtr(Target *target) : _target(target) -{ -} - -template -UniquePtr::UniquePtr(const UniquePtr &unique_ptr) - : _target(new Target(*(unique_ptr._target))) -{ -} - -template -UniquePtr::UniquePtr(UniquePtr &&unique_ptr) noexcept - : _target(unique_ptr._target) -{ - unique_ptr._target = nullptr; -} - -template -UniquePtr &UniquePtr::operator=(const UniquePtr &unique_ptr) -{ - if (&unique_ptr != this) - { - delete _target; - _target = nullptr; - _target = new Target(*(unique_ptr._target)); - } - - return *this; -} - -template -UniquePtr &UniquePtr::operator=(UniquePtr &&unique_ptr) noexcept -{ - if (&unique_ptr != this) - { - delete _target; - _target = unique_ptr._target; - unique_ptr._target = nullptr; - } - - return *this; -} - -template -UniquePtr::~UniquePtr() -{ - if (this->_target != nullptr) - { - delete this->_target; - } -} - -template -Target UniquePtr::operator*() const -{ - return *(this->_target); -} - -template -Target *UniquePtr::operator->() const -{ - return this->_target; -} - -template -UniquePtr make_unique(Args... args) -{ - return UniquePtr(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 - -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 - -SmartString::SmartString(char *c_string) : c_str(c_string) -{ -} - -SmartString::SmartString(unsigned int size) : c_str(malloc_s(size + 1)) -{ -} - -SmartString::SmartString(const SmartString &smart_str) - : c_str(malloc_s(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(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 - -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(_time_micros) * micros_to_secs; -} - -double Time::millisecs() -{ - const double micros_to_millis = 0.001; - - return static_cast(_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 - -/** - * 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(); -- cgit v1.2.3-18-g5258