snielsen
Posts: 11
Joined: Fri Jan 12, 2018 1:06 pm

How to disable GPIO button when scriptis running

Wed Jan 17, 2018 10:47 am

Hello all!
This is for use in an exhibition space, and I am searching around for a method to disable the button (17) as long as the script is running, to avoid several paralell playback`s when the audience are passing the motion sensor. Here is my script so far:

import simpleaudio as sa

from gpiozero import Button
btn_play_obj = Button(17)

wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
play_obj = wave_obj.play
btn_play_obj.when_pressed = wave_obj.play

---
I suppose I will need som "if" or "when" and "sleep" commands ...

pcmanbob
Posts: 7255
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 11:47 am

So reading up on simple audio there is a play_obj.wait_done() function that stops the program until the audio has finished playing.
It can also be used to wait for the audio playback to finish. This is espcially useful when a script or program would otherwise exit before playback is done (stopping the playback thread and consequently the audio):

play_obj.wait_done()
# script exit
http://simpleaudio.readthedocs.io/en/la ... .wait_done()

so using that you could make you program wait for the audio to finish before going back to checking the button input.

Code: Select all

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)


while True:
    if button.is_pressed:
    wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
    play_obj = wave_obj.play
    play_obj.wait_done()
    
    else:
        time.sleep (0.1)
        
the time.sleep in the else is just there to stop your program running very fast and using lots of processor time.

please not this code is un-tested.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

snielsen
Posts: 11
Joined: Fri Jan 12, 2018 1:06 pm

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 1:39 pm

Thanks a lot! I`m trying to run it but get Syntax error - expected an intended block. "wave_obj" is highlighted, I`ve tried diferent spacings around that ... as you understand I`m a beginner

pcmanbob
Posts: 7255
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 1:50 pm

looks like the indenting got messed up when I posted the code, probably because I used your original code plus some I copied from web page and some typed.

try this.

Code: Select all

import simpleaudio as sa
import time

from gpiozero import Button
button = Button(17)


while True:
    if button.is_pressed:
        wave_obj = sa.WaveObject.from_wave_file('samples/test3.wav')
        play_obj = wave_obj.play
        play_obj.wait_done()
    
    else:
        time.sleep (0.1)
        
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

snielsen
Posts: 11
Joined: Fri Jan 12, 2018 1:06 pm

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 2:04 pm

Yes, actually I just put it like that too, but then there is a problem with attributes:

Traceback (most recent call last):
File "/home/pi/gpio-music-box/fra hjelper 01.py", line 13, in <module>
play_obj.wait_done()
AttributeError: 'function' object has no attribute 'wait_done'

pcmanbob
Posts: 7255
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 2:33 pm

From here https://pypi.python.org/pypi/simpleaudio/1.0.2

Simple, asynchronous audio playback for Python 3. <<<<<<<<<<<<<<<<<<<<<<<<< :!:

so try running your program with python3
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 2:58 pm

You might be missing your parentheses... The line should be:

Code: Select all

play_obj = wave_obj.play()
Without the parentheses, play_obj is a reference to the play method of wave_obj (which won't have the "wait_done" method). Adding the parentheses will execute the method and assign the return value to play_obj. I assume this return value does have the attribute you need.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

pcmanbob
Posts: 7255
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 3:07 pm

elParaguayo wrote:
Wed Jan 17, 2018 2:58 pm
You might be missing your parentheses... The line should be:

Code: Select all

play_obj = wave_obj.play()
Without the parentheses, play_obj is a reference to the play method of wave_obj (which won't have the "wait_done" method). Adding the parentheses will execute the method and assign the return value to play_obj. I assume this return value does have the attribute you need.
well spotted :D

Code: Select all

wave_obj = sa.WaveObject.from_wave_file("path/to/file.wav")
play_obj = wave_obj.play()
play_obj.wait_done()
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 3:12 pm

I feel bad for replying - you'd done all the leg work up to then!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

pcmanbob
Posts: 7255
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 3:27 pm

No problem I might never have spotted it :D
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

snielsen
Posts: 11
Joined: Fri Jan 12, 2018 1:06 pm

Re: How to disable GPIO button when scriptis running

Wed Jan 17, 2018 3:45 pm

Thanks a lot for helping me out! I also got rid of the clicking noise when the sound is starting. I will try to learn this from the beginning when there is more time :-)

Return to “Python”