Astrids Fab Academy Projects

Assignment 12: Interface and application programming.

The assignment was to write an application that interfaces with an input and/or output device. My program is made in python with TKinter, a graphical user interface widget set for Python. It is an interface for the hello.light board that visualizes this weeks process:

 

In the movie you can see that the crocodile starts jumping as I cast a shadow over the light sensor on the board.

The process
I did not have experience with any of the languages that were presented in class so I took the hello.light.45.py code provided by Neil and started manipulating it.

Working in Xcode, I changed the colours and shape of the bar that displays the sensor output. Then I tried to do something more by copypasting code from tutorials and seeing what it did. I got great help from Cynthias boyfriend, who explained what each of the initial code lines do and showed me the importance of indentation in the code for example. I also used these websites for TKinter and python that were very helpful:

TKinterbook
TKinter reference

Off course I made lots of mistakes before I had the code working properly. During the process the workflow was to change .py file in Xcode, saved it and then try to flash it to the microcontroller via the terminal. The terminal checks if there is an error in the code and is as nice as to tell you in which line the problem occurs.

Here's the code that makes the crocodile jump up and down, dependent of the sensor value:

#
# hello.light.45.py
#
# receive and display light level
# hello.light.45.py serial_port
#
# Neil Gershenfeld
# CBA MIT 10/24/09
#
# (c) Massachusetts Institute of Technology 2009
# Permission granted for experimental and personal use;
# license for commercial sale available from MIT
#

from Tkinter import *
import serial

WINDOW = 500 # window size
eps = 0.5 # filter time constant
filter = 0.0 # filtered value

 

def idle(parent,canvas):
global filter, eps
#
# idle routine
#
byte2 = 0
byte3 = 0
byte4 = 0
ser.flush()

while 1:
#
# find framing
#
byte1 = byte2
byte2 = byte3
byte3 = byte4
byte4 = ord(ser.read())

if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)):
break

low = ord(ser.read())
high = ord(ser.read())
value = 256*high + low
filter = (1-eps)*filter + eps*value
x = int(.2*WINDOW + (.9-.2)*WINDOW*filter/1024.0)

#canvas.itemconfigure("text",text="%.1f"%filter)
canvas.coords('board',.5*WINDOW,0.9*WINDOW)
canvas.coords('krokodil',.53*WINDOW,x + (0.1*WINDOW))
canvas.update()

parent.after_idle(idle,parent,canvas)

#
# check command line arguments
#

if (len(sys.argv) != 2):
print "command line: hello.light.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('see you later alligator')
root.bind('q','exit')
the_canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')

board = PhotoImage(file="hellolightboard2.gif")

krokodil = PhotoImage(file="bozekrokodil.gif")
#label = Label(image=photo)
#label.image = photo # keep a reference!
#label.pack()

the_canvas.create_text(.5*WINDOW,.05*WINDOW,text=("PLEASE, little board, DO something!!"),font=("Helvetica", 20),tags="text",fill="#ff0099")

the_canvas.create_image(.5*WINDOW,.5*WINDOW, image=board, tags='board')
the_canvas.create_image(.55*WINDOW,.5*WINDOW, image=krokodil, tags='krokodil')
the_canvas.pack()

#
# start idle loop
#
root.after(100,idle,root,the_canvas)
root.mainloop()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

This template was provided free by www.free-templates.org