Skip to main content

An event-based Python keypad library for the Raspberry Pi

Project description

keypad-io

An event-based Python keypad library for the Raspberry Pi

Features

  • Allows for secondary characters for telephone styled keypads
  • Event-based architecture via callback/listener functions
  • Allows for key detection via interruptions or polling
  • Can support arbitrarily sized keypads
  • Allows for custom GPIO numbering choice
  • Dependent only on RPi.GPIO
  • Project is fully typed to reduce ambiguity and confusion during use
  • Can be used with context managers for consistent clean-up

Installation

keypad-io is available on PyPi and can be installed using:

pip install keypad-io

Requirements

keypad-io only has the following dependencies:

  • >= Python 3.11
  • RPi.GPIO

Usage

# Not needed, but convenient to keep example application alive
import signal

try:
  """
  keypad-io can work with your existing RPi.GPIO setup
  given input and output pins don't conflict with other uses

  It will also follow your existing GPIO numbering
  """
  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BCM)
  
  input_pins = #specify your input pins (these usually are called "Column" pins)
  output_pins = #specify your output pins (these usually are called "Row" pins)

  """
  Callback function to be invoked when a key press is detected
  """
  def key_l(c: str, out: int, inp: int, c_index: int):
    """
    c (str) - the character that was entered
    out (int) - the output/row pin that was detected with this key press
    inp (int) - the input/column pin that was detected with this key press
    c_index (int) - the index of "c" from the keypad's character mapping (see below)
    """
    print(" ==> received: ", c, f" ({out}, {inp})")
  
  """
  Character mapping between input and output pin positions with their expected
  characters. 

  Note: See that there are multiple choices for the second row, second column key.
  This means if that key is pressed multiple times sequentially (within a set amount of miliseconds)
  it will return the next key of choice.

  (ex: '8' at the first key press, 'T' at the second, etc.. and wrapping back to '8')
  """
  char_map = [
    ['*', '0', '#'], 
    ['7', ('8', "T", "U", "V"), '9'], 
    ['4', '5', '6'], 
    ['1', '2', '3']
  ]

  """
  Creates a Keypad with out set input_pins and output_pins, as well as character mapping.
  By giving poll=True, this Keypad will rely on continually polling the provided pins
  for key presses.

  We set key_l as an initial listener, and we want characters to be detected via polling
  rather than interrupts (which is the default)
  """
  with Keypad(input_pins, output_pins, char_map, poll=True) as k:
    k.add_listener(key_l)
    signal.pause()
  
  """
  keypad will invoke RPi.GPIO.cleanup() on the given input and output pins, as well as
  removing interrupt listening if it's chosen as the detection method
  """
except KeyboardInterrupt:
  print("Application stopped!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keypad_io-0.2.0.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keypad_io-0.2.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file keypad_io-0.2.0.tar.gz.

File metadata

  • Download URL: keypad_io-0.2.0.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for keypad_io-0.2.0.tar.gz
Algorithm Hash digest
SHA256 354deec57d48052f29f16c6b03578d7c12ec784d00a03309ea8d4b147d4b8bd7
MD5 983d40f025094f8a3cd6bc61fd8a9ee8
BLAKE2b-256 2f1e9632c8b4bf6c9609e7315d53a9af83949d6826052e8eb4f86ed5bde1fa5e

See more details on using hashes here.

File details

Details for the file keypad_io-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: keypad_io-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 19.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for keypad_io-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5581213da9a4cc18f7c4f0199dea51935935d5a2df3f650d9754918dc7808dc0
MD5 501246398ff527caab112e4eca90ebea
BLAKE2b-256 f0d554401803ad5cc1f92133757b18684c4354997ec9ad328f49523419b38ccd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page