classdef applicationspectro_vf_2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                        matlab.ui.Figure
        GainPanel                       matlab.ui.container.Panel
        GainEditField                   matlab.ui.control.NumericEditField
        GainEditFieldLabel              matlab.ui.control.Label
        Button_2                        matlab.ui.control.Button
        Button                          matlab.ui.control.Button
        EnregistrementPanel             matlab.ui.container.Panel
        EnregistrerletableauButton      matlab.ui.control.Button
        Adressedenregistrement          matlab.ui.control.EditField
        NomdufichieravecextensionLabel  matlab.ui.control.Label
        EnregistrerlafigureButton       matlab.ui.control.Button
        AcquisitionButton               matlab.ui.control.Button
        MoyennagePanel                  matlab.ui.container.Panel
        nbderelev                       matlab.ui.control.NumericEditField
        NombredacquisitionsLabel        matlab.ui.control.Label
        TempsdintgrationPanel           matlab.ui.container.Panel
        tempsIntegration                matlab.ui.control.NumericEditField
        EnsLabel                        matlab.ui.control.Label
        Button_4                        matlab.ui.control.Button
        Button_3                        matlab.ui.control.Button
        ConnLamp                        matlab.ui.control.Lamp
        ConnexionButton                 matlab.ui.control.Button
        ConnexionNucloPanel             matlab.ui.container.Panel
        GridLayout11                    matlab.ui.container.GridLayout
        PortList                        matlab.ui.control.DropDown
        PortLabel                       matlab.ui.control.Label
        Refresh                         matlab.ui.control.Button
        UIAxes                          matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        GREEN_COLOR = [0 1 0];
        RED_COLOR = [1 0 0];
        s % serial port
        color_plot % color plot handlers
        is_connected = false;
        precisionY = 1000;

    end
    
    properties (Access = public)
        lambda = 1:64;
        resultat = zeros(64);
    end
    
    methods (Access = private)
        
        function resetApp(app, ~)
            % Communications
            app.is_connected = false;
            app.PortList.Items = serialportlist();
            % Default Coefficients
            app.nbderelev.Value = 1;
            app.tempsintegration.Value = 200;
            app.lambda1.Value = 500;
            app.lambda2.Value = 600;
            app.position1.Value = 1;
            app.position2.Value = 2;

        end
        
%         function  conversionindicelongeuronde(app,x,y)
%             app.lambda;
%             l1 = app.lambda1.Value;
%             l2 = app.lambda2.Value;
%             
%             minpic = 0.2*max(y);
%             
%             [~, position] = findpeaks(y,x,"MinPeakHeight",minpic,"MinPeakDistance",5); %test
%             
%             x1 = position(app.position1.Value);
%             x2 = position(app.position2.Value);
%             
%             app.lambda = (l1-l2).*(x-x1)./(x1-x2) + l1;
%         end
    end
    
    %rajouter un fonction à appeler au lancement

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

        % Button pushed function: Refresh
        function rafraichirpush(app, event)
            app.PortList.Items = serialportlist("available");
        end

        % Button pushed function: ConnexionButton
        function connexionpush(app, event)
