【发布时间】:2016-01-03 17:31:08
【问题描述】:
我对 python 和 python3 非常熟悉。我的大部分背景来自于微处理器工作,例如 arduinos 和 basic stamp。
所以我正在编写一些能够通过操纵杆控制 IP 摄像机的东西。计划是使用 Pi 之类的东西来运行代码。该程序将读取 USB 操纵杆并将其转换为相机 API 的 URL。我已经做到了这一点,但有一个问题。
在 linux os proberly ubuntu 或 raspbian 上运行的代码
到目前为止,我似乎工作得很好,除了我在阅读操纵杆时遇到的一小段代码。
这只是其余代码的 sn-p
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,但这意味着对其余代码进行重大重写。
谢谢
【问题讨论】:
标签: python python-3.x usb joystick