Adding enclosure buttons for previous, stop and next

I’d like to add physical pushbuttons for previous track, stop and next track. I hooked up some retro buttons to GPIO pins 2, 3 and 4, and the other sides to ground. I adapted some code below that does print out each of the three messages.

How do I architect this in to Mycroft to send those three commands?

Any help will be appreciated.

#!/usr/bin/env python3

import signal
import sys
import RPi.GPIO as GPIO

BUTTON_GPIO_2 = 2                          # previous track
BUTTON_GPIO_3 = 3                          # stop
BUTTON_GPIO_4 = 4                          # next track

def signal_handler(sig, frame):

def button_pressed_callback(channel):
    match channel:
      case 2:
        print("Previous track button pressed!")
      case 3:
        print("Stop button pressed!")
      case 4:
        print("Next track button pressed!")

if __name__ == '__main__':
    GPIO.setup(BUTTON_GPIO_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(BUTTON_GPIO_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(BUTTON_GPIO_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    GPIO.add_event_detect(BUTTON_GPIO_2, GPIO.FALLING, callback=button_pressed_callback, bouncetime=100)
    GPIO.add_event_detect(BUTTON_GPIO_3, GPIO.FALLING, callback=button_pressed_callback, bouncetime=100)
    GPIO.add_event_detect(BUTTON_GPIO_4, GPIO.FALLING, callback=button_pressed_callback, bouncetime=100)

    signal.signal(signal.SIGINT, signal_handler)

Picture of the buttons:

I added a button to my Pi to either put it in listen mode (short press of button) or stop playing the current music (long press). Take a look at it here: GitHub - WhyNine/pushbutton-speak-stop-skill.

I guess you can do previous/next track in a similar way

@whynine - thanks for the quick reply. I will definitely look at the code. Good idea on the short press and long press!

-Mike Mac

I git clone from GitHub - WhyNine/pushbutton-speak-stop-skill and the code is copied. I try to install it, but it won’t:

$ mycroft-msm install pushbutton-speak-stop-skill
INFO - building SkillEntry objects for all skills
INFO - Best match (0.4): mycroft-speak by mycroftai
MultipleSkillMatches: mycroft-speak, mycroft-stop
$ echo $?

So I copy ~/pushbutton-speak-stop-skill to /opt/mycroft/skills manually, but will not load with the error:

 File "/opt/mycroft/skills/pushbutton-speak-stop-skill/", line 19, in <module>
    import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'

Yet that same import statement works outside of mycroft in the test file I wrote yesterday. Thoughts?

Any help would be appreciated.

-Mike Mac