function varargout = MILee(varargin)
% MILEE M-file for MILee.fig
%      MILEE, by itself, creates a new MILEE or raises the existing
%      singleton*.
%
%      H = MILEE returns the handle to a new MILEE or the handle to
%      the existing singleton*.
%
%      MILEE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MILEE.M with the given input arguments.
%
%      MILEE('Property','Value',...) creates a new MILEE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before MILee_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to MILee_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help MILee

% Last Modified by GUIDE v2.5 28-Jul-2005 02:04:39

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @MILee_OpeningFcn, ...
                   'gui_OutputFcn',  @MILee_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before MILee is made visible.
function MILee_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to MILee (see VARARGIN)

    %h = plot(blank);
    %set(h,'Color',[1,1,1]);

    set(gcf,'CurrentAxes',handles.robovisual);
    image(imread('line15.bmp','bmp'));
    handles.status = 'closed';
    handles.mode = 's';

% Choose default command line output for MILee
    handles.output = hObject;

% Update handles structure
    guidata(hObject, handles);

% UIWAIT makes MILee wait for user response (see UIRESUME)
    % uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = MILee_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
    varargout{1} = handles.output;


function com_port_Callback(hObject, eventdata, handles)
% hObject    handle to com_port (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of com_port as text
%        str2double(get(hObject,'String')) returns contents of com_port as a double


% --- Executes during object creation, after setting all properties.
function com_port_CreateFcn(hObject, eventdata, handles)
% hObject    handle to com_port (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function com_status_Callback(hObject, eventdata, handles)

% --- Executes during object creation, after setting all properties.
function com_status_CreateFcn(hObject, eventdata, handles)
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


% --- Executes on button press in serial_status.
function serial_status_Callback(hObject, eventdata, handles)
    button_state = get(hObject,'Value');
    if button_state == get(hObject,'Max')
        % toggle button is pressed
        handles.s = serial('COM12');
        set(handles.s,'baudrate',9600,'DataBits',8,'StopBits',1,'Parity','none');
        fopen(handles.s);
        handles.status = 'opened';
        set(handles.com_status, 'String', 'COM12 is OPEN');
        set(handles.serial_status, 'String', 'Close Serial Port');
    elseif button_state == get(hObject,'Min')
        % toggle button is not pressed
        fclose(handles.s);
        delete(handles.s);
        handles.status = 'closed';
        set(handles.com_status, 'String', 'COM Port is CLOSED');
        set(handles.serial_status, 'String', 'Open Serial Port');
    end
    guidata(hObject,handles);


function IRL_Callback(hObject, eventdata, handles)
function IRL_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function IRR_Callback(hObject, eventdata, handles)
function IRR_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function IRLinfo_Callback(hObject, eventdata, handles)
function IRLinfo_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function IRRinfo_Callback(hObject, eventdata, handles)
function IRRinfo_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function bump_Callback(hObject, eventdata, handles)
function bump_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function bumpinfo_Callback(hObject, eventdata, handles)
function bumpinfo_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function servoL_Callback(hObject, eventdata, handles)
function servoL_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function servoR_Callback(hObject, eventdata, handles)
function servoR_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function battery_Callback(hObject, eventdata, handles)
function battery_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function batteryLvl_Callback(hObject, eventdata, handles)
function batteryLvl_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('battery_empty.bmp', 'bmp'))


function batteryinfo_Callback(hObject, eventdata, handles)
function batteryinfo_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function line_Callback(hObject, eventdata, handles)
function line_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end


function arrowUp_Callback(hObject, eventdata, handles)
    if handles.status == 'opened'
        flushinput(handles.s);
        fprintf(handles.s,'U');
    else
        errordlg('Port is currently closed.','','modal');
    end;
    guidata(hObject,handles);
function arrowUp_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('up_arrow.bmp', 'bmp'))

function arrowDown_Callback(hObject, eventdata, handles)
    if handles.status == 'opened'
        flushinput(handles.s);
        fprintf(handles.s,'D');
    else
        errordlg('Port is currently closed.','','modal');
    end;
    guidata(hObject,handles);
function arrowDown_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('down_arrow.bmp', 'bmp'))

