9
submitted 2 months ago by [email protected] to c/[email protected]

I have a Keybow MINI hooked up to a Raspberry Pi Zero W, and I'm using Python to respond to events. I have one button that kicks off playing a song on a passive buzzer, and I'm wondering if there's a way to have a button press stop the song before it completes.

top 5 comments
sorted by: hot top controversial new old
[-] [email protected] 8 points 2 months ago

It should be possible, but the answer is going to depend on your implementation, what libraries you are using, and so on.

For example, if the play sound action is synchronous, then maybe you could start it up in another thread, and interrupt that thread if you want to cancel the sound.

If it’s asynchronous, maybe you need to retain a reference to the sound object and then invoke a stop() call when the other button is pressed.

[-] [email protected] 1 points 1 month ago* (last edited 1 month ago)

Okay, I've got it offloaded to a different process and I can cancel that process now. However, is there anything I need to do to let it access GPIO? It looks like it's working but nothing is coming out of the passive buzzer when I'm playing it off the other process.

EDIT: I think I got it with threading. Thanks!

[-] [email protected] 1 points 1 month ago

Great to hear you got it sorted! May I ask out of curiosity which library you are using to play sounds?

[-] [email protected] 1 points 1 month ago* (last edited 1 month ago)

I converted this into python; you can see it here.

Edit: Err, you will be able to see it there once I get to my laptop and make it public.

Edit: Public now!

[-] [email protected] 2 points 1 month ago

Ah nice, you’re GPIO to generate tones with PWM. Very cool!

this post was submitted on 08 May 2024
9 points (90.9% liked)

Python

5976 readers
9 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

October 2023

November 2023

PastJuly 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS