function floatTrack3()

clear all;
close all;

bathy = getBathy();
iUSBL = getIUSBL();

controls = initChart();
stripChart = initStripChart();
controls = initControls();

iUSBLTimer = timer('ExecutionMode', 'fixedRate', ...
    'Period', 3, ...
    'TimerFcn', {@iUSBLTimerCallback});
start(iUSBLTimer);

run = true;
while(run)
    pause(1);
end

%-----------------------
    function stripChart = initStripChart()
        stripChart = struct();
        
        stripChart.hFigure = figure( 'Name', 'Float Depth', ...
                            'NumberTitle', 'off');
        set(stripChart.hFigure,'DoubleBuffer', 'on');
        
        stripChart.hAxes = axes();
        
        stripChart.startTime = now;
        stripChart.index = 1;
        
        stripChart.arrayLength = 1000;
        stripChart.time = NaN * ones(1,stripChart.arrayLength);
        stripChart.y = NaN * ones(1,stripChart.arrayLength);
        
        stripChart.width = 10;
        stripChart.yMin = -50;
        stripChart.yMax = 0;
        
        plotX = stripChart.time(end-stripChart.width + 1:end);
        plotY = stripChart.y(end-stripChart.width+1:end);
        stripChart.hPlot = plot(plotX, plotY);
        grid;
    end

%-----------------------
    function iUSBL = getIUSBL()
        iUSBL = load('iUSBLMsg.mat');
    end
%
% %-----------------------

    function [] = iUSBLTimerCallback(varargin)
        persistent msgIndex
        if isempty(msgIndex)
            msgIndex = 1;
        end
        
        iUSBL.msg(msgIndex,:);
        
        fields = strsplit(iUSBL.msg(msgIndex,1:end-2), {'X','Y','Z',','});
        set(controls.floatEastingEdit, 'String', fields{3});
        set(controls.floatNorthingEdit, 'String', fields{4});
        set(controls.floatDepthEdit, 'String', fields{5});
        
        msgIndex = msgIndex + 1;
        
        stripChart.time(1:stripChart.arrayLength-1) = stripChart.time(2:end);
        stripChart.y(1:stripChart.arrayLength-1) = stripChart.y(2:end);
        stripChart.time(stripChart.arrayLength) = (now - stripChart.startTime) * 24.0 * 3600.0;
        stripChart.y(stripChart.arrayLength) = -1.0 * str2double(fields{5});
        
        plotX = stripChart.time(end-stripChart.width + 1:end);
        plotY = stripChart.y(end-stripChart.width+1:end);
        
        set(stripChart.hPlot, 'XData', plotX, 'YData', plotY);
        set(stripChart.hAxes, 'XLim', [(stripChart.time(end) - stripChart.width) stripChart.time(end)], ...
                                'YLim', [stripChart.yMin stripChart.yMax]);
        drawnow
    end
%-----------------------
    function bathy = getBathy()
        fprintf('Loading mc15 data set \n');
        bathy = load('mc15.mat');
    end

%------------------------
    function chart = initChart()
        % Create the user interface for the application and return a
        % structure of handles for global use.
        chart = struct();
        % Open a window and add some menus
        screenSize = get(groot,'ScreenSize');
        chart.hFigure = figure( ...
            'Name', 'Float Track', ...
            'NumberTitle', 'off', ...
            'OuterPosition', [0.1*screenSize(3) 0.2*screenSize(4) 0.5*screenSize(3) 0.75*screenSize(4)]);
        
        chart.chartAxes = axes('Parent', chart.hFigure);
        surf(bathy.mc15.x, bathy.mc15.y, bathy.mc15.z, 'Parent', chart.chartAxes, ...
            'FaceColor', 'interp', ...
            'LineStyle', 'none', ...
            'FaceLighting', 'gouraud');
        cm = haxby(256);
        colormap(cm);
        daspect([1, 1, .25]);
        axis tight;
        camlight left;
    end

    function controls = initControls()
        controls = struct();
        
        screenSize = get(groot,'ScreenSize');
        controls.hFigure = figure( ...
            'Name', 'Float Track', ...
            'NumberTitle', 'off', ...
            'MenuBar', 'none', ...
            'Toolbar', 'none', ...
            'Position', [0.7*screenSize(3) 0.4*screenSize(4) 0.1*screenSize(3) 0.4*screenSize(4)]);
        
        controlsBox = uix.VBox('Parent', controls.hFigure, 'Padding', 5);
        
        
        controls.shipLatEdit = uicontrol('Parent', controlsBox, 'Style', 'edit');
        uicontrol('Parent', controlsBox, 'Style', 'text', ...
            'String', 'Ship Latitude');
        
        controls.shipLonEdit = uicontrol('Parent', controlsBox, 'Style', 'edit');
        uicontrol('Parent', controlsBox, 'Style', 'text', ...
            'String', 'Ship Longitude')
        
        controls.floatNorthingEdit = uicontrol('Parent', controlsBox, 'Style', 'edit');
        uicontrol('Parent', controlsBox, 'Style', 'text', ...
            'String', 'Float Northing');
        
        controls.floatEastingEdit = uicontrol('Parent', controlsBox, 'Style', 'edit');
        uicontrol('Parent', controlsBox, 'Style', 'text', ...
            'String', 'Float Easting')
        
        controls.floatDepthEdit = uicontrol('Parent', controlsBox, 'Style', 'edit');
        uicontrol('Parent', controlsBox, 'Style', 'text', ...
            'String', 'Float Depth')
        
        controls.exitButton = uicontrol('Parent', controlsBox, 'Style', 'pushbutton', ...
            'String', 'Exit', ...
            'Callback', @exitCallback);
    end

    function exitCallback(source, callbackData)
        stop(iUSBLTimer);
        run = false;
        close all;        
    end
         
end





