title: Using GPIO buttons to control Raspbmc
body: <p>I am trying to write code for a car xbmc project.  I made my own button keypad with pull-down resistors and plugged into the GPIO ports.  Installed python and the GPIO addon.  My goal is to catch button presses, and if the button is held for 1.5 secs, it will execute a different command to xbmc (for example, the right key would skip to the next track if held).  I'm not too familiar with Python, so it's been a pretty long process getting to where I am.  I chose to use the GPIO.add_event_detect() function since it has a built-in debouncer, but I think it's new because I can't find many examples of it being used.  This is what is throwing errors:</p>
  
  <pre><code>Traceback (most recent call last):
    File "buttons.py", line 15, in &lt;module&gt;
      GPIO.add_event_detect(buttons[index], GPIO.RISING, bouncetime=200)
  RuntimeError: Edge detection already enabled for this GPIO channel
  
  
  #!/usr/bin/env python
  import Rpi.GPIO as GPIO
  import time, os, httplib, json
  GPIO.setmode(GPIO.BCM)
  
  buttons = [4, 17, 18, 22, 23, 24, 27]
  numbuttons = len(buttons)
  index = 0
  for index in numbuttons:
      GPIO.setup(index, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  
  while True:
      index = 0
      for index in numbuttons:
          GPIO.add_event_detect(buttons[index], GPIO.RISING, bouncetime=200)
          if GPIO.event_detected(buttons[index]):
              time_pressed = time.time()
              GPIO.add_event_detect(buttons[index], GPIO.FALLING, bouncetime=200)
              if GPIO.event_detected(buttons[index]):
                  time_released = time.time()
                  if (time_released - time_pressed) &gt;= 1.5:
                      button_held(index)
                  else:
                      button_pressed(index)
      time.sleep(0.01)
  
  def button_pressed(index):
      url = "/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22"
      url2 = ",%22id%22:1}"
      player = "Player.GetActivePlayers%22"
      playcode = "Player.PlayPause%22,%22params%22:{%22playerid%22:"
      guidecode = "Player.GetProperties%22,%22params%22:{%22playerid%22:"
      upcode = "Input.Up%22,%22params%22:{%22playerid%22:"
      downcode = "Input.Down%22,%22params%22:{%22playerid%22:"
      leftcode = "Input.Left%22,%22params%22:{%22playerid%22:"
      rightcode = "Input.Right%22,%22params%22:{%22playerid%22:"
      backcode = "Input.Back%22,%22params%22:{%22playerid%22:"
      conn = httplib.HTTPConnection("127.0.0.1:8080")
      if index == 0:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + guidecode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc guide")
      elif index == 1:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + leftcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc left")
      elif index == 2:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + playcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc play/pause")
      elif index == 3:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + upcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc up")
      elif index == 4:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + backcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc backspace")
      elif index == 5:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + downcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc down")
      elif index == 6:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + rightcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc right")
  
  def button_held(index):
      url = "/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22"
      url2 = ",%22id%22:1}"
      player = "Player.GetActivePlayers%22"
      playcode = "Player.PlayPause%22,%22params%22:{%22playerid%22:"
      upcode = "Input.Up%22,%22params%22:{%22playerid%22:"
      downcode = "Input.Down%22,%22params%22:{%22playerid%22:"
      backcode = "Input.Back%22,%22params%22:{%22playerid%22:"
      nextcode = "Player.GoNext%22,%22params%22:{%22playerid%22:"
      prevcode = "Player.GoPrevious%22,%22params%22:{%22playerid%22:"
      shuffcode = "Player.Shuffle%22,%22params%22:{%22playerid%22:"
      mutecode = "Application.SetMute%22,%22params%22:{%22playerid%22:"
      conn = httplib.HTTPConnection("127.0.0.1:8080")
      if index == 0:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + shuffcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc guide")
      elif index == 1:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + prevcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc left")
      elif index == 2:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + playcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc play/pause")
      elif index == 3:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + upcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc up")
      elif index == 4:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + backcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc backspace")
      elif index == 5:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + downcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc down")
      elif index == 6:
          conn.request("GET", url + player + url2)
          r = conn.getresponse()
          j = json.loads(r.read())
          playeron = j["result"]
          if json.dumps(playeron) == "[]":
              conn.request("GET", url + "input.select%22" + url2)
              conn.close()
          else:
              playerid = json.dumps(j["result"][0]["playerid"])
              conn.request("GET", url + nextcode + playerid + "}" + url2)
              conn.close()
          conn.close()
          print("execute xbmc right")
  </code></pre>