% on connect open serial port, start timer and change lamp color to green

            if not(app.is_connected)
                try
                    app.s = serialport(app.PortList.Value, 115200);
                    configureTerminator(app.s, "CR/LF");
                    %configureCallback(app.s, "terminator", @app.boardCommIT);
                catch
                    warning('Problem using serial.');
                end
                app.is_connected = true;
                app.Refresh.Enable = false;
                drawnow;
                app.ConnLamp.Color = app.GREEN_COLOR; 
                app.ConnexionButton.Text = 'Déconnexion';
            else
                delete(app.s);
                app.is_connected = false;
                app.Refresh.Enable = true;
                drawnow;
                app.ConnLamp.Color = app.RED_COLOR;
                app.ConnexionButton.Text = 'Connexion';
              
            end


        end

        % Button pushed function: EnregistrerlafigureButton
        function enregistrerpush(app, event)
            exportgraphics(app.UIAxes,app.Adressedenregistrement.Value)
        end

        % Button pushed function: AcquisitionButton
        function acquisitionpush(app, event)
            %Effectue la mesure et trace le graph
            
            nbcapture = app.nbderelev.Value;
            r = -1.*ones(nbcapture,64); %creation d'un vecteur de -1
            for k = 1:nbcapture
                writeline(app.s, 'a');  %demande une mesure à la carte
                for i = 1:64
                    x= readline(app.s); %recupere les valeurs une à une
                    r(k,i) = str2num(x)/1000; %conversion en double. Attention à modifier pour le bon code mbed (str2num normalement)
                end
                flush(app.s)
            end
            app.resultat = mean(r,1);
            
            plot(app.UIAxes, app.lambda ,app.resultat,'r'); %trace le graphique dans l'appli.
            


        end

        % Button pushed function: Button_3
        function tpsintup(app, event)
            writeline(app.s, 'b'); %augmenter le temps d'intégration
            app.tempsIntegration.Value = app.tempsIntegration.Value + 100;
        end

        % Button pushed function: Button_4
        function tpsintdown(app, event)
            writeline(app.s, 'c'); %diminuer le temps d'intégration
            app.tempsIntegration.Value = app.tempsIntegration.Value - 100;
        end

        % Callback function
        function augmentertint(app, event)
            %tps = num2str(app.tempsintegration.Value);
            %writeline(app.s, tps);
        end

        % Callback function
        function calibrer(app, event)
            conversionindicelongeuronde(app,1:64,app.resultat); %def des abscisses
            plot(app.UIAxes, app.lambda ,app.resultat,'r'); %trace le graphique dans l'appli.
        end

        % Button pushed function: EnregistrerletableauButton
        function enregistertab(app, event)
            l = num2str (app.lambda);
            r = num2str (app.resultat);
            save(app.Adressedenregistrement.Value, 'l', 'r', '-mat');
        end

        % Button pushed function: Button
        function ButtonPushed(app, event)
            writeline(app.s, 'g'); %augmenter le gain
            app.GainEditField.Value = app.GainEditField.Value + 0.1;
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
            writeline(app.s, 'f'); %diminuer le gain
            app.GainEditField.Value = app.GainEditField.Value - 0.1;
        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.Color = [0.9373 0.9529 0.9608];
            app.UIFigure.Colormap = [9.43369730244145e-12 1.21626190409876e-22 1.1028563449171e-11;0.131252354635855 1.69220225802175e-12 0.15344195118281;0.213220079160744 2.7489906783952e-12 0.249267185099627;0.283199173522115 3.65121282763717e-12 0.331076984326509;0.34637704049916 4.4657485321749e-12 0.404935736860291;0.404935736860291 5.22073047885341e-12 0.47339439920812;0.458627927464429 5.9129698404293e-12 0.536163821611516;0.507006329681403 6.53669991901094e-12 0.59272110358847;0.549511391146739 7.08470655240467e-12 0.642412094538545;0.585503323370283 7.54874111504718e-12 0.684488842971305;0.614269457879883 7.91961535884077e-12 0.718118196283754;0.58289895685415 8.34882114026609e-12 0.757036813871366;0.532671042510151 8.63533098011816e-12 0.783016349504045;0.467387923513217 8.80830950824051e-12 0.798701332042039;0.389829154912278 8.90266192638799e-12 0.807256821831002;0.300481838761817 8.95016346765953e-12 0.811564066513099;0.195388367219121 8.97641993421081e-12 0.813944895069316;0.0501824218114992 9.00170346872195e-12 0.816237502143794;9.02690084206025e-12 0.155254640193926 0.818522296476967;9.05163084935565e-12 0.270351402981362 0.820764711976759;9.07321707748461e-12 0.367538180189133 0.822722062492682;9.08421380976805e-12 0.454698735835039 0.823719201014515;9.07048739809878e-12 0.533993704513846 0.822474546376284;9.01172757929927e-12 0.605177258893312 0.8171464473237;8.88706431818832e-12 0.666705345397634 0.805842494776067;8.6856296602889e-12 0.716962964994113 0.787577227254168;8.41468100189373e-12 0.755540378743563 0.763008715648988;8.66460803518038e-12 0.785671072621414 0.731130754022246;8.93405861991699e-12 0.810103745059559 0.687302562192923;9.12821253083933e-12 0.827708824346313 0.631273650879088;9.25272881867908e-12 0.838999449961308 0.564295563224145;9.32642711740514e-12 0.845682109023942 0.487931070410019;9.37037800493438e-12 0.849667395006592 0.402720961326347;9.40057637837493e-12 0.852405659490817 0.307056578007241;9.42617923651148e-12 0.85472722152024 0.194805833293969;9.45092757275763e-12 0.856971299014013 0.0324324944284617;0.174215776445441 0.859208423664206 9.47559923103199e-12;0.29270102305687 0.861391694816735 9.49967697734389e-12;0.393542967238788 0.86323780094433 9.52003637015389e-12;0.484163255561209 0.863991064572863 9.52834357951394e-12;0.566558577535172 0.862252249097763 9.50916741907954e-12;0.64032207342022 0.856058717379444 9.44086335830556e-12;0.703779407260349 0.843466455473534 9.30199224863495e-12;0.755303502168048 0.823570723288691 9.08257635442439e-12;0.794615128710938 0.797212518880131 8.79189044568993e-12;0.825587797350751 0.762723633264253 9.1048212311097e-12;0.850467966276 0.715595392127937 9.37920693665322e-12;0.868226701612455 0.655812908327459 9.57505541097402e-12;0.879857740838838 0.585623136461541 0.0114337533670117;0.887618230808398 0.508322324776077 0.0304399944076098;0.893915782121304 0.427249785995039 0.0582201024831633;0.900565677294438 0.345671308298629 0.0952824133635513;0.908680394297503 0.26764988684277 0.142244390788589;0.918772173531655 0.199855365894893 0.199855365894893;0.930507777139017 0.264341934982788 0.264341934982788;0.943451070071521 0.333386767361901 0.333386767361901;0.957025217667329 0.405188717515892 0.405188717515892;0.970436601371259 0.478007028597537 0.478007028597537;0.982611413374461 0.549889960687239 0.549889960687239;0.992330373182327 0.618604427046121 0.618604427046121;0.998478381129891 0.683423279659082 0.683423279659082;1 0.743289277608716 0.743289277608716;0.996224511209191 0.797159698510515 0.797159698510515;0.987132576557625 0.84438104450709 0.84438104450709];
            app.UIFigure.Position = [100 100 643 560];
            app.UIFigure.Name = 'MATLAB App';

            % Create UIAxes
            app.UIAxes = uiaxes(app.UIFigure);
            xlabel(app.UIAxes, 'Longueur d''onde')
            ylabel(app.UIAxes, 'Intensité')
            app.UIAxes.Colormap = [9.43369730244145e-12 1.21626190409876e-22 1.1028563449171e-11;0.131252354635855 1.69220225802175e-12 0.15344195118281;0.213220079160744 2.7489906783952e-12 0.249267185099627;0.283199173522115 3.65121282763717e-12 0.331076984326509;0.34637704049916 4.4657485321749e-12 0.404935736860291;0.404935736860291 5.22073047885341e-12 0.47339439920812;0.458627927464429 5.9129698404293e-12 0.536163821611516;0.507006329681403 6.53669991901094e-12 0.59272110358847;0.549511391146739 7.08470655240467e-12 0.642412094538545;0.585503323370283 7.54874111504718e-12 0.684488842971305;0.614269457879883 7.91961535884077e-12 0.718118196283754;0.58289895685415 8.34882114026609e-12 0.757036813871366;0.532671042510151 8.63533098011816e-12 0.783016349504045;0.467387923513217 8.80830950824051e-12 0.798701332042039;0.389829154912278 8.90266192638799e-12 0.807256821831002;0.300481838761817 8.95016346765953e-12 0.811564066513099;0.195388367219121 8.97641993421081e-12 0.813944895069316;0.0501824218114992 9.00170346872195e-12 0.816237502143794;9.02690084206025e-12 0.155254640193926 0.818522296476967;9.05163084935565e-12 0.270351402981362 0.820764711976759;9.07321707748461e-12 0.367538180189133 0.822722062492682;9.08421380976805e-12 0.454698735835039 0.823719201014515;9.07048739809878e-12 0.533993704513846 0.822474546376284;9.01172757929927e-12 0.605177258893312 0.8171464473237;8.88706431818832e-12 0.666705345397634 0.805842494776067;8.6856296602889e-12 0.716962964994113 0.787577227254168;8.41468100189373e-12 0.755540378743563 0.763008715648988;8.66460803518038e-12 0.785671072621414 0.731130754022246;8.93405861991699e-12 0.810103745059559 0.687302562192923;9.12821253083933e-12 0.827708824346313 0.631273650879088;9.25272881867908e-12 0.838999449961308 0.564295563224145;9.32642711740514e-12 0.845682109023942 0.487931070410019;9.37037800493438e-12 0.849667395006592 0.402720961326347;9.40057637837493e-12 0.852405659490817 0.307056578007241;9.42617923651148e-12 0.85472722152024 0.194805833293969;9.45092757275763e-12 0.856971299014013 0.0324324944284617;0.174215776445441 0.859208423664206 9.47559923103199e-12;0.29270102305687 0.861391694816735 9.49967697734389e-12;0.393542967238788 0.86323780094433 9.52003637015389e-12;0.484163255561209 0.863991064572863 9.52834357951394e-12;0.566558577535172 0.862252249097763 9.50916741907954e-12;0.64032207342022 0.856058717379444 9.44086335830556e-12;0.703779407260349 0.843466455473534 9.30199224863495e-12;0.755303502168048 0.823570723288691 9.08257635442439e-12;0.794615128710938 0.797212518880131 8.79189044568993e-12;0.825587797350751 0.762723633264253 9.1048212311097e-12;0.850467966276 0.715595392127937 9.37920693665322e-12;0.868226701612455 0.655812908327459 9.57505541097402e-12;0.879857740838838 0.585623136461541 0.0114337533670117;0.887618230808398 0.508322324776077 0.0304399944076098;0.893915782121304 0.427249785995039 0.0582201024831633;0.900565677294438 0.345671308298629 0.0952824133635513;0.908680394297503 0.26764988684277 0.142244390788589;0.918772173531655 0.199855365894893 0.199855365894893;0.930507777139017 0.264341934982788 0.264341934982788;0.943451070071521 0.333386767361901 0.333386767361901;0.957025217667329 0.405188717515892 0.405188717515892;0.970436601371259 0.478007028597537 0.478007028597537;0.982611413374461 0.549889960687239 0.549889960687239;0.992330373182327 0.618604427046121 0.618604427046121;0.998478381129891 0.683423279659082 0.683423279659082;1 0.743289277608716 0.743289277608716;0.996224511209191 0.797159698510515 0.797159698510515;0.987132576557625 0.84438104450709 0.84438104450709];
            app.UIAxes.LineWidth = 1;
            app.UIAxes.Color = [0.9373 0.9529 0.9608];
            app.UIAxes.ColorOrder = [1 0 0;0 0.9 0;0 0 1;0 0 0;0 1 1;1 0 1;0.9 0.8 0];
            app.UIAxes.Position = [15 277 378 274];

            % Create ConnexionNucloPanel
            app.ConnexionNucloPanel = uipanel(app.UIFigure);
            app.ConnexionNucloPanel.AutoResizeChildren = 'off';
            app.ConnexionNucloPanel.Title = 'Connexion Nucléo';
            app.ConnexionNucloPanel.BackgroundColor = [0.6784 0.8314 0.9529];
            app.ConnexionNucloPanel.Position = [409 411 226 97.922077922078];

            % Create GridLayout11
            app.GridLayout11 = uigridlayout(app.ConnexionNucloPanel);
            app.GridLayout11.ColumnWidth = {'1x', '3x'};
            app.GridLayout11.Padding = [5 5 5 5];

            % Create Refresh
            app.Refresh = uibutton(app.GridLayout11, 'push');
            app.Refresh.ButtonPushedFcn = createCallbackFcn(app, @rafraichirpush, true);
            app.Refresh.BackgroundColor = [0.3961 0.6039 0.7412];
            app.Refresh.Layout.Row = 2;
            app.Refresh.Layout.Column = [1 2];
            app.Refresh.Text = 'Rafraichir';

            % Create PortLabel
            app.PortLabel = uilabel(app.GridLayout11);
            app.PortLabel.HorizontalAlignment = 'center';
            app.PortLabel.Layout.Row = 1;
            app.PortLabel.Layout.Column = 1;
            app.PortLabel.Text = 'Port';

            % Create PortList
            app.PortList = uidropdown(app.GridLayout11);
            app.PortList.Items = {'COM?'};
            app.PortList.Editable = 'on';
            app.PortList.BackgroundColor = [0.9373 0.9529 0.9608];
            app.PortList.Layout.Row = 1;
            app.PortList.Layout.Column = 2;
            app.PortList.Value = 'COM?';

            % Create ConnexionButton
            app.ConnexionButton = uibutton(app.UIFigure, 'push');
            app.ConnexionButton.ButtonPushedFcn = createCallbackFcn(app, @connexionpush, true);
            app.ConnexionButton.BackgroundColor = [0.3961 0.6039 0.7412];
            app.ConnexionButton.Position = [489 518 106 34];
            app.ConnexionButton.Text = 'Connexion';

            % Create ConnLamp
            app.ConnLamp = uilamp(app.UIFigure);
            app.ConnLamp.Position = [428 522 27 27];
            app.ConnLamp.Color = [1 0 0];

            % Create TempsdintgrationPanel
            app.TempsdintgrationPanel = uipanel(app.UIFigure);
            app.TempsdintgrationPanel.Title = 'Temps d''intégration';
            app.TempsdintgrationPanel.BackgroundColor = [0.6784 0.8314 0.9529];
            app.TempsdintgrationPanel.Position = [408 299 225 100];

            % Create Button_3
            app.Button_3 = uibutton(app.TempsdintgrationPanel, 'push');
            app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @tpsintup, true);
            app.Button_3.BackgroundColor = [0.3961 0.6039 0.7412];
            app.Button_3.Position = [7 10 101 26];
            app.Button_3.Text = '+ 100';

            % Create Button_4
            app.Button_4 = uibutton(app.TempsdintgrationPanel, 'push');
            app.Button_4.ButtonPushedFcn = createCallbackFcn(app, @tpsintdown, true);
            app.Button_4.BackgroundColor = [0.3961 0.6039 0.7412];
            app.Button_4.Position = [118 10 102 26];
            app.Button_4.Text = '- 100';

            % Create EnsLabel
            app.EnsLabel = uilabel(app.TempsdintgrationPanel);
            app.EnsLabel.HorizontalAlignment = 'center';
            app.EnsLabel.Position = [7 47 61 22];
            app.EnsLabel.Text = 'En µs';

            % Create tempsIntegration
            app.tempsIntegration = uieditfield(app.TempsdintgrationPanel, 'numeric');
            app.tempsIntegration.Editable = 'off';
            app.tempsIntegration.Position = [83 47 131 22];
            app.tempsIntegration.Value = 100;

            % Create MoyennagePanel
            app.MoyennagePanel = uipanel(app.UIFigure);
            app.MoyennagePanel.Title = 'Moyennage';
            app.MoyennagePanel.BackgroundColor = [0.6784 0.8314 0.9529];
            app.MoyennagePanel.Position = [408 222 224 67];

            % Create NombredacquisitionsLabel
            app.NombredacquisitionsLabel = uilabel(app.MoyennagePanel);
            app.NombredacquisitionsLabel.HorizontalAlignment = 'right';
            app.NombredacquisitionsLabel.Position = [8 13 123 22];
            app.NombredacquisitionsLabel.Text = 'Nombre d''acquisitions';

            % Create nbderelev
            app.nbderelev = uieditfield(app.MoyennagePanel, 'numeric');
            app.nbderelev.Limits = [0 Inf];
            app.nbderelev.RoundFractionalValues = 'on';
            app.nbderelev.BackgroundColor = [0.9373 0.9529 0.9608];
            app.nbderelev.Position = [147 13 73 22];
            app.nbderelev.Value = 1;

            % Create AcquisitionButton
            app.AcquisitionButton = uibutton(app.UIFigure, 'push');
            app.AcquisitionButton.ButtonPushedFcn = createCallbackFcn(app, @acquisitionpush, true);
            app.AcquisitionButton.BackgroundColor = [0.3961 0.6039 0.7412];
            app.AcquisitionButton.FontColor = [0.2706 0.0353 0.1255];
            app.AcquisitionButton.Position = [412 93 219 118];
            app.AcquisitionButton.Text = 'Acquisition';

            % Create EnregistrementPanel
            app.EnregistrementPanel = uipanel(app.UIFigure);
            app.EnregistrementPanel.Title = 'Enregistrement';
            app.EnregistrementPanel.BackgroundColor = [0.6784 0.8314 0.9529];
            app.EnregistrementPanel.Position = [15 15 618 65];

            % Create EnregistrerlafigureButton
            app.EnregistrerlafigureButton = uibutton(app.EnregistrementPanel, 'push');
            app.EnregistrerlafigureButton.ButtonPushedFcn = createCallbackFcn(app, @enregistrerpush, true);
            app.EnregistrerlafigureButton.BackgroundColor = [0.3961 0.6039 0.7412];
            app.EnregistrerlafigureButton.Position = [320 6 142 33];
            app.EnregistrerlafigureButton.Text = 'Enregistrer la figure';

            % Create NomdufichieravecextensionLabel
            app.NomdufichieravecextensionLabel = uilabel(app.EnregistrementPanel);
            app.NomdufichieravecextensionLabel.HorizontalAlignment = 'center';
            app.NomdufichieravecextensionLabel.Position = [17 8 100 28];
            app.NomdufichieravecextensionLabel.Text = {'Nom du fichier '; '(avec extension)'};

            % Create Adressedenregistrement
            app.Adressedenregistrement = uieditfield(app.EnregistrementPanel, 'text');
            app.Adressedenregistrement.BackgroundColor = [0.9373 0.9529 0.9608];
            app.Adressedenregistrement.Position = [125 11 177 22];

            % Create EnregistrerletableauButton
            app.EnregistrerletableauButton = uibutton(app.EnregistrementPanel, 'push');
            app.EnregistrerletableauButton.ButtonPushedFcn = createCallbackFcn(app, @enregistertab, true);
            app.EnregistrerletableauButton.BackgroundColor = [0.3961 0.6039 0.7412];
            app.EnregistrerletableauButton.Position = [469 6 138 33];
            app.EnregistrerletableauButton.Text = 'Enregistrer le tableau';

            % Create GainPanel
            app.GainPanel = uipanel(app.UIFigure);
            app.GainPanel.Title = 'Gain';
            app.GainPanel.Position = [75 93 260 164];

            % Create Button
            app.Button = uibutton(app.GainPanel, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Position = [22 64 100 22];
            app.Button.Text = '+ 0.1';

            % Create Button_2
            app.Button_2 = uibutton(app.GainPanel, 'push');
            app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
            app.Button_2.Position = [137 64 100 22];
            app.Button_2.Text = '- 0.1';

            % Create GainEditFieldLabel
            app.GainEditFieldLabel = uilabel(app.GainPanel);
            app.GainEditFieldLabel.HorizontalAlignment = 'right';
            app.GainEditFieldLabel.Position = [58 99 31 22];
            app.GainEditFieldLabel.Text = 'Gain';

            % Create GainEditField
            app.GainEditField = uieditfield(app.GainPanel, 'numeric');
            app.GainEditField.Position = [104 99 100 22];
            app.GainEditField.Value = 1.8;

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

            % Create UIFigure and components
            createComponents(app)

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

            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