from Tkinter import *
import serial

COMPORT = 'com3'

ardu = serial.Serial(COMPORT,9600,timeout=0, writeTimeout=0)

class Dcounter:           #counter calss def, for updating the display number
    def __init__(self):
        self.D = 0
        self.max=0
        self.min=0

#=====================================================================================

#this part defines the calls that the GUI sends to the Arduino and manages the
#counters so they follow the motor's position



def zeroing(C):           #makes motor go to 0 position 
    ardu.write('z')       #sends the zero position command to the arduino
    C.D=0                 #sets the counter internal variable to 0
    counterVar.set(C.D)   #sets the counter visible variable to internal value

def smallup(C):           #raises the position by 1 degree
    ardu.write('u')       #sends the small up command to the arduino
    if C.D<90:            #check if angle is not over the limit
        C.D+=1            #++ the counter value
    counterVar.set(C.D)   #sets the counter visible variable to internal value
    
def bigup(C):             #raises the position by 5 degrees
    ardu.write('U')       #sends the large up command to the arduino
    if C.D<86:            #check if angle is not over the limit
        C.D+=5            #++5 the counter value
    else:
        C.D=90            #if the angle will overshoot over the limit, change it to the max 
    counterVar.set(C.D)

def smalldown(C):         #same as small up, but down
    ardu.write('d')
    if C.D>-90:
        C.D-=1
    counterVar.set(C.D)

def bigdown(C):          #same as large up, but down
    ardu.write('D')
    if C.D>-86:
        C.D-=5
    else:
        C.D=-90
    counterVar.set(C.D)

def setmin(C):           #establishes a minimum value
    ardu.write('n')
    C.min=C.D
    minVar.set(C.min)
def setmax(C):           #establishes a maximum value
    ardu.write('x')
    C.max=C.D
    maxVar.set(C.max)

def gotomin(C):          #makes motor go to the minimum value established
    ardu.write('g')
    C.D=C.min
    counterVar.set(C.D)
def gotomax(C):          #makes motor go to the maximum value established
    ardu.write('G')
    C.D=C.max
    counterVar.set(C.D)

def startOneHzCycle():   #makes motor do cyclic motion between min position and max position with 1hz frequency
    ardu.write('1')

def startHalfHzCycle():  #makes motor do cyclic motion between min position and max position with 0.5hz frequency
    ardu.write('2')
    
def stopCycle():         #stops cycle movement
    ardu.write('s')
    
def resetPos(C):         #resets all parameters of motor and counter
    ardu.write('r')
    C.D=0
    C.max=0
    C.min=0
    counterVar.set(C.D)
    maxVar.set(C.max)
    minVar.set(C.min)

#=====================================================================================


#this part bind the functions to the different buttons and
#defines the look of all the buttons in the GUI
win=Tk()
counter=Dcounter()
bt1= Button(win, text='+1',width='5',height='3',fg='blue', font='Helvetica 10 ', command=lambda: smallup(counter))
bt2= Button(win, text='-1',width='5',height='3',fg='red' , font='Helvetica 10 ',command=lambda: smalldown(counter))
bt3= Button(win, text='Go To Zero Servo Pos', command=lambda: zeroing(counter))
bt4= Button(win, text='Set Max Servo Position', command=lambda: setmax(counter))
bt5= Button(win, text='Set Min Servo Position', command=lambda: setmin(counter))
bt6= Button(win, text='Go To Max Servo Position', command=lambda: gotomax(counter))
bt7= Button(win, text='Go To Min Servo Position', command=lambda: gotomin(counter))
bt8= Button(win, text='Start 1Hz Cycle', command=startOneHzCycle)
bt9= Button(win, text='Start 0.5Hz Cycle', command=startHalfHzCycle)
bt10= Button(win, text='STOP',bg='red', command=stopCycle)
bt11= Button(win, text='Reset Set Position', command=lambda: resetPos(counter))
bt12= Button(win, text='+5',width='5',height='3',fg='blue', font='Helvetica 10 bold', command=lambda: bigup(counter))
bt13= Button(win, text='-5',width='5',height='3',fg='red', font='Helvetica 10 bold',  command=lambda: bigdown(counter))
line1=Label(win, text='------------------------------------------------', font='Helvetica 18')
line2=Label(win, text='------------------------------------------------', font='Helvetica 18')
maxVar,minVar,counterVar=IntVar(),IntVar(),IntVar()
counterVar.set(counter.D)
minVar.set(counter.min)
maxVar.set(counter.max)
counterlabel=Label(win, textvariable=counterVar,font='Helvetica 18 bold')
minlabel=Label(win, textvariable=minVar,font='Helvetica 10 bold')
maxlabel=Label(win, textvariable=maxVar,font='Helvetica 10 bold')


#=====================================================================================


#this part establishes the layout of the buttons as seen by the user
bt1.grid(row=0, column=2)
counterlabel.grid(row=0, column=1)
bt2.grid(row=0, column=0)
bt3.grid(row=1, column=1)
bt12.grid(row=1, column=2)
bt13.grid(row=1, column=0)
bt4.grid(row=4, column=2)
bt5.grid(row=4, column=0)
minlabel.grid(row=5, column=0)
maxlabel.grid(row=5, column=2)
line1.grid(row=3,columnspan=3)
bt6.grid(row=2, column=2)
bt7.grid(row=2, column=0)
bt8.grid(row=7, columnspan=3)
bt9.grid(row=8, columnspan=3)
line2.grid(row=9,columnspan=3)
bt10.grid(row=10,columnspan=3)
bt11.grid(row=11,columnspan=3)





win.mainloop()

