mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

gpio event button hold time

Thu Aug 28, 2014 11:54 pm

Alright so I'm trying to streamline my scripts mainly because they take 99% cpu at times... one I understand as it is constantly polling for a nfc card (I could add a sleep (1 second)) [would that reduce it by much?]

My problem that I can't figure out as I'm not very versed in python/gpio event aspect. I'm trying to use the event detect e.g.
GPIO.add_event_detect(7, GPIO.FALLING, callback=press, bouncetime=300)
But I also want it to detect hold time.

My old script was

Code: Select all

def press()
     global previoustime
     ontime = time.time() - previoustime
     if ontime > 1and ontime < 3:
         print "hold"
			
		elif ontime > 3:
			print "long hold"
			
		elif ontime > 0.01:
			print "click"
					
		else:
			print "ignore"

while True:
    if(GPIO.input(7) == 0):  ## this line I was attempting to change to GPIO.wait_for_edge(7, GPIO.FALLING)
        previoustime = time.time ()
        GPIO.wait_for_edge(7, GPIO.RISING)
        press()

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: gpio event button hold time

Fri Aug 29, 2014 12:40 am

oh forgot to add what failed
I tried adding GPIO.wait_for_edge(7, GPIO.RISING) into the press function, but I got an error

RuntimeError: Edge detection events already enabled for this GPIO channel

so is possible to figure out the hold time as it seems like the function runs right away and doesn't let you count for hold time

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: gpio event button hold time

Fri Aug 29, 2014 6:34 am

All I can tell you is how I've dealt with this. I don't know if there's a better way or not.

I use the event detect to capture the button press, but I also have a separate thread running which just checks the state of the button which increments a counter when the button is pressed and resets it when the button is released. I can then trigger different events when the button is held for certain amounts of time.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: gpio event button hold time

Fri Aug 29, 2014 5:10 pm

yeah currently this is in a seperate thread. One that detects the button. I wanted to try to merge two scripts together. I guess I'll keep what I have now

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: gpio event button hold time

Fri Aug 29, 2014 5:27 pm

Separate thread can be in the same script (mine is). Just need to use python's Threading module.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

LeoJr2015
Posts: 11
Joined: Sun Mar 01, 2015 4:25 pm

Re: gpio event button hold time

Sun Mar 01, 2015 4:34 pm

elParaguayo wrote:Separate thread can be in the same script (mine is). Just need to use python's Threading module.
Do you have this code example? I am trying to do this same exact thing now. I am new to threading with python, and I am not sure how to put both in the same script. Thanks for any assistance.

Return to “Python”