INPUT

In this assignment you had to write an application that interfaces with an input &/or output device

LESSON >> video recording - web

PROCESS

In my case I used the Hello Light Board made at INPUT DEVICES week (assignment 10).

Remember the purpose of this board is to take input from a phototransistor and display it visually using a python program when the board is connected to a computer via a FTDI cable.

Here you can see how a phototransistor works.

I did a little modification of the program made by Toshiro Tabuchi.

The original idea of the program is to change the colour with the change of the light intensity. In my case I did not show any text as the rgb colors.

The complete code of the program is below:

            #
            # hello.light.45.color2.py
            # receive and display light level with color
            # Author: Henry Sanchez | Fab Lab Tecsup
            #
            # Modified from: hello.color.py
            # Author: Toshiro Tabuchi - FAB LAB UNI
            #
            # Modified from: hello.light.45.py
            # Original Author: 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
               bw = int(filter/1024.0*255.0)
               colorBW = '#%02x%02x%02x' % (bw,bw,bw)
               colorRGB = '#%02x%02x%02x' % rainbow(int(filter),1024)
               x = int(.2*WINDOW + (.9-.2)*WINDOW*filter/1024.0)
               canvas.config(bg=colorRGB)
               canvas.itemconfigure("value",text="%.0f"%filter,fill=colorBW)
               canvas.itemconfigure("RGB",text=rainbow(int(filter),1024),fill=colorBW)
               canvas.itemconfigure("rgbhex",text=colorRGB,fill=colorBW)
               canvas.update()
               parent.after_idle(idle,parent,canvas)
            
            
            #---  Rainbow Color Function ---#
            def rainbow(val,rang):
               if val <= rang/5.0:
                  col = (255,int(val/(rang/5.0)*255.0),0)
               elif val <= 2*rang/5.0:
                  col = (int(255.0-(val-rang/5.0)/(rang/5.0)*255.0),255,0)
               elif val <= 3*rang/5.0:
                  col = (0,255,int((val-2.0*rang/5.0)/(rang/5)*255.0))
               elif val <= 4*rang/5.0:
                  col = (0,int(255.0-(val-3.0*rang/5.0)/(rang/5.0)*255.0),255)
               else:
                  col =(int((val-4.0*rang/5.0)/(rang/5.0)*255.0),0,255) 
               return col
                  
            
            #--- 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.color.py (q to exit)')
            root.bind('q','exit')
            canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')
            canvas.pack()
            
            #--- start idle loop ---#
            root.after(100,idle,root,canvas)
            root.mainloop()

            



>>> COMMENTS