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

% Last Modified by GUIDE v2.5 15-May-2013 14:16:18

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DefSimulator_OpeningFcn, ...
                   'gui_OutputFcn',  @DefSimulator_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 DefSimulator is made visible.
function DefSimulator_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.energie_lichobeznik,'Enable','off','Visible','off');
% 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 DefSimulator (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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


function energie_Callback(hObject, eventdata, handles)
% hObject    handle to energie (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of energie as text
%        str2double(get(hObject,'String')) returns contents of energie as a double

if isnan(str2double(get(handles.energie,'String')))
    set(handles.energie,'String','');
end

% --- Executes during object creation, after setting all properties.
function energie_CreateFcn(hObject, eventdata, handles)
% hObject    handle to energie (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function odpor_Callback(hObject, eventdata, handles)
% hObject    handle to odpor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of odpor as text
%        str2double(get(hObject,'String')) returns contents of odpor as a double

if isnan(str2double(get(handles.odpor,'String')))
    set(handles.odpor,'String','');
end

% --- Executes during object creation, after setting all properties.
function odpor_CreateFcn(hObject, eventdata, handles)
% hObject    handle to odpor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function napeti_Callback(hObject, eventdata, handles)
% hObject    handle to napeti (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of napeti as text
%        str2double(get(hObject,'String')) returns contents of napeti as a double


% --- Executes during object creation, after setting all properties.
function napeti_CreateFcn(hObject, eventdata, handles)
% hObject    handle to napeti (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function inducnost_Callback(hObject, eventdata, handles)
% hObject    handle to inducnost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of inducnost as text
%        str2double(get(hObject,'String')) returns contents of inducnost as a double


% --- Executes during object creation, after setting all properties.
function inducnost_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inducnost (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


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

E = str2double (get(handles.energie, 'String')); % Energie, kterou zadáváme
R = str2double (get(handles.odpor, 'String')); % Odpor, který zadáváme

% Zobrazení varovného okna:
if ~(get(handles.typ_vyboje,'Value') ~= 1 || ~isempty(get(handles.energie,'String'))) || isempty(get(handles.odpor,'String')) % Negace disjunkce 
    warndlg('Zadejte nejprve hodnoty energie defibrilátoru a odporu pacienta.');
    return;
end

a = get(handles.typ_vyboje,'Value'); 

set(handles.informace,'Visible','on');
pause (1)

Fs = 44100;
z = wavread('start_up.wav'); % Otevření zvukového souboru 
sound(z,Fs); % Přehrání zvuku charakterizující nabíjení defibrilátoru

pause (3.5)
set(handles.informace,'Visible','off');

set(handles.informace2,'Visible','on');
pause (2)
set(handles.informace2,'Visible','off');

Fs = 44100;
zz = wavread('vyboj_def.wav'); % Otevření zvukového souboru 
sound(zz,Fs); % Přehrání zvuku charakterizující výboj defibrilátoru


if a == 1 
    
    C = 40*10^-6; % Kapacita kondenzátoru
    set(handles.kapacita,'String',C);
    
    L = 0.15; % Indukčnost ve výstupním obvodu
    set(handles.inducnost,'String',L);
    
    U = sqrt((2*E)/C);% Napětí na kondenzátoru
    set(handles.napeti,'String',U);
    
    y = zeros(1,1000);
    x = linspace(0,40*10^-3,1000);
    
    % Výpočet funkce sinusového impulsu: 
    for i = 1:1000
        y(i) = (2*U*sqrt(C)/sqrt(4*L-C*R^2))*exp(-R/(2*L)*x(i))*sin(sqrt((4*L^2-R^2*L*C)/(4*L^3*C))*x(i)); 
    end
    
    % Vykreslení průbehu funkce:
    plot(x,y);
    grid on;
    xlabel Čas[s]
    ylabel Proud[A]

    
elseif a == 2
    
    E = str2double(get(handles.energie_lichobeznik,'String'));
    E = E(get(handles.energie_lichobeznik,'Value'));
    
    C = 500*10^-6; % Kapacita kondenzátoru
    set(handles.kapacita,'String',C);
    
    L = 'None'; % Indukčnost ve výstupním obvodu
    set(handles.inducnost,'String',L);
    
    if  E==5
        U=270;
        set(handles.napeti,'String',U);
    elseif E==10
           U=270;
           set(handles.napeti,'String',U);
    elseif E==20
           U=365;
           set(handles.napeti,'String',U);
    elseif E==50
           U=584;
           set(handles.napeti,'String',U);
    elseif E==100
           U=837;
           set(handles.napeti,'String',U);
    elseif E==200
           U=1165;
           set(handles.napeti,'String',U);
    elseif E==300
           U=1375;
           set(handles.napeti,'String',U);
    elseif E==360
           U=1375;
           set(handles.napeti,'String',U);           
    end    
    
    y = zeros(1,1000);
    x = linspace(0,C*R*log(U/sqrt((U^2*C-2*E)/C)),1000);

    % Výpočet funkce lichoběžníkového impulsu: 
    for i = 1:1000
        y(i) = U/R*exp((-1/(R*C))*x(i));
    end

    % Vykreslení průbehu funkce:
    stem(x(1),y(1),'Marker','none');
    hold on;
    plot(x,y);
    hold on;
    stem(x(end),y(end),'Marker','none');
    hold off;
    grid on;
    xlabel Čas[s]
    ylabel Proud[A]
    
    
elseif a == 3

    E = str2double(get(handles.energie_lichobeznik,'String'));
    E = E(get(handles.energie_lichobeznik,'Value'));
    
    C = 500*10^-6; % Kapacita kondenzátoru
    set(handles.kapacita,'String',C);
    
    L = 'None'; % Indukčnost ve výstupním obvodu
    set(handles.inducnost,'String',L);
    
    if  E==5
        U=270;
        set(handles.napeti,'String',U);
    elseif E==10
           U=270;
           set(handles.napeti,'String',U);
    elseif E==20
           U=365;
           set(handles.napeti,'String',U);
    elseif E==50
           U=584;
           set(handles.napeti,'String',U);
    elseif E==100
           U=837;
           set(handles.napeti,'String',U);
    elseif E==200
           U=1165;
           set(handles.napeti,'String',U);
    elseif E==300
           U=1375;
           set(handles.napeti,'String',U);
    elseif E==360
           U=1375;
           set(handles.napeti,'String',U);       
    end    
    
    y = zeros(1,1000);
    x = linspace(0,C*R*log(U/sqrt((U^2*C-2*E)/C)),2000);

     % Výpočet funkce bifázického impulsu: 
    for i = 1:2000
        y(i) = U/R*exp((-1/(R*C))*x(i))*sign(1000-i);
    end

    % Vykreslení průbehu funkce:
    stem(x(1),y(1),'Marker','none');
    hold on;
    plot(x,y);
    hold on;
    stem(x(end),y(end),'Marker','none');
    hold off;
    grid on;
    xlabel Čas[s]
    ylabel Proud[A]
    
end


function kapacita_Callback(hObject, eventdata, handles)
% hObject    handle to kapacita (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of kapacita as text
%        str2double(get(hObject,'String')) returns contents of kapacita as a double


% --- Executes during object creation, after setting all properties.
function kapacita_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kapacita (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in typ_vyboje.
function typ_vyboje_Callback(hObject, eventdata, handles)

if get(handles.typ_vyboje,'Value') == 1

   set(handles.energie_lichobeznik,'Enable','off','Visible','off');
   set(handles.energie,'Enable','on','Visible','on');
    
else

   set(handles.energie_lichobeznik,'Enable','on','Visible','on');
   set(handles.energie,'Enable','off','Visible','off');
    
end
% hObject    handle to typ_vyboje (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns typ_vyboje contents as cell array
%        contents{get(hObject,'Value')} returns selected item from typ_vyboje


% --- Executes during object creation, after setting all properties.
function typ_vyboje_CreateFcn(hObject, eventdata, handles)
% hObject    handle to typ_vyboje (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in energie_lichobeznik.
function energie_lichobeznik_Callback(hObject, eventdata, handles)
% hObject    handle to energie_lichobeznik (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns energie_lichobeznik contents as cell array
%        contents{get(hObject,'Value')} returns selected item from energie_lichobeznik


% --- Executes during object creation, after setting all properties.
function energie_lichobeznik_CreateFcn(hObject, eventdata, handles)
% hObject    handle to energie_lichobeznik (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on key press with focus on vyboj and none of its controls.
function vyboj_KeyPressFcn(hObject, eventdata, handles)
% hObject    handle to vyboj (see GCBO)
% eventdata  structure with the following fields (see UICONTROL)
%	Key: name of the key that was pressed, in lower case
%	Character: character interpretation of the key(s) that was pressed
%	Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles    structure with handles and user data (see GUIDATA)



function informace_Callback(hObject, eventdata, handles)
% hObject    handle to informace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of informace as text
%        str2double(get(hObject,'String')) returns contents of informace as a double


% --- Executes during object creation, after setting all properties.
function informace_CreateFcn(hObject, eventdata, handles)
% hObject    handle to informace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function informace2_Callback(hObject, eventdata, handles)
% hObject    handle to informace2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of informace2 as text
%        str2double(get(hObject,'String')) returns contents of informace2 as a double


% --- Executes during object creation, after setting all properties.
function informace2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to informace2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
