Loading...
Searching...
No Matches

Matrix keypad driver for row/column keypads. More...

Detailed Description

Matrix keypad driver for row/column keypads.

This module implements a simple matrix keypad driver where keys are connected between GPIO columns and rows. It works best with diodes in series with the switches to prevent key ghosting, but it can be used without these diodes. CONFIG_MATRIX_KEYPAD_ROWS_USE_OPEN_DRAIN can be enabled when the keypad doesn't use diodes in the switches.

The keypad works by configuring the column GPIOs as input with pull-ups. Each row is configured as open drain with pull-up. One by one the rows are set to pull their output low. For each row the column GPIOs are read and the state is checked. When a key is pressed the column GPIO of that switch will read low as soon as the row it is on is pulled low.

The debouncing algorithm is a pattern style debounce where the switch must be in one position for a number of samples, then a set of "don't care" samples and then in the other position for a number of samples. The samples in the middle allow for a period where the switch can be either low or high without affecting the transition. The exact pattern is determined by CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_BEGIN and CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_END. These are used as mask where the switch must be in a determined state. The bits where neither pattern is set is used as the "don't care" set of samples.

A full scan of the matrix keypad is done via the matrix_keypad_scan function. This function iterates over all rows and columns to update the stored history of every pin.

When a state change is detected on a switch, the matrix_keypad_cb_t callback is called with the row and column number together with the new state of the switch (pressed or not pressed).

Files

file  matrix_keypad.h
 Interface definition for the matrix keypad.
 
file  matrix_keypad_params.h
 Default configuration.
 

Data Structures

struct  matrix_keypad_params_t
 Device initialization parameters. More...
 
struct  matrix_keypad_t
 Device descriptor for the driver. More...
 

Macros

#define CONFIG_MATRIX_KEYPAD_NUM_ROWS   2
 Maximum number of rows.
 
#define CONFIG_MATRIX_KEYPAD_NUM_COLUMNS   2
 Maximum number of columns.
 
#define CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_BEGIN   0xC0
 Debounce pattern high to low bits.
 
#define CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_END   0x7
 Debounce pattern low to high bits.
 
#define CONFIG_MATRIX_KEYPAD_ROWS_USE_OPEN_DRAIN   0
 Use open drain GPIO mode.
 
#define MATRIX_KEYPAD_ROWS_GPIO_MODE   GPIO_OUT
 GPIO mode used for the row GPIOs.
 
#define MATRIX_KEYPAD_DEBOUNCE_MASK    (CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_BEGIN | CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_END)
 Debounce pattern mask.
 

Typedefs

typedef uint8_t matrix_keypad_state_row_t
 Type definition for a full row (all columns) state, variable width depending on the number of columns.
 
typedef void(* matrix_keypad_cb_t) (void *arg, size_t row, size_t column, bool state)
 Callback for key state changes.
 

Functions

int matrix_keypad_init (matrix_keypad_t *dev, const matrix_keypad_params_t *params, matrix_keypad_cb_t callback, void *arg)
 Initialize the given device.
 
size_t matrix_keypad_scan (matrix_keypad_t *dev)
 Scan through the keypad matrix.
 

Macro Definition Documentation

◆ CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_BEGIN

#define CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_BEGIN   0xC0

Debounce pattern high to low bits.

This pattern should consist of bits set on the most significant bits of the value. The number of bits set determines the number of scans the key must be in the current state before a change is processed.

Definition at line 85 of file matrix_keypad.h.

◆ CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_END

#define CONFIG_MATRIX_KEYPAD_DEBOUNCE_PATTERN_END   0x7

Debounce pattern low to high bits.

This pattern should consist of bits set on the least significant bits of the value. The number of bits set determines the number of scans the key must be in the next state before a change is processed.

Definition at line 96 of file matrix_keypad.h.

◆ CONFIG_MATRIX_KEYPAD_NUM_COLUMNS

#define CONFIG_MATRIX_KEYPAD_NUM_COLUMNS   2

Maximum number of columns.

Definition at line 74 of file matrix_keypad.h.

◆ CONFIG_MATRIX_KEYPAD_NUM_ROWS

#define CONFIG_MATRIX_KEYPAD_NUM_ROWS   2

Maximum number of rows.

Definition at line 67 of file matrix_keypad.h.

◆ CONFIG_MATRIX_KEYPAD_ROWS_USE_OPEN_DRAIN

#define CONFIG_MATRIX_KEYPAD_ROWS_USE_OPEN_DRAIN   0

Use open drain GPIO mode.

Definition at line 103 of file matrix_keypad.h.

◆ MATRIX_KEYPAD_DEBOUNCE_MASK

Debounce pattern mask.

Definition at line 118 of file matrix_keypad.h.

◆ MATRIX_KEYPAD_ROWS_GPIO_MODE

#define MATRIX_KEYPAD_ROWS_GPIO_MODE   GPIO_OUT

GPIO mode used for the row GPIOs.

Definition at line 112 of file matrix_keypad.h.

Typedef Documentation

◆ matrix_keypad_cb_t

typedef void(* matrix_keypad_cb_t) (void *arg, size_t row, size_t column, bool state)

Callback for key state changes.

Parameters
argcallback context
rowRow that changed
columnColumn that changed
stateNew state of the key, 1 = pressed, 0 = released

Definition at line 166 of file matrix_keypad.h.

◆ matrix_keypad_state_row_t

typedef uint8_t matrix_keypad_state_row_t

Type definition for a full row (all columns) state, variable width depending on the number of columns.

Definition at line 126 of file matrix_keypad.h.

Function Documentation

◆ matrix_keypad_init()

int matrix_keypad_init ( matrix_keypad_t dev,
const matrix_keypad_params_t params,
matrix_keypad_cb_t  callback,
void *  arg 
)

Initialize the given device.

Parameters
[in,out]devDevice descriptor of the driver
[in]paramsInitialization parameters
[in]callbackCallback to call on state changes
[in]argContext argument for the callback
Returns
0 on success

◆ matrix_keypad_scan()

size_t matrix_keypad_scan ( matrix_keypad_t dev)

Scan through the keypad matrix.

This updates the state of the device descriptor, calling matrix_keypad_cb_t when a key press or release has been detected

Parameters
[in,out]devDevice descriptor of the driver
Returns
Number of keys that changed state