%Andrew Costello (aoc6)
%Dylan Hughes (dah64)
%ECE 476 Final Project
%4/30/2007
function elevator(conn,numfloors)
    index = 0;
    floors = zeros(numfloors,2);
    
    shafts = zeros(3,2);
    shafts(1,:) = [100, 200];
    shafts(2,:) = [400, 500];
    shafts(3,:) = [700, 800];
    
    buttons = zeros(2*numfloors-2,2);
    height = zeros(2*numfloors-2);
    for i = 1:numfloors
        floors(i,:) = [50+(i-1)*250, 200+(i-1)*250];
    end
    for i = 1:2*numfloors-2
        if(mod(i,2))
            height(i) = 430 + (i-3)*205/2;
            buttons(i,:) = [150 + (i-1)*125, 125 + (i-1)*125];
        else
            height(i) = 390 + (i-2)*205/2;
            buttons(i,:) = [325 + (i-2)*125, 350 + (i-2)*125];
        end
    end
    height = height+107*(numfloors-4)/4;
    
    background = imread('background.jpg');
    openup = imread('openup.bmp');
    opendown = imread('opendown.bmp');
    closed = imread('closed.jpg');
    off = imread('offbutton.jpg');
    on = imread('onbutton.jpg');
    onarrow = imread('onarrow.jpg');
    offarrow = imread('offarrow.jpg');
while 1
    figure(1);
    axes('Ylim',[0 250*numfloors+200]);
    hold on;
    image(background,'XData',[1,1200],'YData',[1, 250*numfloors]);
    flag = 1;
    loc = [0,0,0];
    outclocks = zeros(2*numfloors-2,6);
    in = zeros(3*numfloors);
    inclocks = zeros(3*numfloors,6);
    instats = 0;
    innum = 0;
    out = zeros(2*numfloors-2);
    callstats = 0;
    callnum = 0;
    for i = 1:3
        for j = 1:numfloors
            image(closed,'XData',shafts(i,:),'YData',floors(j,:));
            if (numfloors<15)
                if (j==1)
                    image(on,'XData',[shafts(i,1)+20*j-10 shafts(i,1)+5+20*j],'YData',[(250*numfloors+200)-225, (250*numfloors+200)-210]);
                else
                    image(off,'XData',[shafts(i,1)+20*j-10 shafts(i,1)+5+20*j],'YData',[(250*numfloors+200)-225, (250*numfloors+200)-210]);
                end
            end
            image(off,'XData',[25 50]+(i-1)*300,'YData',[50,75]+(j-1)*50);
            uicontrol('String',int2str(j),'Units','normalized','Position',[(225+(i-1)*235)/1400 (171+(j-1)*40 + 107*(numfloors-4)/4)/(250*numfloors+200) 25/1400 25/(400+200*numfloors)],'Callback',{'sendbutton',conn,0,j-1,i});
        end
    end
    for i=1:(2*numfloors-2)
        image(offarrow,'XData',[925,950],'YData',buttons(i,:));
        if(mod(i,2))
            uicontrol('String','Up','Units','normalized','Position',[950/1400 height(i)/(250*numfloors+200) 25/1400 25/(250*numfloors+200)],'Callback',{'sendbutton',conn,1,i,0});
        else
            uicontrol('String','D','Units','normalized','Position',[950/1400 height(i)/(250*numfloors+200) 25/1400 25/(250*numfloors+200)],'Callback',{'sendbutton',conn,1,i,0});
        end
    end
    while flag
        pause(.1)
        x = 'nothing';
        if(conn.bytesavailable>0)
            x = fgets(conn);
        end
        if(strncmp(x,'arrived',7))
            i = str2num(x(8));
            j = str2num(x(9))*10+str2num(x(10))+1;
            if(numfloors<15)
                image(off,'XData',[shafts(i,1)+20*(loc(i)+1)-10 shafts(i,1)+5+20*(loc(i)+1)],'YData',[(250*numfloors+200)-225, (250*numfloors+200)-210]);
                image(on,'XData',[shafts(i,1)+20*j-10 shafts(i,1)+5+20*j],'YData',[(250*numfloors+200)-225, (250*numfloors+200)-210]);
            end
            loc(i)=j-1;
        elseif(strncmp(x,'opening',7))
            z = str2num(x(8));
            if(str2num(x(9))==2)
                image(opendown,'XData',shafts(z,:),'YData',floors(loc(z)+1,:));
            else
                image(openup,'XData',shafts(z,:),'YData',floors(loc(z)+1,:));
            end
            
        elseif(strncmp(x,'closing',7))
            z = str2num(x(8));
            image(closed,'XData',shafts(z,:),'YData',floors(loc(z)+1,:));
        elseif(strncmp(x,'init',4))
            flag = 0;
        elseif(strncmp(x,'outb',4))
            i = str2num(x(5))*10+str2num(x(6));
            if(str2num(x(7)))
                image(onarrow,'XData',[925,950],'YData',buttons(i,:));
                if(out(i)==0)
                    outclocks(i,:) = clock();
                end
                out(i) = 1;
            else
                image(offarrow,'XData',[925,950],'YData',buttons(i,:));
                if(out(i)==1)
                    callstats = callstats + etime(clock(),outclocks(i,:));
                    callnum = callnum + 1;
                end
                out(i) = 0;
            end
        elseif(strncmp(x,'inb',3))
            z = str2num(x(4));
            i = str2num(x(5))*10+str2num(x(6))+1;
            if(str2num(x(7)))
                image(on,'XData',[25 50]+(z-1)*300,'YData',[800,825]+(i-1)*50);
                if(in(i+4*(z-1))==0)
                    inclocks(i+4*(z-1),:) = clock();
                end
                    in(i+4*(z-1)) = 1;
            else
                image(off,'XData',[25 50]+(z-1)*300,'YData',[800,825]+(i-1)*50);
                if(in(i+4*(z-1))==1)
                    instats = instats + etime(clock(),inclocks(i+4*(z-1),:));
                    innum = innum + 1;
                end
                    in(i+4*(z-1)) = 0;
            end
        else
        end
        if(callnum>0 && index==0)
            OutsideWait = callstats/callnum
        end
        if(innum>0 && index==0)
            InsideWait = instats/innum
        end
        index = mod(index + 1,50);
        
    end
end
