Edit january 03 2015 import sys ->import sys,os
EDIT : UPDATE CODE
Hi!
I’m proud to show you my code to add physical buttons to my Volumio project.
Code is inspirated from what I read here and on the web.
It provide buttons for :
-previous
-next
-stop
-play
-stop
-volume control through rotary coder
It is written for Raspberry b+, so for other models need to be adapted.
Script is not finished yet but it works !!!
Now, I have to manage for the display but I’m waiting for my order to arrive…
It’s my first python script … so if you see error or something wrong, let me know
Now the code
[code]#!/usr/bin/python
button-rasp
December 28, 2014
written for Raspberry B+ :for other model please re-assign gpio port!!!
add physical buttons for a mdp player system (used with Volumio)
Provide : -previous
-next
-stop
-play
-volume control through rotary coder
My first python script !!! Surely not perfect…To be improved
To do : add code to manage lcd display
inspirated from :
radio.py
January 23, 2013
Written by Sheldon Hartling for Usual Panic.
MIT license, all text above must be included in any redistribution
Rotary class part from : Author : Bob Rathbone
import RPi.GPIO as GPIO
import sys,os
#import mpd
import time
#import random
from rotary_class import RotaryEncoder
import subprocess
#-----------------------------------------------------------
GPIO.setmode(GPIO.BCM)
Define GPIO output pins for Radio Controls
SW_PREV = 12 # Pin 32
SW_NEXT = 16 # Pin 36
SW_STOP = 20 # Pin 38
SW_SHTDWN = 21 # Pin 40
SW_PLAY = 26 # Pin 37
Define GPIO for rotary encoder + button
PIN_A = 23 # Pin 16
PIN_B = 24 # Pin 18
BUTTON = 25 # Pin 22
#------------------------------------------------------------
#As I have a PI-amp from Iqaudio I need to unmute it at startup
#A piece of code will be used to mute it at shutdown
#------------------------------------------------------------
#Code to manage Rotary encoder + switch
This is the event callback routine to handle events
def switch_event(event):
if event == RotaryEncoder.CLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘+2’ ])
time.sleep(.2)
elif event == RotaryEncoder.ANTICLOCKWISE:
subprocess.call([‘mpc’, ‘volume’, ‘-2’ ])
time.sleep(.2)
elif event == RotaryEncoder.BUTTONDOWN:
subprocess.call([‘mpc’, ‘toggle’ ])
print ‘toggle’
#elif event == RotaryEncoder.BUTTONUP:
#print “Button up”
return
Define the switch
rswitch = RotaryEncoder(PIN_A,PIN_B,BUTTON,switch_event)
#----------------------------------------------------------
#code to manage BUTTONS previous,next,stop,play,shutdown
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_STOP,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_SHTDWN,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(SW_PLAY,GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
try:
prev_switch = GPIO.input(SW_PREV)
next_switch = GPIO.input(SW_NEXT)
stop_switch = GPIO.input(SW_STOP)
shtdwn_switch = GPIO.input(SW_SHTDWN)
play_switch = GPIO.input(SW_PLAY)
if prev_switch == False:
subprocess.call([‘mpc’, ‘prev’ ])
print “PRECED”
elif next_switch == False:
subprocess.call([‘mpc’, ‘next’ ])
print “SUIVANT”
elif stop_switch == False:
subprocess.call([‘mpc’, ‘stop’ ])
print “stop”
elif play_switch == False:
subprocess.call([‘mpc’, ‘play’ ])
print “play”
elif shtdwn_switch == False:
os.system(“shutdown now -h”)
print “shutdown in progress : wait 20 sec before unplug”
time.sleep(0.5)
except KeyboardInterrupt:
print "\nExit"
GPIO.setwarnings(False)
GPIO.cleanup()
sys.exit(0)
End of program[/code]