function varargout = floatChart(varargin)
% FLOATCHART MATLAB code for floatChart.fig
%      FLOATCHART, by itself, creates a new FLOATCHART or raises the existing
%      singleton*.
%
%      H = FLOATCHART returns the handle to a new FLOATCHART or the handle to
%      the existing singleton*.
%
%      FLOATCHART('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FLOATCHART.M with the given input arguments.
%
%      FLOATCHART('Property','Value',...) creates a new FLOATCHART or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before floatChart_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to floatChart_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

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

% Last Modified by GUIDE v2.5 15-Nov-2012 14:28:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @floatChart_OpeningFcn, ...
                   'gui_OutputFcn',  @floatChart_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 floatChart is made visible.
function floatChart_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 floatChart (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

%Setup map axes
shipLOA = 25;
shipBeam = 7;
shipShapeY = [0, shipBeam/2,  shipBeam/2, -shipBeam/2, -shipBeam/2,   0];
shipShapeX = [0 -(.3*shipLOA) -shipLOA,   -shipLOA     -(.3*shipLOA) 0];
shipShapeXY = [shipShapeX; shipShapeY];

shipLat = 36.8020317;
shipLon = -121.7872017;
shipHeading = 10;
shipHeadingXY = -(shipHeading - 90);

zone = utmzone(shipLat, shipLon);
wgs84 = wgs84Ellipsoid;
ellipsoid = [wgs84.SemimajorAxis wgs84.Eccentricity];

axesm utm
setm(gca,'zone', zone, 'geoid', ellipsoid, ...
    'grid','on','meridianlabel','on','parallellabel','on');
c = load('coastline.dat');
load('mb15.mat');

plotm(c(:,2), c(:,1))
contourf(mb15.x,mb15.y,mb15.z)
hA = gca;
set(hA,'DataAspectRatio',[1 1 1]);

[shipE, shipN] = mfwdtran(shipLat, shipLon);
floatE = shipE - 10000;
floatN = shipN - 10000;
[floatLat, floatLon] = minvtran(floatE, floatN);

[dist, az] = distance(shipLat, shipLon, floatLat, floatLon, ellipsoid)
plotm([shipLat floatLat], [shipLon floatLon]);

M = makehgtform('zrotate', degtorad(shipHeadingXY));
M = M(1:2, 1:2);
shipShapeXYRot = M * shipShapeXY;
shipShapeXYNew = [shipE+shipShapeXYRot(1,:); shipN + shipShapeXYRot(2,:)];
[shipShapeLat, shipShapeLon] = minvtran(shipShapeXYNew(1,:), shipShapeXYNew(2,:));
plotm(shipShapeLat, shipShapeLon);

%Setup serial port timer
SPTimer = timer('Name','Reminder',...
            'Period',5,...  % Update the time every 60 seconds.
            'TasksToExecute',inf,...  % number of times to update
            'ExecutionMode','fixedSpacing',...
            'TimerFcn',{@updater, handles}); 
start(SPTimer);  % Start the timer object.
setappdata(hObject,'hSPTimer',SPTimer);

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


% --- Outputs from this function are returned to the command line.
function varargout = floatChart_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 [] = updater(varargin)
handles = varargin{3};
SP = serial( 'COM3', ...
    'BaudRate',9600, ...
    'Parity','none', ...
    'StopBits',1, ...
    'ReadAsyncMode','manual',...
    'Terminator','CR/LF', ...
    'InputBufferSize',8192);
fopen(SP)
closeSP = onCleanup(@()fclose(SP));

test = 1;
while test > 0
    try
        inString = fgets(SP);
        fprintf(1, inString);
        if strcmp(inString(1:6),  '$GPRMC');
            parseRMC(inString, handles);
        end
    catch
        fprintf(1, 'Error reading GSP\r\n');
    end
    test = SP.BytesAvailable;
end
fclose(SP);

function parseRMC(RMCString, handles)
% $GPRMC,004438.00,A,3648.12994,N,12147.28449,W,0.072,,151112,,,A*67
[token, remain] = strtok(RMCString,',');
for i=1:2
    [token, remain] = strtok(remain, ',');
end

[shipLatString, remain] = strtok(remain, ',');
[shipNSString, remain] = strtok(remain, ',');
temp = sscanf(shipLatString, '%2d%f');
shipLatDecDeg = temp(1) + temp(2)/60.0; 
if shipNSString == 'S'
    shipLatDecDeg = -1.0 * shipLatDecDeg;
end

[shipLonString, remain] = strtok(remain, ',');
[EWString, remain] = strtok(remain, ',');
temp = sscanf(shipLonString, '%3d%f');
shipLonDecDeg = temp(1) + temp(2)/60.0;
if EWString == 'W'
    shipLonDecDeg = -1.0 * shipLonDecDeg;
end
set(handles.floatLatMeter, 'String', num2str(shipLatDecDeg));
set(handles.floatLonMeter, 'String', num2str(shipLonDecDeg));
set(handles.shipLatMeter, 'String', num2str(shipLatDecDeg));
set(handles.shipLonMeter, 'String', num2str(shipLonDecDeg));



function RangeMeter_Callback(hObject, eventdata, handles)
% hObject    handle to RangeMeter (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 RangeMeter as text
%        str2double(get(hObject,'String')) returns contents of RangeMeter as a double


% --- Executes during object creation, after setting all properties.
function RangeMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RangeMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function RelBearingMeter_Callback(hObject, eventdata, handles)
% hObject    handle to RelBearingMeter (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 RelBearingMeter as text
%        str2double(get(hObject,'String')) returns contents of RelBearingMeter as a double


% --- Executes during object creation, after setting all properties.
function RelBearingMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to RelBearingMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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


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

rotatem(handles.chartAxes,[0 0 1],30);


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

% Hint: place code in OpeningFcn to populate chartAxes


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

close(handles.figure1);
% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

SPTimer = getappdata(hObject,'hSPTimer');
stop(SPTimer);
delete(SPTimer);



function TrueBearingMeter_Callback(hObject, eventdata, handles)
% hObject    handle to TrueBearingMeter (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 TrueBearingMeter as text
%        str2double(get(hObject,'String')) returns contents of TrueBearingMeter as a double


% --- Executes during object creation, after setting all properties.
function TrueBearingMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to TrueBearingMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function GPSString_Callback(hObject, eventdata, handles)
% hObject    handle to GPSString (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 GPSString as text
%        str2double(get(hObject,'String')) returns contents of GPSString as a double


% --- Executes during object creation, after setting all properties.
function GPSString_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GPSString (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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



function shipLat_Callback(hObject, eventdata, handles)
% hObject    handle to shipLat (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 shipLat as text
%        str2double(get(hObject,'String')) returns contents of shipLat as a double


% --- Executes during object creation, after setting all properties.
function shipLat_CreateFcn(hObject, eventdata, handles)
% hObject    handle to shipLat (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (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 edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function floatLat_Callback(hObject, eventdata, handles)
% hObject    handle to floatLat (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 floatLat as text
%        str2double(get(hObject,'String')) returns contents of floatLat as a double


% --- Executes during object creation, after setting all properties.
function floatLat_CreateFcn(hObject, eventdata, handles)
% hObject    handle to floatLat (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function floatLon_Callback(hObject, eventdata, handles)
% hObject    handle to floatLon (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 floatLon as text
%        str2double(get(hObject,'String')) returns contents of floatLon as a double


% --- Executes during object creation, after setting all properties.
function floatLon_CreateFcn(hObject, eventdata, handles)
% hObject    handle to floatLon (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function shipLatMeter_Callback(hObject, eventdata, handles)
% hObject    handle to shipLatMeter (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 shipLatMeter as text
%        str2double(get(hObject,'String')) returns contents of shipLatMeter as a double


% --- Executes during object creation, after setting all properties.
function shipLatMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to shipLatMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function shipLonMeter_Callback(hObject, eventdata, handles)
% hObject    handle to shipLonMeter (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 shipLonMeter as text
%        str2double(get(hObject,'String')) returns contents of shipLonMeter as a double


% --- Executes during object creation, after setting all properties.
function shipLonMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to shipLonMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function floatLatMeter_Callback(hObject, eventdata, handles)
% hObject    handle to floatLatMeter (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 floatLatMeter as text
%        str2double(get(hObject,'String')) returns contents of floatLatMeter as a double


% --- Executes during object creation, after setting all properties.
function floatLatMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to floatLatMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function floatLonMeter_Callback(hObject, eventdata, handles)
% hObject    handle to floatLonMeter (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 floatLonMeter as text
%        str2double(get(hObject,'String')) returns contents of floatLonMeter as a double


% --- Executes during object creation, after setting all properties.
function floatLonMeter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to floatLonMeter (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
