ESPHome  2022.12.8
sx1509.cpp
Go to the documentation of this file.
1 #include "sx1509.h"
2 #include "esphome/core/helpers.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace sx1509 {
7 
8 static const char *const TAG = "sx1509";
9 
11  ESP_LOGCONFIG(TAG, "Setting up SX1509Component...");
12 
13  ESP_LOGV(TAG, " Resetting devices...");
14  if (!this->write_byte(REG_RESET, 0x12)) {
15  this->mark_failed();
16  return;
17  }
18  this->write_byte(REG_RESET, 0x34);
19 
20  uint16_t data;
21  if (!this->read_byte_16(REG_INTERRUPT_MASK_A, &data)) {
22  this->mark_failed();
23  return;
24  }
25  if (data != 0xFF00) {
26  this->mark_failed();
27  return;
28  }
30  delayMicroseconds(500);
31  if (this->has_keypad_)
32  this->setup_keypad_();
33 }
34 
36  ESP_LOGCONFIG(TAG, "SX1509:");
37  if (this->is_failed()) {
38  ESP_LOGE(TAG, "Setting up SX1509 failed!");
39  }
40  LOG_I2C_DEVICE(this);
41 }
42 
44  if (this->has_keypad_) {
45  uint16_t key_data = this->read_key_data();
46  for (auto *binary_sensor : this->keypad_binary_sensors_)
47  binary_sensor->process(key_data);
48  }
49 }
50 
51 bool SX1509Component::digital_read(uint8_t pin) {
52  if (this->ddr_mask_ & (1 << pin)) {
53  uint16_t temp_reg_data;
54  if (!this->read_byte_16(REG_DATA_B, &temp_reg_data))
55  return false;
56  if (temp_reg_data & (1 << pin))
57  return true;
58  }
59  return false;
60 }
61 
62 void SX1509Component::digital_write(uint8_t pin, bool bit_value) {
63  if ((~this->ddr_mask_) & (1 << pin)) {
64  // If the pin is an output, write high/low
65  uint16_t temp_reg_data = 0;
66  this->read_byte_16(REG_DATA_B, &temp_reg_data);
67  if (bit_value) {
68  temp_reg_data |= (1 << pin);
69  } else {
70  temp_reg_data &= ~(1 << pin);
71  }
72  this->write_byte_16(REG_DATA_B, temp_reg_data);
73  }
74 }
75 
77  this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
78  if (flags == gpio::FLAG_OUTPUT) {
79  this->ddr_mask_ &= ~(1 << pin);
80  } else {
81  this->ddr_mask_ |= (1 << pin);
82 
83  uint16_t temp_pullup;
84  this->read_byte_16(REG_PULL_UP_B, &temp_pullup);
85  uint16_t temp_pulldown;
86  this->read_byte_16(REG_PULL_DOWN_B, &temp_pulldown);
87 
88  if (flags & gpio::FLAG_PULLUP) {
89  temp_pullup |= (1 << pin);
90  } else {
91  temp_pullup &= ~(1 << pin);
92  }
93 
94  if (flags & gpio::FLAG_PULLDOWN) {
95  temp_pulldown |= (1 << pin);
96  } else {
97  temp_pulldown &= ~(1 << pin);
98  }
99 
100  this->write_byte_16(REG_PULL_UP_B, temp_pullup);
101  this->write_byte_16(REG_PULL_DOWN_B, temp_pulldown);
102  }
103  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
104 }
105 
107  uint16_t temp_word = 0;
108  uint8_t temp_byte = 0;
109 
110  this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
111  temp_word |= (1 << pin);
112  this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
113 
114  this->ddr_mask_ &= ~(1 << pin); // 0=output
115  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
116 
117  this->read_byte(REG_CLOCK, &temp_byte);
118  temp_byte |= (1 << 6); // Internal 2MHz oscillator part 1 (set bit 6)
119  temp_byte &= ~(1 << 5); // Internal 2MHz oscillator part 2 (clear bit 5)
120  this->write_byte(REG_CLOCK, temp_byte);
121 
122  this->read_byte(REG_MISC, &temp_byte);
123  temp_byte &= ~(1 << 7); // set linear mode bank B
124  temp_byte &= ~(1 << 3); // set linear mode bank A
125  temp_byte |= 0x70; // Frequency of the LED Driver clock ClkX of all IOs:
126  this->write_byte(REG_MISC, temp_byte);
127 
128  this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &temp_word);
129  temp_word |= (1 << pin);
130  this->write_byte_16(REG_LED_DRIVER_ENABLE_B, temp_word);
131 
132  this->read_byte_16(REG_DATA_B, &temp_word);
133  temp_word &= ~(1 << pin);
134  this->write_byte_16(REG_DATA_B, temp_word);
135 }
136 
137 void SX1509Component::clock_(uint8_t osc_source, uint8_t osc_pin_function, uint8_t osc_freq_out, uint8_t osc_divider) {
138  osc_source = (osc_source & 0b11) << 5; // 2-bit value, bits 6:5
139  osc_pin_function = (osc_pin_function & 1) << 4; // 1-bit value bit 4
140  osc_freq_out = (osc_freq_out & 0b1111); // 4-bit value, bits 3:0
141  uint8_t reg_clock = osc_source | osc_pin_function | osc_freq_out;
142  this->write_byte(REG_CLOCK, reg_clock);
143 
144  osc_divider = clamp<uint8_t>(osc_divider, 1, 7u);
145  this->clk_x_ = 2000000;
146  osc_divider = (osc_divider & 0b111) << 4; // 3-bit value, bits 6:4
147 
148  uint8_t reg_misc = 0;
149  this->read_byte(REG_MISC, &reg_misc);
150  reg_misc &= ~(0b111 << 4);
151  reg_misc |= osc_divider;
152  this->write_byte(REG_MISC, reg_misc);
153 }
154 
156  uint8_t temp_byte = 0;
157 
158  // setup row/col pins for INPUT OUTPUT
159  this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
160  for (int i = 0; i < this->rows_; i++)
161  this->ddr_mask_ &= ~(1 << i);
162  for (int i = 8; i < (this->cols_ * 2); i++)
163  this->ddr_mask_ |= (1 << i);
164  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
165 
166  this->read_byte(REG_OPEN_DRAIN_A, &temp_byte);
167  for (int i = 0; i < this->rows_; i++)
168  temp_byte |= (1 << i);
169  this->write_byte(REG_OPEN_DRAIN_A, temp_byte);
170 
171  this->read_byte(REG_PULL_UP_B, &temp_byte);
172  for (int i = 0; i < this->cols_; i++)
173  temp_byte |= (1 << i);
174  this->write_byte(REG_PULL_UP_B, temp_byte);
175 
176  if (debounce_time_ >= scan_time_) {
177  debounce_time_ = scan_time_ >> 1; // Force debounce_time to be less than scan_time
178  }
179  set_debounce_keypad_(debounce_time_, rows_, cols_);
180  uint8_t scan_time_bits = 0;
181  for (uint8_t i = 7; i > 0; i--) {
182  if (scan_time_ & (1 << i)) {
183  scan_time_bits = i;
184  break;
185  }
186  }
187  scan_time_bits &= 0b111; // Scan time is bits 2:0
188  temp_byte = sleep_time_ | scan_time_bits;
189  this->write_byte(REG_KEY_CONFIG_1, temp_byte);
190  rows_ = (rows_ - 1) & 0b111; // 0 = off, 0b001 = 2 rows, 0b111 = 8 rows, etc.
191  cols_ = (cols_ - 1) & 0b111; // 0b000 = 1 column, ob111 = 8 columns, etc.
192  this->write_byte(REG_KEY_CONFIG_2, (rows_ << 3) | cols_);
193 }
194 
196  uint16_t key_data = 0;
197  this->read_byte_16(REG_KEY_DATA_1, &key_data);
198  return (0xFFFF ^ key_data);
199 }
200 
201 void SX1509Component::set_debounce_config_(uint8_t config_value) {
202  // First make sure clock is configured
203  uint8_t temp_byte = 0;
204  this->read_byte(REG_MISC, &temp_byte);
205  temp_byte |= (1 << 4); // Just default to no divider if not set
206  this->write_byte(REG_MISC, temp_byte);
207  this->read_byte(REG_CLOCK, &temp_byte);
208  temp_byte |= (1 << 6); // default to internal osc.
209  this->write_byte(REG_CLOCK, temp_byte);
210 
211  config_value &= 0b111; // 3-bit value
212  this->write_byte(REG_DEBOUNCE_CONFIG, config_value);
213 }
214 
216  uint8_t config_value = 0;
217 
218  for (int i = 7; i >= 0; i--) {
219  if (time & (1 << i)) {
220  config_value = i + 1;
221  break;
222  }
223  }
224  config_value = clamp<uint8_t>(config_value, 0, 7);
225 
226  set_debounce_config_(config_value);
227 }
228 
230  uint16_t debounce_enable = 0;
231  this->read_byte_16(REG_DEBOUNCE_ENABLE_B, &debounce_enable);
232  debounce_enable |= (1 << pin);
233  this->write_byte_16(REG_DEBOUNCE_ENABLE_B, debounce_enable);
234 }
235 
237 
238 void SX1509Component::set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols) {
239  set_debounce_time_(time);
240  for (uint16_t i = 0; i < num_rows; i++)
242  for (uint16_t i = 0; i < (8 + num_cols); i++)
244 }
245 
246 } // namespace sx1509
247 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:96
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:107
void set_debounce_config_(uint8_t config_value)
Definition: sx1509.cpp:201
const uint8_t REG_RESET
const uint8_t REG_KEY_DATA_1
const uint8_t REG_MISC
const uint8_t REG_DATA_B
const uint8_t REG_LED_DRIVER_ENABLE_B
const uint8_t REG_DEBOUNCE_ENABLE_B
const uint8_t INTERNAL_CLOCK_2MHZ
Definition: sx1509.h:15
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition: sx1509.cpp:76
const uint8_t REG_INPUT_DISABLE_B
const uint8_t REG_DIR_B
void clock_(uint8_t osc_source=2, uint8_t osc_pin_function=1, uint8_t osc_freq_out=0, uint8_t osc_divider=0)
Definition: sx1509.cpp:137
const uint8_t REG_KEY_CONFIG_1
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition: sx1509.h:70
const uint8_t REG_INTERRUPT_MASK_A
void set_debounce_enable_(uint8_t pin)
Definition: sx1509.cpp:229
const uint32_t flags
Definition: stm32flash.h:85
void set_debounce_time_(uint8_t time)
Definition: sx1509.cpp:215
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:123
void setup_led_driver(uint8_t pin)
Definition: sx1509.cpp:106
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
void set_debounce_pin_(uint8_t pin)
Definition: sx1509.cpp:236
void dump_config() override
Definition: sx1509.cpp:35
Definition: a4988.cpp:4
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:29
void digital_write(uint8_t pin, bool bit_value)
Definition: sx1509.cpp:62
void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols)
Definition: sx1509.cpp:238
const uint8_t REG_PULL_UP_B
const uint8_t REG_CLOCK
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:127
const uint8_t REG_KEY_CONFIG_2
const uint8_t REG_OPEN_DRAIN_A
bool digital_read(uint8_t pin)
Definition: sx1509.cpp:51
const uint8_t REG_PULL_DOWN_B
const uint8_t REG_DEBOUNCE_CONFIG