Reading pushbutton statuses with digital inputs and the mraa library

We will create a new PushButton class to represent a pushbutton connected to our board that can use either a pull-up or a pull-down resistor. The following lines show the code for the new PushButton class that works with the mraa library. The code file for the sample is iot_python_chapter_05_01.py.

import mraa import time from datetime import date class PushButton: def __init__(self, pin, pull_up=True): self.pin = pin self.pull_up = pull_up self.gpio = mraa.Gpio(pin) self.gpio.dir(mraa.DIR_IN) @property def is_pressed(self): push_button_status = self.gpio.read() if self.pull_up: # Pull-up resistor connected return push_button_status == 0 else: # Pull-down resistor connected return ...

Get Internet of Things with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.