ESPHome 2025.6.3
Loading...
Searching...
No Matches
sx1509.cpp
Go to the documentation of this file.
1#include "sx1509.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace sx1509 {
7
8static const char *const TAG = "sx1509";
9
11 ESP_LOGCONFIG(TAG, "Running setup");
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 }
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;
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 if (this->keys_.empty())
52 return;
53 if (key_data == 0) {
54 this->last_key_ = 0;
55 return;
56 }
57 int row, col;
58 for (row = 0; row < 7; row++) {
59 if (key_data & (1 << row))
60 break;
61 }
62 for (col = 8; col < 15; col++) {
63 if (key_data & (1 << col))
64 break;
65 }
66 col -= 8;
67 uint8_t key = this->keys_[row * this->cols_ + col];
68 if (key == this->last_key_)
69 return;
70 this->last_key_ = key;
71 ESP_LOGV(TAG, "row %d, col %d, key '%c'", row, col, key);
72 for (auto &trigger : this->key_triggers_)
73 trigger->trigger(key);
74 this->send_key_(key);
75 }
76}
77
79 if (this->ddr_mask_ & (1 << pin)) {
80 uint16_t temp_reg_data;
81 if (!this->read_byte_16(REG_DATA_B, &temp_reg_data))
82 return false;
83 if (temp_reg_data & (1 << pin))
84 return true;
85 }
86 return false;
87}
88
89void SX1509Component::digital_write(uint8_t pin, bool bit_value) {
90 if ((~this->ddr_mask_) & (1 << pin)) {
91 // If the pin is an output, write high/low
92 uint16_t temp_reg_data = 0;
93 this->read_byte_16(REG_DATA_B, &temp_reg_data);
94 if (bit_value) {
95 output_state_ |= (1 << pin); // set bit in shadow register
96 } else {
97 output_state_ &= ~(1 << pin); // reset bit shadow register
98 }
99 for (uint16_t b = 0x8000; b; b >>= 1) {
100 if ((~ddr_mask_) & b) { // transfer bits of outputs, but don't mess with inputs
101 if (output_state_ & b) {
102 temp_reg_data |= b;
103 } else {
104 temp_reg_data &= ~b;
105 }
106 }
107 }
108 this->write_byte_16(REG_DATA_B, temp_reg_data);
109 }
110}
111
112void SX1509Component::pin_mode(uint8_t pin, gpio::Flags flags) {
113 ESP_LOGI(TAG, "Configuring pin %u with flags %x", pin, flags);
114
115 uint16_t temp_word = 0;
116
117 this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
118 if (flags & gpio::FLAG_OUTPUT) {
119 // Always disable input buffer
120 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
121 temp_word |= (1 << pin);
122 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
123
124 if (flags & gpio::FLAG_OPEN_DRAIN) {
125 // Pullup must be disabled for open drain mode
126 this->read_byte_16(REG_PULL_UP_B, &temp_word);
127 temp_word &= ~(1 << pin);
128 this->write_byte_16(REG_PULL_UP_B, temp_word);
129 this->read_byte_16(REG_OPEN_DRAIN_B, &temp_word);
130 temp_word |= (1 << pin);
131 this->write_byte_16(REG_OPEN_DRAIN_B, temp_word);
132 ESP_LOGD(TAG, "Open drain output mode set for %u", pin);
133 } else {
134 ESP_LOGD(TAG, "Output Mode for %u", pin);
135 }
136
137 // Set direction to output
138 this->ddr_mask_ &= ~(1 << pin);
139 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
140 } else {
141 ESP_LOGD(TAG, "Input Mode for %u", pin);
142
143 // Always enable input buffer
144 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
145 temp_word &= ~(1 << pin);
146 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
147
148 // Pullup
149 this->read_byte_16(REG_PULL_UP_B, &temp_word);
150 if (flags & gpio::FLAG_PULLUP) {
151 temp_word |= (1 << pin);
152 } else {
153 temp_word &= ~(1 << pin);
154 }
155 this->write_byte_16(REG_PULL_UP_B, temp_word);
156
157 // Pulldown
158 this->read_byte_16(REG_PULL_DOWN_B, &temp_word);
159 if (flags & gpio::FLAG_PULLDOWN) {
160 temp_word |= (1 << pin);
161 } else {
162 temp_word &= ~(1 << pin);
163 }
164 this->write_byte_16(REG_PULL_DOWN_B, temp_word);
165
166 // Set direction to input
167 this->ddr_mask_ |= (1 << pin);
168 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
169 }
170}
171
173 uint16_t temp_word = 0;
174 uint8_t temp_byte = 0;
175
176 this->read_byte_16(REG_INPUT_DISABLE_B, &temp_word);
177 temp_word |= (1 << pin);
178 this->write_byte_16(REG_INPUT_DISABLE_B, temp_word);
179
180 this->ddr_mask_ &= ~(1 << pin); // 0=output
181 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
182
183 this->read_byte(REG_CLOCK, &temp_byte);
184 temp_byte |= (1 << 6); // Internal 2MHz oscillator part 1 (set bit 6)
185 temp_byte &= ~(1 << 5); // Internal 2MHz oscillator part 2 (clear bit 5)
186 this->write_byte(REG_CLOCK, temp_byte);
187
188 this->read_byte(REG_MISC, &temp_byte);
189 temp_byte &= ~(1 << 7); // set linear mode bank B
190 temp_byte &= ~(1 << 3); // set linear mode bank A
191 temp_byte |= 0x70; // Frequency of the LED Driver clock ClkX of all IOs:
192 this->write_byte(REG_MISC, temp_byte);
193
194 this->read_byte_16(REG_LED_DRIVER_ENABLE_B, &temp_word);
195 temp_word |= (1 << pin);
196 this->write_byte_16(REG_LED_DRIVER_ENABLE_B, temp_word);
197
198 this->read_byte_16(REG_DATA_B, &temp_word);
199 temp_word &= ~(1 << pin);
200 output_state_ &= ~(1 << pin);
201 this->write_byte_16(REG_DATA_B, temp_word);
202}
203
204void SX1509Component::clock_(uint8_t osc_source, uint8_t osc_pin_function, uint8_t osc_freq_out, uint8_t osc_divider) {
205 osc_source = (osc_source & 0b11) << 5; // 2-bit value, bits 6:5
206 osc_pin_function = (osc_pin_function & 1) << 4; // 1-bit value bit 4
207 osc_freq_out = (osc_freq_out & 0b1111); // 4-bit value, bits 3:0
208 uint8_t reg_clock = osc_source | osc_pin_function | osc_freq_out;
209 this->write_byte(REG_CLOCK, reg_clock);
210
211 osc_divider = clamp<uint8_t>(osc_divider, 1, 7u);
212 this->clk_x_ = 2000000;
213 osc_divider = (osc_divider & 0b111) << 4; // 3-bit value, bits 6:4
214
215 uint8_t reg_misc = 0;
216 this->read_byte(REG_MISC, &reg_misc);
217 reg_misc &= ~(0b111 << 4);
218 reg_misc |= osc_divider;
219 this->write_byte(REG_MISC, reg_misc);
220}
221
223 uint8_t temp_byte = 0;
224
225 // setup row/col pins for INPUT OUTPUT
226 this->read_byte_16(REG_DIR_B, &this->ddr_mask_);
227 for (int i = 0; i < this->rows_; i++)
228 this->ddr_mask_ &= ~(1 << i);
229 for (int i = 8; i < (this->cols_ * 2); i++)
230 this->ddr_mask_ |= (1 << i);
231 this->write_byte_16(REG_DIR_B, this->ddr_mask_);
232
233 this->read_byte(REG_OPEN_DRAIN_A, &temp_byte);
234 for (int i = 0; i < this->rows_; i++)
235 temp_byte |= (1 << i);
236 this->write_byte(REG_OPEN_DRAIN_A, temp_byte);
237
238 this->read_byte(REG_PULL_UP_B, &temp_byte);
239 for (int i = 0; i < this->cols_; i++)
240 temp_byte |= (1 << i);
241 this->write_byte(REG_PULL_UP_B, temp_byte);
242
243 if (debounce_time_ >= scan_time_) {
244 debounce_time_ = scan_time_ >> 1; // Force debounce_time to be less than scan_time
245 }
247 uint8_t scan_time_bits = 0;
248 for (uint8_t i = 7; i > 0; i--) {
249 if (scan_time_ & (1 << i)) {
250 scan_time_bits = i;
251 break;
252 }
253 }
254 scan_time_bits &= 0b111; // Scan time is bits 2:0
255 temp_byte = sleep_time_ | scan_time_bits;
256 this->write_byte(REG_KEY_CONFIG_1, temp_byte);
257 temp_byte = ((this->rows_ - 1) & 0b111) << 3; // 0 = off, 0b001 = 2 rows, 0b111 = 8 rows, etc.
258 temp_byte |= (this->cols_ - 1) & 0b111; // 0b000 = 1 column, ob111 = 8 columns, etc.
259 this->write_byte(REG_KEY_CONFIG_2, temp_byte);
260}
261
263 uint16_t key_data = 0;
264 this->read_byte_16(REG_KEY_DATA_1, &key_data);
265 return (0xFFFF ^ key_data);
266}
267
268void SX1509Component::set_debounce_config_(uint8_t config_value) {
269 // First make sure clock is configured
270 uint8_t temp_byte = 0;
271 this->read_byte(REG_MISC, &temp_byte);
272 temp_byte |= (1 << 4); // Just default to no divider if not set
273 this->write_byte(REG_MISC, temp_byte);
274 this->read_byte(REG_CLOCK, &temp_byte);
275 temp_byte |= (1 << 6); // default to internal osc.
276 this->write_byte(REG_CLOCK, temp_byte);
277
278 config_value &= 0b111; // 3-bit value
279 this->write_byte(REG_DEBOUNCE_CONFIG, config_value);
280}
281
283 uint8_t config_value = 0;
284
285 for (int i = 7; i >= 0; i--) {
286 if (time & (1 << i)) {
287 config_value = i + 1;
288 break;
289 }
290 }
291 config_value = clamp<uint8_t>(config_value, 0, 7);
292
293 set_debounce_config_(config_value);
294}
295
297 uint16_t debounce_enable = 0;
298 this->read_byte_16(REG_DEBOUNCE_ENABLE_B, &debounce_enable);
299 debounce_enable |= (1 << pin);
300 this->write_byte_16(REG_DEBOUNCE_ENABLE_B, debounce_enable);
301}
302
304
305void SX1509Component::set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols) {
306 set_debounce_time_(time);
307 for (uint16_t i = 0; i < num_rows; i++)
309 for (uint16_t i = 0; i < (8 + num_cols); i++)
311}
312
313} // namespace sx1509
314} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
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:204
bool digital_read(uint8_t pin)
Definition sx1509.cpp:78
void set_debounce_pin_(uint8_t pin)
Definition sx1509.cpp:303
void set_debounce_config_(uint8_t config_value)
Definition sx1509.cpp:268
void digital_write(uint8_t pin, bool bit_value)
Definition sx1509.cpp:89
std::vector< SX1509KeyTrigger * > key_triggers_
Definition sx1509.h:79
void set_debounce_time_(uint8_t time)
Definition sx1509.cpp:282
void set_debounce_keypad_(uint8_t time, uint8_t num_rows, uint8_t num_cols)
Definition sx1509.cpp:305
void pin_mode(uint8_t pin, gpio::Flags flags)
Definition sx1509.cpp:112
std::vector< SX1509Processor * > keypad_binary_sensors_
Definition sx1509.h:78
void setup_led_driver(uint8_t pin)
Definition sx1509.cpp:172
void set_debounce_enable_(uint8_t pin)
Definition sx1509.cpp:296
const uint32_t min_loop_period_
Definition sx1509.h:82
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_OPEN_DRAIN
Definition gpio.h:20
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_PULLDOWN
Definition gpio.h:22
const char *const TAG
Definition spi.cpp:8
const uint8_t REG_DEBOUNCE_CONFIG
const uint8_t REG_MISC
const uint8_t REG_CLOCK
const uint8_t REG_KEY_CONFIG_2
const uint8_t REG_DATA_B
const uint8_t REG_LED_DRIVER_ENABLE_B
const uint8_t REG_KEY_CONFIG_1
const uint8_t REG_OPEN_DRAIN_B
const uint8_t REG_PULL_UP_B
const uint8_t REG_INPUT_DISABLE_B
const uint8_t REG_RESET
const uint8_t REG_DEBOUNCE_ENABLE_B
const uint8_t REG_INTERRUPT_MASK_A
const uint8_t REG_PULL_DOWN_B
const uint8_t REG_DIR_B
const uint8_t INTERNAL_CLOCK_2MHZ
Definition sx1509.h:16
const uint8_t REG_OPEN_DRAIN_A
const uint8_t REG_KEY_DATA_1
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition helpers.h:102