summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-07 10:07:36 +0100
committerHampusM <hampus@hampusmat.com>2022-03-07 10:07:36 +0100
commitda2ec747227ac1716326bd076219c7f7a2bb3887 (patch)
tree090f3ccc40dff6e86fb04409904f08d1de7a8d56
parent4b4605df15ce7061a08d7911069927bf49778d05 (diff)
refactor: improve sensor methods
-rw-r--r--src/sensor/sensor.cpp53
-rw-r--r--src/sensor/sensor.hpp38
2 files changed, 46 insertions, 45 deletions
diff --git a/src/sensor/sensor.cpp b/src/sensor/sensor.cpp
index 5ca5dc3..9f48e7f 100644
--- a/src/sensor/sensor.cpp
+++ b/src/sensor/sensor.cpp
@@ -16,7 +16,7 @@ Sensor::Sensor(uint8_t address, TwoWire wire, SerialStream sout,
{
}
-bool Sensor::begin()
+bool Sensor::begin() noexcept
{
_wire.begin();
@@ -32,13 +32,13 @@ bool Sensor::begin()
return false;
}
-bool Sensor::isConnected()
+bool Sensor::isConnected() noexcept
{
_wire.beginTransmission(_address);
return (_wire.endTransmission() == 0);
}
-bool Sensor::read()
+bool Sensor::read() noexcept
{
auto now = time_now();
@@ -168,7 +168,7 @@ bool Sensor::read()
return true;
}
-bool Sensor::setAccelSensitivity(uint8_t sensitivity)
+bool Sensor::setAccelSensitivity(uint8_t sensitivity) noexcept
{
if (sensitivity > 3)
{
@@ -177,7 +177,7 @@ bool Sensor::setAccelSensitivity(uint8_t sensitivity)
_accel_sensitivity = sensitivity;
- uint8_t accel_config = getRegister(SensorRegisters::ACCEL_CONFIG);
+ auto accel_config = static_cast<int>(getRegister(SensorRegisters::ACCEL_CONFIG));
if (_status != SensorStatus::OK)
{
@@ -190,9 +190,10 @@ bool Sensor::setAccelSensitivity(uint8_t sensitivity)
const uint8_t magic = 0xE7U;
accel_config &= magic;
- accel_config |= (_accel_sensitivity << 3);
+ accel_config |= (_accel_sensitivity << 3U);
- if (!setRegister(SensorRegisters::ACCEL_CONFIG, accel_config))
+ if (!setRegister(SensorRegisters::ACCEL_CONFIG,
+ static_cast<uint8_t>(accel_config)))
{
return false;
}
@@ -204,7 +205,7 @@ bool Sensor::setAccelSensitivity(uint8_t sensitivity)
return true;
}
-bool Sensor::setGyroSensitivity(uint8_t sensitivity)
+bool Sensor::setGyroSensitivity(uint8_t sensitivity) noexcept
{
if (sensitivity > 3)
{
@@ -213,7 +214,7 @@ bool Sensor::setGyroSensitivity(uint8_t sensitivity)
_gyro_sensitivity = sensitivity;
- uint8_t gyro_config = getRegister(SensorRegisters::GYRO_CONFIG);
+ auto gyro_config = static_cast<int>(getRegister(SensorRegisters::GYRO_CONFIG));
if (_status != SensorStatus::OK)
{
@@ -226,9 +227,9 @@ bool Sensor::setGyroSensitivity(uint8_t sensitivity)
const uint8_t magic = 0xE7U;
gyro_config &= magic;
- gyro_config |= (_gyro_sensitivity << 3);
+ gyro_config |= (_gyro_sensitivity << 3U);
- if (!setRegister(SensorRegisters::GYRO_CONFIG, gyro_config))
+ if (!setRegister(SensorRegisters::GYRO_CONFIG, static_cast<uint8_t>(gyro_config)))
{
return false;
}
@@ -239,57 +240,57 @@ bool Sensor::setGyroSensitivity(uint8_t sensitivity)
return true;
}
-double Sensor::getAccelX() const
+double Sensor::getAccelX() const noexcept
{
return _accel_raw_x;
}
-double Sensor::getAccelY() const
+double Sensor::getAccelY() const noexcept
{
return _accel_raw_y;
}
-double Sensor::getAccelZ() const
+double Sensor::getAccelZ() const noexcept
{
return _accel_raw_z;
}
-double Sensor::getAngleX() const
+double Sensor::getAngleX() const noexcept
{
return _accel_x;
}
-double Sensor::getAngleY() const
+double Sensor::getAngleY() const noexcept
{
return _accel_y;
}
-double Sensor::getGyroX() const
+double Sensor::getGyroX() const noexcept
{
return _gyro_raw_x;
}
-double Sensor::getGyroY() const
+double Sensor::getGyroY() const noexcept
{
return _gyro_raw_y;
}
-double Sensor::getGyroZ() const
+double Sensor::getGyroZ() const noexcept
{
return _gyro_raw_z;
}
-double Sensor::getPitch() const
+double Sensor::getPitch() const noexcept
{
return _pitch;
}
-double Sensor::getRoll() const
+double Sensor::getRoll() const noexcept
{
return _roll;
}
-bool Sensor::setRegister(uint8_t reg, uint8_t value)
+bool Sensor::setRegister(uint8_t reg, uint8_t value) noexcept
{
_wire.beginTransmission(_address);
_wire.write(reg);
@@ -304,7 +305,7 @@ bool Sensor::setRegister(uint8_t reg, uint8_t value)
return true;
}
-uint8_t Sensor::getRegister(uint8_t reg)
+uint8_t Sensor::getRegister(uint8_t reg) noexcept
{
_wire.beginTransmission(_address);
_wire.write(reg);
@@ -322,10 +323,10 @@ uint8_t Sensor::getRegister(uint8_t reg)
return 0U;
}
- return _wire.read();
+ return static_cast<uint8_t>(_wire.read());
}
-SensorStatus Sensor::getStatus()
+SensorStatus Sensor::getStatus() noexcept
{
SensorStatus status = _status;
_status = SensorStatus::OK;
@@ -333,7 +334,7 @@ SensorStatus Sensor::getStatus()
return status;
}
-int16_t Sensor::_readHighLow()
+int16_t Sensor::_readHighLow() noexcept
{
const int16_t high = _wire.read();
const int16_t low = _wire.read();
diff --git a/src/sensor/sensor.hpp b/src/sensor/sensor.hpp
index 7b76ae2..5bdf6ca 100644
--- a/src/sensor/sensor.hpp
+++ b/src/sensor/sensor.hpp
@@ -43,19 +43,19 @@ public:
*
* @returns Whether or not the sensor can be communicated with.
*/
- bool begin();
+ bool begin() noexcept;
/**
* Returns whether or not the sensor is connected.
*/
- bool isConnected();
+ bool isConnected() noexcept;
/**
* Reads from the sensor.
*
* @returns Whether or not it succeeded.
*/
- bool read();
+ bool read() noexcept;
/**
* Sets the accelerometer sensitivity.
@@ -63,7 +63,7 @@ public:
* @param sensitivity Accelerometer sensitivity. 0, 1, 2, 3 => 2g 4g 8g 16g
* @returns Whether or not it succeeded.
*/
- bool setAccelSensitivity(uint8_t sensitivity);
+ bool setAccelSensitivity(uint8_t sensitivity) noexcept;
/**
* Sets the gyro sensitivity.
@@ -72,57 +72,57 @@ public:
* 0, 1, 2, 3 => 250, 500, 1000, 2000 degrees/s
* @returns Whether or not it succeeded.
*/
- bool setGyroSensitivity(uint8_t sensitivity);
+ bool setGyroSensitivity(uint8_t sensitivity) noexcept;
/**
* Returns the current X axis acceleration in g:s (g-force).
*/
- double getAccelX() const;
+ double getAccelX() const noexcept;
/**
* Returns the current Y axis acceleration in g:s (g-force).
*/
- double getAccelY() const;
+ double getAccelY() const noexcept;
/**
* Returns the current Z axis acceleration in g:s (g-force).
*/
- double getAccelZ() const;
+ double getAccelZ() const noexcept;
/**
* Returns the current X angle.
*/
- double getAngleX() const;
+ double getAngleX() const noexcept;
/**
* Returns the current Y angle.
*/
- double getAngleY() const;
+ double getAngleY() const noexcept;
/**
* Returns the current X axis in degrees/s.
*/
- double getGyroX() const;
+ double getGyroX() const noexcept;
/**
* Returns the current Y axis in degrees/s.
*/
- double getGyroY() const;
+ double getGyroY() const noexcept;
/**
* Returns the current Z axis in degrees/s.
*/
- double getGyroZ() const;
+ double getGyroZ() const noexcept;
/**
* Returns the current pitch angle.
*/
- double getPitch() const;
+ double getPitch() const noexcept;
/**
* Returns the current roll angle.
*/
- double getRoll() const;
+ double getRoll() const noexcept;
/**
* Sets the value of a key in the sensor's register.
@@ -131,19 +131,19 @@ public:
* @param value A new value
* @returns Whether or not it succeeded.
*/
- bool setRegister(uint8_t reg, uint8_t value);
+ bool setRegister(uint8_t reg, uint8_t value) noexcept;
/**
* Returns the value of a key in the sensor's registry.
*
* @param reg The address of a registry key
*/
- uint8_t getRegister(uint8_t reg);
+ uint8_t getRegister(uint8_t reg) noexcept;
/**
* Returns the last sensor status.
*/
- SensorStatus getStatus();
+ SensorStatus getStatus() noexcept;
// Accelerometer calibration values
double accel_cal_x = 0.0F;
@@ -193,5 +193,5 @@ private:
double _roll = 0;
double _yaw = 0;
- int16_t _readHighLow();
+ int16_t _readHighLow() noexcept;
};