This project explores creating your own reusable class making it easier to work with a common circuit. Project 5.1 uses interrupts to take button input, this project retains the same circuit but creates a reusable class that can be used for any button.
- Creating your own python classes
Previous projects used classes to make it easier to work with external components like the ultrasonic sensor, the 74HC595 shift register and the LCD. Creating a class for a button reduces the code required to add a button to another project.
What does the button class need?
- The pin number the button will connect to.
- The function that will be called when the button is pressed.
This snippet shows the definition of the class and the 'constructor' function (__init__):
class Button:
def __init__(self, pinNum, callback):Note: notice that the
__init__function takes in a third parameterself.
This snippet shows a button on pin 13 being created.
button = Button(13, reverseGPIO)
Button(13, reverseGPIO)'constructs' a Button object. Doing so calls the__init__function. Notice that the constructor takes in two parameters, but the__init__function takes in three. Theselfparameter is not present, it's function will be explained next.
This snippet expands on the __init__ function. self is used when assigning the input parameters to variables owned by the class. self.button = Pin(pinNum, Pin.IN, Pin.PULL_UP) creates a member variable button and creates a new Pin object using the input pinName.:
class Button:
def __init__(self, pinNum, callback):
self.button = Pin(pinNum, Pin.IN, Pin.PULL_UP)
self.callback = callbackNOTE: Any function defined in this class must take
selfas the first parameter to be able to access member variables. Access to member variables must always start withself.followed by the member variable name.
This project uses the same components and circuit as Project 5.1
This project uses two files, one for the Button class and one that uses the button class to implement the On/Off light behavior.
File: 05_advanced/code/button.py
from machine import Pin
import time
class Button:
def __init__(self, pinNum, callback):
self.button = Pin(pinNum, Pin.IN, Pin.PULL_UP)
self.callback = callback
self.last_press_time = time.ticks_ms()
self.button.irq(trigger=Pin.IRQ_RISING, handler=self.button_handler)
def button_handler(self, pin):
current_time = time.ticks_ms()
delta_time = time.ticks_diff(current_time, self.last_press_time)
if delta_time > 200:
self.callback()
self.last_press_time = current_timeFile: 05_advanced/code/ButtonAndLed_OnOff_ButtonClass.py
from button import Button
from machine import Pin
led = Pin(2, Pin.OUT)
def reverseGPIO():
if led.value():
led.value(0)
else:
led.value(1)
button = Button(13, reverseGPIO)- Open Thonny →
05_advanced/code/. - Right-click
button.py→ Upload to / if they aren't already on the device. - Double-click
ButtonAndLed_OnOff_ButtonClass.py. - Click Run current script
- Classes: A combination of data and behavior that is used to encapsulate functionality for reuse and to make code easier to understand.
- Modify the
Buttonclass so debounce time is configurable.