support keypads with pulldowns (#5404)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2023-09-20 15:26:36 -07:00 committed by Jesse Hills
parent d7e267eca5
commit 5b46088ae4
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
4 changed files with 18 additions and 6 deletions

View file

@ -21,6 +21,7 @@ CONF_COLUMNS = "columns"
CONF_KEYS = "keys" CONF_KEYS = "keys"
CONF_DEBOUNCE_TIME = "debounce_time" CONF_DEBOUNCE_TIME = "debounce_time"
CONF_HAS_DIODES = "has_diodes" CONF_HAS_DIODES = "has_diodes"
CONF_HAS_PULLDOWNS = "has_pulldowns"
def check_keys(obj): def check_keys(obj):
@ -45,6 +46,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_KEYS): cv.string, cv.Optional(CONF_KEYS): cv.string,
cv.Optional(CONF_DEBOUNCE_TIME, default=1): cv.int_range(min=1, max=100), cv.Optional(CONF_DEBOUNCE_TIME, default=1): cv.int_range(min=1, max=100),
cv.Optional(CONF_HAS_DIODES): cv.boolean, cv.Optional(CONF_HAS_DIODES): cv.boolean,
cv.Optional(CONF_HAS_PULLDOWNS): cv.boolean,
} }
), ),
check_keys, check_keys,
@ -69,3 +71,5 @@ async def to_code(config):
cg.add(var.set_debounce_time(config[CONF_DEBOUNCE_TIME])) cg.add(var.set_debounce_time(config[CONF_DEBOUNCE_TIME]))
if CONF_HAS_DIODES in config: if CONF_HAS_DIODES in config:
cg.add(var.set_has_diodes(config[CONF_HAS_DIODES])) cg.add(var.set_has_diodes(config[CONF_HAS_DIODES]))
if CONF_HAS_PULLDOWNS in config:
cg.add(var.set_has_pulldowns(config[CONF_HAS_PULLDOWNS]))

View file

@ -11,11 +11,16 @@ void MatrixKeypad::setup() {
if (!has_diodes_) { if (!has_diodes_) {
pin->pin_mode(gpio::FLAG_INPUT); pin->pin_mode(gpio::FLAG_INPUT);
} else { } else {
pin->digital_write(true); pin->digital_write(!has_pulldowns_);
} }
} }
for (auto *pin : this->columns_) for (auto *pin : this->columns_) {
if (has_pulldowns_) {
pin->pin_mode(gpio::FLAG_INPUT);
} else {
pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP); pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
}
}
} }
void MatrixKeypad::loop() { void MatrixKeypad::loop() {
@ -28,9 +33,9 @@ void MatrixKeypad::loop() {
for (auto *row : this->rows_) { for (auto *row : this->rows_) {
if (!has_diodes_) if (!has_diodes_)
row->pin_mode(gpio::FLAG_OUTPUT); row->pin_mode(gpio::FLAG_OUTPUT);
row->digital_write(false); row->digital_write(has_pulldowns_);
for (auto *col : this->columns_) { for (auto *col : this->columns_) {
if (!col->digital_read()) { if (col->digital_read() == has_pulldowns_) {
if (key != -1) { if (key != -1) {
error = true; error = true;
} else { } else {
@ -39,7 +44,7 @@ void MatrixKeypad::loop() {
} }
pos++; pos++;
} }
row->digital_write(true); row->digital_write(!has_pulldowns_);
if (!has_diodes_) if (!has_diodes_)
row->pin_mode(gpio::FLAG_INPUT); row->pin_mode(gpio::FLAG_INPUT);
} }

View file

@ -28,6 +28,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
void set_keys(std::string keys) { keys_ = std::move(keys); }; void set_keys(std::string keys) { keys_ = std::move(keys); };
void set_debounce_time(int debounce_time) { debounce_time_ = debounce_time; }; void set_debounce_time(int debounce_time) { debounce_time_ = debounce_time; };
void set_has_diodes(int has_diodes) { has_diodes_ = has_diodes; }; void set_has_diodes(int has_diodes) { has_diodes_ = has_diodes; };
void set_has_pulldowns(int has_pulldowns) { has_pulldowns_ = has_pulldowns; };
void register_listener(MatrixKeypadListener *listener); void register_listener(MatrixKeypadListener *listener);
@ -37,6 +38,7 @@ class MatrixKeypad : public key_provider::KeyProvider, public Component {
std::string keys_; std::string keys_;
int debounce_time_ = 0; int debounce_time_ = 0;
bool has_diodes_{false}; bool has_diodes_{false};
bool has_pulldowns_{false};
int pressed_key_ = -1; int pressed_key_ = -1;
std::vector<MatrixKeypadListener *> listeners_{}; std::vector<MatrixKeypadListener *> listeners_{};

View file

@ -667,6 +667,7 @@ matrix_keypad:
- pin: 17 - pin: 17
- pin: 16 - pin: 16
keys: "1234" keys: "1234"
has_pulldowns: true
key_collector: key_collector:
- id: reader - id: reader