function arrowLeft_Callback(hObject, eventdata, handles)
    if handles.status == 'opened'
        flushinput(handles.s);
        fprintf(handles.s,'L');
    else
        errordlg('Port is currently closed.','','modal');
    end;
    guidata(hObject,handles);
function arrowLeft_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('left_arrow.bmp', 'bmp'))

function arrowRight_Callback(hObject, eventdata, handles)
    if handles.status == 'opened'
        flushinput(handles.s);
        fprintf(handles.s,'R');
    else
        errordlg('Port is currently closed.','','modal');
    end;
    guidata(hObject,handles);
function arrowRight_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('right_arrow.bmp', 'bmp'))

function stopbutton_Callback(hObject, eventdata, handles)
    if handles.status == 'opened'
        flushinput(handles.s);
        fprintf(handles.s,'S');
    else
        errordlg('Port is currently closed.','','modal');
    end;
    guidata(hObject,handles);
function stopbutton_CreateFcn(hObject, eventdata, handles)
    set(hObject,'CData',imread('stop.bmp', 'bmp'))



function updateMode_Callback(hObject, eventdata, handles)
    button_state = get(hObject,'Value');
    if button_state == get(hObject,'Max')
        % toggle button is pressed
        handles.mode = 'c';
        set(handles.updateMode, 'String', 'Continuous');
    elseif button_state == get(hObject,'Min')
        % toggle button is not pressed
        handles.mode = 's';
        set(handles.updateMode, 'String', 'Single');
    end
    guidata(hObject,handles);


