Interface and application programming (07/05/2014) Write an application that interfaces with an input or output device

For this week I am going to use the original ‘hello light 45’ board, FTDI cable and python. Basically  I am going to use Neil’s python program as the base for my work and slightly modify it.

Before beginning to program we need to setup our PC to recognize the FTDI cable and the hello light board and connect them to the python.

If you are working on Windows first you need to install the python, then add it to your system’s path, install pyserial and finally install Tkinter. I used the instructions from the Providence lab’s page.


Next is to modify the python program. Below is the final program. I have marked the parts which I have modified.




from Tkinter import *

import serial



WINDOW = 600 # window size

eps = 0.5 # filter time constant

filter = 0.0 # filtered value


#--maz--

def draw_circle(canvas, centre, radius, colour, tag):

        canvas.create_oval(

            int(centre[0] - radius),

            int(centre[1] - radius),

            int(centre[0] + radius),

            int(centre[1] + radius),

            outline=colour,

            fill=colour,

   tags=tag

            )


def move_circle(canvas, centre, radius,tag):

        canvas.coords(

      'ova1',

            int(centre[0] - radius),

            int(centre[1] - radius),

            int(centre[0] + radius),

            int(centre[1] + radius),

            )

#--endmaz--


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)

   #---maz---

   #canvas.coords('rect1',.2*WINDOW,.05*WINDOW,x,.2*WINDOW)

   #canvas.coords('rect2',x,.05*WINDOW,.9*WINDOW,.2*WINDOW)

   if x>0:

      move_circle(canvas, [300,300],x/2+10,'ova1')

   else:

      move_circle(canvas, [300,300],x+10,'ova1')  

   #---endmaz---   

   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('hello.light.45.py (q to exit)')

root.bind('q','exit')

#---maz----

#canvas = Canvas(root, width=WINDOW, height=.25*WINDOW, background='white')

#canvas.create_text(.1*WINDOW,.125*WINDOW,text=".33",font=("Helvetica", 24),tags="text",fill="#0000b0")

#canvas.create_rectangle(.2*WINDOW,.05*WINDOW,.3*WINDOW,.2*WINDOW, tags='rect1', fill='#b00000')

#canvas.create_rectangle(.3*WINDOW,.05*WINDOW,.9*WINDOW,.2*WINDOW, tags='rect2', fill='#0000b0')

#canvas = Canvas(root, width=WINDOW, height=.25*WINDOW, background='white')

canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')

canvas.create_text(300,580,text=".33",font=("Helvetica", 24),tags="text",fill="#0000b0")

draw_circle(canvas, [300,300], 5, '#228b22','ova1')

draw_circle(canvas, [300,300], 5, '#ffd700','ova2')

#---endmaz---


canvas.pack()

#

# start idle loop

#

root.after(100,idle,root,canvas)

root.mainloop()


Basically it is a circle which changes its diameter by the level of light which is received by the light sensor on the hello boards. When it receives more light the diameter is smaller and vice versa. In front there are two pictures showing the sensor receiving light from my mobile’s flash light and in normal room light.