Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2026 EGJ Moorington
#
# SPDX-License-Identifier: Unlicense

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.13
rev: v0.14.10
hooks:
- id: ruff-format
- id: ruff
- id: ruff-check
args: ["--fix"]
- id: ruff-format
- repo: https://github.com/fsfe/reuse-tool
rev: v5.0.2
rev: v6.2.0
hooks:
- id: reuse

- repo: local
hooks:
- id: readme-example-updater
name: update readme examples
language: python
entry: python scripts/update_readme_examples.py
files: (^README\.rst$)|(^examples/.*\.py$)
15 changes: 14 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,26 @@ multiple buttons.
| D9 |
+---------------+

.. include-example::: examples/button_handler_single_button.py
:language: python

.. code-block:: python

# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington
#
# SPDX-License-Identifier: Unlicense

"""
This example demonstrates simple usage of the library for a single button set-up.
"""

import time

import board
from keypad import Keys

from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
from button_handler import ButtonHandler, ButtonInput


def double_press():
Expand Down Expand Up @@ -144,6 +156,7 @@ multiple buttons.
button_handler.update()
time.sleep(0.0025)

.. /include-example:::

Documentation
=============
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# Copyright (c) 2025 EGJ Moorington
# SPDX-FileCopyrightText: Copyright (c) 2025 EGJ Moorington
#
# SPDX-License-Identifier: MIT

Expand Down
29 changes: 23 additions & 6 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ This simple script showcases the usage of this library using a single button.
| D9 |
+---------------+

.. literalinclude:: ../examples/button_handler_singlebutton.py
:caption: examples/button_handler_singlebutton.py
.. literalinclude:: ../examples/button_handler_single_button.py
:caption: examples/button_handler_single_button.py
:linenos:

Advanced single button
Expand All @@ -45,8 +45,8 @@ This advanced script demonstrates how to handle triple presses and configure a b
| D9 |
+---------------+

.. literalinclude:: ../examples/button_handler_advancedsinglebutton.py
:caption: examples/button_handler_advancedsinglebutton.py
.. literalinclude:: ../examples/button_handler_advanced_single_button.py
:caption: examples/button_handler_advanced_single_button.py
:linenos:

Double button
Expand All @@ -62,6 +62,23 @@ This script showcases the usage of this library using two buttons.
| D9 | A2 |
+-----------------+-----------------+

.. literalinclude:: ../examples/button_handler_doublebutton.py
:caption: examples/button_handler_doublebutton.py
.. literalinclude:: ../examples/button_handler_double_button.py
:caption: examples/button_handler_double_button.py
:linenos:

Advanced double button
----------------------

This advanced script demonstrates how to handle triple presses, configure buttons and handle button presses while another button is being held down.

+-----------------+-----------------+
| Button A wiring | Button B wiring |
+=================+=================+
| GND | GND |
+-----------------+-----------------+
| D9 | A2 |
+-----------------+-----------------+

.. literalinclude:: ../examples/button_handler_advanced_double_button.py
:caption: examples/button_handler_advanced_double_button.py
:linenos:
8 changes: 4 additions & 4 deletions docs/examples.rst.license
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington

SPDX-License-Identifier: MIT
SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
SPDX-FileCopyrightText: Copyright (c) 2024, 2026 EGJ Moorington
SPDX-License-Identifier: MIT
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#
# SPDX-License-Identifier: Unlicense

"""
This example demonstrates advanced usage of the library for a two button set-up.

The example shows how to:
- Handle triple presses
- Handle a button press while another is being held down
- Set configurations for buttons (e.g. multi_press_interval)
"""

import time

import board
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#
# SPDX-License-Identifier: Unlicense

#
# Demonstrates:
# - handling a triple press
# - setting configurations for buttons (e.g. multi_press_interval)
#
"""
This example demonstrates advanced usage of the library for a single button set-up.

The example shows how to:
- Handle a triple press
- Set configurations for buttons (e.g. multi_press_interval)
"""

import time

Expand Down
56 changes: 56 additions & 0 deletions examples/button_handler_double_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington
#
# SPDX-License-Identifier: Unlicense

"""
This example demonstrates simple usage of the library for a two button set-up.
"""

import time

import board
from keypad import Keys

from button_handler import ButtonHandler, ButtonInput


def double_press_0():
print("Button 0 has been double pressed!")


def double_press_1():
print("Button 1 has been double pressed!")


def short_press_0():
print("Button 0 has been pressed quickly!")


def short_press_1():
print("Button 1 has been pressed quickly!")


def long_press_0():
print("Button 0 has been pressed for a long time!")


def long_press_1():
print("Button 1 has been pressed for a long time!")


scanner = Keys([board.D9, board.A2], value_when_pressed=False)
callback_inputs = {
ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press_0),
ButtonInput(ButtonInput.DOUBLE_PRESS, 1, double_press_1),
ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press_0),
ButtonInput(ButtonInput.SHORT_PRESS, 1, short_press_1),
ButtonInput(ButtonInput.LONG_PRESS, 0, long_press_0),
ButtonInput(ButtonInput.LONG_PRESS, 1, long_press_1),
}

button_handler = ButtonHandler(scanner.events, callback_inputs, 2)

while True:
button_handler.update()
time.sleep(0.0025)
4 changes: 4 additions & 0 deletions examples/button_handler_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#
# SPDX-License-Identifier: Unlicense

"""
This simple test can be used to ensure the hardware is working properly.
"""

import time

import board
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#
# SPDX-License-Identifier: Unlicense

"""
This example demonstrates simple usage of the library for a single button set-up.
"""

import time

import board
from keypad import Keys

from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
from button_handler import ButtonHandler, ButtonInput


def double_press():
Expand Down
5 changes: 4 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2025 EGJ Moorington
# SPDX-FileCopyrightText: Copyright (c) 2025, 2026 EGJ Moorington
#
# SPDX-License-Identifier: MIT

target-version = "py38"
line-length = 100

[per-file-target-version]
"{docs,scripts,tests}/**/*.py" = "py313"

[lint]
select = ["I", "PL", "UP"]

Expand Down
Loading