jphphotography

Buttons Ver 1.0

Sep 2nd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import os
  4. import subprocess
  5.  
  6. GPIO.setwarnings(False)
  7. GPIO.setmode(GPIO.BOARD)
  8. GPIO.setup(3,GPIO.IN)
  9. GPIO.setup(5,GPIO.IN)
  10. GPIO.setup(7,GPIO.IN)
  11. GPIO.setup(19,GPIO.IN)
  12. GPIO.setup(21,GPIO.IN)
  13. GPIO.setup(23,GPIO.IN)
  14. devnull = open(os.devnull, 'wb')
  15.  
  16. prev_input_stop = 1
  17. prev_input_ff = 1
  18. prev_input_rw = 1
  19. counter_stop = 0
  20. counter_ff = 0
  21. counter_rw = 0
  22.  
  23. def shutdown():
  24.   command = "sudo shutdown -h now"
  25.   process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
  26.   output = process.communicate()[0]
  27.   print output
  28.  
  29.  
  30. def stop():
  31.   command = "mpc -q stop"
  32.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  33.   output = process.communicate()[0]
  34.   #print output
  35.  
  36. def toggle():
  37.   command = "mpc -q toggle"
  38.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  39.   output = process.communicate()[0]
  40.   #print output
  41.  
  42. def vol_up():
  43.   command = "mpc -q volume +10"
  44.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  45.   output = process.communicate()[0]
  46.   #print output
  47.  
  48. def vol_down():
  49.   command = "mpc -q volume -10"
  50.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  51.   output = process.communicate()[0]
  52.   #print output
  53.  
  54. def next():
  55.   command = "mpc -q next"
  56.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  57.   output = process.communicate()[0]
  58.   #print output
  59.  
  60. def prev():
  61.   command = "mpc -q prev"
  62.   process = subprocess.Popen(command.split(), stdout=devnull, shell=False)
  63.   output = process.communicate()[0]
  64.   #print output
  65.  
  66.  
  67. while True:
  68. #Read switches
  69.   play_btn = GPIO.input(7)
  70.   ff_btn = GPIO.input(21)
  71.   rw_btn = GPIO.input(19)
  72.   stop_btn = GPIO.input(23)
  73. ####STOP/POWER BUTTON
  74. #If a button is pressed and the previous state was not before then do primary action
  75.   if ((not play_btn) and (prev_input_play)):
  76.     toggle()
  77.   if ((not stop_btn) and (prev_input_stop)):
  78.     stop()
  79.   if ((not ff_btn) and (prev_input_ff)):
  80.     vol_up()
  81.   if ((not rw_btn) and (prev_input_rw)):
  82.     vol_down()
  83. #If a button is pressed and continues to be pressed then add 1 to appropriate counter
  84.   if ((not stop_btn) and (not prev_input_stop)):
  85.     counter_stop = counter_stop + 1
  86.   if ((not ff_btn) and (not prev_input_ff)):
  87.     counter_ff = counter_ff + 1
  88.   if ((not rw_btn) and (not prev_input_rw)):
  89.     counter_rw = counter_rw + 1
  90. #Update buttons previous states
  91.   prev_input_stop = stop_btn
  92.   prev_input_ff = ff_btn
  93.   prev_input_rw = rw_btn
  94.   prev_input_play = play_btn
  95. #If any buttons have been released then reset counter to zero
  96.   if (stop_btn):
  97.     counter_stop = 0
  98.   if (ff_btn):
  99.     counter_ff = 0
  100.   if (rw_btn):
  101.     counter_rw = 0
  102. #If any button is held 2 seconds then run secondary function for that button
  103. #Power down function requires a longer press
  104.   if (counter_stop == 24):
  105.     shutdown()
  106.   if (counter_ff == 8):
  107.     next()
  108.   if (counter_rw == 8):
  109.     prev()
  110.   time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment