User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

RPIO: An extension of RPi.GPIO with interrupts, PWM and more

Tue Feb 19, 2013 12:43 am

Hey everyone!

I wanted to let you know about RPIO, an extension of RPi.GPIO with interrupt handling, which I've just uploaded to pypi. This is an example which handles events on 3 gpio pins, each with different edge detection:

Code: Select all

import RPIO

def do_something(gpio_id, value):
    print("New value for GPIO %s: %s" % (gpio_id, value))

RPIO.add_interrupt_callback(17, do_something, edge='rising')
RPIO.add_interrupt_callback(18, do_something, edge='falling')
RPIO.add_interrupt_callback(19, do_something, edge='both', threaded_callback=True)
RPIO.wait_for_interrupts()
Besides the new interrupt methods, you can use RPIO just as RPi.GPIO. Note that RPIO uses the BCM gpio numbering scheme by default.

Code: Select all

import RPIO

# set up GPIO output channel
RPIO.setup(17, RPIO.OUT)

# set gpio 17 to high
RPIO.output(17, RPIO.HIGH)

# set up output channel with an initial state
RPIO.setup(18, RPIO.OUT, initial=RPIO.LOW)

# set up input channel with pull-up control
#   (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
RPIO.setup(19, RPIO.IN, pull_up_down=RPIO.PUD_UP)

# read input from gpio 19
input_value = RPIO.input(19)

# to change to BOARD GPIO numbering
RPIO.setmode(RPIO.BOARD)

# to reset every channel that has been set up by this program to INPUT with no pullup/pulldown and no event detection
RPIO.cleanup()
The source code is quite compact and works with Python 2.x and 3.x. Easiest way to get RPIO is with pip or easy_install:

Code: Select all

sudo pip install RPIO
Hope its useful; let me know what you think!
Last edited by metachris on Thu Mar 14, 2013 3:12 pm, edited 1 time in total.
pythonhosted.org/RPIO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2848
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: RPIO: An extension of RPi.GPIO with interrupt management

Tue Feb 19, 2013 1:02 pm

This looks interesting. Thanks :)

I believe Ben Croston was/is working on interrupts (amongst other things). Are you in touch with him?

Can it be used alongside RPi.GPIO in the same script? Or does it have to be used by itself?
Alex Eames RasPi.TV, RasP.iO

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Tue Feb 19, 2013 1:11 pm

alexeames wrote:I believe Ben Croston was/is working on interrupts (amongst other things). Are you in touch with him?

Can it be used alongside RPi.GPIO in the same script? Or does it have to be used by itself?
Ben Croston and I are communicating, but RPi.GPIO does not have a real interrupt solution, just some polling event detection (the experimental methods).

RPIO just extends RPi.GPIO; they can be used alongside (in fact they always are). For instance RPIO.input(...) calls RPi.GPIO.input(...) underneath. You could just use `import RPIO as GPIO` as a drop-in replacement for `import RPi.GPIO as GPIO`, to use the added interrupt functionality.
pythonhosted.org/RPIO

User avatar
croston
Posts: 701
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: RPIO: An extension of RPi.GPIO with interrupt management

Tue Feb 19, 2013 1:32 pm

I am looking at adding the RPIO functionality to RPi.GPIO in the next week or so - thanks Chris for your good work so far. I will probably keep the existing experimental event_detected() functions for the most part and add a way of using a threaded function callback mechanism (as in RPIO).

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2848
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: RPIO: An extension of RPi.GPIO with interrupt management

Tue Feb 19, 2013 4:08 pm

Excellent :)
Alex Eames RasPi.TV, RasP.iO

dauhee
Posts: 59
Joined: Fri Sep 07, 2012 1:50 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Wed Feb 27, 2013 9:57 pm

RPIO rocks! Very powerful.

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Thu Feb 28, 2013 1:11 am

Thank you; comments like these are always motivating :)

I've just put RPIO 0.8.3 on pypi which includes TCP socket interrupts, because they just go along so well with epoll. For now RPIO just sends the strings from the TCP clients to the callback, but i'll be adding more features in the near future. Here is an example; you just need to specify a port and a callback:

Code: Select all

def callback(socket, val):
    print("socket %s: '%s'" % (socket.fileno(), val))
    socket.send("echo: %s\n" % val)

