|
| 1 | +#include "ina219_driver.hh" |
| 2 | + |
| 3 | +#include <string.h> // memset |
| 4 | + |
| 5 | +namespace ina219 { |
| 6 | + |
| 7 | +[[nodiscard]] bool INA219Driver::write_register(Register reg, uint16_t value) const { |
| 8 | + uint16_t write_buffer = ((value & 0xFF) << 8) + (value >> 8); |
| 9 | + if (i2c_write(&i2c_device_, static_cast<uint8_t>(reg), &write_buffer, 2) == -1) { |
| 10 | + return false; |
| 11 | + } |
| 12 | + return true; |
| 13 | +} |
| 14 | + |
| 15 | + [[nodiscard]] bool INA219Driver::read_register(Register reg, uint16_t& value) const { |
| 16 | + uint16_t read_buffer; |
| 17 | + if (i2c_read(&i2c_device_, static_cast<uint8_t>(reg), &read_buffer, 2) == -1) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + value = ((read_buffer & 0xFF) << 8) + (read_buffer >> 8); |
| 21 | + return true; |
| 22 | +} |
| 23 | + |
| 24 | +INA219Driver::INA219Driver(int i2c_handle, int i2c_address, DriverConfiguration config) : config_(config) { |
| 25 | + memset(&i2c_device_, 0, sizeof(i2c_device_)); |
| 26 | + i2c_device_.bus = i2c_handle; |
| 27 | + i2c_device_.addr = i2c_address; |
| 28 | + i2c_device_.iaddr_bytes = I2C_ADDR_BYTES; |
| 29 | + i2c_device_.page_bytes = I2C_PAGE_BYTES; |
| 30 | +} |
| 31 | + |
| 32 | +std::optional<int16_t> INA219Driver::get_raw_bus_voltage() const { |
| 33 | + uint16_t raw_bus_voltage_buffer; |
| 34 | + if (!read_register(Register::BUS_VOLTAGE, raw_bus_voltage_buffer)) { |
| 35 | + return {}; |
| 36 | + } |
| 37 | + |
| 38 | + // Shift to the right 3 to drop CNVR and OVF and multiply by LSB |
| 39 | + return (int16_t)((raw_bus_voltage_buffer >> 3) * 4); |
| 40 | +} |
| 41 | + |
| 42 | +std::optional<int16_t> INA219Driver::get_raw_shunt_voltage() const { |
| 43 | + uint16_t raw_shunt_voltage_buffer; |
| 44 | + if (!read_register(Register::SHUNT_VOLTAGE, raw_shunt_voltage_buffer)) { |
| 45 | + return {}; |
| 46 | + } |
| 47 | + return static_cast<int16_t>(raw_shunt_voltage_buffer); |
| 48 | +} |
| 49 | + |
| 50 | +std::optional<int16_t> INA219Driver::get_raw_current() const { |
| 51 | + if (!write_register(Register::CALIBRATION, config_.calibration_value)) { |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + uint16_t raw_current_buffer; |
| 56 | + if (!read_register(Register::CURRENT, raw_current_buffer)) { |
| 57 | + return {}; |
| 58 | + } |
| 59 | + |
| 60 | + return static_cast<int16_t>(raw_current_buffer); |
| 61 | +} |
| 62 | + |
| 63 | +std::optional<int16_t> INA219Driver::get_raw_power() const { |
| 64 | + if (!write_register(Register::CALIBRATION, config_.calibration_value)) { |
| 65 | + return {}; |
| 66 | + } |
| 67 | + |
| 68 | + uint16_t raw_power_buffer; |
| 69 | + if (!read_register(Register::POWER, raw_power_buffer)) { |
| 70 | + return {}; |
| 71 | + } |
| 72 | + |
| 73 | + return static_cast<int16_t>(raw_power_buffer); |
| 74 | +} |
| 75 | + |
| 76 | +std::optional<float> INA219Driver::get_shunt_voltage_mV() const { |
| 77 | + std::optional<int16_t> raw_shunt_voltage_value = get_raw_shunt_voltage(); |
| 78 | + if (!raw_shunt_voltage_value) { |
| 79 | + return {}; |
| 80 | + } |
| 81 | + return raw_shunt_voltage_value.value() * MILLIVOLTS_PER_MICROVOLT; |
| 82 | +} |
| 83 | + |
| 84 | +std::optional<float> INA219Driver::get_bus_voltage_V() const { |
| 85 | + std::optional<int16_t> raw_bus_voltage_value = get_raw_bus_voltage(); |
| 86 | + if (!raw_bus_voltage_value) { |
| 87 | + return {}; |
| 88 | + } |
| 89 | + return raw_bus_voltage_value.value() * VOLTS_PER_MILLIVOLT; |
| 90 | +} |
| 91 | + |
| 92 | +std::optional<float> INA219Driver::get_current_mA() const { |
| 93 | + std::optional<float> raw_current_value = get_raw_current(); |
| 94 | + if (!raw_current_value) { |
| 95 | + return {}; |
| 96 | + } |
| 97 | + return raw_current_value.value() / config_.current_divider_mA; |
| 98 | +} |
| 99 | + |
| 100 | +std::optional<float> INA219Driver::get_power_mW() const { |
| 101 | + std::optional<float> raw_power_value = get_raw_power(); |
| 102 | + if (!raw_power_value) { |
| 103 | + return {}; |
| 104 | + } |
| 105 | + return raw_power_value.value() * config_.power_multiplier_mW; |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace ina219 |
0 commit comments