zmk/app/drivers/kscan/kscan_gpio.c
Joel Spadin b276a3bfb0 refactor(kscan): batch GPIO reads by port
Changed the GPIO matrix and direct GPIO key scan drivers to do a single
read per port instead of one read per pin. This is much more efficient
for some types of GPIO drivers, such as I2C GPIO expanders.

To accomplish this with minimal overhead, we now sort input pins by port
at driver init. if we iterate through the pins in the sorted order, all
pins on the same port are consecutive, so we only need to read each port
once the first time we see it.
2023-05-28 23:33:34 -06:00

34 lines
826 B
C

/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#include "kscan_gpio.h"
#include <stdlib.h>
static int compare_ports(const void *a, const void *b) {
const struct kscan_gpio *gpio_a = a;
const struct kscan_gpio *gpio_b = b;
return gpio_a->spec.port - gpio_b->spec.port;
}
void kscan_gpio_list_sort_by_port(struct kscan_gpio_list *list) {
qsort(list->gpios, list->len, sizeof(list->gpios[0]), compare_ports);
}
int kscan_gpio_pin_get(const struct kscan_gpio *gpio, struct kscan_gpio_port_state *state) {
if (gpio->spec.port != state->port) {
state->port = gpio->spec.port;
const int err = gpio_port_get(state->port, &state->value);
if (err) {
return err;
}
}
return (state->value & BIT(gpio->spec.pin)) != 0;
}