我目前正在尝试检测树莓派 3 上的蓝牙按钮的连接(该部分有效),一旦连接,检测按钮何时被按下(该部分无效)。
我从 evdev 提供的代码开始,并尝试对其进行调整以供我使用(见下文),但我无法创建正确的文件描述符以与 select 一起使用(如果我正确理解发生了什么)。
import functools
import pyudev
import evdev
from select import select
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='bluetooth')
monitor.start()
fds = {monitor.fileno(): monitor}
finalizers = []
while True:
r, w, x = select(fds, [], [])
if monitor.fileno() in r:
r.remove(monitor.fileno())
for udev in iter(functools.partial(monitor.poll, 0), None):
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for device in devices:
if device.name.strip() == 'AB Shutter3':
if udev.action == u'add':
print('Device added: %s' % udev)
fds[dev.fd] = device #This here breaks. dev.fd undefined.
break
if udev.action == u'remove':
print('Device removed: %s' % udev)
def helper():
global fds
fds = {monitor.fileno(): monitor}
finalizers.append(helper)
break
for fd in r:
dev = fds[fd]
for event in dev.read():
print(event)
for i in range(len(finalizers)):
finalizers.pop()()
问题是当我尝试添加设备时,没有定义 dev.fd。我试图定义它,但我不知道如何定义文件描述符。我应该怎么办 ?
Device added: Device('/sys/devices/platform/soc/3f201000.serial/tty/ttyAMA0/hci0/hci0:64')
Traceback (most recent call last):
File "dev_status.py", line 27, in <module>
fds = {dev.fd:device} #This here breaks. dev.fd undefined.
NameError: name 'dev' is not defined
其他信息:运行 Raspbian Strech 和 Python 3.5.3 的 Raspberry Pi 3
另外,这是我关于 Stack Overflow 的第一个问题,所以如果有任何遗漏或可能更详细,请随时提及。
谢谢,
庞'