#!/usr/bin/python import matplotlib matplotlib.use('TkAgg') from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.backend_bases import key_press_handler from matplotlib.figure import Figure from matplotlib.pyplot import ion import serial from time import sleep, time import Tkinter as _Tk root = _Tk.Tk() root.wm_title('Data-Logging') spinBox = _Tk.Spinbox(root, from_=0, to=240) sbLabel = _Tk.Label(root, text='How long would you like to test (in seconds): ') raw_data = [] _time = [] f = Figure(figsize=(5,4), dpi=100) a = f.add_subplot(111) a.plot(_time, raw_data) a.set_xlabel('Time') a.set_ylabel('Input') a.set_title('Light Reading') ion() canvas = FigureCanvasTkAgg(f, master=root) toolbar = NavigationToolbar2TkAgg(canvas, root) toolbar.update() canvas._tkcanvas.pack(side='bottom', fill='both', expand=1) canvas.get_tk_widget().pack(side='bottom', fill='both', expand=1) def beginLogging(): runTime = int(spinBox.get()) arduino = serial.Serial('/dev/ttyACM0', 9600) sleep(2.0) arduino.flush() arduino.flushInput() arduino.flushOutput() arduino.write('s') startTime = time() while(arduino.isOpen()): raw_data.append(int(arduino.readline())) _time.append(time() - startTime) arduino.flush() a.plot(_time, raw_data) a.set_ylim(0, max(raw_data)+75) canvas.draw() if((time() - startTime) >= runTime): arduino.close() #killButton = _Tk.Button(root, text='Quit', command = root.quit) #killButton.pack(side = 'right', fill = 'both', expand = 0) startButton = _Tk.Button(root, text='Start', command = beginLogging) startButton.pack(side = 'right', fill = 'both', expand = 0) spinBox.pack(side='right', fill='both', expand = 0) sbLabel.pack(side='right', fill='both', expand = 0) def on_key_event(event): print('you pressed %s' %event.key) key_press_handler(event, canvas, toolbar) canvas.mpl_connect('key_press_event', on_key_event) root.mainloop()
Sunday, July 21, 2013
Real-Time Data Acquisition with Tk GUI
Looks like I finally got the interface working the way I want. Here's the source:
Labels:
Interfaces,
SourcePY
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment