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/EG810M 5(EG810MEU series only suprrort 3 Row,see Pin Correspondences) 5(EG810MEU series only suprrort 3 Col,see Pin Correspondences)
EG912N 3 3
EG915N 4 4

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/EG810M Row number (output) and corresponding pins are as follows:
Row number 0 – pin86(EG810MEU series not support)
Row number 1 – pin76
Row number 2 – pin85(EG810MEU series not support)
Row number 3 – pin82
Row number 4 – pin74
Column number (input) and corresponding pins are as follows:
Column number 0 – pin87(EG810MEU series not support)
Column number 1 – pin77
Column number 2 – pin84(EG810MEU series not support)
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
EG915N Row number (output) and corresponding pins are as follows:
Row number 0 – pin39
Row number 1 – pin20
Row number 2 – pin27
Row number 3 – pin26
Column number (input) and corresponding pins are as follows:
Column number 0 – pin83
Column number 2 – pin28
Column number 3 – pin25
Column number 4 – pin1

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

for example,the EC600M module, suppose the key connect pin 49 and pin 52,if press the key, the parameter of callback is [1,0,2],if release the key, the parameter of callback is [0,0,2].

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!')