Skip to content

Commit 406ef8e

Browse files
authored
Add files via upload
1 parent 04c4d49 commit 406ef8e

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

README.rst

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
MicroPython Simple Keypad
2+
=========================
3+
4+
|MicroPython| |License|
5+
6+
MicroPython library for interfacing with a keypad matrix
7+
8+
Feature
9+
-------
10+
11+
- Supports any keypad matrix configuration.
12+
- Easily customizable for different keypad layouts.
13+
- Provides exception handling for error management.
14+
15+
Example Usage
16+
-------------
17+
18+
Example for 4x4 keypad metrix on MicroPython esp32.
19+
20+
.. code:: python
21+
22+
from machine import Pin
23+
from keypad import Keypad
24+
from time import sleep
25+
26+
# Define GPIO pins for rows
27+
row_pins = [Pin(25),Pin(26),Pin(27),Pin(14)]
28+
29+
# Define GPIO pins for columns
30+
column_pins = [Pin(23),Pin(22),Pin(19),Pin(18)]
31+
32+
# Define keypad layout
33+
keys = [
34+
['1', '2', '3', 'A'],
35+
['4', '5', '6', 'B'],
36+
['7', '8', '9', 'C'],
37+
['*', '0', '#', 'D']]
38+
39+
keypad = Keypad(row_pins, column_pins, keys)
40+
41+
while True:
42+
key_pressed = keypad.read_keypad()
43+
if key_pressed:
44+
print("Key pressed:", key_pressed)
45+
sleep(0.1) # debounce and delay
46+
47+
.. |MicroPython| image:: https://img.shields.io/badge/MicroPython-Ready-brightgreen.svg
48+
.. |License| image:: https://img.shields.io/badge/License-MIT-blue.svg
49+
:target: https://opensource.org/licenses/MIT

keypad.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

setup.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import io
2+
from os.path import abspath, dirname, join
3+
import sys
4+
# Remove current dir from sys.path, otherwise setuptools will peek up our
5+
# module instead of system.
6+
sys.path.pop(0)
7+
from setuptools import setup
8+
9+
HERE = dirname(abspath(__file__))
10+
LOAD_TEXT = lambda name: io.open(join(HERE, name), encoding='UTF-8').read()
11+
DESCRIPTION = '\n\n'.join(LOAD_TEXT(_) for _ in [
12+
'README.rst'
13+
])
14+
15+
setup(
16+
name='micropython-simple-keypad',
17+
py_modules=['keypad'],
18+
version='1.0.3',
19+
description='MicroPython library for interfacing with a keypad matrix',
20+
long_description=DESCRIPTION,
21+
keywords= ['keypad', 'esp32', 'esp8266' ,'micropython'],
22+
url='https://github.com/PerfecXX/MicroPython-SimpleKeypad',
23+
author='Teeraphat Kullanankanjana',
24+
author_email='ku.teeraphat@hotmail.com',
25+
maintainer='Teeraphat Kullanankanjana',
26+
maintainer_email='ku.teeraphat@hotmail.com',
27+
license='MIT',
28+
classifiers = [
29+
'Development Status :: 3 - Alpha',
30+
'Programming Language :: Python :: Implementation :: MicroPython',
31+
'License :: OSI Approved :: MIT License',
32+
],
33+
)

0 commit comments

Comments
 (0)