Advertisement
Guest User

raspberry pi gpio buttons

a guest
Sep 22nd, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. from time import sleep
  2. import subprocess
  3. import os
  4. import RPi.GPIO as GPIO
  5. GPIO.cleanup()
  6. GPIO.setmode(GPIO.BOARD)
  7.  
  8. # Buttons
  9. button1 = 21  # GPIO 10 PIN 19  
  10. button2 = 19  # GPIO 09 PIN 21  
  11. button3 = 15  # GPIO 22 PIN 15
  12. button4 = 11  # GPIO 17 PIN 11
  13. button5 = 7   # GPIO 04 PIN  7
  14. # Control LED
  15. ledout   =  8   # GPIO 14 PIN  8
  16. ledout2  = 16   # GPIO 23 PIN  16
  17. LEDON = GPIO.LOW
  18. LEDOFF = GPIO.HIGH
  19.  
  20. # DEBUG
  21. debug = 0
  22. # different modes
  23. next_prev_mode = 0
  24. volume_mode = 1
  25. # current mode
  26. state = next_prev_mode
  27.  
  28. # Set Input Buttons
  29. GPIO.setup(button1, GPIO.IN)
  30. GPIO.setup(button3, GPIO.IN)
  31. GPIO.setup(button2, GPIO.IN)
  32. GPIO.setup(button4, GPIO.IN)
  33. GPIO.setup(button5, GPIO.IN)
  34. # Set Output LED
  35. GPIO.setup(ledout, GPIO.OUT)
  36. GPIO.setup(ledout2, GPIO.OUT)
  37. # and Turn it Off
  38. GPIO.output(ledout, LEDOFF)
  39. GPIO.output(ledout2, LEDOFF)
  40.  
  41. # Function Blink LED
  42. def blink():
  43.  GPIO.output(ledout, LEDON)
  44.  sleep(0.1)
  45.  GPIO.output(ledout, LEDOFF)
  46.  
  47. # Function Call MPC
  48. def mpc(doit,doit2):
  49.  blink()
  50.  if (doit2 ==""):
  51.   p = subprocess.Popen(["mpc", doit], stdout=subprocess.PIPE)
  52.  else:
  53.   p = subprocess.Popen(["mpc", doit,doit2], stdout=subprocess.PIPE)
  54.  output, err = p.communicate()
  55.  print "*** Running mpc \"",doit," ",doit2,"\" command ***\n", output, " err:",err
  56.  print output
  57.  
  58. def cmdit(doit):
  59.  blink()
  60.  p = os.system(doit)
  61.  print "*** Running cmdit \"",doit,"\" command ***\n"
  62.  
  63. #
  64.  
  65. while True:
  66.  
  67.     # X - - - - button 5 button is pressed
  68.         if ( GPIO.input(button5) == True ):
  69.             if (debug == 1) : print("pressed button5", state)
  70.                 GPIO.output(ledout, LEDON)
  71.                 mpc("volume","100")
  72.                 sleep(1.1)
  73.                 GPIO.output(ledout, LEDOFF)
  74.  
  75.     # - X - - - button 4 button is pressed
  76.         if ( GPIO.input(button4) == True ):
  77.             if (debug == 1) : print("pressed button4 ", state)
  78.                 if (state == 0 ):
  79.                  state = volume_mode
  80.                  blink()
  81.                  print ("Volume Mode")
  82.                  GPIO.output(ledout2, LEDON)
  83.                 else:
  84.                  state = next_prev_mode
  85.                  blink()
  86.                  print ("Next Prev Mode")
  87.                  GPIO.output(ledout2, LEDOFF)
  88.  
  89.     # - - N - - state next button is pressed
  90.         if ( GPIO.input(button3) == True ):
  91.         if (debug == 1) : print("pressed button3", state)
  92.                 if (state == next_prev_mode):
  93.                  mpc("prev","")
  94.                  mpc("current","")
  95.                 else:
  96.                  mpc("volume","-10")
  97.                  mpc("status","")
  98.  
  99.     # - - - - P state prev button is pressed
  100.         if ( GPIO.input(button1) == True ):
  101.         if (debug == 1) : print("pressed button1", state)
  102.                 if (state == next_prev_mode):
  103.                  mpc("next","")
  104.                  mpc("current","")
  105.                 else:
  106.                  mpc("volume","+10")
  107.                  mpc("status","")
  108.  
  109.     # - - - X - pause  button is pressed
  110.         if ( GPIO.input(button2) == True ):
  111.         if (debug == 1) : print("pressed button2 ", state)
  112.                 mpc("toggle","")
  113.  
  114.         sleep(0.2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement