• No results found

Reading digital inputs with the wiring-x86 library

In document Internet of Things With Python (Page 171-174)

We created methods that we could use in both an API call and when the user presses the pushbuttons. We can process HTTP requests and run actions when the user presses pushbuttons. As we build our RESTful API with Tornado, we had to create and configure a PeriodicCallback instance to make it possible to check whether the pushbuttons are pressed every 500 milliseconds.

It is very important to take into account consistency when we add features that we can control with pushbuttons or other electronic components that interact with the board. In this case, we made sure that when the user pressed the pushbuttons and changed the brightness values for the three colors, the brightness values read with API calls were exactly the values set. We worked with object-oriented code and with the same methods, and therefore, it was easy to keep consistency.

Reading digital inputs with the wiring-x86 library

So far, we have been using the mraa library to read digital inputs. However, in the first chapter, we also installed the wiring-x86 library. We can change just a few lines of our object-oriented code to replace the mraa library with the wiring-x86 one to check whether the pushbuttons were pressed.

Chapter 5 We will take the code we wrote in the previous chapter when we created the last version of our RESTful API with the wiring-x86 library and we will use this code as a baseline to add the new features. The code file for the sample was iot_python_

chapter_04_04.py.

First, we will create a new version of the 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 wiring-x86 library. The code file for the sample is iot_python_chapter_05_03.py.

from wiringx86 import GPIOGalileoGen2 as GPIO class PushButton:

def __init__(self, pin, pull_up=True):

self.pin = pin

self.pull_up = pull_up self.gpio = Board.gpio

pin_mode = self.gpio.INPUT_PULLUP if pull_up else self.gpio.

INPUT_PULLDOWN

self.gpio.pinMode(pin, pin_mode) @property

def is_pressed(self):

push_button_status = self.gpio.digitalRead(self.pin) if self.pull_up:

def is_released(self):

return not self.is_pressed

We just needed to change a few lines from the previous code of the PushButton class, that is, the version that worked with the mraa library. The new lines that interact with the wiring-x86 library are highlighted in the previous code. The constructor, that is, the __init__ method receives the same argument as the PushButton class that worked with the mraa library. In this case, this method saves a reference to the Board.gpio class attribute in self.gpio. Then, the code determines the value of the pin_mode local variable based on the value of the pull_up parameter. If pull_up is true, the value will be self.gpio.INPUT_PULLUP and self.gpio.INPUT_PULLDOWN otherwise. Finally, the constructor calls the self.

gpio.pinMode method with the received pin as its pin argument and pin_mode as its mode argument. This way, we configure the pin to be a digital input pin with the appropriate pull-up or pull-down resistor. All the PushButton instances will save a reference to the same Board.gpio class attribute that created an instance of the GPIO class, specifically, the wiringx86.GPIOGalileoGen2 class with its debug argument set to False to avoid unnecessary debug information for the low-level communications.

The is_pressed property calls the digitalRead method for the GPIO instance (self.gpio) to set retrieve the digital value for the pin configured as a digital input.

The self.pin attribute specifies the pin value for the analogRead method call. The rest of the code for the is_pressed property and the PushButton class remains the same as the version that works with the mraa library.

Then, it is necessary to make the same edits we made in the previous example to create the new version of the BoardInteraction class, add the PutMinBrightnessHandler and PutMaxBrightnessHandler classes, create the tornado.web.Application instance and the new version of the __main__ method that created and configured the PeriodicCallback instance. Thus, the rest of the code for our RESTful API remains the same one that we have used for the previous example. There is no need to make changes to the rest of the code because it will automatically work with the new PushButton class and there were no changes in the arguments for its constructor or its properties.

Chapter 5 The following line will start the HTTP server and our new version of the RESTful API that works with the wiring-x86 library. Don't forget that you need to transfer the Python source code file to the Yocto Linux with an SFTP client, as explained in the previous chapter.

python iot_python_chapter_05_03.py

We can press the pushbuttons and then make the same HTTP requests we made in our previous example to check that we can achieve exactly the same results with the wiring-x86 library.

Using interrupts to detect pressed

In document Internet of Things With Python (Page 171-174)