classdef appli_simple_RS232 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure               matlab.ui.Figure
        GridLayout             matlab.ui.container.GridLayout
        InterfacedecommunicationRS232Label  matlab.ui.control.Label
        COMPortEditFieldLabel  matlab.ui.control.Label
        COMPortEditField       matlab.ui.control.NumericEditField
        ConnectButton          matlab.ui.control.Button
        SwitchOnButton         matlab.ui.control.Button
        SwitchOffButton        matlab.ui.control.Button
        PortListLabel          matlab.ui.control.Label
        HelpButton             matlab.ui.control.Button
    end

    
    properties (Access = private)
        s % serial port
        portList % list of the different available comm ports
    end
    
    methods (Access = private)
        
        function results = rs232Interrupt(app, src, ~)
            rLine = readline(src);
            rLineChar = char(rLine);
            app.PortListLabel.Text = rLineChar;
            drawnow;
            flush(app.s)
        end
    end
    

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

        % Code that executes after component creation
        function startupFcn(app)
            app.portList = serialportlist();
            app.PortListLabel.Text = app.portList
            app.SwitchOffButton.Enable = false;
            app.SwitchOnButton.Enable = false;
        end

        % Button pushed function: ConnectButton
        function ConnectButtonPushed(app, event)
            % on connect open serial port, start timer and change lamp color to green
            try
                app.s = serialport("COM"+app.COMPortEditField.Value, 115200);
                configureTerminator(app.s, "CR/LF");
                % configureCallback(app.s, "byte", 1, @app.rs232Interrupt)
                configureCallback(app.s, "terminator", @app.rs232Interrupt);
                app.SwitchOffButton.Enable = true;
                app.SwitchOnButton.Enable = true;
            catch
                warning('Problem using serial.');
            end
            drawnow;
        end

        % Button pushed function: HelpButton
        function HelpButtonPushed(app, event)
            open appli_simple_RS232.mlx;
        end

        % Button pushed function: SwitchOnButton
        function SwitchOnButtonPushed(app, event)
            writeline(app.s, 'a');
        end

        % Button pushed function: SwitchOffButton
        function SwitchOffButtonPushed(app, event)
            writeline(app.s, 'e');
        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 640 480];
            app.UIFigure.Name = 'MATLAB App';

            % Create GridLayout
            app.GridLayout = uigridlayout(app.UIFigure);
            app.GridLayout.ColumnWidth = {'2x', '1x'};
            app.GridLayout.RowHeight = {'1x', '3x', '1x', '1x', '1x'};

            % Create InterfacedecommunicationRS232Label
            app.InterfacedecommunicationRS232Label = uilabel(app.GridLayout);
            app.InterfacedecommunicationRS232Label.HorizontalAlignment = 'center';
            app.InterfacedecommunicationRS232Label.FontSize = 20;
            app.InterfacedecommunicationRS232Label.FontColor = [1 0.4118 0.1608];
            app.InterfacedecommunicationRS232Label.Layout.Row = 1;
            app.InterfacedecommunicationRS232Label.Layout.Column = 1;
            app.InterfacedecommunicationRS232Label.Text = 'Interface de communication RS232';

            % Create COMPortEditFieldLabel
            app.COMPortEditFieldLabel = uilabel(app.GridLayout);
            app.COMPortEditFieldLabel.HorizontalAlignment = 'right';
            app.COMPortEditFieldLabel.FontSize = 18;
            app.COMPortEditFieldLabel.Layout.Row = 3;
            app.COMPortEditFieldLabel.Layout.Column = 1;
            app.COMPortEditFieldLabel.Text = 'COM Port';

            % Create COMPortEditField
            app.COMPortEditField = uieditfield(app.GridLayout, 'numeric');
            app.COMPortEditField.FontSize = 18;
            app.COMPortEditField.Layout.Row = 3;
            app.COMPortEditField.Layout.Column = 2;

            % Create ConnectButton
            app.ConnectButton = uibutton(app.GridLayout, 'push');
            app.ConnectButton.ButtonPushedFcn = createCallbackFcn(app, @ConnectButtonPushed, true);
            app.ConnectButton.FontSize = 20;
            app.ConnectButton.Layout.Row = 4;
            app.ConnectButton.Layout.Column = 2;
            app.ConnectButton.Text = 'Connect';

            % Create SwitchOnButton
            app.SwitchOnButton = uibutton(app.GridLayout, 'push');
            app.SwitchOnButton.ButtonPushedFcn = createCallbackFcn(app, @SwitchOnButtonPushed, true);
            app.SwitchOnButton.BackgroundColor = [0.4667 0.6745 0.1882];
            app.SwitchOnButton.FontSize = 20;
            app.SwitchOnButton.Layout.Row = 4;
            app.SwitchOnButton.Layout.Column = 1;
            app.SwitchOnButton.Text = 'Switch On';

            % Create SwitchOffButton
            app.SwitchOffButton = uibutton(app.GridLayout, 'push');
            app.SwitchOffButton.ButtonPushedFcn = createCallbackFcn(app, @SwitchOffButtonPushed, true);
            app.SwitchOffButton.BackgroundColor = [1 0 0];
            app.SwitchOffButton.FontSize = 20;
            app.SwitchOffButton.FontColor = [1 1 1];
            app.SwitchOffButton.Layout.Row = 5;
            app.SwitchOffButton.Layout.Column = 1;
            app.SwitchOffButton.Text = 'Switch Off';

            % Create PortListLabel
            app.PortListLabel = uilabel(app.GridLayout);
            app.PortListLabel.Layout.Row = 2;
            app.PortListLabel.Layout.Column = 1;
            app.PortListLabel.Text = 'Port List';

            % Create HelpButton
            app.HelpButton = uibutton(app.GridLayout, 'push');
            app.HelpButton.ButtonPushedFcn = createCallbackFcn(app, @HelpButtonPushed, true);
            app.HelpButton.Layout.Row = 1;
            app.HelpButton.Layout.Column = 2;
            app.HelpButton.Text = 'Help';

            % 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 = appli_simple_RS232

            % 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