Python3读取USB操纵杆

时间:2015-10-06 17:31:11

标签: python python-3.x usb joystick

我对python和python3非常环保。我的大多数背景来自微处理器工作,如arduinos和基本印章。

所以我正在写一些能够从操纵杆控制IP摄像机的东西。计划是使用类似Pi的东西来运行代码。程序将读取usb操纵杆并将其转换为摄像机API的URL。我有这么远,但有一个问题。

代码可以在linux os proberly ubuntu或raspbian上运行

到目前为止,除了我遇到的用于阅读操纵杆的一些代码之外,我似乎还能正常工作。

这只是其余代码的片段

import sys

pipe = open('/dev/input/js0', 'rb') #open joystick 
action = []
while True:
    StickValue = readStik(pipe)
    print ("StickValue")

    def readStick(pipe):
        action = []
        while stop == 1:
            for character in pipe.read(1):
                action += [int(character)]
                if len(action) == 8:
                    StickValue = action
                    action = []
                    stop = 2
                    ##when joystick is stationary code hangs here.
                    return  StickValue


#do some more stuff here while waiting for new joystick inputs apposed to hanging

我可以看到为什么它等待直到所有8字节都被读出然后停止while循环但是我正在努力弄清楚我是如何绕过它还是有更好的方法来阅读操纵杆。我现在一直在看pygame,但这意味着重新编写其余的代码。

由于

1 个答案:

答案 0 :(得分:0)

尝试通过向buffering=0电话添加open来关闭缓冲。