This week’s assignment was to write an application that interfaces an input device we made.
Having very little experience with coding I started looking into Neil’s code to try to modify it and make my own version of it.
I grabbed the python code for the step response board just because I had one laying around.
I started experimenting with simple things like background and color but I really wanted to understand as much of the whole code as I could.
I learned that the columns are made out of two rectangles, a red one and a blue one. The red one enlarges to the right and the blue one shrinks the same direction once the value from the input device changes.
I got rid of the blue rectangle, changed the color of everything and made the columns thinner.
I then changed which direction one of the columns moved and repositioned it along with the number value.
This interface might be handy for an x,y accelerometer.
Here’s the modified version of the code:
- Download Python Code





#
# hello.load.45.py
#
# receive and display loading step response
# hello.step.45.py serial_port
#
# Neil Gershenfeld
# CBA MIT 10/29/10
#
# (c) Massachusetts Institute of Technology 2010
# Permission granted for experimental and personal use;
# license for commercial sale available from MIT
#
from Tkinter import *
import serial
WINDOW = 200 # window size
eps = 0.5 # filter time constant
filter1 = 0.0 # filtered value
filter2 = 0.0 # filtered value
filter3 = 0.0 # filtered value
def idle(parent,canvas):
global filter1, filter2, filter3, eps
#
# idle routine
#
byte2 = 0
byte3 = 0
byte4 = 0
ser.flush()
#
# find framing
#
while 1:
byte1 = byte2
byte2 = byte3
byte3 = byte4
byte4 = ord(ser.read())
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)):
break
#
# read and plot
#
up_low1 = ord(ser.read())
up_high1 = ord(ser.read())
down_low1 = ord(ser.read())
down_high1 = ord(ser.read())
up_low2 = ord(ser.read())
up_high2 = ord(ser.read())
down_low2 = ord(ser.read())
down_high2 = ord(ser.read())
up_low3 = ord(ser.read())
up_high3 = ord(ser.read())
down_low3 = ord(ser.read())
down_high3 = ord(ser.read())
up_value1 = 256*up_high1 + up_low1
down_value1 = 256*down_high1 + down_low1
up_value2 = 256*up_high2 + up_low2
down_value2 = 256*down_high2 + down_low2
up_value3 = 256*up_high3 + up_low3
down_value3 = 256*down_high3 + down_low3
value1 = (up_value1 + (1023 - down_value1))/2.0
value2 = (up_value2 + (1023 - down_value2))/2.0
value3 = (up_value3 + (1023 - down_value3))/2.0
filter1 = (1-eps)*filter1 + eps*value1
filter2 = (1-eps)*filter2 + eps*value2
filter3 = (1-eps)*filter3 + eps*value3
x1 = int(.2*WINDOW + (.9-.2)*WINDOW*filter1/1023.0)
x2 = int(.2*WINDOW + (.9-.2)*WINDOW*filter2/1023.0)
x3 = int(.2*WINDOW + (.9-.2)*WINDOW*filter3/1023.0)
canvas.itemconfigure("text1",text="%.1f"%filter1)
canvas.itemconfigure("text2",text="%.1f"%filter2)
canvas.itemconfigure("text3",text="%.1f"%filter3)
canvas.coords('rect11',.2*WINDOW,.35*WINDOW,x1,.36*WINDOW)
#canvas.coords('rect12',x1,.05*WINDOW,.9*WINDOW,.2*WINDOW)
canvas.coords('rect21',.4*WINDOW,.1*WINDOW,.41*WINDOW,x2)
#canvas.coords('rect22',x2,.3*WINDOW,.9*WINDOW,.45*WINDOW)
#canvas.coords('rect31',.2*WINDOW,.55*WINDOW,x3,.7*WINDOW)
#canvas.coords('rect32',x3,.55*WINDOW,.9*WINDOW,.7*WINDOW)
canvas.update()
parent.after_idle(idle,parent,canvas)
#
# check command line arguments
#
if (len(sys.argv) != 2):
print "command line: hello.load.45.py serial_port"
sys.exit()
port = sys.argv[1]
#
# open serial port
#
ser = serial.Serial(port,9600)
ser.setDTR()
#
# set up GUI
#
root = Tk()
root.title('hello.load.45.py (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=.75*WINDOW, background='black')
#
canvas.create_text(.1*WINDOW,.35*WINDOW,text="1",font=("Helvetica", 19),tags="text1",fill="#fdfd00")
canvas.create_rectangle(.2*WINDOW,.3*WINDOW,.3*WINDOW,.31*WINDOW, tags='rect11', fill='#fdfd00')
#
canvas.create_text(.5*WINDOW,.1*WINDOW,text="2",font=("Helvetica", 19),tags="text2",fill="#fdfd00")
canvas.create_rectangle(.4*WINDOW,.1*WINDOW,.41*WINDOW,.51*WINDOW, tags='rect21', fill='#fdfd00')
#
#canvas.create_text(.1*WINDOW,.625*WINDOW,text="3",font=("Helvetica", 24),tags="text3",fill="#fdfd00")
#canvas.create_rectangle(.2*WINDOW,.55*WINDOW,.3*WINDOW,.7*WINDOW, tags='rect31', fill='#fdfd00')
canvas.pack()
#
# start idle loop
#
root.after(100,idle,root,canvas)
root.mainloop()