One of the great advantages of working with Python as our programming language to interact with the board is that we have plenty of packages available for Python.
We have been using the mraa library to interact with the digital outputs. However, in the previous 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 turn on and off the LEDs.
The following lines shows the code for a Board class followed by the new version of the Led class that works with the wiring-x86 library instead of using mraa. The code file for the sample is iot_python_chapter_03_07.py.
from wiringx86 import GPIOGalileoGen2 as GPIO import time
class Board:
gpio = GPIO(debug=False)
class Led:
def __init__(self, pin, position):
self.pin = pin
self.position = position self.gpio = Board.gpio
self.gpio.pinMode(pin, self.gpio.OUTPUT) def turn_on(self):
self.gpio.digitalWrite(self.pin, self.gpio.HIGH)
print("I've turned on the LED connected to GPIO Pin #{0}, in position {1}.".format(self.pin, self.position))
def turn_off(self):
self.gpio.digitalWrite(self.pin, self.gpio.LOW)
print("I've turned off the LED connected to GPIO Pin #{0}, in position {1}.".format(self.pin, self.position))
The wiring-x86 library doesn't include automatic detection of the board, and therefore, it is necessary to use the class that represents our board. The GPIOGalileoGen2 represents the Intel Galileo Gen 2 board, and therefore, the first line of code uses an import statement to import it as GPIO from wiringx86. This way, whenever we reference GPIO, we will be really using wiringx86.
GPIOGalileoGen2. Notice that the library name is wiring-x86 but the module name is wiringx86.
When we create an instance of the Led class, we have to specify the GPIO digital pin to which the LED is connected and the position in the breadboard, that is the LED number in the breadboard. The constructor, that is, the __init__ method, saves a reference to the Board.gpio class attribute in self.gpio and calls its pinMode method with the received pin as its pin argument and self.gpio.OUTPUT as its mode argument. This way, we configure the pin to be an output pin. All the Led 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 turn_on method calls the digitalWrite method for the GPIO instance to send a high value (self.GPIO.HIGH) to the pin specified by the self.pin attribute value and prints a message about the performed action.
The turn_off method calls the digitalWrite method for the GPIO instance to send a low value (self.GPIO.LOW) to the pin specified by the self.pin attribute value and prints a message about the performed action.
The code for the NumberInLeds class remains the same one that we have used for the previous example. There is no need to make changes to this class because it will automatically work with the new Led class and there were no changes in the arguments for its constructor or its two methods. We just need to replace the lines that printed information about the mraa library in the __main__ method because we aren't using the mraa library anymore.
The following lines shows the code for the NumberInLeds class and the __main__
method. The code file for the sample is iot_python_chapter_03_07.py. class NumberInLeds:
Chapter 3 def print_number(self, number):
print("==== Turning on {0} LEDs ====".format(number))
print ("Working with wiring-x86 on Intel Galileo Gen 2") number_in_leds = NumberInLeds()
# Count from 0 to 9 for i in range(0, 10):
number_in_leds.print_number(i) time.sleep(3)
We just needed to change a few lines of code and we can see how the Python code makes LEDs in the breadboard count from 0 to 9 using the wiring-x86 library. The way in which we work with the GPIO pins for digital output with this library is a bit different from the mechanism used in the mraa library. However, we could easily encapsulate the changes by taking advantage of Python's object-oriented features.
We can decide which library is more convenient for our projects based on our preferences and needs. It is always a nice idea to have more than just one option.
Test your knowledge
1. When we send a high value (1) to a GPIO pin configured as output, the GPIO pin will have:
1. 0 V.
2. 6 V.
3. The voltage specified in the position in which the IOREF jumper is located.
2. An instance of the mraa.Gpio class represents:
1. A single GPIO pin in the board.
2. All the I/O pins in the board.
3. Two GPIO pins in the board.
3. When we create an instance of the mraa.Gpio class, we must specify:
1. The pin number as an argument.
2. The specific board and a pin number as arguments.
3. The pin number and the desired direction: mraa.DIR_OUT or mraa.
DIR_IN.
4. Which of the following lines write a high value to the GPIO pin configured as output with the instance of mraa.Gpio named gpio10:
1. gpio10.write(0) 2. gpio10.write(1)
3. gpio10.write(mraa.HIGH_VALUE)
5. Which of the following lines configure the instance of mraa.Gpio named gpio10 for digital output:
1. gpio10.dir(mraa.DIR_DIGITAL).out() 2. gpio10.dir(mraa.DIR_OUT)
3. gpio10.dir(mraa.DIR_OUT, mraa.DIGITAL)
Summary
In this chapter, we worked with Python with two different libraries: mraa and wiring-x86. We connected LEDs and resistors to a breadboard and we wrote code to turn on from 0 to 9 LEDs. We improved our Python code to take advantage of Python's object-oriented features and we prepared the code to make it easy to build an API that will allow us to print numbers with LEDs with a REST API.
Now that we finished our first wirings and we started controlling the board with Python, we can start working with additional outputs and combine them with a REST API, which is the topic of the next chapter.