ESPHome  2024.9.0
ch422g.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
6 
7 namespace esphome {
8 namespace ch422g {
9 
10 class CH422GComponent : public Component, public i2c::I2CDevice {
11  public:
12  CH422GComponent() = default;
13 
15  void setup() override;
17  void loop() override;
19  bool digital_read(uint8_t pin);
21  void digital_write(uint8_t pin, bool value);
23  void pin_mode(uint8_t pin, gpio::Flags flags);
24 
25  float get_setup_priority() const override;
26 
27  float get_loop_priority() const override;
28 
29  void dump_config() override;
30 
31  void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
32 
33  protected:
34  bool read_inputs_();
35 
36  bool write_output_(uint8_t value);
37 
39  uint8_t state_mask_{0x00};
41  uint8_t pin_read_cache_ = {0x00};
45  bool restore_value_{false};
46 };
47 
49 class CH422GGPIOPin : public GPIOPin {
50  public:
51  void setup() override;
52  void pin_mode(gpio::Flags flags) override;
53  bool digital_read() override;
54  void digital_write(bool value) override;
55  std::string dump_summary() const override;
56 
57  void set_parent(CH422GComponent *parent) { parent_ = parent; }
58  void set_pin(uint8_t pin) { pin_ = pin; }
59  void set_inverted(bool inverted) { inverted_ = inverted; }
60  void set_flags(gpio::Flags flags) { flags_ = flags; }
61 
62  protected:
64  uint8_t pin_;
65  bool inverted_;
67 };
68 
69 } // namespace ch422g
70 } // namespace esphome
CH422GComponent * parent_
Definition: ch422g.h:63
float get_loop_priority() const override
Definition: ch422g.cpp:112
void setup() override
Check i2c availability and setup masks.
Definition: ch422g.cpp:13
void loop() override
Poll for input changes periodically.
Definition: ch422g.cpp:31
void set_restore_value(bool restore_value)
Definition: ch422g.h:31
void digital_write(uint8_t pin, bool value)
Helper function to write the value of a pin.
Definition: ch422g.cpp:57
void set_parent(CH422GComponent *parent)
Definition: ch422g.h:57
uint8_t pin_read_cache_
Flags to check if read previously during this loop.
Definition: ch422g.h:41
void set_flags(gpio::Flags flags)
Definition: ch422g.h:60
uint8_t state_mask_
The mask to write as output state - 1 means HIGH, 0 means LOW.
Definition: ch422g.h:39
bool digital_read(uint8_t pin)
Helper function to read the value of a pin.
Definition: ch422g.cpp:47
bool write_output_(uint8_t value)
Definition: ch422g.cpp:88
void dump_config() override
Definition: ch422g.cpp:36
void pin_mode(uint8_t pin, gpio::Flags flags)
Helper function to set the pin mode of a pin.
Definition: ch422g.cpp:45
esphome::i2c::ErrorCode last_error_
Storage for last I2C error seen.
Definition: ch422g.h:43
const uint32_t flags
Definition: stm32flash.h:85
float get_setup_priority() const override
Definition: ch422g.cpp:108
bool restore_value_
Whether we want to override stored values on expander.
Definition: ch422g.h:45
void set_pin(uint8_t pin)
Definition: ch422g.h:58
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_inverted(bool inverted)
Definition: ch422g.h:59
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
Helper class to expose a CH422G pin as an internal input GPIO pin.
Definition: ch422g.h:49