RPIO.add_tcp_callback(8080, socket_callback)
RPIO.wait_for_interrupts()
While RPIO is waiting for interrupts it accepts TCP socket connections. You can test this with the terminal command "telnet localhost 8080". To update RPIO to 0.8.3 you can either use either easy_install or pip:

Code: Select all

sudo easy_install -U RPIO
# or
sudo pip install -U RPIO
You can a more detailled documentation here: pythonhosted.org/RPIO.
pythonhosted.org/RPIO

dauhee
Posts: 59
Joined: Fri Sep 07, 2012 1:50 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 11:24 am

Its probably something silly I'm doing but when I get a TCP callback, the following error is raised (python 2.7):
print("socket %s: '%s'" % (socket.fileno(), val))
AttributeError: 'tuple' object has no attribute 'fileno'
Just wondering if you have any ideas?

Would be really interested to hear your future plans for this also Chris. This library is making my life so easy :)

Thanks.

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 12:31 pm

Hi Chris,
obviously a new thread exclusively ;-) for RPIO. It is probably better if I continue here.
First of all, RPIO is really perfect for me. I took the chance to rewrite my code and base it on RPIO.
I have two questions, which are perhaps silly, but perhaps your answers could be of interest for others as well.

- when I run several programs in parallel which are using the same GPIO-pins: do I need to make similar RPIO.setup() calls in every program or is a setup of global (for all programs under the same user id) nature ?

- from an external source, I receive TCP-packets. At some time I know, that there will be three consecutive packets which have to be dealt with. In my "olld" code, I wait for them in blocking mode and would like to do the same with RPIO. Shall I use threaded=false for this purpose and after this sequence use threaded=true ?
I hope I have made myself clear ...
Thank for this excellent piece of work. I am looking forward for the extensions :-)

Cheers
Josh

texy
Forum Moderator
Forum Moderator
Posts: 5155
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 12:45 pm

....how about the ability to use the hardware PWM as well ;)

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 1:14 pm

I'm working on DMA PWM right now. Its awesome -- I can produce really good pulses down to exactly 1 microsecond. With software PWM i can't seem to go below 100 microseconds. I've documented my tests with an oscilloscope on video and will post it with the RPIO update.

It will be out in the next days - watch this space :)
pythonhosted.org/RPIO

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 1:17 pm

print("socket %s: '%s'" % (socket.fileno(), val))
AttributeError: 'tuple' object has no attribute 'fileno'
Its an error in the programming on my side. I'll push an update in one or two hours. Sorry about that.
Last edited by metachris on Fri Mar 01, 2013 1:21 pm, edited 1 time in total.
pythonhosted.org/RPIO

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 1:19 pm

joshmosh wrote:- when I run several programs in parallel which are using the same GPIO-pins: do I need to make similar RPIO.setup() calls in every program or is a setup of global (for all programs under the same user id) nature ?

- from an external source, I receive TCP-packets. At some time I know, that there will be three consecutive packets which have to be dealt with. In my "olld" code, I wait for them in blocking mode and would like to do the same with RPIO. Shall I use threaded=false for this purpose and after this sequence use threaded=true ?
First question: To use a GPIO in several programs, you should call setup() in each program.

Second question: threaded=True will have no effect on this. You'll need to save the history yourself, and if the third packet is coming in as expected then do what you wanna do. (Hope this helps a bit).
pythonhosted.org/RPIO

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 01, 2013 3:46 pm

Alright, I've just released RPIO v0.8.4, which includes the following:
  • "rpio-curses", a curses based GPIO inspection and manipulation tool
  • Bugfix for "rpio --sysinfo"
  • Bugfix for tcp callback parameter
After installing/updating RPIO, the command "rpio-curses" is available on your system. Here are a few screenshots of it in action:

Image Image Image

You can update with "easy_install" or "pip":

Code: Select all

sudo easy_install -U RPIO
Enjoy! :)

License: GPLv3+/ Source Code: https://github.com/metachris/RPIO
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Sun Mar 03, 2013 8:29 pm

Hi Chris,
I was able to clean my code significantly, so again thank you for RPIO !
I have another question. At some point in my communication sequenceit is required that I close the TCP connection (socket.close()). The remote side will bring up the connection again. Is it ok just to close the socket or is it required to do something else ?
Thank you
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Sun Mar 03, 2013 11:04 pm

socket.close() is all you need to do; no further cleanup necessary.
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Mon Mar 04, 2013 7:05 am

