This blog post is about controlling the digital io pins of the xplained board using Python.
My previous blog showed how to control the pins using standard Bash commands in the system shell / terminal. The Python method works along exactly that principle, for the time being we are going to use the standard os library in Python and use os.system() to export Bash commands to run in a subshell. In short, we can just use the Bash commands that we used before directly in our Python scripts.
For the time being, I'm going to test the functionality in the live Python interpreter, those of you familiar with Python or have read up a getting started tutorial will know that later on we can save python scripts in files and run them as a normal program.
You need to be at the Xplained command prompt either using the usb method from the getting started guide or by following my SSH blog. Once there just type "python" and the interpreter should startup in live mode and the prompt changes to 3 greater than arrows ">>>".
Start by importing the os library.
Then use the os library to issue a bash command in a subshell and get the directory listing of /sys/class/gpio
If you followed the GPIO Bash blog that I did previously, you can see that we are issuing the same command, only passing it into the os.system function instead of writing it straight out. In this way, we can issue all of the commands just like before and control the direction and value of the IO pin.
We can also do something like os.system("python my_script.py") to execute another script which can be useful to us.
I'm in the process of creating a simple GPIO library that includes the basics aswell as creating state change watches, Much of the time, especially with button pushes, were really not too concerned with the state of an input pin, rather we just want something to happen when it's state changes from high to low or low to high depending. The library is eventually going to be able to watch pin states in a seperate thread and either run a callback function if the pin changes or execute another script altogether.
Once I'm happy with a basic library, I'll post a link for it to be downloadable and write a blog on how to use it. Then it's onto LCD control, I really want a splash screen once the Xplained is booted up!