From e63af64ee789d2cd78c45b8a7de7403619e76ca7 Mon Sep 17 00:00:00 2001 From: dahanzimin <353767514@qq.com> Date: Thu, 29 May 2025 17:19:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=94=AF=E6=8C=81=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E6=8C=89=E9=94=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../micropython/origin/build/lib/keypad.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 boards/default_src/micropython/origin/build/lib/keypad.py diff --git a/boards/default_src/micropython/origin/build/lib/keypad.py b/boards/default_src/micropython/origin/build/lib/keypad.py new file mode 100644 index 00000000..abc94c29 --- /dev/null +++ b/boards/default_src/micropython/origin/build/lib/keypad.py @@ -0,0 +1,49 @@ +""" +Simple Keypad + +Micropython library for the Simple Keypad +======================================================= + +#Based on Author: 'Teeraphat Kullanankanjana' +@dahanzimin From the Mixly Team +""" + +from machine import Pin +from time import sleep + +class Keypad: + def __init__(self, row_pins, column_pins, keys): + """Initialize the keypad object.""" + if not all(isinstance(pin, Pin) for pin in row_pins): + raise ValueError("Row pins must be instances of Pin.") + + if not all(isinstance(pin, Pin) for pin in column_pins): + raise ValueError("Column pins must be instances of Pin.") + + if not isinstance(keys, list) or not all(isinstance(row, list) for row in keys): + raise ValueError("Keys must be a 2D list.") + + self.row_pins = row_pins + self.column_pins = column_pins + self.keys = keys + + for pin in self.row_pins: + pin.init(Pin.IN, Pin.PULL_UP) + + for pin in self.column_pins: + pin.init(Pin.OUT) + + if len(self.row_pins) > len(self.keys) or len(self.column_pins) > len(self.keys[0]): + raise ValueError("Number of row/column pins does not match the key layout size.") + + def read_keypad(self): + """Read the keypad and return the pressed key.""" + for col_pin in self.column_pins: + col_pin.value(0) + for i, row_pin in enumerate(self.row_pins): + if not row_pin.value(): + key_pressed = self.keys[i][self.column_pins.index(col_pin)] + col_pin.value(1) + return key_pressed + col_pin.value(1) + return None