2015-01-06 107 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

相關問題