# get Tkinter
from Tkinter import *
import Tkinter
import tkMessageBox

from LEDGrid import *
from LEDSetting import *
from arduino import Arduino


class ContentWindowDraw:

    # __init__: the initializing function
    #       sets up the ContentWindowDraw widget
    #       with an LEDGrid and a configure and quit button
    #   parameters:
    #       self: the required hidden parameter
    #       master: the widget containing this widget
    def __init__(self, master):

        self.master = master
        # set up and initialize frames for the ContentWindowDraw widget
        self.mainframe = Frame(self.master, height=525,width=850)
        self.mainframe.grid(sticky=W+E+N+S, columnspan=4, rowspan=4, padx=10, pady=10)
        self.mainframe.grid_propagate(0)
        
        self.frame1 = Frame(self.mainframe)
        self.frame1.grid(sticky=W+E+N+S, rowspan=3, padx=5, pady=5)
        self.frame2 = Frame(self.mainframe)
        self.frame2.grid(sticky=W+E+N+S, column=2, row=1, padx=5, pady=5)
        
        # place the LEDGrid widget
        self.ledGrid = LEDGridDraw(self.frame1,self.frame2)
        self.ledGrid.retGridFrame().grid(sticky=W+E+N+S)
        
        # create and place the buttons at the bottom of the GUI
        self.configButton = Button(self.mainframe, text="Configure", height = 2, width = 8, command = self.sendLEDValues)
        self.configButton.grid(row=3)
        self.quitButton = Button(self.mainframe, text="Quit", height = 2, width = 8, command = self.quitProgram)
        self.quitButton.grid(column=1,row=3)

    # redrawContent: replaces the name and setting of a given LED
    #   parameters:
    #       self: the required hidden parameter
    #       squareNum: the number of the LED to be adjusted
    #       naming: the name to be set
    #       setting: the duty cycle to be set
    def redrawContent(self, squareNum, naming, setting):
        self.ledGrid.redrawGrid(squareNum, naming, setting)

    # retContent: returns the frame containing the whole content,
    #       so as to allow for packing on upper levels
    #   return: the main frame
    def retContent(self):
        return self.mainframe
    
    def quitProgram(self):
        """this function is called when the user quits the program. It sets
           all of the LED duty cycles to zero, sends them to the device, and
           closes the GUI"""
        
        self.setToZero()
        self.master.destroy()

    def setToZero(self):
        """this function sets all of the LED duty cycles on the GUI to zero and
           sends them to the Arduino"""

        try:
            #open a link with the Arduino microcontroller
            #NOTE: COM3 is specific to the computer that the program is being 
            #      run on so it might have to change
            device = Arduino("COM3")
            
            #for each pin on the Arduino make an 
            #array containing the position of the
            #LED in the GUI and the pin number
            redPin4 = [0,11];
            bluePin3 = [1,9];
            greenPin3 = [2,10];
    
            redPin2 = [3,5];
            bluePin1 = [4,3];
            greenPin1 = [5,4];
    
            redPin3 = [6,8];
            bluePin2 = [7,6];
            greenPin2 = [8,7];
    
            redPin5 = [9,13];
            bluePin4 = [10,44];
            greenPin4 = [11,12];
    
            redPin1 = [12,2];
            bluePin5 = [13,45];
            greenPin5 = [14,46];
            
    
            #turn all the leds off
            device.analogWrite(25,255) #send initial dummy integer to arduino to setup communication
            device.analogWrite(redPin1[1], 255)
            device.analogWrite(redPin2[1], 255)
            device.analogWrite(redPin3[1], 255)
            device.analogWrite(redPin4[1], 255)
            device.analogWrite(redPin5[1], 255)
    
            device.analogWrite(greenPin1[1], 255)
            device.analogWrite(greenPin2[1], 255)
            device.analogWrite(greenPin3[1], 255)
            device.analogWrite(greenPin4[1], 255)
            device.analogWrite(greenPin5[1], 255)
    
            device.analogWrite(bluePin1[1], 255)
            device.analogWrite(bluePin2[1], 255)
            device.analogWrite(bluePin3[1], 255)
            device.analogWrite(bluePin4[1], 255)
            device.analogWrite(bluePin5[1], 255)
        except:
            print "Could not connect to the Arduino."
            return

    # sendLEDValues: sends the duty cycle information defined by the user to the corresponding
    #                Arduino pins in turn illuminating the LEDs with the correct brightness
    #
    def sendLEDValues(self):

        #get the LEDGrid array
        ledarray = self.ledGrid.array
        ledsettings = []

        #get led duty cycle settings specified by the user in the GUI
        for elem in ledarray:
            ledsettings.append(elem.getDutyCycle())

        try:
            #open a link with the Arduino microcontroller
                #NOTE: COM3 is specific to the computer that the program is being 
                #      run on so it might have to change
            device = Arduino("COM3")
            
            #for each pin on the Arduino make an 
            #array containing the position of the
            #LED in the GUI and the pin number
            redPin4 = [0,11];
            bluePin3 = [1,9];
            greenPin3 = [2,10];

            redPin2 = [3,5];
            bluePin1 = [4,3];
            greenPin1 = [5,4];

            redPin3 = [6,8];
            bluePin2 = [7,6];
            greenPin2 = [8,7];

            redPin5 = [9,13];
            bluePin4 = [10,44];
            greenPin4 = [11,12];

            redPin1 = [12,2];
            bluePin5 = [13,45];
            greenPin5 = [14,46];
            

            device.analogWrite(25,255) #send initial dummy integer to arduino to setup communication
            device.analogWrite(25,255) #send initial dummy integer to arduino to setup communication
            
            #write duty cycles to each pin on the Arduino
            device.analogWrite(redPin1[1], 255-ledsettings[redPin1[0]])
            device.analogWrite(redPin2[1], 255-ledsettings[redPin2[0]])
            device.analogWrite(redPin3[1], 255-ledsettings[redPin3[0]])
            device.analogWrite(redPin4[1], 255-ledsettings[redPin4[0]])
            device.analogWrite(redPin5[1], 255-ledsettings[redPin5[0]])

            device.analogWrite(greenPin1[1], 255-ledsettings[greenPin1[0]])
            device.analogWrite(greenPin2[1], 255-ledsettings[greenPin2[0]])
            device.analogWrite(greenPin3[1], 255-ledsettings[greenPin3[0]])
            device.analogWrite(greenPin4[1], 255-ledsettings[greenPin4[0]])
            device.analogWrite(greenPin5[1], 255-ledsettings[greenPin5[0]])

            device.analogWrite(bluePin1[1], 255-ledsettings[bluePin1[0]])
            device.analogWrite(bluePin2[1], 255-ledsettings[bluePin2[0]])
            device.analogWrite(bluePin3[1], 255-ledsettings[bluePin3[0]])
            device.analogWrite(bluePin4[1], 255-ledsettings[bluePin4[0]])
            device.analogWrite(bluePin5[1], 255-ledsettings[bluePin5[0]])

        except:
            print "Could not connect to the Arduino."
            return
    

#root = Tkinter.Tk()
#content = ContentWindowDraw(root)
#root.mainloop()


