首页 > 解决方案 > python rfid evdev 和树莓派 GPIO 输入

问题描述

我有usb rfid。与脚本正常连接:https ://domoticx.com/nfc-rfid-hardware-usb-stick-syc-idic-usb-reader/

我的问题是如何使用 GPIO 按钮创建条件?

我在这个网站上搜索了线程/问题没有找到答案。

import evdev
from evdev import categorize, ecodes

class Device():
    name = 'Sycreader RFID Technology Co., Ltd SYC ID&IC USB Reader'

    @classmethod
    def list(cls, show_all=False):
        # list the available devices
        devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
        if show_all:
            for device in devices:
                print("event: " + device.fn, "name: " + device.name, "hardware: " + device.phys)
        return devices

    @classmethod
    def connect(cls):
        # connect to device if available
        try:
            device = [dev for dev in cls.list() if cls.name in dev.name][0]
            device = evdev.InputDevice(device.fn)
            return device
        except IndexError:
            print("Device not found.\n - Check if it is properly connected. \n - Check permission of /dev/input/ (see README.md)")
            exit()

    @classmethod
    def run(cls):
        device = cls.connect()
        container = []
        try:
            device.grab()
            # bind the device to the script
            print("RFID scanner is ready....")
            print("Press Control + c to quit.")
            for event in device.read_loop():
                    # enter into an endeless read-loop
                    if event.type == ecodes.EV_KEY and event.value == 1:
                        digit = evdev.ecodes.KEY[event.code]
                        if digit == 'KEY_ENTER':
                            # create and dump the tag
                            tag = "".join(i.strip('KEY_') for i in container)
                            print(tag)
                            container = []
                        else:
                            container.append(digit)

        except:
            # catch all exceptions to be able release the device
            device.ungrab()
            print('Quitting.')

Device.run()

所以。我只想要简单的条件

if rfid scan
 print scan
else button press
 print button press

谢谢 :)

标签: pythonif-statementraspberry-pi

解决方案


推荐阅读