##GUI and control software:: ---------------------------------------------

win = GraphWin("Blast Furnace GUI V0.1",500,500)  #initialize graphics window
win.setCoords(-300,-300,100,100) #adjust coordinates
Line(Point(-100,0),Point(100,0)).draw(win)          #Setup data plot
Line(Point(0,-100),Point(0,100)).draw(win)          #^
Rectangle(Point(-100,-100),Point(100,100)).draw(win)# ^
graphArray = [] #pen for graph objects
dataA = [0,0,0] #list for plot data

#setup Buttons for GUI:---------
FanButton = Rectangle(Point(-290,0),Point(-190,50))
FanButton.draw(win)
FanLabel = Text(Point(-240,25),"Fan")
FanLabel.draw(win)
fanCircle = Circle(Point(-175,25),10)
fanCircle.setFill("red")
fanCircle.draw(win)

O2Button = Rectangle(Point(-290,-60),Point(-190,-10))
O2Button.draw(win)
O2Label = Text(Point(-240,-35),"O2")
O2Label.draw(win)
O2Circle = Circle(Point(-175,-32),10)
O2Circle.setFill("red")
O2Circle.draw(win)

ThermocoupleButton = Rectangle(Point(-290,-120),Point(-190,-70))
ThermocoupleButton.draw(win)
ThermocoupleLabel = Text(Point(-240,-95),"Thermocouple")
ThermocoupleLabel.draw(win)
TCbox = Entry(Point(0,-150),6)
TCbox.setText(str(0))
TCbox.draw(win)
TCboxT = Text(Point(-85,-150),"Thermocouple read")
TCboxT.draw(win)

FuelButton = Rectangle(Point(-290,-180),Point(-190,-130))
FuelButton.draw(win)
FuelLabel = Text(Point(-240,-155),"Fuel")
FuelLabel.draw(win)
Fuelbox = Entry(Point(0,-180),6)
Fuelbox.setText(str(.1))
Fuelbox.draw(win)
FuelboxT = Text(Point(-65,-180),"Fuel duration")
FuelboxT.draw(win)

OffButton = Rectangle(Point(-290,-240),Point(-190,-190))
OffButton.draw(win)
OffLabel = Text(Point(-240,-215),"Off")
OffLabel.draw(win)

#end setup Buttons for GUI:---------

def getButton(x,y):
    #return which button was pressed, if any
    Button = 0
    if -290<x<-190:
        if 50>y>0:
            Button = 1
        elif -10>y>-60:
            Button = 2
        elif -70>y>-120:
            Button = 3
        elif -130>y>-180:
            Button = 4
        elif -190>y>-240:
            Button = 5
    return Button

def checkMouse():
    #edited checkMouse() for float output
    P = win.checkMouse() #load checkMouse into point object
    try:
        return P.getX(),P.getY() #get X and Y from P
    except:
        return 9999,9999 #numerical note for case: 'None'
    
def wipeGraph(graphArray):
    #clears the graph data
    for a in graphArray:
        a.undraw()

def plotArray(dataA,win):
    #plot a given array of data 
    L = len(dataA)              #scale the plot increment to fit the window
    t = 100.0/L                 #  ^
    dataMax = max(dataA)        #^
    dataMin = min(dataA)        # ^
    rangeData = dataMax-dataMin #   ^
    P0 = Point(0,0) #initialize points for line drawing
    n = 1 #incrementor for drawing
    for a in dataA:
        #loop through dataA and plot points
        try:
            img = (float(a-dataMin)/rangeData)*100 #scale the data to the window
        except:
            img = 0
        P1 = Point(n,img)   #create new line element
        L = Line(P0,P1)     #^
        L.setFill("red")    # ^
        graphArray.append(L) #note L so it can be erased later
        L.draw(win) #draw L
        P0 = P1 #cycle through points
        n = n + t #increase incrementor 
    return 0

def smooth(data):
    #the analog data collected tends to be unstable,
    #this function averages the data so that the real-time graph is more useful
    #the root data is left unmolested
    n = 0
    l = 0
    c = 0
    D = []
    while l < len(data):
        if n < 3:
            c = c + data[l]
        n = n + 1
        if n == 3:
          D.append(c/3)
          n = 0
          c = 0
        l = l + 1
    if n != 0:
        D.append(c/n)
    return D

##End GUI and control software:: ---------------------------------------------