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/as5600/tests/test_as5600.cpp b/Src/drivers/as5600/tests/test_as5600.cpp index 9f3b051..e5baa28 100644 --- a/Src/drivers/as5600/tests/test_as5600.cpp +++ b/Src/drivers/as5600/tests/test_as5600.cpp @@ -13,30 +13,27 @@ TEST(AS5600Test, HelloWorld) { TEST(AS5600Test, init) { Driver::AS5600 as5600; - as5600.init(); + (void)as5600; + Driver::AS5600::init(); } TEST(AS5600Test, is_ready) { - Driver::AS5600 as5600; - as5600.is_ready(); + Driver::AS5600::is_ready(); } TEST(AS5600Test, get_angle) { - Driver::AS5600 as5600; - as5600.get_angle(); + Driver::AS5600::get_angle(); } TEST(AS5600Test, get_magnitude) { - Driver::AS5600 as5600; - as5600.get_magnitude(); + Driver::AS5600::get_magnitude(); } TEST(AS5600Test, get_status) { - Driver::AS5600 as5600; - as5600.get_status(); + Driver::AS5600::get_status(); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +} diff --git a/Src/drivers/sht3x/sht3x.cpp b/Src/drivers/sht3x/sht3x.cpp index 3d0f371..79eb2ab 100644 --- a/Src/drivers/sht3x/sht3x.cpp +++ b/Src/drivers/sht3x/sht3x.cpp @@ -9,17 +9,15 @@ namespace Driver { bool SHT3X::read(float *temperature, float *humidity) const { - sendCommand(device_address, SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH); + sendCommand(SHT3XCommand::SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH); uint8_t buffer[6]; - if (!HAL::I2C::receive(device_address << 1u, buffer, sizeof(buffer))) { + if (!HAL::I2C::receive(i2c_address, buffer, sizeof(buffer))) { 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; } @@ -32,12 +30,13 @@ bool SHT3X::read(float *temperature, float *humidity) const { return true; } -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)}; +bool SHT3X::sendCommand(SHT3XCommand command) const { + uint8_t command_buffer[2] = { + (uint8_t)((uint16_t)command >> 8u), + (uint8_t)((uint16_t)command & 0xffu) + }; - return HAL::I2C::transmit(device_address << 1u, command_buffer, - sizeof(command_buffer)); + return HAL::I2C::transmit(i2c_address, 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..d3a07cf 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), i2c_address(dev_addr << 1) {} /** * @brief Takes a single temperature and humidity measurement. @@ -42,16 +42,16 @@ class SHT3X { private: /** * @brief Execute a command defined in SHT3XCommand - * @param handle Handle to the SHT3x device. * @param command SHT3XCommand * @return True on success, false otherwise. */ - static bool sendCommand(uint8_t device_address, SHT3XCommand command); + bool sendCommand(SHT3XCommand command) const; static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb); static uint8_t calculate_crc(const uint8_t* data, size_t length); uint8_t device_address; + uint8_t i2c_address; }; } // namespace Driver diff --git a/Src/drivers/sht3x/tests/test_sht3x.cpp b/Src/drivers/sht3x/tests/test_sht3x.cpp index 7251c5a..2bf515a 100644 --- a/Src/drivers/sht3x/tests/test_sht3x.cpp +++ b/Src/drivers/sht3x/tests/test_sht3x.cpp @@ -26,4 +26,4 @@ TEST(Shx3xTest, read) { int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +} diff --git a/Src/peripheral/i2c/i2c.hpp b/Src/peripheral/i2c/i2c.hpp index 8453b97..7033bc0 100644 --- a/Src/peripheral/i2c/i2c.hpp +++ b/Src/peripheral/i2c/i2c.hpp @@ -21,26 +21,36 @@ class I2C { /** * @return 0 on success, otherwise negative error code + * @param address Target device address: The device 7 bits address value + * in datasheet must be shifted to the left before calling the interface */ static int8_t is_device_ready(uint16_t address, uint8_t trials = 100); /** * @return Number of transmited bytes on success, otherwise negative error code + * @param id Target device address: The device 7 bits address value + * in datasheet must be shifted to the left before calling the interface */ static int8_t transmit(uint16_t id, uint8_t tx[], uint8_t len); /** * @return Number of received bytes on success, otherwise negative error code + * @param id Target device address: The device 7 bits address value + * in datasheet must be shifted to the left before calling the interface */ static int8_t receive(uint16_t id, uint8_t *rx, uint8_t len); /** * @return Register value [0, 255] on success, otherwise negative error code + * @param device_id Target device address: The device 7 bits address value + * in datasheet must be shifted to the left before calling the interface */ static int32_t read_register_1_byte(uint16_t device_id, uint8_t reg_address); /** * @return Register value [0, 65335] on success, otherwise negative error code + * @param device_id Target device address: The device 7 bits address value + * in datasheet must be shifted to the left before calling the interface */ static int32_t read_register_2_bytes(uint16_t device_id, uint8_t reg_address); };