【问题标题】:Defining a file descriptor in python3 - to work with pyudev/evdev在 python3 中定义文件描述符 - 使用 pyudev/evdev
【发布时间】:2018-05-05 13:18:31
【问题描述】:

我目前正在尝试检测树莓派 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'

【问题讨论】:

    标签: python-3.x select file-descriptor evdev pyudev


    【解决方案1】:

    好的,我已经设法找到了解决方案。在这里,如果它可以帮助任何人。

    #!/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()
    

    我敢肯定,几个月后我会惊恐地看着这个,但就目前而言,它确实起到了作用。

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多