ESPHome 2025.12.0-dev
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 {
6namespace gp8403 {
7
8static const char *const TAG = "gp8403";
9
10static const uint8_t RANGE_REGISTER = 0x01;
11static const uint8_t OUTPUT_REGISTER = 0x02;
12
13const LogString *model_to_string(GP8403Model model) {
14 switch (model) {
16 return LOG_STR("GP8403");
18 return LOG_STR("GP8413");
19 }
20 return LOG_STR("Unknown");
21};
22
23void GP8403Component::setup() { this->write_register(RANGE_REGISTER, (uint8_t *) (&this->voltage_), 1); }
24
26 ESP_LOGCONFIG(TAG,
27 "GP8403:\n"
28 " Voltage: %dV\n"
29 " Model: %s",
30 this->voltage_ == GP8403_VOLTAGE_5V ? 5 : 10, LOG_STR_ARG(model_to_string(this->model_)));
31 LOG_I2C_DEVICE(this);
32}
33
34void GP8403Component::write_state(float state, uint8_t channel) {
35 uint16_t val = 0;
36 switch (this->model_) {
38 val = ((uint16_t) (4095 * state)) << 4;
39 break;
41 val = ((uint16_t) (32767 * state)) << 1;
42 break;
43 default:
44 ESP_LOGE(TAG, "Unknown model %s", LOG_STR_ARG(model_to_string(this->model_)));
45 return;
46 }
47 ESP_LOGV(TAG, "Calculated DAC value: %" PRIu16, val);
48 i2c::ErrorCode err = this->write_register(OUTPUT_REGISTER + (2 * channel), (uint8_t *) &val, 2);
49 if (err != i2c::ERROR_OK) {
50 ESP_LOGE(TAG, "Error writing to %s, code %d", LOG_STR_ARG(model_to_string(this->model_)), err);
51 }
52}
53
54} // namespace gp8403
55} // namespace esphome
void write_state(float state, uint8_t channel)
Definition gp8403.cpp:34
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:44
bool state
Definition fan.h:0
mopeka_std_values val[4]
@ GP8403_VOLTAGE_5V
Definition gp8403.h:10
const LogString * model_to_string(GP8403Model model)
Definition gp8403.cpp:13
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:31
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7