Great, that makes my life easy :-)
Thank you Chris.
Cheers
Josh

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Thu Mar 07, 2013 2:06 pm

Hi Chris - me again ...
Is there a chance that your RPIO can be used without root privileges ?
Currently, I have to run my Python program with 'sudo', which makes me feel a little bit uneasy.
Or am I doing something wrong ?

Thank you and best regards,
Josh

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Thu Mar 07, 2013 5:57 pm

To directly access the GPIOs, the process needs superuser privileges, there is no way around. But there are other ways than just sudo, most notably the SUID bit (http://en.wikipedia.org/wiki/Setuid).
pythonhosted.org/RPIO

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Thu Mar 07, 2013 6:52 pm

Ah, ok, thank you.
I am relatively new to Linux, so please excuse my silly questions. Will have a look at setuid.

Thank you and have a nice evening
Josh

joshmosh
Posts: 62
Joined: Fri Aug 03, 2012 12:04 pm

Re: RPIO: An extension of RPi.GPIO with interrupt management

Thu Mar 07, 2013 8:32 pm

Sorry Chris,
another probably silly question: I have setup waitforinterrupt with an epoll_timeout=60, since I want to regain control at least every minute in case nothing else happens. My problem is that the program is stuck in the waitforinterrupt routine. I have included a print statement right after waitforinterrupt, but this is never reached.
Any guidance would be highly welcome.
Cheers
Josh

Christoph1985
Posts: 63
Joined: Sat Jul 28, 2012 11:58 am
Location: Germany

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 08, 2013 7:44 am

Hey,
i am quite a newbie to python and have a question for understanding.

Currently iam using big while, if, elif loops with sleep.

This is getting very unhandable, because I try to use a gamepad for steering 2 motors.
With my method I have delay times when pressing a button or moving a stick.

Is your interrupt method a way to get nearly no delay times?
So, will a button be recognised directly when pushing it?

For example:
in a while - if- elif loop, I have to "wait" until the programm is in the "if part" which is asks if my pressed button is true oder not.
When my loop is quite big (for example 10 if parts), with 0.1 sec sleep for every step, it takes max 1 second I have to hold a button down, before it gets recognised.

I hope you understand my problem and can answer me if this extension solves it ;-)

Thanks in advance :)

Christoph

User avatar
croston
Posts: 701
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 08, 2013 7:52 am

You could use the recently added interrupt features in RPi.GPIO 0.5.0a. Examples of replacing a polling loop are here:
http://code.google.com/p/raspberry-gpio ... iki/Inputs

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 08, 2013 12:08 pm

joshmosh wrote:Sorry Chris,
another probably silly question: I have setup waitforinterrupt with an epoll_timeout=60, since I want to regain control at least every minute in case nothing else happens. My problem is that the program is stuck in the waitforinterrupt routine. I have included a print statement right after waitforinterrupt, but this is never reached.
Any guidance would be highly welcome.
Cheers
Josh
The wait_for_interrupts method waits indefinitely. You can end it with RPIO.stop_waiting_for_interrupts(), then it will exit after the next specified timeout (eg. 60sec in your case).
pythonhosted.org/RPIO

User avatar
metachris
Posts: 96
Joined: Wed Feb 06, 2013 1:52 pm
Location: Vienna, Austria

Re: RPIO: An extension of RPi.GPIO with interrupt management

Fri Mar 08, 2013 12:10 pm

Christoph1985 wrote:Hey,
i am quite a newbie to python and have a question for understanding.

Currently iam using big while, if, elif loops with sleep.

This is getting very unhandable, because I try to use a gamepad for steering 2 motors.
With my method I have delay times when pressing a button or moving a stick.

Is your interrupt method a way to get nearly no delay times?
So, will a button be recognised directly when pushing it?

For example:
in a while - if- elif loop, I have to "wait" until the programm is in the "if part" which is asks if my pressed button is true oder not.
When my loop is quite big (for example 10 if parts), with 0.1 sec sleep for every step, it takes max 1 second I have to hold a button down, before it gets recognised.

I hope you understand my problem and can answer me if this extension solves it ;-)

Thanks in advance :)

Christoph
Yeah, interrupts are the way to go here, since they eliminate your need for constant polling with delays. Like in this example: https://github.com/metachris/RPIO/blob/ ... errupts.py
pythonhosted.org/RPIO

Return to “Python”