from Tkinter import * import thread import time import serial class App: def __init__(self, master): canvas = Canvas(master, width=400, height=780) canvas.pack() self.canvas = canvas # Setup arrays to hold the values for each graph self.values = [] for i in range(0,20): self.values.append(0) self.avgValues = [] for i in range(0,20): self.avgValues.append(0) self.scanValues = [] for i in range(0,20): self.scanValues.append(0) # Create rectangles to be the graphs canvas.create_rectangle(20, 20, 380, 380, fill="yellow") canvas.create_rectangle(20, 400, 380, 760, fill="yellow") self.distanceGraph = [] self.avgDistanceGraph = [] self.scanGraph = [] # Create 20 line elements for each graph x = 20 for i in range(0,20): self.distanceGraph.append(canvas.create_line(x, 370, x + 18, 370, fill="blue")) x = x + 18 x = 20 for i in range(0,20): self.avgDistanceGraph.append(canvas.create_line(x, 370, x + 18, 370, fill="purple")) x = x + 18 x = 20 for i in range(0,20): self.scanGraph.append(canvas.create_line(x, 750, x + 18, 750, fill="blue")) x = x + 18 # Create lines to mark the two thresholds and the zero line canvas.create_line(20, 260, 380, 260, fill="red") canvas.create_line(20, 310, 380, 310, fill="red") canvas.create_line(20, 370, 380, 370, fill="black") # Create lines to represent the four sections of the scan graph canvas.create_line(20 + (5 *18), 400, 20 + (5 *18), 760, fill="black") canvas.create_line(20 + (10 *18), 400, 20 + (10 *18), 760, fill="black") canvas.create_line(20 + (15 *18), 400, 20 + (15 *18), 760, fill="black") def addValue(self, value): # Cap values at 370 if value > 370: value = 370 # Remove the first value from the array and add the new value to the end of the array del self.values[0] self.values.append(value) # Update the 20 lines on the graph to represent the values in the array for i in range(0,20): line = self.distanceGraph[i] coords = self.canvas.coords(line) coords[1] = 370 - self.values[i] coords[3] = 370 - self.values[i + 1] self.canvas.coords(line, *coords) def addAvgValue(self, value): # Cap values at 370 if value > 370: value = 370 # Remove the first value from the array and add the new value to the end of the array del self.avgValues[0] self.avgValues.append(value) # Update the 20 lines on the graph to represent the values in the array for i in range(0,20): line = self.avgDistanceGraph[i] coords = self.canvas.coords(line) coords[1] = 370 - self.avgValues[i] coords[3] = 370 - self.avgValues[i + 1] self.canvas.coords(line, *coords) def addSelfValue(self, value): # Cap values at 370 if value > 370: value = 370 # Remove the first value from the array and add the new value to the end of the array del self.scanValues[0] self.scanValues.append(value) # Update the 20 lines on the graph to represent the values in the array for i in range(0,20): line = self.scanGraph[i] coords = self.canvas.coords(line) coords[1] = 750 - self.scanValues[i] coords[3] = 750 - self.scanValues[i + 1] self.canvas.coords(line, *coords) def funcThread(app, rt, ser): # Read the next line from the serial port data = ser.readline() # If the data is prefixed with Dist then add the value to the Blue Distance graph if data[0:6] == 'Dist: ': try: val = float(data[6:]) app.addValue(val) except ValueError: print "err" # If the data is prefixed with Avrg then add the value to the Purple Average graph if data[0:6] == 'Avrg: ': try: val = float(data[6:]) app.addAvgValue(val) except ValueError: print "err" # If the data is prefixed with Scan then add the value to the Scan graph if data[0:6] == 'Scan: ': try: val = float(data[6:]) app.addSelfValue(val) except ValueError: print "err" # Call the funcThread function again after 50 milliseconds in a loop rt.after(50, funcThread, app, rt, ser) ser = serial.Serial('/dev/tty.usbserial-A6008ckQ', 9600) root = Tk() app = App(root) # Call the funcThread function 50 milliseconds after starting the UI Main Loop root.after(50,funcThread, app, root, ser) root.mainloop()