High level interface for interacting with the KeyScan.
The KeyScan driver monitors a key matrix for actions and provides keycodes to the application for processing.
See the implementation-specific documentation for information about device-specific limits on row count, column count, and buffer depth.
Initialize a KeyScan instance using cyhal_keyscan_init, providing the pins that should be used for the rows and columns of the key matrix. Use cyhal_keyscan_read to read key actions.
See Snippet 1: KeyScan initialization for an example initialization.
This snippet initializes a KeyScan resource to scan a matrix of pins.
This snippet illustrates periodic polling for key actions
This snippet shows how to register a callback which is invoked when a key action occurs.
Data Structures | |
struct | cyhal_keyscan_action_t |
Key action description. More... | |
Macros | |
#define | CYHAL_KEYSCAN_RSLT_ERR_INVALID_PIN (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_KEYSCAN, 0)) |
An invalid pin location was specified. | |
#define | CYHAL_KEYSCAN_RSLT_ERR_INVALID_ARG (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_KEYSCAN, 1)) |
An invalid argument was provided. | |
#define | CYHAL_KEYSCAN_RSLT_ERR_INIT_FAILED (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_KEYSCAN, 2)) |
Initialization of the KeyScan hardware failed. | |
Typedefs | |
typedef void(* | cyhal_keyscan_event_callback_t) (void *callback_arg, cyhal_keyscan_event_t event) |
Handler for KeyScan event callbacks. | |
Enumerations | |
enum | cyhal_keyscan_event_t { CYHAL_KEYSCAN_EVENT_NONE = 0 , CYHAL_KEYSCAN_EVENT_ACTION_DETECTED = 1 << 0 , CYHAL_KEYSCAN_EVENT_BUFFER_FULL = 1 << 1 } |
KeyScan events. More... | |
enum | cyhal_keyscan_action_type_t { CYHAL_KEYSCAN_ACTION_PRESS , CYHAL_KEYSCAN_ACTION_RELEASE } |
Key action types. | |
Functions | |
cy_rslt_t | cyhal_keyscan_init (cyhal_keyscan_t *obj, uint8_t num_rows, const cyhal_gpio_t *rows, uint8_t num_columns, const cyhal_gpio_t *columns, const cyhal_clock_t *clock) |
Initialize the KeyScan peripheral. More... | |
void | cyhal_keyscan_free (cyhal_keyscan_t *obj) |
Deinitialize the KeyScan object and release the associated hardware resources. More... | |
cy_rslt_t | cyhal_keyscan_read (cyhal_keyscan_t *obj, uint8_t *count, cyhal_keyscan_action_t *keys) |
Reads up to the specified number of key actions. More... | |
void | cyhal_keyscan_register_callback (cyhal_keyscan_t *obj, cyhal_keyscan_event_callback_t callback, void *callback_arg) |
Register a keyscan callback handler. More... | |
void | cyhal_keyscan_enable_event (cyhal_keyscan_t *obj, cyhal_keyscan_event_t event, uint8_t intr_priority, bool enable) |
Configure KeyScan events. More... | |
cy_rslt_t | cyhal_keyscan_init_cfg (cyhal_keyscan_t *obj, const cyhal_keyscan_configurator_t *cfg) |
Initialize the KeyScan peripheral using a configurator generated configuration struct. More... | |
struct cyhal_keyscan_action_t |
Data Fields | ||
---|---|---|
uint8_t | keycode |
Code indicating which key the action applies to. Keycodes are assigned sequentially in column order. For example, in a key matrix with five rows and two columns, column 0 would be represented by keycode 0 - 4, and column 1 by keycode 5-9. |
cyhal_keyscan_action_type_t | action | The type of key action that was performd. |
cy_rslt_t cyhal_keyscan_init | ( | cyhal_keyscan_t * | obj, |
uint8_t | num_rows, | ||
const cyhal_gpio_t * | rows, | ||
uint8_t | num_columns, | ||
const cyhal_gpio_t * | columns, | ||
const cyhal_clock_t * | clock | ||
) |
Initialize the KeyScan peripheral.
[out] | obj | Pointer to a KeyScan object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | num_rows | The number of rows in the key matrix |
[in] | rows | Array of pins corresponding to the key matrix rows |
[in] | num_columns | The number of columns in the key matrix |
[in] | columns | Array of pins corresponding to the key matrix columns |
[in] | clock | Clock source to use for this instance. If NULL, a dedicated clock will be automatically allocated for this instance. |
void cyhal_keyscan_free | ( | cyhal_keyscan_t * | obj | ) |
Deinitialize the KeyScan object and release the associated hardware resources.
[in] | obj | The KeyScan object |
cy_rslt_t cyhal_keyscan_read | ( | cyhal_keyscan_t * | obj, |
uint8_t * | count, | ||
cyhal_keyscan_action_t * | keys | ||
) |
Reads up to the specified number of key actions.
[in] | obj | The KeyScan object |
[in,out] | count | The number of key action to read. Updated with the number of keys actually read. |
[out] | keys | The array into which key action descriptions should be written, starting from the least recent key action at index 0. |
void cyhal_keyscan_register_callback | ( | cyhal_keyscan_t * | obj, |
cyhal_keyscan_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a keyscan callback handler.
This function will be called when one of the events enabled by cyhal_keyscan_enable_event occurs.
[in] | obj | The KeyScan object |
[in] | callback | The callback handler which will be invoked when the event occurs |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void cyhal_keyscan_enable_event | ( | cyhal_keyscan_t * | obj, |
cyhal_keyscan_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Configure KeyScan events.
When an enabled event occurs, the function specified by cyhal_keyscan_register_callback will be called.
[in] | obj | The KeyScan object |
[in] | event | The KeyScan event type |
[in] | intr_priority | The priority for NVIC interrupt events |
[in] | enable | True to turn on the specified event, False to turn off |
cy_rslt_t cyhal_keyscan_init_cfg | ( | cyhal_keyscan_t * | obj, |
const cyhal_keyscan_configurator_t * | cfg | ||
) |
Initialize the KeyScan peripheral using a configurator generated configuration struct.
[in] | obj | Pointer to a KeyScan object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | cfg | Configuration structure generated by a configurator. |