9 template<
typename T,
unsigned B>
inline T
signextend(
const T
x) {
27 static const char *
const TAG =
"max9611";
28 static const uint8_t SETUP_DELAY = 4;
29 static const float VOUT_LSB = 14.0 / 1000.0;
30 static const float TEMP_LSB = 0.48;
31 static const float MICRO_VOLTS_PER_VOLT = 1000000.0;
33 ESP_LOGCONFIG(TAG,
"Setting up max9611...");
36 this->read(&value, 1);
43 if (this->write(reinterpret_cast<const uint8_t *>(&setup_dat),
sizeof(setup_dat)) !=
ErrorCode::ERROR_OK) {
44 ESP_LOGE(TAG,
"Failed to setup Max9611 during GAIN SET");
48 if (this->write(reinterpret_cast<const uint8_t *>(&fast_mode_dat),
sizeof(fast_mode_dat)) !=
ErrorCode::ERROR_OK) {
49 ESP_LOGE(TAG,
"Failed to setup Max9611 during FAST MODE SET");
54 ESP_LOGCONFIG(TAG,
"Dump Config max9611...");
55 ESP_LOGCONFIG(TAG,
" CSA Gain Register: %x", gain_);
60 const uint8_t reg_base = 0x0;
61 const ErrorCode write_result = this->write(®_base, 1);
63 const ErrorCode read_result = this->read(register_map_,
sizeof(register_map_));
65 ESP_LOGW(TAG,
"MAX9611 Update FAILED!");
71 float voltage = rs_register * VOUT_LSB;
72 float shunt_voltage = (csa_register *
gain_to_lsb(gain_)) / MICRO_VOLTS_PER_VOLT;
73 float temp = signextend<signed int, 9>(t_register) * TEMP_LSB;
74 float amps = shunt_voltage / current_resistor_;
75 float watts = amps * voltage;
77 if (voltage_sensor_ !=
nullptr) {
78 voltage_sensor_->publish_state(voltage);
80 if (current_sensor_ !=
nullptr) {
81 current_sensor_->publish_state(amps);
83 if (watt_sensor_ !=
nullptr) {
84 watt_sensor_->publish_state(watts);
86 if (temperature_sensor_ !=
nullptr) {
87 temperature_sensor_->publish_state(temp);
90 ESP_LOGD(TAG,
"V: %f, A: %f, W: %f, Deg C: %f", voltage, amps, watts, temp);
float gain_to_lsb(MAX9611Multiplexer gain)
Implementation of SPI Controller mode.
void dump_config() override
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
void IRAM_ATTR HOT delay(uint32_t ms)