from Tkinter import *
import serial

eps = 0.75 # filter fraction
filt = 0.0 # filtered value

class MainWindow(Frame):
    """
    Create a homepage and a page for each region that will appear if that region is touched
    """
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
        self.msg = Label(self, text='Welcome to the beta Interactive Brain.\n\
Touch a region of the brain to see more about it.')
        self.msg.pack(padx=20,pady=10)
        # self.button = Button(self, text="Frontal Lobe", 
        #                         command=self.frontal_lobe)
        # self.button.pack(padx=10)
        self.button_close = Button(self, text="Quit", 
                                command='exit')
        self.button_close.pack(padx=10,pady=10)

    def frontal_lobe(self):
        root = Toplevel(self)
        root.title('Frontal Lobe')

        # setting names as variables to handle calling 5 regions
        region_name = StringVar()
        Label(root, textvariable=region_name).pack(pady=10)
        region_name.set('Frontal Lobe')

        # separator is showing in homepage instead of region page????
        # separator = Frame(height=2, bd=1, relief=SUNKEN)
        # separator.pack(fill=X, padx=5, pady=5)

        region_description = StringVar()
        Label(root, textvariable=region_description).pack(fill=BOTH, expand=1, padx=10,pady=10)
        region_description.set("This is the frontal lobe.\n\
        The region that is asssociated with reasoning, planning\n\
        parts of speech, movement, emotions, and problem solving")

        close_button = Button(root, text="Close", command=lambda:self.deleteChild(root))
        close_button.pack(pady=10)
        
        # parent.after_idle(idle,parent)

    def deleteChild(self, w):
    	w.destroy()

    @staticmethod
    def idle():
        """
        Read serial data, if a region is touched call the respective window from MainWindow class
        """
        global filt, 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_low = ord(ser.read())
        up_high = ord(ser.read())
        down_low = ord(ser.read())
        down_high = ord(ser.read())
        up_value = 256*up_high + up_low
        down_value = 256*down_high + down_low
        value = (up_value - down_value)
        filt = (1-eps)*filt + eps*value
        # deltaup = up_high-down_low
        # diff = deltaup - lastup
        # if diff<0: upS = -diff
        # else: upS=diff
        # lastup = deltaup;
        return abs(filt)

#
#  check command line arguments
#
if (len(sys.argv) != 2):
   print "command line: delta.serial.ss.py serial_port"
   sys.exit()
port = sys.argv[1]
#
# open serial port
#
ser = serial.Serial(port,9600)
ser.setDTR()
#
# set up GUI
#
if __name__ == "__main__":
    root = Tk()
    main = MainWindow(root)
    root.title('brain beta')
    main.pack(side="top", fill="both", expand=True)
    # diff = root.after(100,idle,root)
    diff = main.idle()
    print diff
    if diff>3000:
        print diff
        main.frontal_lobe()
    root.mainloop()