ESPHome  2024.4.1
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  output_state_ |= (1 << pin); // set bit in shadow register
72  } else {
73  output_state_ &= ~(1 << pin); // reset bit shadow register
74  }
75  for (uint16_t b = 0x8000; b; b >>= 1) {
76  if ((~ddr_mask_) & b) { // transfer bits of outputs, but don't mess with inputs
77  if (output_state_ & b) {
78  temp_reg_data |= b;
79  } else {
80  temp_reg_data &= ~b;
81  }
82  }
83  }
84  this->write_byte_16(REG_DATA_B, temp_reg_data);
85  }
86 }
87 
89  this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
90  if (flags == gpio::FLAG_OUTPUT) {
91  this->ddr_mask_ &= ~(1 << pin);
92  } else {
93  this->ddr_mask_ |= (1 << pin);
94 
95  uint16_t temp_pullup;
96  this->read_byte_16(REG_PULL_UP_B, &temp_pullup);
97  uint16_t temp_pulldown;
98  this->read_byte_16(REG_PULL_DOWN_B, &temp_pulldown);
99 
100  if (flags & gpio::FLAG_PULLUP) {
101  temp_pullup |= (1 << pin);
102  } else {
103  temp_pullup &= ~(1 << pin);
104  }
105 
106  if (flags & gpio::FLAG_PULLDOWN) {
107  temp_pulldown |= (1 << pin);
108  } else {
109  temp_pulldown &= ~(1 << pin);
110  }
111 
112  this->write_byte_16(REG_PULL_UP_B, temp_pullup);
113  this->write_byte_16(REG_PULL_DOWN_B, temp_pulldown);
114  }
115  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
116 }
117 
119  uint16_t temp_word = 0;
120  uint8_t temp_byte = 0;
121 
122  this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
123  temp_word |= (1 << pin);
124  this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
125 
126  this->ddr_mask_ &= ~(1 << pin); // 0=output
127  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
128 
129  this->read_byte(REG_CLOCK, &temp_byte);
130  temp_byte |= (1 << 6); // Internal 2MHz oscillator part 1 (set bit 6)
131  temp_byte &= ~(1 << 5); // Internal 2MHz oscillator part 2 (clear bit 5)
132  this->write_byte(REG_CLOCK, temp_byte);
133 
134  this->read_byte(REG_MISC, &temp_byte);
135  temp_byte &= ~(1 << 7); // set linear mode bank B
136  temp_byte &= ~(1 << 3); // set linear mode bank A
137  temp_byte |= 0x70; // Frequency of the LED Driver clock ClkX of all IOs:
138  this->write_byte(REG_MISC, temp_byte);
139 
140  this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &temp_word);
141  temp_word |= (1 << pin);
142  this->write_byte_16(REG_LED_DRIVER_ENABLE_B, temp_word);
143 
144  this->read_byte_16(REG_DATA_B, &temp_word);
145  temp_word &= ~(1 << pin);
146  output_state_ &= ~(1 << pin);
147  this->write_byte_16(REG_DATA_B, temp_word);
148 }
149 
150 void SX1509Component::clock_(uint8_t osc_source, uint8_t osc_pin_function, uint8_t osc_freq_out, uint8_t osc_divider) {
151  osc_source = (osc_source & 0b11) << 5; // 2-bit value, bits 6:5
152  osc_pin_function = (osc_pin_function & 1) << 4; // 1-bit value bit 4
153  osc_freq_out = (osc_freq_out & 0b1111); // 4-bit value, bits 3:0
154  uint8_t reg_clock = osc_source | osc_pin_function | osc_freq_out;
155  this->write_byte(REG_CLOCK, reg_clock);
156 
157  osc_divider = clamp<uint8_t>(osc_divider, 1, 7u);
158  this->clk_x_ = 2000000;
159  osc_divider = (osc_divider & 0b111) << 4; // 3-bit value, bits 6:4
160 
161  uint8_t reg_misc = 0;
162  this->read_byte(REG_MISC, &reg_misc);
163  reg_misc &= ~(0b111 << 4);
164  reg_misc |= osc_divider;
165  this->write_byte(REG_MISC, reg_misc);
166 }
167 
169  uint8_t temp_byte = 0;
170 
171  // setup row/col pins for INPUT OUTPUT
172  this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
173  for (int i = 0; i < this->rows_; i++)
174  this->ddr_mask_ &= ~(1 << i);
175  for (int i = 8; i < (this->cols_ * 2); i++)
176  this->ddr_mask_ |= (1 << i);
177  this->write_byte_16(REG_DIR_B, this->ddr_mask_);
178 
179  this->read_byte(REG_OPEN_DRAIN_A, &temp_byte);
180  for (int i = 0; i < this->rows_; i++)
181  temp_byte |= (1 << i);
182  this->write_byte(REG_OPEN_DRAIN_A, temp_byte);
183 
184  this->read_byte(REG_PULL_UP_B, &temp_byte);
185  for (int i = 0; i < this->cols_; i++)
186  temp_byte |= (1 << i);
187  this->write_byte(REG_PULL_UP_B, temp_byte);
188 
189  if (debounce_time_ >= scan_time_) {
190  debounce_time_ = scan_time_ >> 1; // Force debounce_time to be less than scan_time
191  }
192  set_debounce_keypad_(debounce_time_, rows_, cols_);
193  uint8_t scan_time_bits = 0;
194  for (uint8_t i = 7; i > 0; i--) {
195  if (scan_time_ & (1 << i)) {
196  scan_time_bits = i;
197  break;
198  }
199  }
200  scan_time_bits &= 0b111; // Scan time is bits 2:0
201  temp_byte = sleep_time_ | scan_time_bits;
202  this->write_byte(REG_KEY_CONFIG_1, temp_byte);
203  rows_ = (rows_ - 1) & 0b111; // 0 = off, 0b001 = 2 rows, 0b111 = 8 rows, etc.
204  cols_ = (cols_ - 1) & 0b111; // 0b000 = 1 column, ob111 = 8 columns, etc.
205  this->write_byte(REG_KEY_CONFIG_2, (rows_ << 3) | cols_);
206 }
207 
209  uint16_t key_data = 0;
210  this->read_byte_16(REG_KEY_DATA_1, &key_data);
211  return (0xFFFF ^ key_data);
212 }
213 
214 void SX1509Component::set_debounce_config_(uint8_t config_value) {
215  // First make sure clock is configured
216  uint8_t temp_byte = 0;
217  this->read_byte(REG_MISC, &temp_byte);
218  temp_byte |= (1 << 4); // Just default to no divider if not set
219  this->write_byte(REG_MISC, temp_byte);
220  this->read_byte(REG_CLOCK, &temp_byte);
221  temp_byte |= (1 << 6); // default to internal osc.
222  this->write_byte(REG_CLOCK, temp_byte);
223 
224  config_value &= 0b111; // 3-bit value
225  this->write_byte(REG_DEBOUNCE_CONFIG, config_value);
226 }
227 
229  uint8_t config_value = 0;
230 
231  for (int i = 7; i >= 0; i--) {
232  if (time & (1 << i)) {
233  config_value = i + 1;
234  break;
235  }
236  }
237  config_value = clamp<uint8_t>(config_value, 0, 7);
238 
239  set_debounce_config_(config_value);
240 }
241 
243  uint16_t debounce_enable = 0;
244  this->read_byte_16(REG_DEBOUNCE_ENABLE_B, &debounce_enable);
245  debounce_enable |= (1 << pin);
246  this->write_byte_16(REG_DEBOUNCE_ENABLE_B, debounce_enable);
247 }
248 
250 
251 void SX1509Component::set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols) {
252  set_debounce_time_(time);
253  for (uint16_t i = 0; i < num_rows; i++)
255  for (uint16_t i = 0; i < (8 + num_cols); i++)
257 }
258 
259 } // namespace sx1509
260 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
void set_debounce_config_(uint8_t config_value)
Definition: sx1509.cpp:214
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:25
const uint8_t REG_DEBOUNCE_ENABLE_B
const uint8_t INTERNAL_CLOCK_2MHZ
Definition: sx1509.h:15
const char *const TAG
Definition: spi.cpp:8
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition: sx1509.cpp:88
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:150
const uint8_t REG_KEY_CONFIG_1
const uint32_t min_loop_period_
Definition: sx1509.h:74
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition: sx1509.h:71
const uint8_t REG_INTERRUPT_MASK_A
void set_debounce_enable_(uint8_t pin)
Definition: sx1509.cpp:242
const uint32_t flags
Definition: stm32flash.h:85
void set_debounce_time_(uint8_t time)
Definition: sx1509.cpp:228
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
void setup_led_driver(uint8_t pin)
Definition: sx1509.cpp:118
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void set_debounce_pin_(uint8_t pin)
Definition: sx1509.cpp:249
void dump_config() override
Definition: sx1509.cpp:35
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
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:251
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:266
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