#
# hello.HC-SR501.py
#
# HC-SR501 motion detectorr hello-world
#    hello.HC-SR501.py serial_port
#
# Neil Gershenfeld 11/16/15
# (c) Massachusetts Institute of Technology 2015
#
# This work may be reproduced, modified, distributed,
# performed, and displayed for any purpose. Copyright is
# retained and must be preserved. The work is provided
# as is; no warranty is provided, and users accept all 
# liability.
#

# very slightly modified by Gerard B (FabAcademy 16 #277) for week 11 assignement)

from Tkinter import *
import serial

WINDOW = 200 # window size

def idle(parent,canvas):
   global filt,eps
   #
   # idle routine
   #
   char = ser.read()
   
   # Just to make things more clear, I converted the received char in hex format
   canvas.itemconfigure("text",text=hex(ord(char)) + " : " + char)
   
   if (ord(char) == 73):
      x = 1
      text = 'enter'
      canvas.itemconfigure("rect",fill="#b00000")
      canvas.itemconfigure("text",text="enter")
   else:
      x = 0
      text = 'leave'
      canvas.itemconfigure("rect",fill="#0000b0")
      canvas.itemconfigure("text",text="leave")
   canvas.update()
   parent.after_idle(idle,parent,canvas)

#
#  check command line arguments
#
if (len(sys.argv) != 2):
   print "command line: hello.HC-SR501.py serial_port"
   sys.exit()
port = sys.argv[1]
#
# open serial port
#
ser = serial.Serial(port,9600)
ser.setDTR()
ser.flush()
#
# set up GUI
#
root = Tk()
root.title('hello.HC-SR501.py (q to exit)')
root.bind('q','exit')
canvas = Canvas(root, width=2*WINDOW, height=WINDOW, background='white')
canvas.create_text(.5*WINDOW,.5*WINDOW,text="read",font=("Helvetica", 24),tags="text",fill="#0000b0")
canvas.create_rectangle(WINDOW,0,2*WINDOW,WINDOW, tags='rect', fill='#b00000')
canvas.pack()
#
# start idle loop
#
root.after(100,idle,root,canvas)
root.mainloop()
