summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-02-15 12:33:52 +0100
committerHampusM <hampus@hampusmat.com>2022-02-15 12:33:52 +0100
commitbcdce9633dc351d3bc7f347a165348b8fab87cd9 (patch)
tree88c2f5d8f0c5fac2bca28e4b543e5209f3bc98fb
parent917adc6a2b6b166e37fc3d4f94b41488f0c245a5 (diff)
refactor: reorganize files & improve classes
-rw-r--r--src/gyronardo.cpp6
-rw-r--r--src/sensor/calibration.cpp (renamed from src/calibration.cpp)6
-rw-r--r--src/sensor/calibration.hpp (renamed from src/calibration.hpp)6
-rw-r--r--src/sensor/sensor.cpp28
-rw-r--r--src/sensor/sensor.hpp27
-rw-r--r--src/serial.cpp (renamed from src/utils/serial.cpp)4
-rw-r--r--src/serial.hpp (renamed from src/utils/serial.hpp)4
-rw-r--r--src/std/conversion.cpp (renamed from src/utils/general.cpp)7
-rw-r--r--src/std/conversion.hpp (renamed from src/utils/general.hpp)9
-rw-r--r--src/std/memory.hpp (renamed from src/utils/memory.hpp)0
-rw-r--r--src/std/memory.tpp (renamed from src/utils/memory.tpp)3
-rw-r--r--src/std/smart_string.cpp (renamed from src/utils/smart_string.cpp)2
-rw-r--r--src/std/smart_string.hpp (renamed from src/utils/smart_string.hpp)0
-rw-r--r--src/std/time.cpp37
-rw-r--r--src/std/time.hpp (renamed from src/utils/time.hpp)11
-rw-r--r--src/utils.cpp6
-rw-r--r--src/utils.hpp6
-rw-r--r--src/utils/time.cpp41
18 files changed, 101 insertions, 102 deletions
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 <Arduino.h>
#include <Wire.h>
diff --git a/src/calibration.cpp b/src/sensor/calibration.cpp
index 66a52bf..40bb52b 100644
--- a/src/calibration.cpp
+++ b/src/sensor/calibration.cpp
@@ -1,7 +1,7 @@
#include "calibration.hpp"
-#include "utils/general.hpp"
-#include "utils/time.hpp"
+#include "std/time.hpp"
+#include "utils.hpp"
SensorCalibrator::SensorCalibrator(UniquePtr<Sensor> &sensor, SerialStream sout)
: _sensor(sensor), _sout(sout)
@@ -85,7 +85,7 @@ void SensorCalibrator::_adjustValues()
_gyro_z *= SENSOR_VAL_ADJUST;
}
-bool SensorCalibrator::_isValuesInRange()
+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 &&
diff --git a/src/calibration.hpp b/src/sensor/calibration.hpp
index e29602a..03e2e9c 100644
--- a/src/calibration.hpp
+++ b/src/sensor/calibration.hpp
@@ -1,8 +1,8 @@
#pragma once
#include "sensor/sensor.hpp"
-#include "utils/memory.hpp"
-#include "utils/serial.hpp"
+#include "serial.hpp"
+#include "std/memory.hpp"
// Calibration precision
constexpr float ACCEL_CAL_X_MAX = 0.006;
@@ -55,7 +55,7 @@ private:
void _resetValues();
void _updateValues();
void _adjustValues();
- bool _isValuesInRange();
+ bool _isValuesInRange() const;
void _adjustCalibration();
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 <Arduino.h>
#include <Wire.h>
@@ -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/utils/serial.cpp b/src/serial.cpp
index 8e4194f..d03f8e8 100644
--- a/src/utils/serial.cpp
+++ b/src/serial.cpp
@@ -1,6 +1,6 @@
#include "serial.hpp"
-#include "general.hpp"
+#include "std/conversion.hpp"
SerialStream::SerialStream(Serial_ serial, uint64_t baud_rate) : _serial(serial)
{
@@ -39,7 +39,7 @@ SerialStream &SerialStream::operator<<(const unsigned int &num)
return *this;
}
-SerialStream &SerialStream::operator<<(const uint32_t &num)
+SerialStream &SerialStream::operator<<(const unsigned long &num)
{
this->write(uintToStr(num)->c_str);
return *this;
diff --git a/src/utils/serial.hpp b/src/serial.hpp
index 0d4f929..9a9488d 100644
--- a/src/utils/serial.hpp
+++ b/src/serial.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "smart_string.hpp"
+#include "std/smart_string.hpp"
#include <USBAPI.h>
@@ -14,7 +14,7 @@ public:
SerialStream &operator<<(const double &num);
SerialStream &operator<<(const int &num);
SerialStream &operator<<(const unsigned int &num);
- SerialStream &operator<<(const uint32_t &num);
+ SerialStream &operator<<(const unsigned long &num);
SerialStream &operator<<(void (*manipulator)(SerialStream *));
diff --git a/src/utils/general.cpp b/src/std/conversion.cpp
index 3464c30..a2a624d 100644
--- a/src/utils/general.cpp
+++ b/src/std/conversion.cpp
@@ -1,9 +1,4 @@
-#include "general.hpp"
-
-void stop()
-{
- while (true) {}
-}
+#include "conversion.hpp"
UniquePtr<SmartString> doubleToStr(double num, unsigned int width, unsigned int precision)
{
diff --git a/src/utils/general.hpp b/src/std/conversion.hpp
index dbdbdfc..726ce88 100644
--- a/src/utils/general.hpp
+++ b/src/std/conversion.hpp
@@ -1,12 +1,7 @@
#pragma once
-#include "memory.hpp"
-#include "smart_string.hpp"
-
-/**
- * Stops code execution.
- */
-void stop();
+#include "std/memory.hpp"
+#include "std/smart_string.hpp"
/**
* Converts a double number to a string.
diff --git a/src/utils/memory.hpp b/src/std/memory.hpp
index e870b0b..e870b0b 100644
--- a/src/utils/memory.hpp
+++ b/src/std/memory.hpp
diff --git a/src/utils/memory.tpp b/src/std/memory.tpp
index 6261c81..fbfcd32 100644
--- a/src/utils/memory.tpp
+++ b/src/std/memory.tpp
@@ -1,8 +1,9 @@
#pragma once
-#include "general.hpp"
+#include "utils.hpp"
#include <Arduino.h>
+#include <stdlib.h>
template <typename memType>
memType *malloc_s(unsigned int size)
diff --git a/src/utils/smart_string.cpp b/src/std/smart_string.cpp
index ac9a862..b24a1a5 100644
--- a/src/utils/smart_string.cpp
+++ b/src/std/smart_string.cpp
@@ -1,6 +1,6 @@
#include "smart_string.hpp"
-#include "memory.hpp"
+#include "std/memory.hpp"
#include <stdlib.h>
diff --git a/src/utils/smart_string.hpp b/src/std/smart_string.hpp
index 6390465..6390465 100644
--- a/src/utils/smart_string.hpp
+++ b/src/std/smart_string.hpp
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 <Arduino.h>
+
+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<double>(_time_micros) * MICROS_TO_SECS;
+}
+
+double Time::millisecs() const
+{
+ return static_cast<double>(_time_micros) * MICROS_TO_MILLIS;
+}
+
+uint64_t Time::microsecs() const
+{
+ return _time_micros;
+}
+
+Time time_now()
+{
+ return Time(micros());
+}
diff --git a/src/utils/time.hpp b/src/std/time.hpp
index 1577bb7..a544a5a 100644
--- a/src/utils/time.hpp
+++ b/src/std/time.hpp
@@ -2,6 +2,9 @@
#include <stdint.h>
+constexpr double MICROS_TO_SECS = 0.000001;
+constexpr double MICROS_TO_MILLIS = 0.001;
+
/**
* A representation of time.
*/
@@ -25,22 +28,22 @@ public:
*
* @param prev_time A previous point in time
*/
- Time diff(Time prev_time);
+ Time diff(Time prev_time) const;
/**
* Returns the time in seconds.
*/
- double secs();
+ double secs() const;
/**
* Returns the time in milliseconds.
*/
- double millisecs();
+ double millisecs() const;
/**
* Returns the time in microseconds.
*/
- uint64_t microsecs();
+ uint64_t microsecs() const;
private:
uint64_t _time_micros;
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/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());
-}