mirror of
https://github.com/esphome/esphome.git
synced 2026-09-04 03:56:04 +00:00
[mcp4725] Use constexpr bit shift instead of powf for full-scale value (#17261)
This commit is contained in:
@@ -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,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;
|
||||
|
||||
Reference in New Issue
Block a user