ESPHome  2024.5.0
xl9535.cpp
Go to the documentation of this file.
1 #include "xl9535.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace xl9535 {
6 
7 static const char *const TAG = "xl9535";
8 
10  ESP_LOGCONFIG(TAG, "Setting up XL9535...");
11 
12  // Check to see if the device can read from the register
13  uint8_t port = 0;
15  this->mark_failed();
16  return;
17  }
18 }
19 
21  ESP_LOGCONFIG(TAG, "XL9535:");
22  LOG_I2C_DEVICE(this);
23 
24  if (this->is_failed()) {
25  ESP_LOGE(TAG, "Communication with XL9535 failed!");
26  }
27 }
28 
29 bool XL9535Component::digital_read(uint8_t pin) {
30  bool state = false;
31  uint8_t port = 0;
32 
33  if (pin > 7) {
35  this->status_set_warning();
36  return state;
37  }
38 
39  state = (port & (1 << (pin - 10))) != 0;
40  } else {
42  this->status_set_warning();
43  return state;
44  }
45 
46  state = (port & (1 << pin)) != 0;
47  }
48 
49  this->status_clear_warning();
50  return state;
51 }
52 
53 void XL9535Component::digital_write(uint8_t pin, bool value) {
54  uint8_t port = 0;
55  uint8_t register_data = 0;
56 
57  if (pin > 7) {
58  if (this->read_register(XL9535_OUTPUT_PORT_1_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
59  this->status_set_warning();
60  return;
61  }
62 
63  register_data = register_data & (~(1 << (pin - 10)));
64  port = register_data | value << (pin - 10);
65 
67  this->status_set_warning();
68  return;
69  }
70  } else {
71  if (this->read_register(XL9535_OUTPUT_PORT_0_REGISTER, &register_data, 1) != i2c::ERROR_OK) {
72  this->status_set_warning();
73  return;
74  }
75  register_data = register_data & (~(1 << pin));
76  port = register_data | value << pin;
77 
79  this->status_set_warning();
80  return;
81  }
82  }
83 
84  this->status_clear_warning();
85 }
86 
88  uint8_t port = 0;
89 
90  if (pin > 7) {
92 
93  if (mode == gpio::FLAG_INPUT) {
94  port = port | (1 << (pin - 10));
95  } else if (mode == gpio::FLAG_OUTPUT) {
96  port = port & (~(1 << (pin - 10)));
97  }
98 
100  } else {
102 
103  if (mode == gpio::FLAG_INPUT) {
104  port = port | (1 << pin);
105  } else if (mode == gpio::FLAG_OUTPUT) {
106  port = port & (~(1 << pin));
107  }
108 
110  }
111 }
112 
113 void XL9535GPIOPin::setup() { this->pin_mode(this->flags_); }
114 
115 std::string XL9535GPIOPin::dump_summary() const { return str_snprintf("%u via XL9535", 15, this->pin_); }
116 
117 void XL9535GPIOPin::pin_mode(gpio::Flags flags) { this->parent_->pin_mode(this->pin_, flags); }
118 bool XL9535GPIOPin::digital_read() { return this->parent_->digital_read(this->pin_) != this->inverted_; }
119 void XL9535GPIOPin::digital_write(bool value) { this->parent_->digital_write(this->pin_, value != this->inverted_); }
120 
121 } // namespace xl9535
122 } // namespace esphome
void pin_mode(uint8_t pin, gpio::Flags mode)
Definition: xl9535.cpp:87
void digital_write(uint8_t pin, bool value)
Definition: xl9535.cpp:53
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool digital_read() override
Definition: xl9535.cpp:118
bool digital_read(uint8_t pin)
Definition: xl9535.cpp:29
void digital_write(bool value) override
Definition: xl9535.cpp:119
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
const uint32_t flags
Definition: stm32flash.h:85
void dump_config() override
Definition: xl9535.cpp:20
std::string dump_summary() const override
Definition: xl9535.cpp:115
void pin_mode(gpio::Flags flags) override
Definition: xl9535.cpp:117
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:298
bool state
Definition: fan.h:34