2015-01-06 113 views
0

我正在使用这里的一些代码从我的linux笔记本电脑上的usb鼠标获取x,y三角洲。这是一个脚本,可以获取deltas并用matplotlib绘制它。但主要问题是,我不能停止测量而不杀死整个脚本。我在编程方面仍然是初学者,所以任何帮助都会很好。如何在不停止整个脚本的情况下停止数据测量

我的代码:

import struct 
import matplotlib.pyplot as plt 
import numpy as np 
import time 
from drawnow import * 

file = open("/dev/input/mouse2", "rb"); 
test = [] 
plt.ion() 

def makeFig(): 
plt.plot(test) 
#plt.show() 

def getMouseEvent(): 
    buf = file.read(3); 
    button = ord(buf[0]); 
    bLeft = button & 0x1; 
    x,y = struct.unpack("bb", buf[1:]) 
    print ("x: %d, y: %d\n" % (x, y)) 
    return x,y 


while True: 
test.append(getMouseEvent()) 
drawnow(makeFig) 

file.close(); 
+0

你只想鼠标数据的追加切换到'test',或做你想摆脱'while'循环吗? –

回答

0

你必须在你想要的脚本停止什么条件来决定。例如,这将在5秒后停止:

start_time = time.time() 
elapsed = 0 
while elapsed < 5: 
    elapsed = time.time() - start_time: 
    test.append(getMouseEvent()) 

drawnow(makeFig) 

如果你想让它在100个测量停止:

count = 0 
while count < 100: 
    count += 1 
    test.append(getMouseEvent()) 
    time.sleep(1) # <-- optional 

drawnow(makeFig) 
+0

我喜欢你的第一个解决方案。在我问这里之前,我确实尝试了数数的事情,我忘了告诉^^ – user3759978

相关问题