Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO
- import time
- import os
- import subprocess
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(3,GPIO.IN)
- GPIO.setup(5,GPIO.IN)
- GPIO.setup(7,GPIO.IN)
- GPIO.setup(19,GPIO.IN)
- GPIO.setup(21,GPIO.IN)
- GPIO.setup(23,GPIO.IN)
- devnull = open(os.devnull, 'wb')
- prev_input_stop = 1
- prev_input_ff = 1
- prev_input_rw = 1
- counter_stop = 0
- counter_ff = 0
- counter_rw = 0
- def shutdown():
- command = "sudo shutdown -h now"
- process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
- output = process.communicate()[0]
- print output
- def stop():
- command = "mpc -q stop"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- def toggle():
- command = "mpc -q toggle"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- def vol_up():
- command = "mpc -q volume +10"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- def vol_down():
- command = "mpc -q volume -10"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- def next():
- command = "mpc -q next"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- def prev():
- command = "mpc -q prev"
- process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
- output = process.communicate()[0]
- #print output
- while True:
- #Read switches
- play_btn = GPIO.input(7)
- ff_btn = GPIO.input(21)
- rw_btn = GPIO.input(19)
- stop_btn = GPIO.input(23)
- ####STOP/POWER BUTTON
- #If a button is pressed and the previous state was not before then do primary action
- if ((not play_btn) and (prev_input_play)):
- toggle()
- if ((not stop_btn) and (prev_input_stop)):
- stop()
- if ((not ff_btn) and (prev_input_ff)):
- vol_up()
- if ((not rw_btn) and (prev_input_rw)):
- vol_down()
- #If a button is pressed and continues to be pressed then add 1 to appropriate counter
- if ((not stop_btn) and (not prev_input_stop)):
- counter_stop = counter_stop + 1
- if ((not ff_btn) and (not prev_input_ff)):
- counter_ff = counter_ff + 1
- if ((not rw_btn) and (not prev_input_rw)):
- counter_rw = counter_rw + 1
- #Update buttons previous states
- prev_input_stop = stop_btn
- prev_input_ff = ff_btn
- prev_input_rw = rw_btn
- prev_input_play = play_btn
- #If any buttons have been released then reset counter to zero
- if (stop_btn):
- counter_stop = 0
- if (ff_btn):
- counter_ff = 0
- if (rw_btn):
- counter_rw = 0
- #If any button is held 2 seconds then run secondary function for that button
- #Power down function requires a longer press
- if (counter_stop == 24):
- shutdown()
- if (counter_ff == 8):
- next()
- if (counter_rw == 8):
- prev()
- time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment