import serial;
from Tkinter import *

master = Tk()
thrust = 0
direction = 127
elevator = 127

def printstat(*ignore):
    global thrust;
    global direction;
    global elevator;
    print 'Thrust = ' + str(thrust) + ' | Direction = ' + str(direction) + ' | Elevator = ' + str(elevator);

def thrust10(*ignore):
    global thrust;
    global direction;
    global elevator;
    if thrust==255:
        thrust = 255
    else:
        thrust = thrust + 10
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def thrustdown10(*ignore):
    global thrust;
    global direction;
    global elevator;
    if thrust==0:
         thrust = 0
    else:
         thrust = thrust - 10;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def hover(*ignore):
    global thrust;
    global direction;
    global elevator;
    
    direction = 127;
    elevator = 127;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def down(*ignore):
    ser.write('0 127 127 127 :');

def plusdirection(*ignore):
    global thrust;
    global direction;
    global elevator;
    if direction==255:
        direction = 255
    else:
        direction = direction + 10;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat(); 

def minusdirection(*ignore):
    global thrust;
    global direction;
    global elevator;
    if direction==0:
        direction = 0
    else:
        direction = direction - 10;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def elevatorplus(*ignore):
    global thrust;
    global direction;
    global elevator;

    if elevator==255:
        elevator = 255
    else:
        elevator = elevator + 10;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def elevatorminus(*ignore):
    global thrust;
    global direction;
    global elevator;

    if elevator==0:
        elevator = 0
    else:
        elevator = elevator - 10;
    ser.write(str(thrust) + ' ' + str(direction) + ' ' + str(elevator) + ' 127 :')
    printstat();

def stopall(*ignore):
    global thrust;
    global direction;
    global elevator;
    thrust = 0;
    direction = 127;
    elevator = 127;

    ser.write('0 127 127 127 :');
    print 'All Stopped';

def liftoff(*ignore):
    global thrust;
    thrust = 70;
    ser.write(str(thrust) + ' 127 127 127 :');

ser = serial.Serial('/dev/tty.usbmodemfa141', 9600);    

master.bind('a', thrust10);
master.bind('q', down);
master.bind('z', thrustdown10);
master.bind('n', minusdirection);
master.bind('m', plusdirection);
master.bind('w', elevatorplus);
master.bind('d', elevatorminus);
master.bind('h', hover);
master.bind('p', stopall);
master.bind('l', liftoff);
mainloop()

