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