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

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

Fri Mar 08, 2013 12:40 pm

metachris wrote:
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).
Thank you for your help and guidance. Since most of my interrupts occur very rarely - at least I hope so: smoke detectors, intruder alarms - I need to do the following: first a call to stop_waiting_for_interrupts(), then wait_for_interrupts(epoll_timeout=60). Is that correct ?
Because other way round stop_waiting_for_interrupts() would not be executed ...
Thanks a lot !
Cheeers
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

Fri Mar 08, 2013 12:53 pm

joshmosh wrote:Thank you for your help and guidance. Since most of my interrupts occur very rarely - at least I hope so: smoke detectors, intruder alarms - I need to do the following: first a call to stop_waiting_for_interrupts(), then wait_for_interrupts(epoll_timeout=60). Is that correct ?
Because other way round stop_waiting_for_interrupts() would not be executed ...
You don't need to call stop_waiting_for_interrupts() first; only when you want to quit the wait_for_interrupts() loop (and thereby shutting down the interrupt detection). stop_waiting_for_interrupts() does nothing but quit a currently running wait_for_interrupts() loop.
pythonhosted.org/RPIO

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

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

Fri Mar 08, 2013 1:31 pm

metachris wrote:
joshmosh wrote:Thank you for your help and guidance. Since most of my interrupts occur very rarely - at least I hope so: smoke detectors, intruder alarms - I need to do the following: first a call to stop_waiting_for_interrupts(), then wait_for_interrupts(epoll_timeout=60). Is that correct ?
Because other way round stop_waiting_for_interrupts() would not be executed ...
You don't need to call stop_waiting_for_interrupts() first; only when you want to quit the wait_for_interrupts() loop (and thereby shutting down the interrupt detection). stop_waiting_for_interrupts() does nothing but quit a currently running wait_for_interrupts() loop.
Sorry, I am afraid I'm lost now.
My understanding of the timeout mechanism in epoll is/was, that you wait for interrupts. If there is no interrupt within epoll_timeout, then waiting is stopped and my program gets control again (in my example after a minute). Then I do some housekeeping and after that wait again for interrupts - for a minute.
How would I realize that with your package ?
Sorry for my stubborness ...
Cheers
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

Fri Mar 08, 2013 1:36 pm

Okay now I understand. The solution is this: wait_for_interrupts() does not quit after the timeout, but runs indefinitely (until stop_waiting_for_interrupts()) is called. The timeout is just there to allow your app to quit the wait_for_interrupts() loop with the stop_waiting_for_interrupts() method.
pythonhosted.org/RPIO

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

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

Fri Mar 08, 2013 1:56 pm

metachris wrote:Okay now I understand. The solution is this: wait_for_interrupts() does not quit after the timeout, but runs indefinitely (until stop_waiting_for_interrupts()) is called. The timeout is just there to allow your app to quit the wait_for_interrupts() loop with the stop_waiting_for_interrupts() method.
So how would I invoke the stop_waiting_for_interrupts() method ?
Before I call wait_for_interrupts() ?
Sorry, but I am still lost :-(
Thank you for your patience
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

Fri Mar 08, 2013 2:24 pm

If you want to do something at the same time as waiting for interrupts, you'll need to use a thread. You can also call RPIO.stop_waiting_for_interrupts() from a callback. You should probably only use `stop_waiting_for_interrupts()` when shutting down your program.
pythonhosted.org/RPIO

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

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

Fri Mar 08, 2013 3:43 pm

metachris wrote:If you want to do something at the same time as waiting for interrupts, you'll need to use a thread. You can also call RPIO.stop_waiting_for_interrupts() from a callback. You should probably only use `stop_waiting_for_interrupts()` when shutting down your program.
All this is pretty new to me. Although I know very well how to deal with interrupts in a microcontroller (part of my project involves a few of them), I am lost with the thread-thing in Linux.
What I have read so far about epoll gave me the idea that the concept of an epoll timeout is exactly what I need. The epoll_timeout parameter in wait_for_interrupts() gave me the impression that you have implemeted it the same way - which you obviously have not.
I will probably return to my initial coding and resume from where I left. The requirement to use a thread (which is difficult to understand for me, but this is entirely my fault or problem) seems somewhat artificial to me, since epoll has the ability to abandon a wait after a given time and to resume just after the wait.
Anyway, thank you for your help and your patience. I have learned quite a bit - although not (yet) how to start a thread ;-)
Cheers
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

Fri Mar 08, 2013 3:51 pm

