简体   繁体   中英

Python pause or stop realtime data

This time I want to know what possible solutions exist to following situation: I let my Laptop read the raw mouse data (Ubuntu OS) with a python script I already posted here. It has a method, which reads the mouse file and extracts the x,y data from it. a while true loop uses this method to put the data into an array. when I use a time counter to stop reading after a while the script puts the data into an excel file. What I now need is a option to pause the data stream, ie to change mouse position without creating data, and then resume it. I would like to have something to stop the reading and to write it into excel.

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

workbook = xlsxwriter.Workbook('/path/test.xlsx')
worksheet = workbook.add_worksheet()
file = open( "/dev/input/mouse2", "rb" );
test = [(0,0,0)]
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:] ) 
  _zeit = time.time()-test[-1][-1]
  print ("x: %d, y: %d, Zeit: %d\n" % (x, y, _zeit) )  
  return x,y, _zeit

zeit = time.time()

warte = 0
while warte < 20:
 test.append(getMouseEvent())
 warte = time.time()-zeit     

row = 1
col = 0
worksheet.write(0,0, 'x-richtung')
worksheet.write('C1', 'Zeit')
for x, y , t in (test):
    worksheet.write(row, col,     x)
    worksheet.write(row, col + 1, y)
    worksheet.write(row, col + 2, t)
    row += 1
chart = workbook.add_chart({'type': 'line'})
chart.add_series({'values': '=Sheet1!$A$1:$A$'+str(len(test))})
worksheet.insert_chart('D2', chart)
workbook.close()
 #drawnow(makeFig)
 #plt.pause(.00001)
file.close();

It would be awesome to have something like "hit space for pause/Unpause. q for end and save" but I have no clue how to do it. Any idea would be nice :) Oh and I tried to Plot the Data with matplotlib, which worked but it's something for future improvements ;)

Here is an example with the standard threading module - I actually don't know how responsive it will be. Also if you want to pause or start input based on a global hotkey, rather than from the script this will depend on your desktop environment - I've only used xlib but there should be a python wrapper for it floating around somewhere.

import threading
import struct

data =[]
file = open("/dev/input/mouse0", "rb") 
e=threading.Event()

def getMouseEvent():
  buf = file.read(3);
  #python 2 & 3 compatibility 
  button = buf[0] if isinstance(buf[0], int) else ord(buf[0])
  bLeft = button & 0x1;
  bMiddle = ( button & 0x4 ) > 0;
  bRight = ( button & 0x2 ) > 0;
  x,y = struct.unpack( "bb", buf[1:] );
  return "L:%d, M: %d, R: %d, x: %d, y: %d\n" % (bLeft,bMiddle,bRight, x, y) 


def mouseCollect():
    global e
    #this will wait while e is False (without breaking the loop)
    #and loop while e is True
    while e.wait(): 
        #do something with MouseEvent data, like append to an array, or redirect to pipe etc. 
        data.append(getMouseEvent()) 
mouseCollectThread = threading.Thread(target=mouseCollect)
mouseCollectThread.start()
#toggle mouseCollect with any keyboard input
#type "q" or "quit" to quit.
while True: 
    x = input() 
    if x.lower() in ['quit', 'q', 'exit']:
        mouseCollectThread._stop()
        file.close() 
        break
    elif x:
        e.clear() if e.isSet() else e.set()

edit: I was missing a () after e.isSet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM