0
0
Raspberry Piprogramming~7 mins

Button class with callbacks in Raspberry Pi

Choose your learning style9 modes available
Introduction

A Button class with callbacks helps you run specific code when a button is pressed or released. It makes your program respond to button actions easily.

You want to turn on a light when a button is pressed.
You want to play a sound when a button is released.
You want to count how many times a button is pressed.
You want to control a robot using button presses.
You want to make a menu selection using buttons.
Syntax
Raspberry Pi
class Button:
    def __init__(self, pin):
        # Setup button on given pin
        pass

    def on_press(self, callback):
        # Set function to call when button is pressed
        pass

    def on_release(self, callback):
        # Set function to call when button is released
        pass

The __init__ method sets up the button on a specific pin.

The on_press and on_release methods let you assign functions that run when the button is pressed or released.

Examples
This example creates a button on pin 17 and prints messages when pressed or released.
Raspberry Pi
button = Button(pin=17)

button.on_press(lambda: print('Button pressed!'))
button.on_release(lambda: print('Button released!'))
Here, a named function greet is called when the button is pressed.
Raspberry Pi
def greet():
    print('Hello!')

button = Button(pin=4)
button.on_press(greet)
Sample Program

This program creates a Button object and assigns print functions to run when the button is pressed and released. It then simulates pressing and releasing the button.

Raspberry Pi
import time

class Button:
    def __init__(self, pin):
        self.pin = pin
        self._press_callback = None
        self._release_callback = None
        self._pressed = False

    def on_press(self, callback):
        self._press_callback = callback

    def on_release(self, callback):
        self._release_callback = callback

    # Simulate button press
    def press(self):
        if not self._pressed:
            self._pressed = True
            if self._press_callback:
                self._press_callback()

    # Simulate button release
    def release(self):
        if self._pressed:
            self._pressed = False
            if self._release_callback:
                self._release_callback()

# Example usage
button = Button(pin=21)

button.on_press(lambda: print('Button was pressed!'))
button.on_release(lambda: print('Button was released!'))

# Simulate button actions
button.press()
time.sleep(0.5)
button.release()
OutputSuccess
Important Notes

Callbacks are functions you give to the Button class to run later when events happen.

In real Raspberry Pi projects, you connect the button to a GPIO pin and detect physical presses.

This example simulates button presses for easy testing without hardware.

Summary

A Button class with callbacks lets your program react to button presses and releases.

You assign functions to run on press or release using on_press and on_release.

This makes your code cleaner and easier to manage for button actions.