Why would you stop waiting for interrupts to do other stuff (eg. the housekeeping you mentioned)? If you don't _always_ listen for interrupts, you might miss one! You talked about intruder alarm, fire alarm, etc... if you don't always listen, you might miss the interrupt while you do your "housekeeping".

You need to read up on some basics, including threads in Python. For instance you can do stuff while waiting for interrupts like this:

Code: Select all

from threading import Thread
import RPIO

...

def my_background_task():
    # do whatever you want to do while waiting for interrupts here
    pass

Thread(target=my_background_task).start()
RPIO.wait_for_interrupts()
pythonhosted.org/RPIO

User avatar
croston
Posts: 702
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 3:54 pm

There is a different way of doing interrupts without needing to know about threads yourself using RPi.GPIO. Details 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 4:00 pm

croston wrote:There is a different way of doing interrupts without needing to know about threads yourself using RPi.GPIO. Details are here:
http://code.google.com/p/raspberry-gpio ... iki/Inputs
Ben, you've plugged your project already twice today in this thread :P Would be nice of you to at least put a little bit of effort into posting why/how its different instead of just pasting that one-liner. You mean it works the same like this code?

Code: Select all

from threading import Thread

Thread(target=RPIO.wait_for_interrupts).start()
pythonhosted.org/RPIO

User avatar
croston
Posts: 702
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 4:05 pm

Yes. The epoll is already in a different thread and on any events, a flag is set that you can check later. You can also set callbacks. Full details are in the link I posted :)

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

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

Fri Mar 08, 2013 4:11 pm

croston wrote:There is a different way of doing interrupts without needing to know about threads yourself using RPi.GPIO. Details are here:
http://code.google.com/p/raspberry-gpio ... iki/Inputs
Thank you for trying to help, but what I like in Chris' solution is that I can wait for GPIO interrupts as well as for TCP packets.
If I could register a sort of a timer event in Chris' implementation (some kind of RPIO.add_timer_event(routine_to_start, timeout_in_seconds)) I would be more than happy :-)

Anyway, thank you for your proposal
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

Fri Mar 08, 2013 4:15 pm

Here is an example code how to do something all 20 seconds:

Code: Select all

import time
from threading import Thread
import RPIO

...

timeout_in_seconds = 20

def my_background_task():
    while True:
        time.sleep(timeout_in_seconds)
        # your stuff goes here

Thread(target=my_background_task).start()
RPIO.wait_for_interrupts()
edit: another way to do it is by putting the epoll loop in a thread, as RPi.GPIO does:

Code: Select all

import time
from threading import Thread
import RPIO

...

Thread(target=RPIO.wait_for_interrupts).start()

try:
    while True:
        time.sleep(timeout_in_seconds)
        # your stuff goes here
finally:
    # stop the epoll thread before quitting
    RPIO.stop_waiting_for_interrupts()
pythonhosted.org/RPIO

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

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

Fri Mar 08, 2013 4:32 pm

metachris wrote:Here is an example code how to do something all 20 seconds:

Code: Select all

import time
from threading import Thread
import RPIO

...

timeout_in_seconds = 20

def my_background_task():
    while True:
        time.sleep(timeout_in_seconds)
        # your stuff goes here

Thread(target=my_background_task).start()
RPIO.wait_for_interrupts()
edit: another way to do it is by putting the epoll loop in a thread, as RPi.GPIO does:

Code: Select all

import time
from threading import Thread
import RPIO

...

Thread(target=RPIO.wait_for_interrupts).start()

try:
    while True:
        time.sleep(timeout_in_seconds)
        # your stuff goes here
finally:
    # stop the epoll thread before quitting
    RPIO.stop_waiting_for_interrupts()
Chris,
thank you very much for this example. Although I am (not yet) familiar with the exception handling in Python, I will do some reading (as suggested ;-)) and in parallel use your example in my code.
Thank you !
Josh

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

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

Sat Mar 09, 2013 5:47 am

