Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Problem: GPIO Buttons
#1
I have four hardware buttons connected to the GPIO pins of the RPi and they work fine on the configuration, but is there a possibility to use a double function for a button? I would need the play/stop from the central button... I tried to put the double command but nothing!
Reply
#2
You could try the "toggle" command that flips between play and pause. Alternatively map the button to a script that checks the status and stops or plays accordingly.
----------------
Robert
Reply
#3
(06-10-2025, 04:12 PM)the_bertrum Wrote: You could try the "toggle" command that flips between play and pause.  Alternatively map the button to a script that checks the status and stops or plays accordingly.

from gpiozero import Button
from signal import pause
import socket
import json

# GPIO17 = pin fisico 11
button = Button(17)

# Path del socket creato da MPV
MPV_SOCKET = "/tmp/mpvsocket"

def toggle_play_pause():
    try:
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(MPV_SOCKET)
        # MPV usa JSON per i comandi
        command = {"command": ["cycle", "pause"]}
        client.send((json.dumps(command) + "\n").encode("utf-8"))
        client.close()
       
    except Exception as e:
        print(f"Errore nel contattare MPV: {e}")

# Collega la funzione alla pressione del bottone
button.when_pressed = toggle_play_pause

This is my 
python3 toggle_mpv.py

Da provare
Reply
#4
(06-11-2025, 08:46 AM)nadirfly Wrote:
(06-10-2025, 04:12 PM)the_bertrum Wrote: You could try the "toggle" command that flips between play and pause.  Alternatively map the button to a script that checks the status and stops or plays accordingly.

from gpiozero import Button
from signal import pause
import socket
import json

# GPIO17 = pin fisico 11
button = Button(17)

# Path del socket creato da MPV
MPV_SOCKET = "/tmp/mpvsocket"

def toggle_play_pause():
    try:
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(MPV_SOCKET)
        # MPV usa JSON per i comandi
        command = {"command": ["cycle", "pause"]}
        client.send((json.dumps(command) + "\n").encode("utf-8"))
        client.close()
       
    except Exception as e:
        print(f"Errore nel contattare MPV: {e}")

# Collega la funzione alla pressione del bottone
button.when_pressed = toggle_play_pause

This is my 
python3 toggle_mpv.py

Da provare

That's pretty nifty, but I was just thinking of the MPC Toggle command. Having said that, I don't like certain aspects of the toggle so use a script as well, but not as sophisticated as yours.
----------------
Robert
Reply
#5
(06-11-2025, 08:46 AM)nadirfly Wrote:
(06-10-2025, 04:12 PM)the_bertrum Wrote: You could try the "toggle" command that flips between play and pause.  Alternatively map the button to a script that checks the status and stops or plays accordingly.

from gpiozero import Button
from signal import pause
import socket
import json

# GPIO17 = pin fisico 11
button = Button(17)

# Path del socket creato da MPV
MPV_SOCKET = "/tmp/mpvsocket"

def toggle_play_pause():
    try:
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        client.connect(MPV_SOCKET)
        # MPV usa JSON per i comandi
        command = {"command": ["cycle", "pause"]}
        client.send((json.dumps(command) + "\n").encode("utf-8"))
        client.close()
       
    except Exception as e:
        print(f"Errore nel contattare MPV: {e}")

# Collega la funzione alla pressione del bottone
button.when_pressed = toggle_play_pause

This is my 
python3 toggle_mpv.py

Da provare

Hi mate! Mind me if you can, share and do instruction to work with buttons i have harware momentary here. I just want to use these button to run. And can you share the code and it is necessary to add a command on peripheral gpio button command after importing a code. 
Button 1: play/pause gpio 22
Button 2: shutdown gpio 4
Button 3: next
Button 4: prev

Thank you
Reply


Forum Jump: