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

% Last Modified by GUIDE v2.5 26-Nov-2018 17:54:00

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

setappdata(0,'Channel1val',0);
setappdata(0,'Channel2val',0);
setappdata(0,'Channel3val',0);
setappdata(0,'Channel4val',0);

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


% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FrontGUI_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;



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

% Hint: get(hObject,'Value') returns toggle state of Channel1box
Channel1val=get(hObject,'Value');
%assignin('base','Channel1val',Channel1val);
%handles.Channel1val=Channel1val;
%handles.output = hObject;
setappdata(0,'Channel1val',Channel1val);


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

% Hint: get(hObject,'Value') returns toggle state of Channel2Box

Channel2val=get(hObject,'Value');
%assignin('base','Channel2val',Channel2val);
%handles.Channel2val=Channel2val;
%handles.output = hObject;
setappdata(0,'Channel2val',Channel2val);


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

% Hint: get(hObject,'Value') returns toggle state of Channel3Box
Channel3val=get(hObject,'Value');
%assignin('base','Channel3val',Channel3val);
setappdata(0,'Channel3val',Channel3val);




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

% Hint: get(hObject,'Value') returns toggle state of Channel4Box
Channel4val=get(hObject,'Value');
%assignin('base','Channel4val',Channel4val);
setappdata(0,'Channel4val',Channel4val);


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


    
% folder settings
filename_prefix =getappdata(0,'PrefixName');

folder_path =getappdata(0,'FolderDest');
L = 2^15;
V = 4.096;
%serial settings
comport = getappdata(0,'compopval');
s = serial(comport);
s.Timeout = 0.5;
instrfind
delete(instrfind)
s = serial("COM3");
s.Timeout = 0.5;
baudrated=getappdata(0,'BaudVal');
s.BaudRate=baudrated;
fopen(s);

%freq and sampperlog
x=getappdata(0,'LogFreq');
y=getappdata(0,'samplog');
% get data

date=getappdata(0,'DateFormat');
time=getappdata(0,'TimeFormat');
n = 1;
%tp1 = 'dd mm yyyy HH MM SS';
tp1 = [date,' ',time];
t=[];
a=10;
ylim = 1;
for i=1:1:50
  
    t=[t,i];
    pause(x);
    filename = sprintf('%s.csv',datestr(now,tp1));
    fprintf(s,'a');
    dat1 = fgets(s);
    filename = [folder_path,filename_prefix,filename]
    %data conversion
    ch1 = dat1(1:4);
    ch1 = hex2dec(ch1);
    if (ch1>32767)
        ch1= 65535 - ch1;
    end
    ch1_v(i) = ch1*V/L;
    
    ch2 = dat1(5:8);
    ch2 = hex2dec(ch2);
    if (ch2>32767)
        ch2= 65535 - ch2;
    end
    
    ch2_v(i) = ch2*V/L;
    
    ch3 = dat1(9:12);
    ch3 = hex2dec(ch3);
    
    if (ch3>32767)
        ch3= 65535 - ch3;
    end
    ch3_v(i) = ch3*V/L;
    
    ch4 = dat1(13:16);
    ch4 = hex2dec(ch4);
    
    if (ch4>32767)
        ch4= 65535 - ch4;
    end
    ch4_v(i) = ch4*V/L;
    
    dat2(ylim,:) = [ch1_v(i),ch2_v(i),ch3_v(i),ch4_v(i)];
    if(ylim==y)
    csvwrite(filename,dat2,y,4);
    ylim = 0;
    end
    ylim = ylim+1;
    m=getappdata(0,'Channel1val');
    n=getappdata(0,'Channel2val');
    b=getappdata(0,'Channel3val');
    v=getappdata(0,'Channel4val');
    
    switch m
        case 0
            switch n
                case 0
                switch b
                    case 0
                    switch v
                        case 0
                            
                        case 1
                             plot(t,ch4_v);
                             legend('channel 3');
                    end
                    case 1 
                        switch v
                        case 0
                            
                             plot(t,ch3_v);
                             legend('channel 2');
                           
                        case 1
                            
                                plot(t,ch3_v,t,ch4_v);
                                legend('channel 2','channel 3');
   
                        end
                end
                
                case 1
                    switch b
                    case 0
                    switch v
                        case 0
                            
                             plot(t,ch2_v);
                             legend('channel 1');
   
                        case 1
                            
                                  plot(t,ch2_v,t,ch4_v);
                                  legend('channel 1','channel 3');
   
                         end
                    case 1 
                        switch v
                        case 0
                            
                            
                             plot(t,ch2_v,t,ch3_v);
                             legend('channel 1','channel 2');
   
                           
                        case 1
                            
                                         plot(t,ch2_v,t,ch3_v,t,ch4_v);
                                        legend('channel 1','channel 2','channel 3');
   
   
                        end
                end
            end
        case 1
            
             switch n
                case 0
                switch b
                    case 0
                    switch v
                        case 0
                          
                             plot(t,ch1_v);
                                legend('channel 0');
   
                        case 1
                             
                                plot(t,ch1_v,t,ch4_v);
                                legend('channel 0','channel 3');
   
                    end
                    case 1 
                        switch v
                        case 0
                            
                           
                            plot(t,ch1_v,t,ch3_v);
                             legend('channel 0','channel 2');
   
                           
                        case 1
                            
                                
                            plot(t,ch1_v,t,ch3_v,t,ch4_v);
                             legend('channel 0','channel 2','channel 3');
   
   
                        end
                end
                
                case 1
                    switch b
                    case 0
                    switch v
                        case 0
                            
                            
                                plot(t,ch1_v,t,ch2_v);
                                 legend('channel 0','channel 1');
   
   
                        case 1
                            
                                  
                            plot(t,ch1_v,t,ch2_v,t,ch4_v);
                            legend('channel 0','channel 1','channel 3');
   
   
                         end
                    case 1 
                        switch v
                        case 0
                            
                            
                             
                             plot(t,ch1_v,t,ch2_v,t,ch3_v);
                             legend('channel 0','channel 1','channel 2');
   
   
                           
                        case 1
                            
                                       
                                plot(t,ch1_v,t,ch2_v,t,ch3_v,t,ch4_v);
                                legend('channel 0','channel 1','channel 2','channel 3');
   
   
   
                        end
                end
             end
    end
            
                        
                        
                            
   
  
   
  
   
   
   
   
   
   
   
   
  
   
   
   

   
    if(mod(i,10)==0)
      a=a+10;
    end
   % legend('channel 0','channel 1','channel 2','channel 3');
    axis([0 a -5 5]);
    
   handles.output = hObject;

end
