Introducing the Python library SimulRPi
SimulRPi is a Python library that I wrote as part of my Darth-Vader-RPi project. It partly fakes RPi.GPIO and simulates some I/O devices connected to a Raspberry Pi (RPi):
Use the
Output:
- push buttons by listening to pressed keyboard keys and
- LEDs by displaying blinking red dots in the terminal along with their GPIO pin numbers.
The Python package pynput is used to monitor the keyboard for any pressed key. Thus, the SimulRPi library can be useful in the case that you want to try your RPi.GPIO-based script by running it on your computer when no RPi is available at the moment.
Example: terminal output
Simulating LEDs on an RPi via a terminal
Each dot represents a blinking LED connected to an RPi and the number between brackets is the associated GPIO channel number. Here the LED on channel 22 toggles between on and off when a key is pressed.
Dependencies
- Platforms: Linux, macOS
- Python: 3.5, 3.6, 3.7, 3.8
- pynput >=1.6.8: for monitoring the keyboard for any pressed key
- Platforms: Linux, macOS
- Python: 3.5, 3.6, 3.7, 3.8
- pynput >=1.6.8: for monitoring the keyboard for any pressed key
Installation instructions
- It is highly recommended to install SimulRPi in a virtual environment using for example venv or conda.
Install theSimulRPi
package withpip
:$ pip install SimulRPi
pynput
if it is not already found in your system. - Test your installation by importing
SimulRPi
and printing its version:$ python -c "import SimulRPi; print(SimulRPi.__version__)"
SimulRPi
installation, you will get access to the run_examples
script which allows you to run different code examples on your RPi or computer. If it is run on your computer, it will make use of the SimulRPi.GPIO module which partly fakes RPi.GPIO.The run_examples script can be called from the terminal by providing some arguments. For example, to run the code example # 1 which turns on a LED using the simulation package SimulRPi:
$ run_examples -e 1 -s
-h
flag to get a list of options: $ run_examples -h
Also, check out the script's documentation.
Output:
Usage
You can try importing
RPi.GPIO
first and if it is not found, then fallback on the SimulRPi.GPIO module:try: import RPi.GPIO as GPIO except ImportError: import SimulRPi.GPIO as GPIO
# Rest of your code
Examples
The following two examples are part of the suite of code examples that is included with SimulRPi.
Example 1: display 3 LEDs
This example consists in displaying three LEDs on channels 9, 10, and 11, respectively. Here is the code along with the output from the terminal:
import SimulRPi.GPIO as GPIO led_channels = [9, 10, 11] GPIO.setmode(GPIO.BCM) GPIO.setup(led_channels, GPIO.OUT) GPIO.output(led_channels, GPIO.HIGH) GPIO.cleanup()
Example 2: blink a LED if a key is pressed
In this example, we will blink a LED on channel 10 for 3 seconds if the key shift_r is pressed. And then exiting from the program. The program can also be terminated at any time by pressing ctrl + c. Here is the code along with the output from the terminal:
import time import SimulRPi.GPIO as GPIO led_channel = 10 key_channel = 27 GPIO.setmode(GPIO.BCM) GPIO.setup(led_channel, GPIO.OUT) GPIO.setup(key_channel, GPIO.IN, pull_up_down=GPIO.PUD_UP) print("Press the key 'shift_r' to turn on light ...\n") while True: try: if not GPIO.input(key_channel): print("The key 'shift_r' was pressed!") start = time.time() while (time.time() - start) < 3: GPIO.output(led_channel, GPIO.HIGH) time.sleep(0.5) GPIO.output(led_channel, GPIO.LOW) time.sleep(0.5) break except KeyboardInterrupt: break GPIO.cleanup()
Resources
- SimulRPi documentation
- SimulRPi Changelog
- SimulRPi GitHub: source code
- SimulRPi PyPI
- Darth-Vader-RPi: personal project using
RPi.GPIO
for activating a Darth Vader action figure with light and sounds andSimulRPi.GPIO
as fallback if testing on a computer when no RPi is available
Comments
Post a Comment