input: gpio_kbd_matrix: define a type for the row data
Add a typedef for the row type rather than using uint8_t directly, this allow supporting bigger matrix as an option by using a different type. Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
parent
65b6d2f180
commit
adcb2f580c
|
@ -64,7 +64,7 @@ static void gpio_kbd_matrix_drive_column(const struct device *dev, int col)
|
||||||
data->last_col_state = state;
|
data->last_col_state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int gpio_kbd_matrix_read_row(const struct device *dev)
|
static kbd_row_t gpio_kbd_matrix_read_row(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct gpio_kbd_matrix_config *cfg = dev->config;
|
const struct gpio_kbd_matrix_config *cfg = dev->config;
|
||||||
const struct input_kbd_matrix_common_config *common = &cfg->common;
|
const struct input_kbd_matrix_common_config *common = &cfg->common;
|
||||||
|
|
|
@ -79,7 +79,7 @@ static void it8xxx2_kbd_drive_column(const struct device *dev, int col)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int it8xxx2_kbd_read_row(const struct device *dev)
|
static kbd_row_t it8xxx2_kbd_read_row(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct it8xxx2_kbd_config *const config = dev->config;
|
const struct it8xxx2_kbd_config *const config = dev->config;
|
||||||
struct kscan_it8xxx2_regs *const inst = config->base;
|
struct kscan_it8xxx2_regs *const inst = config->base;
|
||||||
|
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(input_kbd_matrix, CONFIG_INPUT_LOG_LEVEL);
|
LOG_MODULE_REGISTER(input_kbd_matrix, CONFIG_INPUT_LOG_LEVEL);
|
||||||
|
|
||||||
#define INPUT_KBD_MATRIX_ROW_MASK UINT8_MAX
|
|
||||||
|
|
||||||
void input_kbd_matrix_poll_start(const struct device *dev)
|
void input_kbd_matrix_poll_start(const struct device *dev)
|
||||||
{
|
{
|
||||||
struct input_kbd_matrix_common_data *data = dev->data;
|
struct input_kbd_matrix_common_data *data = dev->data;
|
||||||
|
@ -28,7 +26,7 @@ void input_kbd_matrix_poll_start(const struct device *dev)
|
||||||
static bool input_kbd_matrix_ghosting(const struct device *dev)
|
static bool input_kbd_matrix_ghosting(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
||||||
const uint8_t *state = cfg->matrix_new_state;
|
const kbd_row_t *state = cfg->matrix_new_state;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Matrix keyboard designs are suceptible to ghosting.
|
* Matrix keyboard designs are suceptible to ghosting.
|
||||||
|
@ -59,7 +57,7 @@ static bool input_kbd_matrix_ghosting(const struct device *dev)
|
||||||
* using z&(z-1) which is non-zero only if z has more
|
* using z&(z-1) which is non-zero only if z has more
|
||||||
* than one bit set.
|
* than one bit set.
|
||||||
*/
|
*/
|
||||||
uint8_t common_row_bits = state[c] & state[c_next];
|
kbd_row_t common_row_bits = state[c] & state[c_next];
|
||||||
|
|
||||||
if (common_row_bits & (common_row_bits - 1)) {
|
if (common_row_bits & (common_row_bits - 1)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -74,8 +72,8 @@ static bool input_kbd_matrix_scan(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
||||||
const struct input_kbd_matrix_api *api = cfg->api;
|
const struct input_kbd_matrix_api *api = cfg->api;
|
||||||
int row;
|
kbd_row_t row;
|
||||||
uint8_t key_event = 0U;
|
kbd_row_t key_event = 0U;
|
||||||
|
|
||||||
for (int col = 0; col < cfg->col_size; col++) {
|
for (int col = 0; col < cfg->col_size; col++) {
|
||||||
api->drive_column(dev, col);
|
api->drive_column(dev, col);
|
||||||
|
@ -83,7 +81,7 @@ static bool input_kbd_matrix_scan(const struct device *dev)
|
||||||
/* Allow the matrix to stabilize before reading it */
|
/* Allow the matrix to stabilize before reading it */
|
||||||
k_busy_wait(cfg->settle_time_us);
|
k_busy_wait(cfg->settle_time_us);
|
||||||
|
|
||||||
row = api->read_row(dev) & INPUT_KBD_MATRIX_ROW_MASK;
|
row = api->read_row(dev);
|
||||||
cfg->matrix_new_state[col] = row;
|
cfg->matrix_new_state[col] = row;
|
||||||
key_event |= row;
|
key_event |= row;
|
||||||
}
|
}
|
||||||
|
@ -97,10 +95,10 @@ static void input_kbd_matrix_update_state(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
const struct input_kbd_matrix_common_config *cfg = dev->config;
|
||||||
struct input_kbd_matrix_common_data *data = dev->data;
|
struct input_kbd_matrix_common_data *data = dev->data;
|
||||||
uint8_t *matrix_new_state = cfg->matrix_new_state;
|
kbd_row_t *matrix_new_state = cfg->matrix_new_state;
|
||||||
uint32_t cycles_now;
|
uint32_t cycles_now;
|
||||||
uint8_t row_changed;
|
kbd_row_t row_changed;
|
||||||
uint8_t deb_col;
|
kbd_row_t deb_col;
|
||||||
|
|
||||||
cycles_now = k_cycle_get_32();
|
cycles_now = k_cycle_get_32();
|
||||||
|
|
||||||
|
@ -143,8 +141,8 @@ static void input_kbd_matrix_update_state(const struct device *dev)
|
||||||
|
|
||||||
/* Debouncing for each row key occurs here */
|
/* Debouncing for each row key occurs here */
|
||||||
for (int r = 0; r < cfg->row_size; r++) {
|
for (int r = 0; r < cfg->row_size; r++) {
|
||||||
uint8_t mask = BIT(r);
|
kbd_row_t mask = BIT(r);
|
||||||
uint8_t row_bit = matrix_new_state[c] & mask;
|
kbd_row_t row_bit = matrix_new_state[c] & mask;
|
||||||
|
|
||||||
/* Continue if we already debounce a key */
|
/* Continue if we already debounce a key */
|
||||||
if (!(deb_col & mask)) {
|
if (!(deb_col & mask)) {
|
||||||
|
|
|
@ -97,7 +97,7 @@ static void npcx_kbd_drive_column(const struct device *dev, int col)
|
||||||
inst->KBSOUT1 = ((mask >> 16) & 0x03);
|
inst->KBSOUT1 = ((mask >> 16) & 0x03);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int npcx_kbd_read_row(const struct device *dev)
|
static kbd_row_t npcx_kbd_read_row(const struct device *dev)
|
||||||
{
|
{
|
||||||
const struct npcx_kbd_config *config = dev->config;
|
const struct npcx_kbd_config *config = dev->config;
|
||||||
const struct input_kbd_matrix_common_config *common = &config->common;
|
const struct input_kbd_matrix_common_config *common = &config->common;
|
||||||
|
|
|
@ -29,6 +29,12 @@
|
||||||
/** Number of tracked scan cycles */
|
/** Number of tracked scan cycles */
|
||||||
#define INPUT_KBD_MATRIX_SCAN_OCURRENCES 30U
|
#define INPUT_KBD_MATRIX_SCAN_OCURRENCES 30U
|
||||||
|
|
||||||
|
/** Row entry data type */
|
||||||
|
typedef uint8_t kbd_row_t;
|
||||||
|
|
||||||
|
/** Maximum number of rows */
|
||||||
|
#define INPUT_KBD_MATRIX_ROW_BITS NUM_BITS(kbd_row_t)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Keyboard matrix internal APIs.
|
* @brief Keyboard matrix internal APIs.
|
||||||
*/
|
*/
|
||||||
|
@ -49,7 +55,7 @@ struct input_kbd_matrix_api {
|
||||||
*
|
*
|
||||||
* @param dev Pointer to the keyboard matrix device.
|
* @param dev Pointer to the keyboard matrix device.
|
||||||
*/
|
*/
|
||||||
int (*read_row)(const struct device *dev);
|
kbd_row_t (*read_row)(const struct device *dev);
|
||||||
/**
|
/**
|
||||||
* @brief Request to put the matrix in detection mode.
|
* @brief Request to put the matrix in detection mode.
|
||||||
*
|
*
|
||||||
|
@ -80,10 +86,10 @@ struct input_kbd_matrix_common_config {
|
||||||
bool ghostkey_check;
|
bool ghostkey_check;
|
||||||
|
|
||||||
/* extra data pointers */
|
/* extra data pointers */
|
||||||
uint8_t *matrix_stable_state;
|
kbd_row_t *matrix_stable_state;
|
||||||
uint8_t *matrix_unstable_state;
|
kbd_row_t *matrix_unstable_state;
|
||||||
uint8_t *matrix_previous_state;
|
kbd_row_t *matrix_previous_state;
|
||||||
uint8_t *matrix_new_state;
|
kbd_row_t *matrix_new_state;
|
||||||
uint8_t *scan_cycle_idx;
|
uint8_t *scan_cycle_idx;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -96,12 +102,12 @@ struct input_kbd_matrix_common_config {
|
||||||
* specify row and col count.
|
* specify row and col count.
|
||||||
*/
|
*/
|
||||||
#define INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(node_id, _row_size, _col_size) \
|
#define INPUT_KBD_MATRIX_DT_DEFINE_ROW_COL(node_id, _row_size, _col_size) \
|
||||||
BUILD_ASSERT(IN_RANGE(_row_size, 1, 8), "invalid row-size"); \
|
BUILD_ASSERT(IN_RANGE(_row_size, 1, INPUT_KBD_MATRIX_ROW_BITS), "invalid row-size"); \
|
||||||
BUILD_ASSERT(IN_RANGE(_col_size, 1, UINT8_MAX), "invalid col-size"); \
|
BUILD_ASSERT(IN_RANGE(_col_size, 1, UINT8_MAX), "invalid col-size"); \
|
||||||
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \
|
static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, stable_state)[_col_size]; \
|
||||||
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \
|
static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, unstable_state)[_col_size]; \
|
||||||
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \
|
static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, previous_state)[_col_size]; \
|
||||||
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \
|
static kbd_row_t INPUT_KBD_MATRIX_DATA_NAME(node_id, new_state)[_col_size]; \
|
||||||
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx)[_row_size * _col_size];
|
static uint8_t INPUT_KBD_MATRIX_DATA_NAME(node_id, scan_cycle_idx)[_row_size * _col_size];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue