diff --git a/Src/drivers/as5600/as5600.cpp b/Src/drivers/as5600/as5600.cpp index e731f14..db11dd0 100644 --- a/Src/drivers/as5600/as5600.cpp +++ b/Src/drivers/as5600/as5600.cpp @@ -12,18 +12,6 @@ namespace Driver { -/** - * @note AS5600 registers - */ -static constexpr uint8_t REG_RAW_ANGLE = 0x0C; // Unscaled and unmodified angle -static constexpr uint8_t REG_ANGLE = 0x0E; // Scaled output value -static constexpr uint8_t REG_STATUS = 0x0B; // Bits that indicate the current state -static constexpr uint8_t REG_MAGNITUDE = 0x1B; // The magnitude value of the internal CORDIC - -static constexpr uint8_t I2C_ADDRESS_AS5600 = 0x36; -static constexpr uint8_t I2C_AS5600 = (I2C_ADDRESS_AS5600 << 1) + 1; - - int8_t AS5600::init() { if (auto i2c_init_status = HAL::I2C::init(); i2c_init_status < 0) { return i2c_init_status; @@ -57,12 +45,12 @@ int16_t AS5600::get_angle() { int16_t AS5600::get_status() { auto reg_value = HAL::I2C::read_register_1_byte(I2C_AS5600, REG_STATUS); - return (reg_value >= 0) ? ((uint8_t)reg_value & 0b111000) : reg_value; + return reg_value >= 0 ? reg_value & 0b111000 : reg_value; } int16_t AS5600::get_magnitude() { auto reg_value = HAL::I2C::read_register_2_bytes(I2C_AS5600, REG_MAGNITUDE); - return (reg_value >= 0) ? std::clamp(reg_value, (int32_t)0, (int32_t)4095) : reg_value; + return reg_value >= 0 ? std::clamp(reg_value, 0, 4095) : reg_value; } } // namespace Driver diff --git a/Src/drivers/as5600/as5600.hpp b/Src/drivers/as5600/as5600.hpp index b0c7345..38a0ee8 100644 --- a/Src/drivers/as5600/as5600.hpp +++ b/Src/drivers/as5600/as5600.hpp @@ -17,24 +17,24 @@ class AS5600 { * @brief Verify that the device is avaliable via I2C and status is ok * @return 0 on success, negative error code otherwise */ - int8_t init(); + static int8_t init(); /** * @return True if the device is ready for measurement, False otherwise */ - bool is_ready(); + static bool is_ready(); /** * @brief The RAW ANGLE register contains the unscaled and unmodified angle. * @return Angle within [0, 360] on success, negative error code otherwise */ - int16_t get_angle(); + static int16_t get_angle(); /** * @brief The MAGNITUDE register indicates the magnitude value of the internal CORDIC. * @return Unitless magnitude within [0, 4095] on success, negative error code otherwise */ - int16_t get_magnitude(); + static int16_t get_magnitude(); /** * @brief Read the STATUS register @@ -44,11 +44,22 @@ class AS5600 { * MD (6) - Magnet was detected * @return Register register value on success, negative error code otherwise */ - int16_t get_status(); + static int16_t get_status(); - constexpr static const char* STATUS_MH = "AGC minimum gain overflow, magnet too strong"; - constexpr static const char* STATUS_ML = "AGC maximum gain overflow, magnet too weak"; - constexpr static const char* STATUS_MD = "Magnet was detected"; + static constexpr const char* STATUS_MH = "AGC minimum gain overflow, magnet too strong"; + static constexpr const char* STATUS_ML = "AGC maximum gain overflow, magnet too weak"; + static constexpr const char* STATUS_MD = "Magnet was detected"; + + /** + * @note AS5600 registers + */ + static constexpr uint8_t REG_RAW_ANGLE = 0x0C; // Unscaled and unmodified angle + static constexpr uint8_t REG_ANGLE = 0x0E; // Scaled output value + static constexpr uint8_t REG_STATUS = 0x0B; // Bits that indicate the current state + static constexpr uint8_t REG_MAGNITUDE = 0x1B; // The magnitude value of the internal CORDIC + + static constexpr uint8_t I2C_ADDRESS_AS5600 = 0x36; + static constexpr uint8_t I2C_AS5600 = (I2C_ADDRESS_AS5600 << 1) + 1; }; } // namespace Driver diff --git a/Src/drivers/sht3x/sht3x.cpp b/Src/drivers/sht3x/sht3x.cpp index 3d0f371..e7dd2cc 100644 --- a/Src/drivers/sht3x/sht3x.cpp +++ b/Src/drivers/sht3x/sht3x.cpp @@ -17,9 +17,7 @@ bool SHT3X::read(float *temperature, float *humidity) const { return false; } - uint8_t temperature_crc = calculate_crc(buffer, 2); - uint8_t humidity_crc = calculate_crc(buffer + 3, 2); - if (temperature_crc != buffer[2] || humidity_crc != buffer[5]) { + if (calculate_crc(buffer, 2) != buffer[2] || calculate_crc(buffer + 3, 2) != buffer[5]) { return false; } @@ -33,11 +31,12 @@ bool SHT3X::read(float *temperature, float *humidity) const { } bool SHT3X::sendCommand(uint8_t device_address, SHT3XCommand command) { - uint8_t command_buffer[2] = {(uint8_t)((uint16_t)command >> 8u), - (uint8_t)((uint16_t)command & 0xffu)}; + uint8_t command_buffer[2] = { + (uint8_t)(std::to_underlying(command) >> 8u), + (uint8_t)(std::to_underlying(command) & 0xffu) + }; - return HAL::I2C::transmit(device_address << 1u, command_buffer, - sizeof(command_buffer)); + return HAL::I2C::transmit(device_address << 1u, command_buffer, sizeof(command_buffer)); } uint16_t SHT3X::uint8_to_uint16(uint8_t msb, uint8_t lsb) { diff --git a/Src/drivers/sht3x/sht3x.hpp b/Src/drivers/sht3x/sht3x.hpp index c84b892..e1099b7 100644 --- a/Src/drivers/sht3x/sht3x.hpp +++ b/Src/drivers/sht3x/sht3x.hpp @@ -11,7 +11,7 @@ namespace Driver { -enum class SHT3XCommand { +enum class SHT3XCommand : uint16_t { SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06, SHT3X_COMMAND_CLEAR_STATUS = 0x3041, SHT3X_COMMAND_SOFT_RESET = 0x30A2, @@ -29,7 +29,7 @@ class SHT3X { static constexpr uint8_t DEV_ADDR_PIN_LOW = 0x44; static constexpr uint8_t DEV_ADDR_PIN_HIGH = 0x45; - SHT3X(uint8_t dev_addr): device_address(dev_addr) {} + explicit SHT3X(uint8_t dev_addr): device_address(dev_addr) {} /** * @brief Takes a single temperature and humidity measurement.