classdef CPFC3 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                     matlab.ui.Figure
        StartButton                  matlab.ui.control.Button
        StopButton                   matlab.ui.control.Button
        TabGroup                     matlab.ui.container.TabGroup
        DashboardTab                 matlab.ui.container.Tab
        UIAxes                       matlab.ui.control.UIAxes
        UIAxes2                      matlab.ui.control.UIAxes
        UIAxes2_2                    matlab.ui.control.UIAxes
        UIAxes2_3                    matlab.ui.control.UIAxes
        ScienceDataTab               matlab.ui.container.Tab
        EngrDataTab                  matlab.ui.container.Tab
        SendCommandTab               matlab.ui.container.Tab
        GoButton                     matlab.ui.control.Button
        CommandStringEditFieldLabel  matlab.ui.control.Label
        CommandStringEditField       matlab.ui.control.EditField
        RecoveryModeTrueButton       matlab.ui.control.Button
        RecoveryModeFalseButton      matlab.ui.control.Button
        ParkDepthButton              matlab.ui.control.Button
        metersSpinnerLabel           matlab.ui.control.Label
        metersSpinner                matlab.ui.control.Spinner
        ParkTimeButton               matlab.ui.control.Button
        minutesSpinnerLabel          matlab.ui.control.Label
        minutesSpinner               matlab.ui.control.Spinner
        hoursSpinnerLabel            matlab.ui.control.Label
        hoursSpinner                 matlab.ui.control.Spinner
        SendCommandButton            matlab.ui.control.Button
        ClearCommandButton           matlab.ui.control.Button
        DownloadDataTab              matlab.ui.container.Tab
        SetupTab                     matlab.ui.container.Tab
        ChangeSBDFolderButton        matlab.ui.control.Button
        SBDFolderEditFieldLabel      matlab.ui.control.Label
        SBDFolderEditField           matlab.ui.control.EditField
        DataFolderEditFieldLabel     matlab.ui.control.Label
        DataFolderEditField          matlab.ui.control.EditField
        ChangeDataFolderButton       matlab.ui.control.Button
    end


    properties (Access = private)
        RandTimer          % Timer object
        PlotLine           % Line object
        
        %How do I make this a struct?
        latDD = [36.9];
        lonDD = [-121.9];
        northing;
        easting;

        positionNum = 0; % Description
    end
    
    methods (Access = private)
    
        function RandTimerFcn(app,~,~)
            app.positionNum = app.positionNum + 1;

            [app.northing(app.positionNum), app.easting(app.positionNum)] = DecDegToUTM(app, app.latDD(app.positionNum), app.lonDD(app.positionNum));

            app.northing(app.positionNum) = app.northing(app.positionNum) + 100 * randn(1);
            app.easting(app.positionNum) = app.easting(app.positionNum) + 100 * randn(1);
            
            app.latDD(app.positionNum + 1) = app.latDD(app.positionNum) + 0.01;
            app.lonDD(app.positionNum + 1) = app.lonDD(app.positionNum) + 0.01;

            app.PlotLine = plot(app.UIAxes, app.easting, app.northing, 'x-');
       end        
        
        
       function [northing, easting] = DecDegToUTM(~, latDD, lonDD)
           [northing, easting] = utmups_fwd(latDD, lonDD);
        end
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            % Configure x- and y- axis
            app.UIAxes.DataAspectRatio = [1 1 1];
            
            % Create timer object
            app.RandTimer = timer(...
                'ExecutionMode', 'fixedRate', ...    % Run timer repeatedly
                'Period', 1, ...                     % Period is 1 second
                'BusyMode', 'queue',...              % Queue timer callbacks when busy
                'TimerFcn', @app.RandTimerFcn);      % Specify callback function
        end

        % Button pushed function: StartButton
        function StartButtonPushed(app, event)
            % If timer is not running, start it
            if strcmp(app.RandTimer.Running, 'off')
               start(app.RandTimer);
            end
        end

        % Button pushed function: StopButton
        function StopButtonPushed(app, event)
            % Stop the timer
            stop(app.RandTimer);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            % Stop timer, then delete timer and app
            stop(app.RandTimer);
            delete(app.RandTimer);
            delete(app);
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 1029 663];
            app.UIFigure.Name = 'CPF Mission Control';
            app.UIFigure.Resize = 'off';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create StartButton
            app.StartButton = uibutton(app.UIFigure, 'push');
            app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @StartButtonPushed, true);
            app.StartButton.Position = [549 68 100 22];
            app.StartButton.Text = 'Start';

            % Create StopButton
            app.StopButton = uibutton(app.UIFigure, 'push');
            app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
            app.StopButton.Position = [700 68 100 22];
            app.StopButton.Text = 'Stop';

            % Create TabGroup
            app.TabGroup = uitabgroup(app.UIFigure);
            app.TabGroup.Position = [16 99 982 565];

            % Create DashboardTab
            app.DashboardTab = uitab(app.TabGroup);
            app.DashboardTab.Title = 'Dashboard';

            % Create UIAxes
            app.UIAxes = uiaxes(app.DashboardTab);
            title(app.UIAxes, 'CPF Position')
            xlabel(app.UIAxes, 'Easting (meters)')
            ylabel(app.UIAxes, 'Northing (meters)')
            app.UIAxes.XGrid = 'on';
            app.UIAxes.YGrid = 'on';
            app.UIAxes.Box = 'on';
            app.UIAxes.Position = [10 20 605 504];

            % Create UIAxes2
            app.UIAxes2 = uiaxes(app.DashboardTab);
            title(app.UIAxes2, 'Battery Voltage')
            xlabel(app.UIAxes2, 'Profile Num')
            ylabel(app.UIAxes2, 'Bus Voltage')
            zlabel(app.UIAxes2, 'Z')
            app.UIAxes2.XGrid = 'on';
            app.UIAxes2.YGrid = 'on';
            app.UIAxes2.Position = [663 366 269 154];

            % Create UIAxes2_2
            app.UIAxes2_2 = uiaxes(app.DashboardTab);
            title(app.UIAxes2_2, 'Can Humidity')
            xlabel(app.UIAxes2_2, 'Profile Num')
            ylabel(app.UIAxes2_2, 'Humidity (RH)')
            zlabel(app.UIAxes2_2, 'Z')
            app.UIAxes2_2.XGrid = 'on';
            app.UIAxes2_2.YGrid = 'on';
            app.UIAxes2_2.Position = [663 20 269 145];

            % Create UIAxes2_3
            app.UIAxes2_3 = uiaxes(app.DashboardTab);
            title(app.UIAxes2_3, 'Descend Bellows Position')
            xlabel(app.UIAxes2_3, 'Profile Num')
            ylabel(app.UIAxes2_3, 'Bellows Position (mm)')
            zlabel(app.UIAxes2_3, 'Z')
            app.UIAxes2_3.XGrid = 'on';
            app.UIAxes2_3.YGrid = 'on';
            app.UIAxes2_3.Position = [664 182 269 154];

            % Create ScienceDataTab
            app.ScienceDataTab = uitab(app.TabGroup);
            app.ScienceDataTab.Title = 'Science Data';

            % Create EngrDataTab
            app.EngrDataTab = uitab(app.TabGroup);
            app.EngrDataTab.Title = 'Engr Data';

            % Create SendCommandTab
            app.SendCommandTab = uitab(app.TabGroup);
            app.SendCommandTab.Title = 'Send Command';

            % Create GoButton
            app.GoButton = uibutton(app.SendCommandTab, 'push');
            app.GoButton.Position = [118 461 100 22];
            app.GoButton.Text = 'Go';

            % Create CommandStringEditFieldLabel
            app.CommandStringEditFieldLabel = uilabel(app.SendCommandTab);
            app.CommandStringEditFieldLabel.HorizontalAlignment = 'right';
            app.CommandStringEditFieldLabel.Position = [51 92 96 22];
            app.CommandStringEditFieldLabel.Text = 'Command String';

            % Create CommandStringEditField
            app.CommandStringEditField = uieditfield(app.SendCommandTab, 'text');
            app.CommandStringEditField.Position = [162 92 547 22];

            % Create RecoveryModeTrueButton
            app.RecoveryModeTrueButton = uibutton(app.SendCommandTab, 'push');
            app.RecoveryModeTrueButton.Position = [82 423 137 22];
            app.RecoveryModeTrueButton.Text = 'Recovery Mode = True';

            % Create RecoveryModeFalseButton
            app.RecoveryModeFalseButton = uibutton(app.SendCommandTab, 'push');
            app.RecoveryModeFalseButton.Position = [75 387 143 22];
            app.RecoveryModeFalseButton.Text = 'Recovery Mode = False';

            % Create ParkDepthButton
            app.ParkDepthButton = uibutton(app.SendCommandTab, 'push');
            app.ParkDepthButton.Position = [118 354 100 22];
            app.ParkDepthButton.Text = 'Park Depth = ';

            % Create metersSpinnerLabel
            app.metersSpinnerLabel = uilabel(app.SendCommandTab);
            app.metersSpinnerLabel.HorizontalAlignment = 'right';
            app.metersSpinnerLabel.Position = [233 354 42 22];
            app.metersSpinnerLabel.Text = 'meters';

            % Create metersSpinner
            app.metersSpinner = uispinner(app.SendCommandTab);
            app.metersSpinner.Position = [290 354 100 22];

            % Create ParkTimeButton
            app.ParkTimeButton = uibutton(app.SendCommandTab, 'push');
            app.ParkTimeButton.Position = [118 314 100 22];
            app.ParkTimeButton.Text = 'Park Time = ';

            % Create minutesSpinnerLabel
            app.minutesSpinnerLabel = uilabel(app.SendCommandTab);
            app.minutesSpinnerLabel.HorizontalAlignment = 'right';
            app.minutesSpinnerLabel.Position = [403 314 48 22];
            app.minutesSpinnerLabel.Text = 'minutes';

            % Create minutesSpinner
            app.minutesSpinner = uispinner(app.SendCommandTab);
            app.minutesSpinner.Position = [466 314 100 22];

            % Create hoursSpinnerLabel
            app.hoursSpinnerLabel = uilabel(app.SendCommandTab);
            app.hoursSpinnerLabel.HorizontalAlignment = 'right';
            app.hoursSpinnerLabel.Position = [236 314 36 22];
            app.hoursSpinnerLabel.Text = 'hours';

            % Create hoursSpinner
            app.hoursSpinner = uispinner(app.SendCommandTab);
            app.hoursSpinner.Position = [287 314 74 22];

            % Create SendCommandButton
            app.SendCommandButton = uibutton(app.SendCommandTab, 'push');
            app.SendCommandButton.Position = [360 53 102 22];
            app.SendCommandButton.Text = 'Send Command';

            % Create ClearCommandButton
            app.ClearCommandButton = uibutton(app.SendCommandTab, 'push');
            app.ClearCommandButton.Position = [303 260 103 22];
            app.ClearCommandButton.Text = 'Clear Command';

            % Create DownloadDataTab
            app.DownloadDataTab = uitab(app.TabGroup);
            app.DownloadDataTab.Title = 'Download Data';

            % Create SetupTab
            app.SetupTab = uitab(app.TabGroup);
            app.SetupTab.Title = 'Setup';

            % Create ChangeSBDFolderButton
            app.ChangeSBDFolderButton = uibutton(app.SetupTab, 'push');
            app.ChangeSBDFolderButton.Position = [106 423 123 22];
            app.ChangeSBDFolderButton.Text = 'Change SBD Folder';

            % Create SBDFolderEditFieldLabel
            app.SBDFolderEditFieldLabel = uilabel(app.SetupTab);
            app.SBDFolderEditFieldLabel.HorizontalAlignment = 'right';
            app.SBDFolderEditFieldLabel.Position = [51 461 68 22];
            app.SBDFolderEditFieldLabel.Text = 'SBD Folder';

            % Create SBDFolderEditField
            app.SBDFolderEditField = uieditfield(app.SetupTab, 'text');
            app.SBDFolderEditField.Position = [134 461 100 22];

            % Create DataFolderEditFieldLabel
            app.DataFolderEditFieldLabel = uilabel(app.SetupTab);
            app.DataFolderEditFieldLabel.HorizontalAlignment = 'right';
            app.DataFolderEditFieldLabel.Position = [293 461 68 22];
            app.DataFolderEditFieldLabel.Text = 'Data Folder';

            % Create DataFolderEditField
            app.DataFolderEditField = uieditfield(app.SetupTab, 'text');
            app.DataFolderEditField.Position = [376 461 100 22];

            % Create ChangeDataFolderButton
            app.ChangeDataFolderButton = uibutton(app.SetupTab, 'push');
            app.ChangeDataFolderButton.Position = [364 423 124 22];
            app.ChangeDataFolderButton.Text = 'Change Data Folder';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = CPFC3

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end