diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-07 10:07:36 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-03-07 10:07:36 +0100 |
commit | da2ec747227ac1716326bd076219c7f7a2bb3887 (patch) | |
tree | 090f3ccc40dff6e86fb04409904f08d1de7a8d56 /src/sensor/sensor.cpp | |
parent | 4b4605df15ce7061a08d7911069927bf49778d05 (diff) |
refactor: improve sensor methods
Diffstat (limited to 'src/sensor/sensor.cpp')
-rw-r--r-- | src/sensor/sensor.cpp | 53 |
1 files changed, 27 insertions, 26 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(); |