Skip to main content

Five-Way Button (button)

This module manages the input of 5 buttons. It supports debouncing, short press and long press detection, automatic interrupt handling, and eliminates the need for frequent polling. It lets you register short press and long press callbacks for each button separately.

Button Layout and Hardware Connection

Button NameGPIO Pin
up18
down45
left46
right0
center44
note

All buttons use internal pull-up and are triggered on a low level (active-low press).

Class Definition

1. Button(long_press_threshold=1000, debounce_time=50)

Function: Initialize the button manager and start listening for all button inputs.

Arguments:

ArgumentTypeDescriptionDefault
long_press_thresholdintLong press threshold (ms)1000
debounce_timeintDebounce time (ms)50

Example:

from button import Button

buttons = Button(long_press_threshold=800, debounce_time=30)

2. register_callback(button_name, press_type, callback)

Function: Register a short press or long press callback for a specified button.

Arguments:

ArgumentTypeDescription
button_namestrButton name: 'up', 'down', 'left', 'right', 'center'
press_typestrEvent type: 'short' (short press), 'long' (long press)
callbackfunctionCallback function (no arguments)

Example:

def on_center_short():
print("center short pressed")

def on_left_long():
print("left long pressed")

buttons.register_callback('center', 'short', on_center_short)
buttons.register_callback('left', 'long', on_left_long)

3. get_button_state(button_name) -> bool

Function: Query whether the specified button is currently pressed.

Return value:

  • True: pressed
  • False: not pressed
  • None: invalid button name

Example:

if buttons.get_button_state('center'):
print("center is pressed")

4. deinit()

Function: Release all resources and close interrupts and timers. Call it when button control is no longer needed.

Example:

buttons.deinit()

Workflow

  • Uses hardware interrupts (IRQ) to detect button state changes;
  • Built-in debounce logic avoids false triggers from button bounce;
  • On press, starts a long press timer; when long_press_threshold is reached, triggers the long press event;
  • On release, judges the press duration; if it is below the long press threshold, triggers the short press event.

Complete Usage Example

from button import Button
import time

# Initialize the button manager
buttons = Button()

# Register short press events
buttons.register_callback('center', 'short', lambda: print("center short"))
buttons.register_callback('up', 'short', lambda: print("up short"))

# Register long press events
buttons.register_callback('center', 'long', lambda: print("center long"))
buttons.register_callback('down', 'long', lambda: print("down long"))

# Main loop (can run alongside other tasks)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
buttons.deinit()

⚠ Notes

  • This module suits embedded real-time control and avoids frequent polling.
  • You can register callbacks with lambda or custom functions.
  • It is recommended to call deinit() to release hardware resources when the system exits.

Hardware Limitations

  • The button logic level is active-low (internal pull-up).
  • The number of buttons is fixed at 5, and the GPIOs cannot be customized (unless you change the code).