0

I have raspberry pi where I am using python to create a small buzzer script. In the script if a condition becomes True, I need to print some information and also make buzzer sound. Buzzer sounds are made in two different format i.e. High and Low. In High, I have to run below code:

GPIO.output(BUZZER, 1)
time.sleep(5)
GPIO.output(BUZZER, 0)
GPIO.cleanup()

so that buzzer make continuous sound for 5 sec. In Low, I have to run below code:

for i in range(5):
    print(i)
    state = GPIO.input(BUZZER)
    print("state is {}".format(state))
    GPIO.output(BUZZER, 1)
    time.sleep(0.3)

    state = GPIO.input(BUZZER)
    print("state is {}".format(state))
    GPIO.output(BUZZER, 0)
    time.sleep(0.3)

It will make 5 beep sounds.

Below is the python script:

def generate_sound(tempo):
    if tempo == "High":
        GPIO.output(BUZZER, 1)
        time.sleep(5)
        GPIO.output(BUZZER, 0)
        GPIO.cleanup()
    else:
        for i in range(5):
            state = GPIO.input(BUZZER)
            print("state is {}".format(state))
            GPIO.output(BUZZER, 1)
            time.sleep(0.3)

            state = GPIO.input(BUZZER)
            print("state is {}".format(state))
            GPIO.output(BUZZER, 0)
            time.sleep(0.3)



if some_condition is True:
    generate_sound("High")
    print("This condition is True")
    print("Here is the information you need")
    print("Some more information")

else:
    generate_sound("Low")
    print("This condition is False")
    print("Here is the information you need")
    print("Some more information")

The above code is working fine but the problem is that I have to display information and generate sound at the same time. But with current approach, the sound is generated and waits for 5sec and then information is printed.

To resolve this I though of putting the generating sound function in a thread so that it can run parallel with printing information, something like below:

sound = Thread(target=generate_sound)

But here I am not sure how do I pass the values High and Low to generate sound function. I am not very expert in threading. Can anyone please give me some ideas. Please help. Thanks

2
  • 1
    Did you check the documentation? What problem did you encounter when trying to pass parameters? Commented Jun 20, 2020 at 5:57
  • 1
    Does this answer your question? How can I use threading in Python? Commented Jun 20, 2020 at 6:01

1 Answer 1

4

Pardon; reflexive habit there. The threading library in particular provides a direct solution for you, so the workaround below the line is not necessary.

See the Thread documentation:

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, \*, daemon=None)

[ ... ]

args is the argument tuple for the target invocation. Defaults to ().

So we can just provide the args as appropriate:

# Note the comma in `('High',)`; we want a 1-element tuple.
sound = Thread(target=generate_sound, args=('High',))

But here I am not sure how do I pass the values High and Low to generate sound function.

This doesn't depend on understanding threading; it's a general technique for these kinds of "callback" functions (any time that you pass a function as a parameter to something else, basically). For example, frequently you need this technique for buttons when making a GUI with tkinter (or other toolkits).

Bind the parameter to the call, for example using functools.partial from the standard library:

from functools import partial
sound = Thread(target=partial(generate_sound, 'High'))
Sign up to request clarification or add additional context in comments.

4 Comments

What is the use of partial in here.?
It... does the thing that I said needs to be done. I have added a link to the relevant documentation.
Sorry, never mind that. Specifically for threading, there is a specific thing you're meant to do rather than this general-purpose hammer. Please see the update. Some other libraries do analogous things too. The first step should always be to read the documentation; even the best of us can forget things :)
Can the same code be used to get the return value from the thread function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.