Wednesday, December 5, 2012

Fake RPi.GPIO

The PYPTUG python workshops are about bridging the virtual and the physical, using Python. Workshop #01 and the following, #02, pretty much require a Raspberry Pi to follow.

But what if you don't have one? The workshops are also designed to learn about electronics and about Python, so all is not lost. In fact, the Python techniques you will learn can be applied to a wide gamut of problems.

Still, wouldn't it be nice if you could run the code on your laptop? I decided to do something about it.

Meet Fake RPi.GPIO


It implements a setmode(), setup(), cleanup(), input(), output(), tracks 54 GPIO states and directions, broadcom and board modes, placeholders for the 4 set_*_event() functions, has minimal error handling. If there is demand for it, I'll expand it. There is no way to simulate the press of a button on the GPIOs right now.

If you need to install mercurial first:

debian based
$ sudo apt-get install mercurial

or fedora
$ sudo yum install mercurial

Under solaris, it is available under the packagemanager. On windows or mac you can get it here: http://mercurial.selenic.com/

Assuming you already have mercurial:


$ hg clone https://bitbucket.org/fdion/fablocker

This will pull in all the PyHack workshops up to now. We are interested in the 2nd workshop.

You will now have a folder named fablocker/PyHack/workshop02

There is a test.py that imports RPi.GPIO. Since there is a directory named RPi and under that, a python file named GPIO.py, it will import this GPIO instead. To try it, just do python test.py (fake RPi.GPIO doesn't require sudo like the real deal). On a real Raspberry Pi, just rename the folder so it is no longer found by Python.

The subprojects for workshop #02 will be added to the repository a short time after the workshop. I'll let you know.

Updating the repository


As always, if you already cloned the fablocker repository, then you will need to:


pi@raspberrypi ~/bitbucket $cd fablocker
pi@raspberrypi ~/bitbucket/fablocker $ hg pull http://bitbucket.org/fdion/fablocker
real URL is https://bitbucket.org/fdion/fablocker
pulling from http://bitbucket.org/fdion/fablocker
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 5 changes to 5 files
(run 'hg update' to get a working copy)
pi@raspberrypi ~/bitbucket/fablocker $ hg update
5 files updated, 0 files merged, 0 files removed, 0 files unresolved
pi@raspberrypi ~/bitbucket/fablocker $ 

2 comments:

Unknown said...

I could not find the RPi.GPIO fake package in your archive !
Is it available somewhere ?

erndog said...

In the workshop02 directory there is a directory named RPi. In that directory there is a a file called GPIO.py. If you are using Python 3 you will need to edit a couple of the print statements as the brackets are missing.