ESPHome  2025.4.0
mcp4461.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace mcp4461 {
9 
10 struct WiperState {
11  bool enabled = true;
12  uint16_t state = 0;
14  bool terminal_a = true;
15  bool terminal_b = true;
16  bool terminal_w = true;
17  bool terminal_hw = true;
18  bool wiper_lock_active = false;
19  bool update_level = false;
20  bool update_terminal = false;
21 };
22 
23 // default wiper state is 128 / 0x80h
24 enum class Mcp4461Commands : uint8_t { WRITE = 0x00, INCREMENT = 0x04, DECREMENT = 0x08, READ = 0x0C };
25 
26 enum class Mcp4461Addresses : uint8_t {
27  MCP4461_VW0 = 0x00,
28  MCP4461_VW1 = 0x10,
29  MCP4461_VW2 = 0x60,
30  MCP4461_VW3 = 0x70,
31  MCP4461_STATUS = 0x50,
32  MCP4461_TCON0 = 0x40,
33  MCP4461_TCON1 = 0xA0,
34  MCP4461_EEPROM_1 = 0xB0
35 };
36 
37 enum class Mcp4461WiperIdx : uint8_t {
38  MCP4461_WIPER_0 = 0,
39  MCP4461_WIPER_1 = 1,
40  MCP4461_WIPER_2 = 2,
41  MCP4461_WIPER_3 = 3,
42  MCP4461_WIPER_4 = 4,
43  MCP4461_WIPER_5 = 5,
44  MCP4461_WIPER_6 = 6,
45  MCP4461_WIPER_7 = 7
46 };
47 
48 enum class Mcp4461EepromLocation : uint8_t {
49  MCP4461_EEPROM_0 = 0,
50  MCP4461_EEPROM_1 = 1,
51  MCP4461_EEPROM_2 = 2,
52  MCP4461_EEPROM_3 = 3,
54 };
55 
56 enum class Mcp4461TerminalIdx : uint8_t { MCP4461_TERMINAL_0 = 0, MCP4461_TERMINAL_1 = 1 };
57 
58 class Mcp4461Wiper;
59 
60 // Mcp4461Component
61 class Mcp4461Component : public Component, public i2c::I2CDevice {
62  public:
63  Mcp4461Component(bool disable_wiper_0, bool disable_wiper_1, bool disable_wiper_2, bool disable_wiper_3)
64  : wiper_0_disabled_(disable_wiper_0),
65  wiper_1_disabled_(disable_wiper_1),
66  wiper_2_disabled_(disable_wiper_2),
67  wiper_3_disabled_(disable_wiper_3) {
68  this->reg_[0].enabled = !wiper_0_disabled_;
69  this->reg_[1].enabled = !wiper_1_disabled_;
70  this->reg_[2].enabled = !wiper_2_disabled_;
71  this->reg_[3].enabled = !wiper_3_disabled_;
72  }
73 
74  void setup() override;
75  void dump_config() override;
76  float get_setup_priority() const override { return setup_priority::HARDWARE; }
77  void loop() override;
81  uint16_t get_eeprom_value(Mcp4461EepromLocation location);
85  bool set_eeprom_value(Mcp4461EepromLocation location, uint16_t value);
89  void set_initial_value(Mcp4461WiperIdx wiper, float initial_value);
93  void initialize_terminal_disabled(Mcp4461WiperIdx wiper, char terminal);
95  void read_status_register_to_log();
96 
97  protected:
98  friend class Mcp4461Wiper;
99  void update_write_protection_status_();
100  uint8_t get_wiper_address_(uint8_t wiper);
101  uint16_t read_wiper_level_(uint8_t wiper);
102  uint8_t get_status_register_();
103  uint16_t get_wiper_level_(Mcp4461WiperIdx wiper);
104  bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value);
105  bool update_wiper_level_(Mcp4461WiperIdx wiper);
106  void enable_wiper_(Mcp4461WiperIdx wiper);
107  void disable_wiper_(Mcp4461WiperIdx wiper);
108  bool increase_wiper_(Mcp4461WiperIdx wiper);
109  bool decrease_wiper_(Mcp4461WiperIdx wiper);
110  void enable_terminal_(Mcp4461WiperIdx wiper, char terminal);
111  void disable_terminal_(Mcp4461WiperIdx, char terminal);
112  bool is_writing_();
113  bool is_eeprom_ready_for_writing_(bool wait_if_not_ready);
114  void write_wiper_level_(uint8_t wiper, uint16_t value);
115  bool mcp4461_write_(uint8_t addr, uint16_t data, bool nonvolatile = false);
116  uint8_t calc_terminal_connector_byte_(Mcp4461TerminalIdx terminal_connector);
117  void update_terminal_register_(Mcp4461TerminalIdx terminal_connector);
118  uint8_t get_terminal_register_(Mcp4461TerminalIdx terminal_connector);
119  bool set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data);
120 
121  // Converts a status to a human readable string
122  static const LogString *get_message_string(int status) {
123  switch (status) {
124  case MCP4461_STATUS_I2C_ERROR:
125  return LOG_STR("I2C error - communication with MCP4461 failed!");
126  case MCP4461_STATUS_REGISTER_ERROR:
127  return LOG_STR("Status register could not be read");
128  case MCP4461_STATUS_REGISTER_INVALID:
129  return LOG_STR("Invalid status register value - bits 1,7 or 8 are 0");
130  case MCP4461_VALUE_INVALID:
131  return LOG_STR("Invalid value for wiper given");
132  case MCP4461_WRITE_PROTECTED:
133  return LOG_STR("MCP4461 is write protected. Setting nonvolatile wipers/eeprom values is prohibited.");
134  case MCP4461_WIPER_ENABLED:
135  return LOG_STR("MCP4461 Wiper is already enabled, ignoring cmd to enable.");
136  case MCP4461_WIPER_DISABLED:
137  return LOG_STR("MCP4461 Wiper is disabled. All actions on this wiper are prohibited.");
138  case MCP4461_WIPER_LOCKED:
139  return LOG_STR("MCP4461 Wiper is locked using WiperLock-technology. All actions on this wiper are prohibited.");
140  case MCP4461_STATUS_OK:
141  return LOG_STR("Status OK");
142  default:
143  return LOG_STR("Unknown");
144  }
145  }
146 
147  enum ErrorCode {
148  MCP4461_STATUS_OK = 0, // CMD completed successfully
149  MCP4461_FAILED, // component failed
150  MCP4461_STATUS_I2C_ERROR, // Unable to communicate with device
151  MCP4461_STATUS_REGISTER_INVALID, // Status register value was invalid
152  MCP4461_STATUS_REGISTER_ERROR, // Error fetching status register
154  MCP4461_VALUE_INVALID, // Invalid value given for wiper / eeprom
155  MCP4461_WRITE_PROTECTED, // The value was read, but the CRC over the payload (valid and data) does not match
156  MCP4461_WIPER_ENABLED, // The wiper is enabled, discard additional enabling actions
157  MCP4461_WIPER_DISABLED, // The wiper is disabled - all actions for this wiper will be aborted/discarded
158  MCP4461_WIPER_LOCKED, // The wiper is locked using WiperLock-technology - all actions for this wiper will be
159  // aborted/discarded
160  } error_code_{MCP4461_STATUS_OK};
161 
162  WiperState reg_[8];
163  bool last_eeprom_write_timed_out_{false};
164  bool write_protected_{false};
165  bool wiper_0_disabled_{false};
166  bool wiper_1_disabled_{false};
167  bool wiper_2_disabled_{false};
168  bool wiper_3_disabled_{false};
169 };
170 } // namespace mcp4461
171 } // namespace esphome
void setup()
void loop()
optional< float > initial_value
Definition: mcp4461.h:13
static const LogString * get_message_string(int status)
Definition: mcp4461.h:122
float get_setup_priority() const override
Definition: mcp4461.h:76
uint8_t status
Definition: bl0942.h:74
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Mcp4461Component(bool disable_wiper_0, bool disable_wiper_1, bool disable_wiper_2, bool disable_wiper_3)
Definition: mcp4461.h:63
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133