summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.clang-format2
-rw-r--r--src/common/conversion.cpp39
-rw-r--r--src/common/conversion.hpp (renamed from src/std/conversion.hpp)17
-rw-r--r--src/common/memory.hpp (renamed from src/std/memory.hpp)5
-rw-r--r--src/common/memory.tpp (renamed from src/std/memory.tpp)5
-rw-r--r--src/common/string.cpp (renamed from src/std/smart_string.cpp)25
-rw-r--r--src/common/string.hpp25
-rw-r--r--src/common/time.cpp42
-rw-r--r--src/common/time.hpp60
-rw-r--r--src/gyronardo.cpp2
-rw-r--r--src/sensor/calibration.cpp6
-rw-r--r--src/sensor/sensor.cpp2
-rw-r--r--src/sensor/sensor.hpp4
-rw-r--r--src/serial.cpp12
-rw-r--r--src/serial.hpp4
-rw-r--r--src/std/conversion.cpp33
-rw-r--r--src/std/smart_string.hpp20
-rw-r--r--src/std/time.cpp37
-rw-r--r--src/std/time.hpp55
19 files changed, 217 insertions, 178 deletions
diff --git a/.clang-format b/.clang-format
index 461886c..67d3e49 100644
--- a/.clang-format
+++ b/.clang-format
@@ -9,7 +9,5 @@ AllowShortFunctionsOnASingleLine: None
IndentCaseLabels: false
ColumnLimit: 90
AccessModifierOffset: -4
-NamespaceIndentation: All
-FixNamespaceComments: false
AlwaysBreakTemplateDeclarations: true
ConstructorInitializerAllOnOneLineOrOnePerLine: true
diff --git a/src/common/conversion.cpp b/src/common/conversion.cpp
new file mode 100644
index 0000000..0bdc81a
--- /dev/null
+++ b/src/common/conversion.cpp
@@ -0,0 +1,39 @@
+#include "conversion.hpp"
+
+namespace common
+{
+
+common::UniquePtr<common::String> doubleToStr(double num, unsigned int width,
+ unsigned int precision)
+{
+ auto str = common::make_unique<common::String>(width + precision);
+
+ dtostrf(num, static_cast<signed char>(width), static_cast<unsigned char>(precision),
+ str->c_str);
+
+ return str;
+}
+
+common::UniquePtr<common::String> intToStr(int num)
+{
+ auto width = static_cast<unsigned int>(log10(num));
+
+ auto str = common::make_unique<common::String>(width + 1U);
+
+ dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str);
+
+ return str;
+}
+
+common::UniquePtr<common::String> uintToStr(unsigned int num)
+{
+ auto width = static_cast<unsigned int>(log10(num));
+
+ auto str = common::make_unique<common::String>(width + 1U);
+
+ dtostrf(num, static_cast<signed char>(width + 1U), 0U, str->c_str);
+
+ return str;
+}
+
+} // namespace common
diff --git a/src/std/conversion.hpp b/src/common/conversion.hpp
index 726ce88..81a4a52 100644
--- a/src/std/conversion.hpp
+++ b/src/common/conversion.hpp
@@ -1,7 +1,10 @@
#pragma once
-#include "std/memory.hpp"
-#include "std/smart_string.hpp"
+#include "common/memory.hpp"
+#include "common/string.hpp"
+
+namespace common
+{
/**
* Converts a double number to a string.
@@ -11,8 +14,8 @@
* @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);
+common::UniquePtr<common::String> doubleToStr(double num, unsigned int width = 3,
+ unsigned int precision = 2);
/**
* Converts a integer to a string.
@@ -20,7 +23,7 @@ UniquePtr<SmartString> doubleToStr(double num, unsigned int width = 3,
* @param num A number
* @returns The number as a string.
*/
-UniquePtr<SmartString> intToStr(int num);
+common::UniquePtr<common::String> intToStr(int num);
/**
* Converts a unsigned integer to a string.
@@ -28,4 +31,6 @@ UniquePtr<SmartString> intToStr(int num);
* @param num A number
* @returns The number as a string.
*/
-UniquePtr<SmartString> uintToStr(unsigned int num);
+common::UniquePtr<common::String> uintToStr(unsigned int num);
+
+} // namespace common
diff --git a/src/std/memory.hpp b/src/common/memory.hpp
index d1ca762..de72d80 100644
--- a/src/std/memory.hpp
+++ b/src/common/memory.hpp
@@ -2,6 +2,9 @@
#include <stddef.h>
+namespace common
+{
+
template <typename memType>
memType *malloc_s(unsigned int size);
@@ -36,4 +39,6 @@ private:
template <typename Target, typename... Args>
UniquePtr<Target> make_unique(Args... args);
+} // namespace common
+
#include "memory.tpp"
diff --git a/src/std/memory.tpp b/src/common/memory.tpp
index b2f39bf..ace2432 100644
--- a/src/std/memory.tpp
+++ b/src/common/memory.tpp
@@ -7,6 +7,9 @@
#include <Arduino.h>
#include <stdlib.h>
+namespace common
+{
+
template <typename memType>
memType *malloc_s(unsigned int size)
{
@@ -69,3 +72,5 @@ UniquePtr<Target> make_unique(Args... args)
{
return UniquePtr<Target>(new Target(args...));
}
+
+} // namespace common
diff --git a/src/std/smart_string.cpp b/src/common/string.cpp
index b24a1a5..27b65d4 100644
--- a/src/std/smart_string.cpp
+++ b/src/common/string.cpp
@@ -1,29 +1,32 @@
-#include "smart_string.hpp"
+#include "string.hpp"
-#include "std/memory.hpp"
+#include "common/memory.hpp"
#include <stdlib.h>
-SmartString::SmartString(char *c_string) : c_str(c_string)
+namespace common
+{
+
+String::String(char *c_string) : c_str(c_string)
{
}
-SmartString::SmartString(unsigned int size) : c_str(malloc_s<char>(size + 1))
+String::String(unsigned int size) : c_str(malloc_s<char>(size + 1))
{
}
-SmartString::SmartString(const SmartString &smart_str)
+String::String(const String &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)
+String::String(String &&smart_str) noexcept : c_str(smart_str.c_str)
{
smart_str.c_str = nullptr;
}
-SmartString &SmartString::operator=(const SmartString &smart_str)
+String &String::operator=(const String &smart_str)
{
if (&smart_str != this)
{
@@ -39,7 +42,7 @@ SmartString &SmartString::operator=(const SmartString &smart_str)
return *this;
}
-SmartString &SmartString::operator=(SmartString &&smart_str) noexcept
+String &String::operator=(String &&smart_str) noexcept
{
if (&smart_str != this)
{
@@ -51,7 +54,7 @@ SmartString &SmartString::operator=(SmartString &&smart_str) noexcept
return *this;
}
-SmartString::~SmartString()
+String::~String()
{
if (c_str != nullptr)
{
@@ -59,7 +62,9 @@ SmartString::~SmartString()
}
}
-SmartString::operator char *() const
+String::operator char *() const
{
return c_str;
}
+
+} // namespace common
diff --git a/src/common/string.hpp b/src/common/string.hpp
new file mode 100644
index 0000000..2641457
--- /dev/null
+++ b/src/common/string.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+namespace common
+{
+
+class String
+{
+public:
+ explicit String(char *c_str);
+ explicit String(unsigned int size);
+ String(const String &smart_str);
+ String(String &&smart_str) noexcept;
+
+ String &operator=(const String &smart_str);
+
+ String &operator=(String &&smart_str) noexcept;
+
+ ~String();
+
+ explicit operator char *() const;
+
+ char *c_str = nullptr;
+};
+
+} // namespace common
diff --git a/src/common/time.cpp b/src/common/time.cpp
new file mode 100644
index 0000000..ee297d7
--- /dev/null
+++ b/src/common/time.cpp
@@ -0,0 +1,42 @@
+#include "time.hpp"
+
+#include <Arduino.h>
+
+namespace common
+{
+
+ 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());
+ }
+
+} // namespace common
diff --git a/src/common/time.hpp b/src/common/time.hpp
new file mode 100644
index 0000000..8efbd28
--- /dev/null
+++ b/src/common/time.hpp
@@ -0,0 +1,60 @@
+#pragma once
+
+#include <stdint.h>
+
+constexpr double MICROS_TO_SECS = 0.000001;
+constexpr double MICROS_TO_MILLIS = 0.001;
+
+namespace common
+{
+
+ /**
+ * 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();
+
+} // namespace common
diff --git a/src/gyronardo.cpp b/src/gyronardo.cpp
index 7c01985..4b6b5fd 100644
--- a/src/gyronardo.cpp
+++ b/src/gyronardo.cpp
@@ -1,7 +1,7 @@
+#include "common/memory.hpp"
#include "sensor/calibration.hpp"
#include "sensor/sensor.hpp"
#include "serial.hpp"
-#include "std/memory.hpp"
#include <Arduino.h>
#include <Wire.h>
diff --git a/src/sensor/calibration.cpp b/src/sensor/calibration.cpp
index e01bb34..1a2b91e 100644
--- a/src/sensor/calibration.cpp
+++ b/src/sensor/calibration.cpp
@@ -1,6 +1,6 @@
#include "calibration.hpp"
-#include "std/time.hpp"
+#include "common/time.hpp"
#include "utils.hpp"
SensorCalibrator::SensorCalibrator(Sensor &sensor, SerialStream sout)
@@ -11,11 +11,11 @@ SensorCalibrator::SensorCalibrator(Sensor &sensor, SerialStream sout)
bool SensorCalibrator::calibrate(unsigned int throttle_time)
{
bool done = false;
- auto start_time = time_now();
+ auto start_time = common::time_now();
while (!done)
{
- if (time_now().diff(start_time).millisecs() >= CALIBRATION_TIMEOUT)
+ if (common::time_now().diff(start_time).millisecs() >= CALIBRATION_TIMEOUT)
{
return false;
}
diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp
index 4d7f256..af25a96 100644
--- a/src/sensor/sensor.cpp
+++ b/src/sensor/sensor.cpp
@@ -40,7 +40,7 @@ bool Sensor::isConnected() noexcept
bool Sensor::read() noexcept
{
- auto now = time_now();
+ auto now = common::time_now();
if (_throttle_enabled && now.diff(_last_time).millisecs() < _throttle_time)
{
diff --git a/src/sensor/sensor.hpp b/src/sensor/sensor.hpp
index b79b918..1e7e488 100644
--- a/src/sensor/sensor.hpp
+++ b/src/sensor/sensor.hpp
@@ -1,7 +1,7 @@
#pragma once
+#include "common/time.hpp"
#include "serial.hpp"
-#include "std/time.hpp"
#include <Arduino.h>
#include <Wire.h>
@@ -164,7 +164,7 @@ private:
bool _throttle_enabled;
unsigned int _throttle_time;
- Time _last_time;
+ common::Time _last_time;
SensorStatus _status;
diff --git a/src/serial.cpp b/src/serial.cpp
index d3b3989..e60de6c 100644
--- a/src/serial.cpp
+++ b/src/serial.cpp
@@ -1,6 +1,6 @@
#include "serial.hpp"
-#include "std/conversion.hpp"
+#include "common/conversion.hpp"
SerialStream::SerialStream(Serial_ serial, const unsigned long &baud_rate)
: _serial(serial)
@@ -14,7 +14,7 @@ SerialStream &SerialStream::operator<<(const char *str)
return *this;
}
-SerialStream &SerialStream::operator<<(const SmartString &str)
+SerialStream &SerialStream::operator<<(const common::String &str)
{
write(str.c_str);
return *this;
@@ -22,25 +22,25 @@ SerialStream &SerialStream::operator<<(const SmartString &str)
SerialStream &SerialStream::operator<<(const double &num)
{
- write(doubleToStr(num, 3U, 4U)->c_str);
+ write(common::doubleToStr(num, 3U, 4U)->c_str);
return *this;
}
SerialStream &SerialStream::operator<<(const int &num)
{
- write(intToStr(num)->c_str);
+ write(common::intToStr(num)->c_str);
return *this;
}
SerialStream &SerialStream::operator<<(const unsigned int &num)
{
- write(uintToStr(num)->c_str);
+ write(common::uintToStr(num)->c_str);
return *this;
}
SerialStream &SerialStream::operator<<(const unsigned long &num)
{
- write(uintToStr(num)->c_str);
+ write(common::uintToStr(num)->c_str);
return *this;
}
diff --git a/src/serial.hpp b/src/serial.hpp
index f32ca63..36aa6ed 100644
--- a/src/serial.hpp
+++ b/src/serial.hpp
@@ -1,6 +1,6 @@
#pragma once
-#include "std/smart_string.hpp"
+#include "common/string.hpp"
#include <USBAPI.h>
@@ -10,7 +10,7 @@ public:
SerialStream(Serial_ serial, const unsigned long &baud_rate);
SerialStream &operator<<(const char *str);
- SerialStream &operator<<(const SmartString &str);
+ SerialStream &operator<<(const common::String &str);
SerialStream &operator<<(const double &num);
SerialStream &operator<<(const int &num);
SerialStream &operator<<(const unsigned int &num);
diff --git a/src/std/conversion.cpp b/src/std/conversion.cpp
deleted file mode 100644
index 3d75e8a..0000000
--- a/src/std/conversion.cpp
+++ /dev/null
@@ -1,33 +0,0 @@
-#include "conversion.hpp"
-
-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), static_cast<unsigned char>(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), 0U, 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), 0U, str->c_str);
-
- return str;
-}
diff --git a/src/std/smart_string.hpp b/src/std/smart_string.hpp
deleted file mode 100644
index 6390465..0000000
--- a/src/std/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/std/time.cpp b/src/std/time.cpp
deleted file mode 100644
index cca8955..0000000
--- a/src/std/time.cpp
+++ /dev/null
@@ -1,37 +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) 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/std/time.hpp b/src/std/time.hpp
deleted file mode 100644
index a544a5a..0000000
--- a/src/std/time.hpp
+++ /dev/null
@@ -1,55 +0,0 @@
-#pragma once
-
-#include <stdint.h>
-
-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();