ESPHome 2026.5.0
Loading...
Searching...
No Matches
gp8403.cpp
Go to the documentation of this file.
1#include "gp8403.h"
2
3#include "esphome/core/log.h"
4
5namespace esphome::gp8403 {
6
7static const char *const TAG = "gp8403";
8
9static const uint8_t RANGE_REGISTER = 0x01;
10static const uint8_t OUTPUT_REGISTER = 0x02;
11
12const LogString *model_to_string(GP8403Model model) {
13 switch (model) {
15 return LOG_STR("GP8403");
17 return LOG_STR("GP8413");
18 }
19 return LOG_STR("Unknown");
20};
21
22void GP8403Component::setup() { this->write_register(RANGE_REGISTER, (uint8_t *) (&this->voltage_), 1); }
23
25 ESP_LOGCONFIG(TAG,
26 "GP8403:\n"
27 " Voltage: %dV\n"
28 " Model: %s",
29 this->voltage_ == GP8403_VOLTAGE_5V ? 5 : 10, LOG_STR_ARG(model_to_string(this->model_)));
30 LOG_I2C_DEVICE(this);
31}
32
33void GP8403Component::write_state(float state, uint8_t channel) {
34 uint16_t val = 0;
35 switch (this->model_) {
37 val = ((uint16_t) (4095 * state)) << 4;
38 break;
40 val = ((uint16_t) (32767 * state)) << 1;
41 break;
42 default:
43 ESP_LOGE(TAG, "Unknown model %s", LOG_STR_ARG(model_to_string(this->model_)));
44 return;
45 }
46 ESP_LOGV(TAG, "Calculated DAC value: %" PRIu16, val);
47 i2c::ErrorCode err = this->write_register(OUTPUT_REGISTER + (2 * channel), (uint8_t *) &val, 2);
48 if (err != i2c::ERROR_OK) {
49 ESP_LOGE(TAG, "Error writing to %s, code %d", LOG_STR_ARG(model_to_string(this->model_)), err);
50 }
51}
52
53} // namespace esphome::gp8403
void write_state(float state, uint8_t channel)
Definition gp8403.cpp:33
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
bool state
Definition fan.h:2
mopeka_std_values val[3]
@ GP8403_VOLTAGE_5V
Definition gp8403.h:9
const LogString * model_to_string(GP8403Model model)
Definition gp8403.cpp:12
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14