Software KeyScan

This sample code guide is designed to help users easily and comprehensively understand KeyScan sample. This sample demonstrates how software KeyScan works in DLPS mode.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

The EVB is connected to a matrix keyboard module, connect P2_2 to ROW0, P2_3 to ROW1, P2_4 to ROW2, P2_5 to ROW3, P0_0 to COLUMN0, and P0_1 to COLUMN1, P0_2 to COLUMN2, and P0_3 to COLUMN3. The hardware connection of KeyScan sample code is shown in the figure below.

../../../_images/Software_KeyScan_Sample_Code_Hardware_Connection_Diagram.png

KeyScan Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify the row size and column size definitions.

    • #define KEYSCAN_ROW_SIZE                4

    • #define KEYSCAN_ROW_SIZE                4

  2. The following variables can be configured to modify pin definitions.

    • static uint8_t keyscan_row_pin[KEYSCAN_ROW_SIZE] = {P2_2, P2_3, P2_4, P2_5};

    • static uint8_t keyscan_column_pin[KEYSCAN_COLUMN_SIZE] = {P0_0, P0_1, P0_2, P0_3};

  3. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    sw_keyscan_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. Press the Reset button on the EVB, when keys are pressed, KeyScan starts scan. After the scan ends, it executes the KeyScan scan end callback function and prints the key information. If keys remain pressed continuously, it will keep printing the key information.

    key_data[xx] xx, row at xx, column at xx
    
  2. After all the keys are released, print the following log.

    sw_keyscan_release_cb: All key release
    

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\keyscan\sw_keyscan\sw_keyscan_demo.c.

KeyScan Initialization

Software KeyScan initialization flow is shown in the following figure.

../../../_images/Software_KeyScan_Initialization_Flow_Chart.png

Software KeyScan Initialization Flow Chart

  1. Define the T_KEYSCAN_INIT_PARA type keyscan_init_para, and modify the keyscan_init_para parameters.

    1. Modify the row_size parameter to configure row size.

    2. Modify the column_size parameter to configure column size.

    3. Modify the keyscan_row_pins parameter to configure row pins.

    4. Modify the keyscan_column_pins parameter to configure column pins.

    5. Modify the keyscan_scan_end_cb parameter to register KeyScan scan end callback function to software KeyScan module.

    6. Modify the keyscan_release_cb parameter to register KeyScan release callback function to software KeyScan module.

  2. Call sw_keyscan_init to initialize the software KeyScan.

  3. Call sw_keyscan_filter_data_config to configure software KeyScan filter data.

DLPS Mode Initialization

  1. Call io_dlps_register() to initialize IO store/restore and do not need to worry about which IO peripheral requires specific handling.

  2. Call bt_power_mode_set() to set Bluetooth MAC deep sleep mode.

  3. Call power_mode_set() to switch the system to DLPS mode.

Functional Implementation

Callback Handle

  1. After the scan key matrix is completed, the KeyScan scan end callback is executed.

    1. Get the number of pressed keys and key data.

  2. When the release time reaches the set value, if no key is pressed, the KeyScan all release callback is executed.

    1. Print log.

      sw_keyscan_release_cb: All key release