Kalman
Posts: 31
Joined: Mon Apr 22, 2013 3:38 pm
Location: Sweden

simulate run program

Fri Mar 29, 2019 7:48 pm

Hi!
This is maybe a newbie question and wrong thread?

I'm writing a program to run some motors and other things in python 3... for the Raspberry Pi 2 B.
I use 22 of the GPIO pins for signal out and incoming.
Somehow I would like to run the program in "simulate" mode so I can see on the screen what happens and different functions and so on and the status for the GPIO pins.
Earlier I programmed a ZEN relay system with "ladder" language and was able to simulate the program on the screen.
I would like to do the same with the Python and the Pi. The Geany does not do what I meant-want. It only steps one line but not "simulate".
Is there any program doing this?
Thank You for any suggestions.

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: simulate run program

Fri Mar 29, 2019 9:28 pm

You can use mock pins provided by GPIO Zero.

First open a terminal and set an environment variable:

Code: Select all

export GPIOZERO_PIN_FACTORY=mock
Then open a Python prompt (or an IDE):

Code: Select all

python3
or

Code: Select all

idle3 &
(for example)

Now when you do GPIO stuff with GPIO Zero it's not touching the pins, and you can simulate button presses and things:

Code: Select all

>>> from gpiozero import *
>>> led = LED(2)
>>> led.pin_factory
<gpiozero.pins.mock.MockFactory at 0x7fbc588381d0>
>>> led.value
0
>>> led.on()
>>> led.value
1
>>> led.off()
>>> led.value
0
>>> btn = Button(3)
>>> btn.when_pressed = led.on
>>> btn.when_released = led.off
>>> btn.pin.drive_low()
>>> btn.value
1
>>> led.value
1
>>> btn.pin.drive_high()
>>> btn.value
0
>>> led.value
0
If you want this mode to persist, you can add the export line to your .profile file in your homedir and it will be enforced after a reboot.

You can even do this on your PC if you install Python, pip and gpiozero. See the docs for more info: https://gpiozero.readthedocs.io
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

User avatar
B.Goode
Posts: 8873
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: simulate run program

Sat Mar 30, 2019 9:40 pm

Another thought, not in disagreement with the very knowledgeable reply provided but tackling it in a different way.

Firstly, Geany is fine as a general purpose programmers editor. But you might like to look at Thonny and the Mu-editor, both of which are specifically designed to work with python. The debugging and single stepping facilities are probably superior to Geany.

But neither natively 'knows' anything about the gpio pins.

So you might like to augment your choice of editor with a standalone tool that dynamically displays the current status of the gpio pins. Here are two that I am aware of:

https://github.com/rricharz/pi-gpio-display

https://github.com/scotty3785/python/bl ... UI/gpio.py

Both developers are active members of these forums.

Kalman
Posts: 31
Joined: Mon Apr 22, 2013 3:38 pm
Location: Sweden

Re: simulate run program

Sun Mar 31, 2019 6:52 pm

Thank You both!
:)
New silly questions, can I use both suggestions and run and test on my LinuxMint laptop?

It is nice be able to see the simulated Zero GPIO pins lighting up on the screen as high or low.
Now I wonder if I want to rise a GPIO pin to High by pushing/hit a key on the laptop, will that work? Or do I need to connect to the real GPIO pins? In that case I must run it on the Rasp.Pi itself.

And finally Mr Be-good :D
(nice nick/alias)
Can you tell me how to , from where download those editors Tony and the Mu? And can I run them on the linux laptop as well?

Thank You again

User avatar
B.Goode
Posts: 8873
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: simulate run program

Sun Mar 31, 2019 7:20 pm

The Thonny Python IDE has been a standard component of Raspbian since June 2017. If you have installed Raspbian since then you should find Thonny in the Raspberry Pi Desktop drop-down menu system, under the Programming section.

If your Raspbian OS pre-dates that, try:

Code: Select all

sudo apt update
sudo apt install python3-thonny

Generic documentation about Thonny, independent of Raspbian, is here: https://thonny.org



To install the Mu editor, see this Raspberry Pi Foundation documentation: https://projects.raspberrypi.org/en/pro ... ed-with-mu

After installation you should find Mu in the Raspberry Pi Desktop drop-down menu system, under the Programming section.

Generic documentation about Mu, independent of Raspbian, is here: https://codewith.mu

Kalman
Posts: 31
Joined: Mon Apr 22, 2013 3:38 pm
Location: Sweden

Re: simulate run program

Tue Apr 02, 2019 2:14 pm

Tahnk You!
:)

Return to “Python”