Skip to content

Commit

Permalink
fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Nov 11, 2024
1 parent f618da5 commit a15ebc8
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
16 changes: 2 additions & 14 deletions Src/drivers/as5600/as5600.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
27 changes: 19 additions & 8 deletions Src/drivers/as5600/as5600.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Src/drivers/as5600/tests/test_as5600.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ TEST(AS5600Test, get_status) {
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
}
13 changes: 6 additions & 7 deletions Src/drivers/sht3x/sht3x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)((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(device_address << 1u, command_buffer, sizeof(command_buffer));
}

uint16_t SHT3X::uint8_to_uint16(uint8_t msb, uint8_t lsb) {
Expand Down
4 changes: 2 additions & 2 deletions Src/drivers/sht3x/sht3x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Src/drivers/sht3x/tests/test_sht3x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ TEST(Shx3xTest, read) {
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
}

0 comments on commit a15ebc8

Please # to comment.