我目前正在尝试检测覆盆子pi 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的第一个问题,所以如果有任何遗漏或可以更详细的内容,请随时提出。
谢谢,
Pém‘
发布于 2018-05-06 02:28:37
好了,我已经找到了一个解决方案。这就是它,如果它能帮助任何人的话。
#!/usr/bin/env python3
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}
time = 0.0
udevices = context.list_devices(subsystem='bluetooth')
link_up = False
for udevice in udevices :
if udevice.device_type == 'link' and udevice.is_initialized :
link_up = True
print('yiha')
evdevices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
if len(evdevices) > 0:
for evdevice in evdevices:
if evdevice.name.strip() == 'AB Shutter3' and link_up:
print('Device existing: %s' % udevice)
fds[evdevice.fileno()] = evdevice
while True:
r, w, x = select(fds, [], [])
if monitor.fileno() in r:
for udevice in iter(functools.partial(monitor.poll, 0), None):
evdevices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for evdevice in evdevices:
if evdevice.name.strip() == 'AB Shutter3':
if udevice.action == u'add':
print('Device added: %s' % udevice)
fds[evdevice.fileno()] = evdevice
break
if udevice.action == u'remove':
print('Device removed: %s' % udevice)
fds.pop(evdevice.fileno(),None)
break
if evdevice.fileno() in r:
dev = fds[evdevice.fileno()]
for event in dev.read():
if event.type == evdev.ecodes.EV_KEY:
data = evdev.categorize(event)
if data.keystate == 1 and data.event.timestamp() - time > 0.05 :
if data.scancode == 115:
print("Big button")
elif data.scancode == 28:
print("Small button")
time = data.event.timestamp()我很确定几个月后我会惊恐地看着这一点,但就目前而言,它起到了作用。
https://stackoverflow.com/questions/50189901
复制相似问题