The PyBBIO
library was developed with Arduino users in mind. It emulates the structure of an Arduino (http://arduino.cc) program, as well as the Arduino API where appropriate. If you've never seen an Arduino program, it consists of a setup()
function, which is called once when the program starts, and a loop()
function, which is called repeatedly until the end of time (or until you turn off the Arduino). PyBBIO
accomplishes a similar structure by defining a run()
function that is passed two callable objects, one that is called once when the program starts, and another that is called repeatedly until the program stops. So the basic PyBBIO
template looks like this:
from bbio import * def setup(): pinMode(GPIO1_16, OUTPUT) def loop(): digitalWrite(GPIO1_16, HIGH) delay(500) digitalWrite(GPIO1_16, LOW) delay(500) run(setup, loop)
The first line imports everything from the PyBBIO
library (the Python package is installed with the name bbio
). Then, two functions are defined, and they are passed to run()
, which tells the PyBBIO
loop to begin. In this example, setup()
will be called once, which configures the GPIO pin GPIO1_16 as a digital output with the pinMode()
function. Then, loop()
will be called until the PyBBIO
loop is stopped, with each digitalWrite()
call setting the GPIO1_16 pin to either a high (on) or low (off) state, and each delay()
call causing the program to sleep for 500 milliseconds. The loop can be stopped by either pressing Ctrl + C or calling the stop()
function. Any other error raised in your program will be caught, allowing PyBBIO
to run any necessary cleanup, then it will be reraised. Don't worry if the program doesn't make sense yet, we'll learn about all that soon!
Not everyone wants to use the Arduino style loop, and it's not always suitable depending on the program you're writing. PyBBIO
can also be used in a more Pythonic way, for example, the above program can be rewritten as follows:
import bbio bbio.pinMode(bbio.GPIO1_16, bbio.OUTPUT) while True: bbio.digitalWrite(bbio.GPIO1_16, bbio.HIGH) bbio.delay(500) bbio.digitalWrite(bbio.GPIO1_16, bbio.LOW) bbio.delay(500)
This still allows the bbio
API to be used, but it is kept out of the global namespace.
3.147.84.157