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