primetime
Posts: 5
Joined: Sat Sep 24, 2016 7:42 am

Coding for background process?

Sun Oct 02, 2016 10:57 am

So I've been trying to refine the Panic Button that I'm making for my family. Reference to my post: viewtopic.php?f=41&t=161149

Just need help trying to write the code so that I can run it as a background process, one that would continuously run as we are going to use this as one of my mom's primary means of communication (She has a disease that has robbed her of her voice). The only way I could think of is to code is so that instead of using event detection, would code my own event detection using if statements trapped in a while loop.

Code: Select all

while(1):
	if GPIO.input(buttonA)==GPIO.HIGH:
		pushMsg('A')
		sleep(.5)
	if GPIO.input(buttonB)==GPIO.HIGH:
		pushMsg('B')
		sleep(.5)
	if GPIO.input(buttonC)==GPIO.HIGH:
		pushMsg('C')
		sleep(.5)
	if GPIO.input(buttonD)==GPIO.HIGH:
		pushMsg('D')
		sleep(.5)
Is this the proper way to code my python script? I thought that using a while loop is extremely inefficient that's why I tried learning to do it the event detection way.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Coding for background process?

Sun Oct 02, 2016 5:03 pm

I think the GPIO event callback system would be better. You should be able to find lots of different examples on this forum. Also it might be worth looking at the gpio zero module.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

primetime
Posts: 5
Joined: Sat Sep 24, 2016 7:42 am

Re: Coding for background process?

Sun Oct 02, 2016 11:14 pm

Thanks for the reply paddyg.

If I use the event callback system, and I want this to run as a background process I still have to put a try+while loop at the end of my script so that the script is constantly waiting for input?

Code: Select all

#.... all the other code stuff 
GPIO.add_event_detect(buttonC, GPIO.RISING, pushMsg, bouncetime=50)
GPIO.add_event_detect(buttonD, GPIO.RISING, pushMsg, bouncetime=50)

print "Waiting for button press"
try:
	while True:
         time.sleep(0.5)
except KeyboardInterrupt:
	GPIO.cleanup()
GPIO.cleanup()

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Coding for background process?

Mon Oct 03, 2016 7:56 am

yes, that looks to be the way I would do it. I don't know what the callback functions are going to do but if they are variations of a similar thing I might have each of them call a general function but pass different arguments. The alternative is to set all callbacks to one function and differentiate using the pin argument but sometimes this seems to cause issues {might have been fixed now)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

primetime
Posts: 5
Joined: Sat Sep 24, 2016 7:42 am

Re: Coding for background process?

Mon Oct 03, 2016 8:25 am

Thanks again for the help paddyg.

With regards to the callback, I have a method that takes the GPIO number that is passed during the event and that is parsed into the specific button that was pressed.

I now have my little project up and running and I thank you again for all your help!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Coding for background process?

Mon Oct 03, 2016 9:46 am

Good. I hope it helps your mum.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”