Jim's
Tutorials

Fall 2012
course
navigation

PyBBIO

I've spent the semester continuing work on PyBBIO, my Python library for Arduino-style hardware IO support on the BeagleBone. I've made a lot of progress in way of the PyBBIO's structure and control flow, fixing bugs reported through Github's issue tracker, and building up API and extra libraries.
There are two ways in which PyBBIO can be used; it can either be imported into a *.py file and its control loop invoked by calling the run() function, or it can be imported directly into the Python interactive interpreter. There is a some amount of required initialization and cleanup when accessing IO modules (e.g. enabling/disabling module clocks, etc.), so this has to be handled automatically in either case. Here is the code which handles both the interactive initialization and cleanup, as well as the Arduino-style 'main loop':
# The following code detects if Python is running interactively, # and if so initializes PyBBIO on import and registers PyBBIO's # cleanup to be called at exit, otherwise it defines the run() and # stop() methods for the file based control flow: import __main__ if not hasattr(__main__, '__file__'): # We're in the interpreter, see: # http://stackoverflow.com/questions/2356399/tell-if-python-is-in-interactive-mode bbio_init() print "PyBBIO initialized" import atexit def interactive_cleanup(): bbio_cleanup() print "Finished PyBBIO cleanup" atexit.register(interactive_cleanup) else: # Imported in a Python file, define run() and stop(): def run(setup, loop): """ The main loop; must be passed a setup and a loop function. First the setup function will be called once, then the loop function wil be called continuously until a stop signal is raised, e.g. CTRL-C or a call to the stop() function from within the loop. """ try: bbio_init() setup() while (True): loop() except KeyboardInterrupt: # Manual exit signal, clean up and exit happy bbio_cleanup() except Exception, e: # Something may have gone wrong, clean up and re-raise exception bbio_cleanup() raise e def stop(): """ Preffered way for a program to stop itself. """ raise KeyboardInterrupt # Expected happy stop condition in run()
As PyBBIO's users have started to grow in numbers, I've realized just how valuable Github's issue tracking is (and issue tracking in general). Most of the issues that have been reported have had to do with inconsistencies between Python builds on different versions of the BeagleBone's Angstrom Linux image. Though annoying, through sorting these issues out I've been able to build up an install script which can elegantly handle them, and even patches certain 'broken' Python builds which are missing one of the core modules for compiling Python files. (PyBBIO's closed issues here.)
The area I've made the most progress in is building the core API for the different IO modules, as well as additional importable libraries for specific tasks and external components. At the time of writing this, the core API covers basic GPIO, the analog-to-digital converter, pulse width modulation, serial, and most recently a simple software SPI implementation (the BeagleBone's AM3359 processor includes hardware SPI which I plan to add support for soon). I've also implemented extra libraries for the ADS786x series ADCs, the DACx311 series DACs, the MAX31855 thermocouple amplifier, servo motors, a library for creating simple custom web interfaces, and a couple other small utility libraries.
The full API documentation can be found here.
My latest addition to PyBBIO was a pinout reference for the BeagleBone's expansion headers, which shows all of the pins which are supported by PyBBIO. I'll continue to add to the image as I add support for more IO modules.
Links: