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

S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • 1
    Did you check the documentation? What problem did you encounter when trying to pass parameters? – MisterMiyagi Jun 20 '20 at 05:57
  • 1
    Does this answer your question? [How can I use threading in Python?](https://stackoverflow.com/questions/2846653/how-can-i-use-threading-in-python) – Pynchia Jun 20 '20 at 06:01

1 Answers1

3

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'))
Community
  • 1
  • 1
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • What is the use of partial in here.? – S Andrew Jun 20 '20 at 05:45
  • It... does the thing that I said needs to be done. I have added a link to the relevant documentation. – Karl Knechtel Jun 20 '20 at 05:48
  • 1
    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 :) – Karl Knechtel Jun 20 '20 at 05:54
  • Can the same code be used to get the return value from the thread function – S Andrew Jun 20 '20 at 10:35