You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
reudsh3/keyboard.py

59 lines
1.1 KiB

from machine import Pin
# 定义按键
NUM7, NUM8, NUM9, KEY_UP = range(4)
NUM4, NUM5, NUM6, KEY_DOWN = range(4, 8)
NUM1, NUM2, NUM3, KEY_OK = range(8, 12)
NUM0, KEY_L, KEY_R, KEY_CANCEL = range(12, 16)
NO_KEY = -1
# 定义键盘行和列的GPIO引脚
ROW_PINS = [
Pin(33, Pin.OUT),
Pin(34, Pin.OUT),
Pin(35, Pin.OUT),
Pin(36, Pin.OUT),
]
COLUMN_PINS = [
Pin(38, Pin.IN),
Pin(39, Pin.IN),
Pin(40, Pin.IN),
Pin(41, Pin.IN)
]
def init_keys():
# 初始化键盘行
for row in ROW_PINS:
row.value(0)
# 初始化键盘列
for col in COLUMN_PINS:
col.init(mode=Pin.IN)
def read_key():
for r, row in enumerate(ROW_PINS):
row.value(1)
for c, col in enumerate(COLUMN_PINS):
if col.value() == 1:
row.value(0)
return (4 * r) + c
row.value(0)
return NO_KEY
prev_key = NO_KEY
curr_key = NO_KEY
def read_key_on_released():
global prev_key, curr_key
curr_key = read_key()
if prev_key != NO_KEY and curr_key == NO_KEY:
p = prev_key
prev_key = NO_KEY
return p
prev_key = curr_key
return NO_KEY