我正在尝试为汽车 xbmc 项目编写代码。我用下拉电阻制作了自己的按钮键盘,并插入了 GPIO 端口。安装了 python 和 GPIO 插件。我的目标是捕捉按钮按下,如果按住按钮 1.5 秒,它将执行与 xbmc 不同的命令(例如,如果按住,右键将跳到下一曲目)。我对 Python 不太熟悉,所以到我现在的位置是一个相当长的过程。我选择使用 GPIO.add_event_detect() 函数,因为它有一个内置的去抖动器,但我认为它是新的,因为我找不到很多使用它的例子。这就是引发错误的原因:
Traceback (most recent call last):
File "buttons.py", line 15, in <module>
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) >= 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")