Chris,
I haven given up the the idea of using your code, sorry. After some reading I learned (at least ist is now my understanding, maybe I am wrong), that I cannot access variables, which are defined in my main program, inside the callback function - but this is what I need. Passing of data between the callback function and the main program is a requirement for me.
The other annoying issue is that having set up a thread (as you have suggested) for a time out, I cannot stop the running program (it is an endless loop !) with ctrl-C. After some fiddling I could stop it with ctrl-Z, but then I had two processes hanging around, which would not die. Only the brute force method (kill -9) helped - don't like it.
I will now return to my original concept and organise waiting with epoll myself. I think after a little reading I can have a solution, that exactly will match my needs - and not a solution, that is tailored to be in line with a specific software environment.
Sorry for having wasted your time (and mine) and again thank you for trying to help.
In my projects, I try to apply the KISS principle: Keep It Stupidly Simple. For me and my limited knowledge the callback and thread stuff is far from simple. This is entirely my fault, but I like to really understand what I am doing. I do not want to pester you with every little problem I surely will encounter if I would use your extension - the past few days are a good example.

Sorry and thank you so much.
All the best to 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

Sat Mar 09, 2013 11:30 am

No problem, and good luck with your endeavors! Be sure to get a good grasp on Python fundamentals. Go through some tutorials and read the Python docs, they're quite well organized. I can recommend this link to start off with: http://docs.python.org/2/tutorial/introduction.html

Small tip at the end: you can access variables from the callbacks. To write variables, you'll want to use either globals or classes, where classes usually are the recommended approach. Good luck and have fun!
pythonhosted.org/RPIO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

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

Sat Mar 09, 2013 6:40 pm

I thought I'd try using RPIO instead of GPIO, but I'm having trouble. It installed OK, I replaced "import RPi.GPIO as GPIO" with "import RPIO as GPIO", as suggested, but when I run my python(3) script, I just get "ImportError: No module named RPIO".

Any suggestions?

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

Sat Mar 09, 2013 7:06 pm

Does it work with Python 2? How exactly did you install RPIO? I think you may have installed the python2 module. In this case you'd need to install it for Python 3.
pythonhosted.org/RPIO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

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

Sat Mar 09, 2013 8:53 pm

Hmm, good point, I didn't think of that.

I installed with:
$ sudo apt-get install python-setuptools
$ sudo easy_install -U RPIO

I havn't tried python2. Any idea how to install RPIO for python3?

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

Sat Mar 09, 2013 9:04 pm

Code: Select all

$ sudo apt-get install python3-setuptools
$ easy_install3 -U RPIO
pythonhosted.org/RPIO

ejfield
Posts: 17
Joined: Fri Oct 19, 2012 10:21 am

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

Sat Mar 09, 2013 9:17 pm

That's fixed it! Thanks a lot.

regards
Ed

BerryPicker
Posts: 177
Joined: Tue Oct 16, 2012 3:03 pm
Location: The East of England

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

Sun Mar 10, 2013 12:47 pm

metachris wrote: Would be nice of you to at least put a little bit of effort into posting why/how
Yes, some of us need a little more hand holding to come on board. So, thank you for sharing your creation, and for giving usage examples to help beginners get started. For me, interrupt mamagement of the GPIO is fascinating and offers new motivation to continue with the python tutorials.

sej7278
Posts: 249
Joined: Tue Jan 22, 2013 6:43 pm

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

Sun Mar 10, 2013 6:26 pm

how do you handle debouncing with this, as i'm having trouble with the new GPIO 0.5.0a which has interrupting handling and edge detection now.

i'm using a voltage divider instead of the usual pull-up resistor which means adding a capacitor to debounce my switch is not working, and i can't really find a software solution that works either, e.g. adding a sleep() or averaging the return of multiple calls to GPIO.event_detected() doesn't fix things.

BerryPicker
Posts: 177
Joined: Tue Oct 16, 2012 3:03 pm
Location: The East of England

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

Mon Mar 11, 2013 11:42 am

sej7278 wrote:how do you handle debouncing with this
Using the listen for GPIO example here
http://pythonhosted.org/RPIO/rpio_py.html#example
it seems we should expect to receive an interrupt for every switch contact bounce,
and the callback function should print out a record of each switch contact bounce,
the option being one or two records per bounce depending on the choice of edge specified.

During switch bounce, it may be that the Python software may not keep up with the rate of received notifications from the kernel when GPIO state changes occur, and in this case some RPIO interupts may be missed.

To allow for switch contact bounce without spawning multiple callbacks, it could be a useful improvement to add another option in RPIO.add_interrupt_callback, to delay change notification for a specified length of time, the value being chosen according to the particular switch's bounce settling time.

Hopefully metachris will consider this suggestion.

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

Mon Mar 11, 2013 11:46 am

Thank you for this excellent suggestion! I will implement that today and push it with a few other minor improvements tonight or tomorrow.
pythonhosted.org/RPIO

Return to “Python”