KeyPad - Matrix Keyboard

This class provides the matrix keyboard interface.

EC600SCN_LB, EC800NCN_LA, EC600NCN_LC, EC200UCN_LB, EC600UCN_LB, EC600MCN_LA, EC800MCN_LA, EC800MCN_GA, EG912NEN_AA series module is supported this feature.

Constructor

machine.KeyPad

class machine.KeyPad(row,col)

Parameter:

  • row - Integer type. Row number. It shall be greater than 0 and cannot exceed the maximum value supported by the module.
  • col - Integer type. Column number. It shall be greater than 0. The value cannot exceed the maximum value supported by the module.

If you do not set the row and column value, the default value is 4X4.

Module Maximum Row Maximum Column
EC800N/EC600N 4 4
EC600S 5 5
EC200U 6(EC200UXXAA series only suprrort 4 Row,see Pin Correspondences) 4
EC600U 6 6
EC600M 5 5
EC800M 5 5
EG912N 3 3

KeyPad Pin Correspondences:

When part of pins are used, you shall connect the keyboard and the pin according to row and column numbers in ascending order. For example, for EC600M, when a 2x2 matrix keyboard is used, the hardware will use 49, 51 and 48, 50 pins.

Module Pin
EC600M Row number (output) and corresponding pins are as follows:
Row number 0 – pin49
Row number 1 – pin51
Row number 2 – pin53
Row number 3 – pin55
Row number 4 – pin56
Column number (input) and corresponding pins are as follows:
Column number 0 – pin48
Column number 1 – pin50
Column number 2 – pin52
Column number 3 – pin54
Column number 4 – pin57
EC800M Row number (output) and corresponding pins are as follows:
Row number 0 – pin86
Row number 1 – pin76
Row number 2 – pin85
Row number 3 – pin82
Row number 4 – pin74
Column number (input) and corresponding pins are as follows:
Column number 0 – pin87
Column number 1 – pin77
Column number 2 – pin84
Column number 3 – pin83
Column number 4 – pin75
EG912N Row number (output) and corresponding pins are as follows:
Row number 1 – pin20
Row number 2 – pin16
Row number 3 – pin116
Column number (input) and corresponding pins are as follows:
Column number 2 – pin105
Column number 3 – pin21
Column number 4 – pin1
EC200U Row number (output) and corresponding pins are as follows:
Row number 0 – pin83
Row number 1 – pin84
Row number 2 – pin113
Row number 3 – pin114
Row number 4 – pin81(EC200UXXAA series not support)
Row number 5 – pin82(EC200UXXAA series not support)
Column number (input) and corresponding pins are as follows:
Column number 0 – pin115
Column number 1 – pin78
Column number 2 – pin79
Column number 3 – pin80
EC600U Row number (output) and corresponding pins are as follows:
Row number 0 – pin105
Row number 1 – pin106
Row number 2 – pin107
Row number 3 – pin108
Row number 4 – pin104
Row number 5 – pin103
Column number (input) and corresponding pins are as follows:
Column number 0 – pin55
Column number 1 – pin129
Column number 2 – pin128
Column number 3 – pin127
Column number 4 – pin126
Column number 5 – pin125

Example:

>>> # Creates a keypad object
>>> import machine
>>> keypad=machine.KeyPad(2,3)  # Sets a matrix keyboard with 2 rows and 3 columes
>>> keypad=machine.KeyPad()     # Default parameter. The default setting is a matrix keyboard with 4 rows and 4 columns
>>> keypad=machine.KeyPad(2)    # Sets the row value to 2. Default column Value. The default column value is 4. A matrix keyboard with 2 rows and 4 columes is initialized. 

Methods

keypad.init

keypad.init()

This method initializes keypad settings.

Return Value:

0 - Successful execution

-1 - Failed execution

keypad.set_callback

keypad.set_callback(usrFun)

This method sets callback function. After the external keyboard button is connected to the module, this callback function will be triggered when the external keyboard button is pressed and released.

Parameter:

  • usrFun - Matrix keyboard callback function. Prototype:

    usrFun(result_list)
    

    Parameter of callback function:

    • result_list[0]: Key status (1 indicates the button is pressed and 0 indicates the button is released).

    • result_list[1]: Row number

    • result_list[2]: Column number

Return Value:

0 - Successful execution

-1 - Failed execution

keypad.deinit

keypad.deinit()

This method deinitializes to release initialized resources and callback function settings.

Return Value:

0 - Successful execution

-1 - Failed execution

Example:

import machine
import utime
is_loop = 1
keypad=machine.KeyPad()  
keypad.init()
def userfun(l_list):
    global is_loop 
    if  l_list[0] != 1 :
        is_loop = 0
        print('will exit')
    print(l_list)
keypad.set_callback(userfun)
loop_num = 0
while is_loop == 1 and loop_num < 10:
    utime.sleep(5)
    loop_num = loop_num +1
    print(" running..... ",is_loop,loop_num)
keypad.deinit()
print('exit!')