Thank you for your donation!


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


Instruction Guide Use hardware momentary switches to control moOde
#12
It would be easy enough to make a config screen for this.

Is this the complete source code for the feature?
Code:
#!/usr/bin/python
#coding: utf8
#script for moode audio GPIO buttons
#on playlist names no spaces

#import
import RPi.GPIO as GPIO
import sys
import time
import datetime
import os
import subprocess

# Set GPIO mode
GPIO.setmode(GPIO.BCM)

# Define the GPIO pins to be used
SW_PREV = 10
SW_NEXT = 11
SW_POWEROFF = 9
SW_PLAY = 12
SW_VOLUP = 20
SW_VOLDOWN = 26
SW_PL1 = 16
SW_PL2 = 13
# Adjust to the button characteristics if necessary
PRELL = 1000

# Code to manage buttons
GPIO.setup(SW_PREV,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_NEXT,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_POWEROFF,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLUP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_VOLDOWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PL1,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PL2,GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Event procedures
def eSW_PREV(channel):
      subprocess.call(['mpc', 'prev' ])
      print str(datetime.datetime.now())[:19] + " previous"

def eSW_NEXT(channel):
      subprocess.call(['mpc', 'next' ])
      print str(datetime.datetime.now())[:19] + " next"

def eSW_POWEROFF(channel):
      subprocess.call(['mpc', 'stop' ])
      subprocess.call(['sudo', 'poweroff' ])
      print str(datetime.datetime.now())[:19] + " shutting down"

def eSW_PLAY(channel):
      subprocess.call(['mpc', 'toggle' ])
      print str(datetime.datetime.now())[:19] + " play"

def eSW_VOLUP(channel):
      subprocess.call(['/var/www/vol.sh', 'up 1' ])
      print str(datetime.datetime.now())[:19] + " volup"

def eSW_VOLDOWN(channel):
      subprocess.call(['/var/www/vol.sh', 'dn 1' ])
      print str(datetime.datetime.now())[:19] + " voldown"

def eSW_PL1(channel):
      subprocess.call(['mpc', 'load playlist1' ])
      print str(datetime.datetime.now())[:19] + " playlist 1"

def eSW_PL2(channel):
      subprocess.call(['mpc', 'load playlist2' ])
      print str(datetime.datetime.now())[:19] + " playlist 2"
     
# Event declarations
GPIO.add_event_detect(SW_PREV, GPIO.RISING, callback = eSW_PREV, bouncetime = PRELL)
GPIO.add_event_detect(SW_NEXT, GPIO.RISING, callback = eSW_NEXT, bouncetime = PRELL)
GPIO.add_event_detect(SW_POWEROFF, GPIO.RISING, callback = eSW_POWEROFF, bouncetime = PRELL)
GPIO.add_event_detect(SW_PLAY, GPIO.RISING, callback = eSW_PLAY, bouncetime = PRELL)
GPIO.add_event_detect(SW_VOLUP, GPIO.RISING, callback = eSW_VOLUP, bouncetime = PRELL)
GPIO.add_event_detect(SW_VOLDOWN, GPIO.RISING, callback = eSW_VOLDOWN, bouncetime = PRELL)
GPIO.add_event_detect(SW_PL1, GPIO.RISING, callback = eSW_PL1, bouncetime = PRELL)
GPIO.add_event_detect(SW_PL2, GPIO.RISING, callback = eSW_PL2, bouncetime = PRELL)

# Actual program sequence
while True:
  time.sleep(1)
The config screen would list the 8 events along with fields for GPIO pin number, Enable/Disable, and Playlist name for the two playlist options. Config would prolly be stored in a simple .conf file.
-Tim
Enjoy the Music!
moodeaudio.org | Mastodon Feed | GitHub
Reply


Messages In This Thread
RE: Use hardware momentary switches to control moOde - by Tim Curtis - 03-25-2019, 02:33 PM

Forum Jump: