This Raspberry Pi emulator simulates some of the functions used in the RPi.GPIO library (using python). The intention of this library is educational.
Installation
The easiest way is to download the zip file and extract the files in the same working environment of your script. To use the emulator just type the following at the beginning of your script.
from EmulatorGUI import GPIO
Simulation
This library simulates the following functions which are used in the RPi.GPIO library.
- GPIO.setmode()
- GPIO.setwarnings()
- GPIO.setup()
- GPIO.input()
- GPIO.output()
Test Example
from EmulatorGUI import GPIO #import RPi.GPIO as GPIO import time import traceback def Main(): try: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(4, GPIO.OUT) GPIO.setup(17, GPIO.OUT, initial = GPIO.LOW) GPIO.setup(18, GPIO.OUT, initial = GPIO.LOW) GPIO.setup(21, GPIO.OUT, initial = GPIO.LOW) GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_UP) GPIO.setup(15, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) GPIO.setup(24, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) GPIO.setup(26, GPIO.IN) while(True): if (GPIO.input(23) == False): GPIO.output(4,GPIO.HIGH) GPIO.output(17,GPIO.HIGH) time.sleep(1) if (GPIO.input(15) == True): GPIO.output(18,GPIO.HIGH) GPIO.output(21,GPIO.HIGH) time.sleep(1) if (GPIO.input(24) == True): GPIO.output(18,GPIO.LOW) GPIO.output(21,GPIO.LOW) time.sleep(1) if (GPIO.input(26) == True): GPIO.output(4,GPIO.LOW) GPIO.output(17,GPIO.LOW) time.sleep(1) except Exception as ex: traceback.print_exc() finally: GPIO.cleanup() #this ensures a clean exit Main()
Wonderful! Thank you for this!
LikeLiked by 1 person
This is extremely helpful and works with beautiful ease! No more than 30 seconds to get set up, clear indication of the state of the GPIOs (laid out to reflect the physical pins) and simple click-control of the input pins. Really nicely done, thank you!
LikeLiked by 1 person
Wonderful! but I am a beginner. still I could not run the emulator. Are there any requirements are needed. I tried to run it windows as well as in raspberry itself. My python version is 2.7. looking for your assist.
LikeLike
Use python 3. Latest Raspbian comes also with Python 3.
LikeLike
Hi vellarod
I installed python35. The emulator is perfect. Thank you very much.
LikeLiked by 1 person
I was wondering why you didn’t implement the cleanup function? I means that exceptions are thrown when things are run twice.
LikeLike
It is implemented but it does nothing so it’s not going to give you any exceptions. Normally GPIO.Cleanup is used at the end of the script so for the emulator you simply restart your script and cleanup is done automatically.
LikeLike
But if I wish to run tests which repeatedly set up the pins and then check that the outputs are changing as expected I get exceptions as the pins are already assigned. I have altered the code to allow me to do this, but I am trying to see if there is a way of closing the window from the code. Is there an easy way to do this? Thanks!
LikeLike
It is highly recommended to setup your pins at the start of the script and keep them set the same through the whole script. Why do you want your script to close the simulator window?
LikeLike
“Why do you want your script to close the simulator window?”
I’d like to close the simulator window (ask it to go away) because it’s preventing a clean shutdown of the script that uses it. pygame sees the ESC key press event[1], which then hits sys.exit, but unless I’m missing something the simulator thread won’t stop. At that point the script is basically frozen and has to be killed.
My ‘atm_mirror’ project runs the pi more or less in kiosk mode, but I’m trying to test/verify in simulation that ESC will make the script (and kiosk mode) quit.
[1] https://github.com/rjhornsby/atm_mirror/blob/ca9aea68aedb6a5c47d4b97a06e62c91bf7f991b/mirror.py3#L148-L153
LikeLike
try and get the current process id by python and terminate the process id by code. There are various solutions how this can be achieved: https://stackoverflow.com/questions/17856928/how-to-terminate-process-from-python-using-pid
or https://stackoverflow.com/questions/37344987/get-a-pid-number-of-a-process-and-than-kill-it-with-python
LikeLike
There may be a better way, but I solved this by changing the cleanup method in EmulatorGUI.py to:
def cleanup():
global app
app.root.destroy()
@vellarod: Thanks very much for this! It’s been incredibly useful while developing software for the Pi.
LikeLike
Hello there, what about getmode() function?
Regards
LikeLike
It is not implemented
LikeLike
Hi,
Nice code indeed, very useful, I’m using it to debug a multi on/off sensor setup. I wonder if you could make buttons stick to “pressed” or “released” just with 1 click, instead of having to keep it down (which only lets you make 1 change at a time).
Good work 🙂
LikeLike
Already figured out that just putting a “return;” at the beginning of the buttonClickRelease function makes it. Thanks for your work!
LikeLike
Thanks. I found it to be pretty handy for getting started writing the code on my laptop instead of having to do it on the Rpi. I need to make event-driven code now, instead of polling, and I realized that you’ve only emulated a small subset of the GPIO functions. Do you have any intentions of adding GPIO.add_event_detect() and similar?
LikeLike
maybe in the future.
LikeLike
It would be nice. Need this function too!
LikeLike
Hi,
I work with Windows 7 and Python 3.1.2 .
When I try the file “tester2.py”, I have :
Traceback (most recent call last):
File “C:\Users\François\Desktop\GPIOEmulator\tester2.py”, line 1, in
File “C:\Users\François\Desktop\GPIOEmulator\EmulatorGUI.py”, line 4, in
from TypeChecker import typeassert
File “C:\Users\François\Desktop\GPIOEmulator\TypeChecker.py”, line 1, in
from inspect import signature
ImportError: cannot import name signature
Have you a solution ?
Thank you
François
LikeLike
Are you sure you are using python 3?
LikeLike
Yes :
this is the title of python schell :
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type “copyright”, “credits” or “license()” for more information.
What is a “name signature” please ?
LikeLike
Hi,
I change the Python’ version. I take Python 3.7.0.
And now it is OK
Thanks a lot
LikeLike
yall mind if i flex?
LikeLike
go ahead
LikeLike
Thanks broski
💪😎👌
LikeLike
Excelent, just what I needed
LikeLike
Hi,
Good work, but ‘GPIO.PWM’ don’t work…
LikeLike
This is very cool, thx mate.
LikeLiked by 1 person
Thanks for creating and providing this code. Very helpful, please keep up the good work.
LikeLike