% --- Executes on button press in update.
function update_Callback(hObject, eventdata, handles)
% hObject    handle to update (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    go_on = 'ok';

    if handles.status == 'closed'
        errordlg('Port is currently closed.','','modal')
    end;

    while go_on == 'ok'
        if handles.status == 'opened'
            flushinput(handles.s);
            fprintf(handles.s,'1');
            A = fscanf(handles.s,'%c',27);
            set(handles.IRL, 'String', A(1:3));
            set(handles.IRR, 'String', A(5:7));
            set(handles.battery, 'String', A(9:11));
            set(handles.bump, 'String', A(13:15));
            set(handles.line, 'String', A(17:19));
            set(handles.servoR, 'String', A(21:23));
            set(handles.servoL, 'String', A(25:27));
    %    end;
    %    guidata(hObject,handles);

            %// IR DETECTOR VALUES
            %#define in13L   0x55	// max
            %#define in12L   0x5A    // avg
            %#define in11L   0x5C    // avg
            %#define in10L   0x5E    // avg
            %#define in09L   0x61    // avg
            %#define in08L   0x63    // avg
            %#define in07L   0x66    // avg
            %#define in06L   0x6B    // avg
            %#define in05L   0x6F    // avg

            IRL = str2num(A(1:3));
            if (IRL <= 88)
                set(handles.IRLinfo, 'String', 'Out of Range');
            elseif (IRL >= 89) && (IRL < 91)
                set(handles.IRLinfo, 'String', 'At 12 inches');
            elseif (IRL >= 91) && (IRL < 93)
                set(handles.IRLinfo, 'String', 'At 11 inches');
            elseif (IRL >= 93) && (IRL < 96)
                set(handles.IRLinfo, 'String', 'At 10 inches');
            elseif (IRL >= 96) && (IRL < 98)
                set(handles.IRLinfo, 'String', 'At 9 inches');
            elseif (IRL >= 98) && (IRL < 101)
                set(handles.IRLinfo, 'String', 'At 8 inches');
            elseif (IRL >= 101) && (IRL < 105)
                set(handles.IRLinfo, 'String', 'At 7 inches');
            elseif (IRL >= 105) && (IRL < 109)
                set(handles.IRLinfo, 'String', 'At 6 inches');
            elseif (IRL >= 109)
                set(handles.IRLinfo, 'String', 'Going to Hit');
            end;

            %// IR DETECTOR VALUES
            %#define in13R   0x60	// max
            %#define in12R   0x69    // avg
            %#define in11R   0x6C    // avg
            %#define in10R   0x6E    // avg
            %#define in09R   0x72    // avg
            %#define in08R   0x75    // avg
            %#define in07R   0x79    // avg
            %#define in06R   0x7D    // avg
            %#define in05R   0x81    // min

            IRR = str2num(A(5:7));
            if (IRR <= 102)
                set(handles.IRRinfo, 'String', 'Out of Range');
            elseif (IRR >= 103) && (IRR < 107)
                set(handles.IRRinfo, 'String', 'At 12 inches');
            elseif (IRR >= 107) && (IRR < 110)
                set(handles.IRRinfo, 'String', 'At 11 inches');
            elseif (IRR >= 110) && (IRR < 113)
                set(handles.IRRinfo, 'String', 'At 10 inches');
            elseif (IRR >= 113) && (IRR < 116)
                set(handles.IRRinfo, 'String', 'At 9 inches');
            elseif (IRR >= 116) && (IRR < 119)
                set(handles.IRRinfo, 'String', 'At 8 inches');
            elseif (IRR >= 119) && (IRR < 123)
                set(handles.IRRinfo, 'String', 'At 7 inches');
            elseif (IRR >= 123) && (IRR < 127)
                set(handles.IRRinfo, 'String', 'At 6 inches');
            elseif (IRR >= 127)
                set(handles.IRRinfo, 'String', 'Going to Hit');
            end;

            battery = str2num(A(9:11));
            battery = (battery * 10 / 255);
            if (battery >= 9)
                set(handles.batteryLvl,'CData',imread('battery_full.bmp', 'bmp'))
            elseif (battery >= 8) && (battery < 9)
                set(handles.batteryLvl,'CData',imread('battery_nearfull.bmp', 'bmp'))
            elseif (battery >= 7) && (battery < 8)
                set(handles.batteryLvl,'CData',imread('battery_nearempty.bmp', 'bmp'))
            elseif (battery < 7)
                set(handles.batteryLvl,'CData',imread('battery_empty.bmp', 'bmp'))
            end;
            set(handles.batteryinfo, 'String', num2str(battery));

            bump = str2num(A(13:15));
            if bump == 3
                set(handles.bumpinfo, 'String', 'No Bumps');
            elseif bump == 2
                set(handles.bumpinfo, 'String', 'Front Bump');
            elseif bump == 1
                set(handles.bumpinfo, 'String', 'Back Bump');
            elseif bump == 0
                set(handles.bumpinfo, 'String', 'Both Bumps');
            end;

            line = str2num(A(17:19));
            set(gcf,'CurrentAxes',handles.robovisual);
            if line == 0
                image(imread('line0.bmp','bmp'));
            elseif line == 1
                image(imread('line1.bmp','bmp'));
            elseif line == 2
                image(imread('line2.bmp','bmp'));
            elseif line == 3
                image(imread('line3.bmp','bmp'));
            elseif line == 4
                image(imread('line4.bmp','bmp'));
            elseif line == 5
                image(imread('line5.bmp','bmp'));
            elseif line == 6
                image(imread('line6.bmp','bmp'));
            elseif line == 7
                image(imread('line7.bmp','bmp'));
            elseif line == 8
                image(imread('line8.bmp','bmp'));
            elseif line == 9
                image(imread('line9.bmp','bmp'));
            elseif line == 10
                image(imread('line10.bmp','bmp'));
            elseif line == 11
                image(imread('line11.bmp','bmp'));
            elseif line == 12
                image(imread('line12.bmp','bmp'));
            elseif line == 13
                image(imread('line13.bmp','bmp'));
            elseif line == 14
                image(imread('line14.bmp','bmp'));
            elseif line == 15
                image(imread('line15.bmp','bmp'));
            end;

            servoR = str2num(A(21:23));
            servoR = servoR - 47;
            set(handles.servR, 'String', num2str(servoR));

            servoL = str2num(A(25:27));
            servoL = 47 - servoL;
            set(handles.servL, 'String', num2str(servoL));

        end;

        %guidata(hObject,handles);
        %updateMode_Callback(hObject, eventdata, handles);
        if (handles.mode == 's')
            go_on = 'no';
        end;
        guidata(hObject,handles);
        pause(.125);
   end;


