RPi.GPIO wrapper. Write code once, test it anywhere, run on Raspberry Pi
I wanted to test some ideas I had in mind for my Raspberry Pi, but I didn't have it with me. So I made this really tiny library which acts as a wrapper for RPi.GPIO. You can write your code on your laptop, test it and then deploy it on your Raspberry Pi by only changing a value in a configuration file, only when it's really working.
First create a config file with this content:
{
"test":true
}
This enables the mocking. If you're executing your script on a anctual raspberry you may want to set 'test'
to false
. This transforms every call to an actual GPIO call.
Now load your configuration file:
pin.config('/path/to/config/file')
Then you are ready to interact with the library exactly how you'd do with Rpi.GPIO:
pin.setmode(pin.BCM)
pin.setup(4,pin.OUT)
pin.output(4,pin.LOW)
The file and the call to config()
are mandatory, since apparently RPi.GPIO
can't be imported on a system that's not a Raspberry Pi. So the method also imports the module if the "test"
value is false
.
You may see a pin.conf
dictionary and be tempted to set "test"
there without an external file, and without calling config()
. This way RPi.GPIO isn't imported and your code won't work on an actual Raspberry Pi. Use at your own risk.
What if you want your pin to read an input?
pin.setup(4,pin.IN)
value = pin.input(4)
This returns a random value.
If you want to control the value read you can use pin.set_value()
:
# before starting your script
pin.set_value(4,0.5)
# your script
value = pin.input(4)
This way other applications can be built. For instance a GUI that simulates a Raspberry with devices attached.
You need to install RPi.GPIO for this to work https://sourceforge.net/p/raspberry-gpio-python/wiki/install/In test mode this isn't required since RPi.GPIO isn't imported.
Tested under Python 3.6
Clone this repository, then runpython3 setup.py install
setup(channel,in_or_out,initial=pin.HIGH)
same as GPIO, works also with a list of channels. Usepin.IN
orpin.OUT
, as well aspin.HIGH
orpin.LOW
setmode(mode)
same as GPIO, usepin.BCM
orpin.BOARD
. In test mode this won't do anything.input(channel)
same as GPIO, ifpin.set_value(channel,value)
isn't called before then a random number in range [0,1] is returned. Otherwise what you set withset_value()
for that channeloutput(channel,value)
same as GPIO, writesvalue
to pinchannel
. In test mode you can retrieve what was set withpin.get_output(channel)
. You may usepin.HIGH
orpin.LOW
. Assetup()
works with a list of channelscleanup(channel)
same as GPIO, works also with a list of channels
set_value(channel,value)
every timeinput(channel)
is called,value
is returned instead of a random numberget_output(channel)
returns whateveroutput(channel,value)
wrote tochannel
Since I haven't found a way to properly and automatically test this library behaviour on a Rapsberry, either tell me how or report issues about the actual RPi.GPIO calls. What this library is trying to solve is exactly this apparent difficulty of testing Raspberry code properly.