[mcp4725] Use constexpr bit shift instead of powf for full-scale value (#17261)

This commit is contained in:
J. Nick Koston
2026-06-28 17:07:51 -04:00
committed by GitHub
parent 8434d54cc7
commit a336ad6732
2 changed files with 6 additions and 4 deletions
+2 -1
View File
@@ -24,7 +24,8 @@ void MCP4725::dump_config() {
// https://learn.sparkfun.com/tutorials/mcp4725-digital-to-analog-converter-hookup-guide?_ga=2.176055202.1402343014.1607953301-893095255.1606753886
void MCP4725::write_state(float state) {
const uint16_t value = (uint16_t) roundf(state * (powf(2, MCP4725_RES) - 1));
constexpr uint16_t max_value = (1U << MCP4725_RES) - 1;
const uint16_t value = (uint16_t) roundf(state * max_value);
this->write_byte_16(64, value << 4);
}
+4 -3
View File
@@ -4,10 +4,11 @@
#include "esphome/core/component.h"
#include "esphome/components/i2c/i2c.h"
static const uint8_t MCP4725_ADDR = 0x60;
static const uint8_t MCP4725_RES = 12;
namespace esphome::mcp4725 {
static constexpr uint8_t MCP4725_ADDR = 0x60;
static constexpr uint8_t MCP4725_RES = 12;
class MCP4725 final : public Component, public output::FloatOutput, public i2c::I2CDevice {
public:
void setup() override;