Hardware Reference
In-Depth Information
Controlling the hardware with pyBBIO
We installed the
Stem
library when we installed Tor; so, now we need to install the
Ada-
fruit
BBIO library by performing the following commands:
sudo apt-get install build-essential python-dev
python-setuptools python-pip python-smbus -y
sudo pip install Adafruit_BBIO
sudo pip install pyserial
The
Adafruit
library conveniently enables the device tree overlays, which are the files
that describe the hardware configuration of BBB to the kernel, at runtime. Therefore, there
is no need to manipulate the configuration via the
sysfs
as everything is handled in the
Python library. The code models each hardware component as its own Python class. The
LED modeled by the
TorFreedomLED
class is the most straightforward. We need the
LED to blink; this is accomplished by toggling the output high, sleeping the executing
thread briefly, and then toggling the output low to turn it off. To set up the GPIO, we only
need to call
GPIO.setup
, by passing in the pin and the direction:
import Adafruit_BBIO.GPIO as GPIO
class TorFreedomLED(object):
def __init__(self):
self.pin = 'P9_15'
GPIO.setup(self.pin, GPIO.OUT)
def on(self):
GPIO.output(self.pin, GPIO.HIGH)
def off(self):
GPIO.output(self.pin, GPIO.LOW)
def blink(self):
self.on()
sleep(.5)
self.off()
The LCD, controlled by the
FrontPanelDisplay
class, writes to the serial port,
/dev/ttyO4
, on BBB's UART 4 at 9600 baud. The LCD only receives information;