|
| 1 | +from machine import Pin |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +__version__ = '1.0.3' |
| 5 | +__author__ = 'Teeraphat Kullanankanjana' |
| 6 | + |
| 7 | +class KeypadException(Exception): |
| 8 | + """ |
| 9 | + Exception class for keypad-related errors. |
| 10 | + """ |
| 11 | + pass |
| 12 | + |
| 13 | +class Keypad: |
| 14 | + def __init__(self, row_pins, column_pins, keys): |
| 15 | + """ |
| 16 | + Initialize the keypad object. |
| 17 | +
|
| 18 | + Args: |
| 19 | + row_pins (list): List of row pins. |
| 20 | + column_pins (list): List of column pins. |
| 21 | + keys (list): 2D list representing the key layout. |
| 22 | +
|
| 23 | + Raises: |
| 24 | + KeypadException: If pins or keys are not properly defined. |
| 25 | + """ |
| 26 | + if not all(isinstance(pin, Pin) for pin in row_pins): |
| 27 | + raise KeypadException("Row pins must be instances of Pin.") |
| 28 | + |
| 29 | + if not all(isinstance(pin, Pin) for pin in column_pins): |
| 30 | + raise KeypadException("Column pins must be instances of Pin.") |
| 31 | + |
| 32 | + if not isinstance(keys, list) or not all(isinstance(row, list) for row in keys): |
| 33 | + raise KeypadException("Keys must be a 2D list.") |
| 34 | + |
| 35 | + self.row_pins = row_pins |
| 36 | + self.column_pins = column_pins |
| 37 | + self.keys = keys |
| 38 | + |
| 39 | + for pin in self.row_pins: |
| 40 | + pin.init(Pin.IN, Pin.PULL_UP) |
| 41 | + |
| 42 | + for pin in self.column_pins: |
| 43 | + pin.init(Pin.OUT) |
| 44 | + |
| 45 | + if len(self.row_pins) != len(self.keys) or len(self.column_pins) != len(self.keys[0]): |
| 46 | + raise KeypadException("Number of row/column pins does not match the key layout size.") |
| 47 | + |
| 48 | + def read_keypad(self): |
| 49 | + """ |
| 50 | + Read the keypad and return the pressed key. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + str or None: Pressed key or None if no key is pressed. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + KeypadException: If pins or keys are not properly defined. |
| 57 | + """ |
| 58 | + if not self.column_pins: |
| 59 | + raise KeypadException("No column pins defined.") |
| 60 | + |
| 61 | + if not self.row_pins: |
| 62 | + raise KeypadException("No row pins defined.") |
| 63 | + |
| 64 | + if not self.keys: |
| 65 | + raise KeypadException("No key layout defined.") |
| 66 | + |
| 67 | + for col_pin in self.column_pins: |
| 68 | + col_pin.value(0) # Set column pin to LOW |
| 69 | + for i, row_pin in enumerate(self.row_pins): |
| 70 | + if not row_pin.value(): # If row pin reads LOW |
| 71 | + key_pressed = self.keys[i][self.column_pins.index(col_pin)] |
| 72 | + col_pin.value(1) # Set column pin back to HIGH |
| 73 | + return key_pressed |
| 74 | + col_pin.value(1) # Set column pin back to HIGH |
| 75 | + return None # Return None if no key is pressed |
0 commit comments