Raspberry pi Pygame PiTFT触碰事件-主循环等待检测到触碰

Raspberry pi Pygame PiTFT触碰事件-主循环等待检测到触碰,raspberry-pi,pygame,touch,Raspberry Pi,Pygame,Touch,我使用树莓圆周率和皮夫特来测量传感器并显示结果。 使用来自所有人的提示很好,但有一个问题: 主回路等待触摸lcd。 为了测试主循环,我从程序中删除了所有不重要的部分。 这段代码正在打印触摸和释放后的状态。 只要触摸lcd,主回路就会继续打印,释放lcd会停止回路。我如何重写循环,使其在没有按键的情况下仍能继续运行 #!/usr/bin/python3 import subprocess, pygame, evdev, select pygame.init() surfaceSize = (320

我使用树莓圆周率和皮夫特来测量传感器并显示结果。 使用来自所有人的提示很好,但有一个问题: 主回路等待触摸lcd。 为了测试主循环,我从程序中删除了所有不重要的部分。 这段代码正在打印触摸和释放后的状态。 只要触摸lcd,主回路就会继续打印,释放lcd会停止回路。我如何重写循环,使其在没有按键的情况下仍能继续运行

#!/usr/bin/python3
import subprocess, pygame, evdev, select
pygame.init()
surfaceSize = (320, 240)
##
# Everything that follows is for handling the touchscreen touch events via evdev
##

# Used to map touch event from the screen hardware to the pygame surface pixels.
# (Those values have been found empirically, but I'm working on a simple interactive calibration tool
tftOrig = (3750, 180)
tftEnd = (150, 3750)
tftDelta = (tftEnd [0] - tftOrig [0], tftEnd [1] - tftOrig [1])
tftAbsDelta = (abs(tftEnd [0] - tftOrig [0]), abs(tftEnd [1] - tftOrig [1]))

# We use evdev to read events from our touchscreen
# (The device must exist and be properly installed for this to work)
touch = evdev.InputDevice('/dev/input/touchscreen')

# We make sure the events from the touchscreen will be handled only by this program
# (so the mouse pointer won't move on X when we touch the TFT screen)
touch.grab()
# Prints some info on how evdev sees our input device
print(touch)
# Even more info for curious people
#print(touch.capabilities())

# Here we convert the evdev "hardware" touch coordinates into pygame surface pixel coordinates
def getPixelsFromCoordinates(coords):
    # TODO check divide by 0!
    if tftDelta [0] < 0:
        x = float(tftAbsDelta [0] - coords [0] + tftEnd [0]) / float(tftAbsDelta [0]) * float(surfaceSize [0])
    else:
        x = float(coords [0] - tftOrig [0]) / float(tftAbsDelta [0]) * float(surfaceSize [0])
    if tftDelta [1] < 0:
        y = float(tftAbsDelta [1] - coords [1] + tftEnd [1]) / float(tftAbsDelta [1]) * float(surfaceSize [1])
    else:
        y = float(coords [1] - tftOrig [1]) / float(tftAbsDelta [1]) * float(surfaceSize [1])
    return (int(x), int(y))


# Was useful to see what pieces I would need from the evdev events
def printEvent(event):
    print(evdev.categorize(event))
    print("Value: {0}".format(event.value))
    print("Type: {0}".format(event.type))
    print("Code: {0}".format(event.code))

while True:

    r,w,x = select.select([touch], [], [])
    for event in touch.read():
        if event.type == evdev.ecodes.EV_ABS:
            if event.code == 1:
                X = event.value
            elif event.code == 0:
                Y = event.value
        elif event.type == evdev.ecodes.EV_KEY:
            if event.code == 330 and event.value == 1:
                printEvent(event)
                p = getPixelsFromCoordinates((X, Y))
                if 12 <= p[0] <= 72 and 210 <= p[1] <= 230:
                    num = 0
                if 92 <= p[0] <= 154 and 210 <= p[1] <= 230:
                    num = 1
                if 173 <= p[0] <= 234 and 210 <= p[1] <= 230:
                    num = 2
                if 255 <= p[0] <= 317 and 210 <= p[1] <= 230:
                    num = 3
                if 290 <= p[0] <= 320 and 0 <= p[1] <= 30:
                    pygame.quit()
                    quit()

    print(num)


pygame.quit()
quit()
解决了! 根据@sloth和@Kingsley的评论,我将代码改写为:

    r,w,x = select.select([touch], [], [], 0)
    if ( touch in r ):
        for event in touch.read():
            if event.type == evdev.ecodes.EV_ABS:
                if event.code == 1:
                    X = event.value
                elif event.code == 0:
                    Y = event.value
            elif event.type == evdev.ecodes.EV_KEY:
                if event.code == 330 and event.value == 1:
                    printEvent(event)

现在,即使没有输入,循环也会运行,输入也可以正常工作。

我认为您可以使用select。选择[touch]、[]、[]、0而不是select。选择[touch]、[]、[]IIRCTis的结果是:```设备/dev/input/touchscreen,名称stmpe ts,phys stmpe ts/input0回溯最近的调用last:File events.py,第53行,在for event in touch.read:File/usr/local/lib/python2.7/dist-packages/evdev/eventio.py的第71行中,在read events=input.device\u read\u manyself.fd IOError:[Errno 11]资源暂时不可用异常类型错误:TypeError'super至少接受一个给定的参数0',在被忽略的“`@kopke-@sloth是正确的,第四个参数是select上的超时。所以我认为您看到的是没有发生任何事件,但代码仍在尝试读取它们。代码必须检查touch是否在代码中返回的读取列表r中,这意味着有一些内容可以读取。因此,如果触摸r:对于触摸中的事件。读取:。。。手册如下: