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):
  • 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 

Installation instructions

  1. It is highly recommended to install SimulRPi in a virtual environment using for example venv or conda.
    Install the SimulRPi package with pip:
    $ pip install SimulRPi
    It will install the dependency pynput if it is not already found in your system.

  2. Test your installation by importing SimulRPi and printing its version:
    $ python -c "import SimulRPi; print(SimulRPi.__version__)"
    
As part of the 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

Use the -h flag to get a list of options:
$ run_examples -h

Also, check out the script's documentation.

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()
Output:

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()
Output:

Resources

References

  • pynput: a package to control and monitor input devices.
  • RPi.GPIO: a module to control RPi GPIO channels.
Image credit: "Raspberry Pi Closeup" by Gijs Peijs is licensed under CC BY 2.0

Comments

Popular posts from this blog

Deactivate conda's base environment on startup

Product review: SMONET wireless security camera system

Draw arrows with GIMP plugins