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

% Last Modified by GUIDE v2.5 17-May-2012 09:29:27

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

% nastavení funkce volané před ukončením (ochrana)
set(gcf,'CloseRequestFcn', {@funkceKonec handles})

% smazani promennych, u kterych by mohla vzniknout kolize a nastaveni
% techto promennych na globalni
clc
clear global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty
global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty

% uvodni nastaveni viditelnosti panelu pro jednotliva mereni
set(handles.panel_va, 'Visible', 'on');
set(handles.panel_propzot, 'Visible', 'off');
set(handles.panel_zavzot, 'Visible', 'off');


set(handles.pol_hl, 'String', num2str('-1'));

% zjisteni seznamu dostupnych zarizeni
er = false;
try
    daq.reset;
    poc_zar = length(daq.getDevices);
catch chyba
    % pravdepodobne neni nainstalovan Data Acquisition Toolbox
    errordlg('Pravděpodobně není nainstalován Data Acquisition Toolbox', 'Nedostupnost využívaného softwaru');
    poc_zar = 0;
end

set(handles.vyberzarizeni, 'Enable', 'on');
dev = '';
if poc_zar == 0
    dev = '';
    set(handles.vyberzarizeni, 'Enable', 'off');
    set(handles.vyberzarizeni, 'String', num2cell('Nenalezeno žádné zařízení', 2));
elseif poc_zar == 1
    % pouze 1 zarizeni, ktere je automaticky vybrano
    dev = get(daq.getDevices, 'ID');
    popis = get(daq.getDevices, 'Description');
    set(handles.vyberzarizeni, 'String', num2cell([dev ' (' popis ')'], 2));
    try
        fgen = elvis.Fgen;
        s = daq.createSession('ni');
        vps = elvis.VarPowSupply;
    catch chyba
        errordlg('Nastala chyba při konfiguraci zařízení. Opravdu je zařízení připojeno?', 'Chyba konfigurace');
    end
else
    dev = '';
    zar = get(daq.getDevices, 'ID');
    popis = get(daq.getDevices, 'Description');
    %shluk = cat(2, zar, popis);
    seznam = cell(poc_zar+1, 1);
    seznam(1) = num2cell('Vybrat zařízení...', 2);
    for i=1:poc_zar
        seznam(i+1) = num2cell([cell2mat(zar(i)) ' (' cell2mat(popis(i)) ')'], 2);
    end
    set(handles.vyberzarizeni, 'String', seznam);
end

% nastaveni grafu VA charakteristik
axes(handles.graf_va);
%set(h, 'Box', 'on'); % zapnuti boxu kolem celeho grafu
axis([-10 10 -20 20]);
xlabel('U (V) \rightarrow');
ylabel('I (mA) \rightarrow');

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = tester_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 selection change in vyberzarizeni.
function vyberzarizeni_Callback(hObject, eventdata, handles)
% hObject    handle to vyberzarizeni (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 vyberzarizeni contents as cell array
%        contents{get(hObject,'Value')} returns selected item from vyberzarizeni
global dev s vps fgen
% do globalni promenne dev zavede nazev zarizeni, ktere je vybrano
poc_zar = length(daq.getDevices);
x = get(handles.vyberzarizeni, 'Value');
if poc_zar == 0
    dev = '';
elseif poc_zar == 1
    % pouze jedno zarizeni
    %dev = get(handles.vyberzarizeni, 'Value');
    dev = get(daq.getDevices, 'ID');
else
    % vice zarizeni (prvni volba je automaticka, ta se nebere)
    zar = get(daq.getDevices, 'ID');
    if x == 1
        dev = '';
    else
        dev = cell2mat(zar(x-1));
    end
end
try
    %dev = get(handles.vyberzarizeni, 'Value');
    fgen = elvis.Fgen;
    s = daq.createSession('ni');
    vps = elvis.VarPowSupply;
    %error('konfigurace zarizeni');
catch chyba
    errordlg(['Nastala chyba při konfiguraci zařízení. Opravdu je zařízení připojeno?'], 'Chyba konfigurace');
end
%zarizeni = seznam(get(handles.vyberzarizeni, 'Value'));






% --- Executes during object creation, after setting all properties.
function vyberzarizeni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vyberzarizeni (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 castmereni.
function castmereni_Callback(hObject, eventdata, handles)
% hObject    handle to castmereni (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 castmereni contents as cell array
%        contents{get(hObject,'Value')} returns selected item from castmereni
volba = get(handles.castmereni, 'Value');
set(handles.panel_va, 'Visible', 'off');
set(handles.panel_propzot, 'Visible', 'off');
set(handles.panel_zavzot, 'Visible', 'off');
if volba == 1
    set(handles.panel_va, 'Visible', 'on');
elseif volba == 2
    set(handles.panel_propzot, 'Visible', 'on');
else
    set(handles.panel_zavzot, 'Visible', 'on');
end




% --- Executes during object creation, after setting all properties.
function castmereni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to castmereni (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 button press in spustitmereni.
function spustitmereni_Callback(hObject, eventdata, handles)
% hObject    handle to spustitmereni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty

set(handles.probihamereni, 'String', '1');
set(handles.spustitmereni, 'Enable', 'off');
set(handles.premerit_va, 'Enable', 'off');
set(handles.premerit_propzot, 'Enable', 'off');
set(handles.premerit_zavzot, 'Enable', 'off');

% vymazani predeslych hodnot
set(handles.Uprahove, 'String', '');
set(handles.Uzener, 'String', '');
set(handles.Izbyt, 'String', '');
set(handles.pol, 'String', '');

set(handles.fgen_prop_amp, 'String', '2.5');
set(handles.offset_prop, 'String', '2.5');
set(handles.propzotdob, 'String', '');
set(handles.propzot_spatna, 'String', '');

set(handles.zavzotdob, 'String', '');
set(handles.fgen_zav_amp, 'String', '5');

set(handles.vysledek, 'String', '');

if isempty(dev)
    msgbox('Nutno vybrat zařízení, se kterým se bude pracovat', 'Výběr zařízení');
    set(handles.probihamereni, 'String', '0');
    set(handles.spustitmereni, 'Enable', 'on');
    set(handles.premerit_va, 'Enable', 'on');
    set(handles.premerit_propzot, 'Enable', 'on');
    set(handles.premerit_zavzot, 'Enable', 'on');
    return;
end

axes(handles.graf_va);
cla;
axes(handles.graf_propzot);
cla;
axes(handles.graf_zavzot);
cla;

% nejdrive test zapnute prototypovaci desky
if ~prototypovaci_deska
    errordlg('Pravděpodobně není zapnuta prototypovací deska. Měření nemůže proběhnout.', 'Stav prototypovací desky');
    set(handles.probihamereni, 'String', '0');
    set(handles.spustitmereni, 'Enable', 'on');
    set(handles.premerit_va, 'Enable', 'on');
    set(handles.premerit_propzot, 'Enable', 'on');
    set(handles.premerit_zavzot, 'Enable', 'on');
    return;
end

Umax = str2num(get(handles.maxnapeti, 'String'));
Imax = str2num(get(handles.maxproud, 'String'))/1000;
krok = str2num(get(handles.krok_napeti, 'String'));
vzorku = str2num(get(handles.vzorku, 'String'));

ID_cas_va = tic;
[Uprahove Uzener Izbyt pol] = zmerstaticke(handles.graf_va, Umax, Imax, krok, vzorku);
cas_va = toc(ID_cas_va);
if isnan(Uprahove)
    warndlg('Součástka pravděpodobně není zapojena', 'Nezapojená součástka');
    set(handles.probihamereni, 'String', '0');
    set(handles.spustitmereni, 'Enable', 'on');
    set(handles.premerit_va, 'Enable', 'on');
    set(handles.premerit_propzot, 'Enable', 'on');
    set(handles.premerit_zavzot, 'Enable', 'on');
    return;
end

% nastaveni novych hodnot
set(handles.Uprahove, 'String', num2str(Uprahove));
if isnan(Uzener)
    set(handles.Uzener, 'String', '-');
else
    set(handles.Uzener, 'String', num2str(Uzener));
end
if isnan(Izbyt)
    set(handles.Izbyt, 'String', '-');
else
    set(handles.Izbyt, 'String', num2str(round(Izbyt * 1e9)));
end
if pol == 1
    set(handles.pol, 'String', 'propustná');
else
    set(handles.pol, 'String', 'závěrná');
end
set(handles.pol_hl, 'String', num2str(pol));
if isnan(Uzener)
    set(handles.jezener, 'String', '0')
else
    set(handles.jezener, 'String', '1')
end

% propustna zotavovaci doba
amp1 = str2num(get(handles.fgen_prop_amp, 'String'));
ind = get(handles.fgen_prop_frek, 'Value');
volby=get(handles.fgen_prop_frek, 'String');
frek1=str2num(cell2mat(volby(ind))) * 1e3;
%rozdilper = str2num(get(handles.rozdilper, 'String'));
polarizace = str2num(get(handles.pol_hl, 'String'));

ID_cas_propzot = tic;
[t_fr] = zmerpropzot(handles.graf_propzot, amp1, frek1, 10, 1.25e6, polarizace);
cas_propzot = toc(ID_cas_propzot);

set(handles.propzotdob, 'String', num2str(t_fr*1e9));
if t_fr > 1e-6
    set(handles.propzot_spatna, 'String', 'Tato hodnota je nejspíš nepřesná.');
end

% zaverna zotavovaci doba
t_rr = -1;
amp2 = 0; frek2 = 0;
if isnan(Uzener)
    %msgbox('bude se merit i zaverna zotavovaci doba');
    amp2 = str2num(get(handles.fgen_zav_amp, 'String'));
    ind = get(handles.fgen_zav_frek, 'Value');
    volby=get(handles.fgen_zav_frek, 'String');
    frek2=str2num(cell2mat(volby(ind))) * 1e3;
    polarizace = str2num(get(handles.pol_hl, 'String'));
    
    ID_cas_zavzot = tic;
    [t_rr] = zmerzavzot(handles.graf_zavzot, amp2, frek2, 10, 1.25e6, polarizace);
    cas_zavzot = toc(ID_cas_zavzot);

    if t_rr == -1
        set(handles.zavzotdob, 'String', 'nezjištěno');
    else
        set(handles.zavzotdob, 'String', num2str(t_rr*1e9));
    end
    %set(handles.zavzotdob, 'String', num2str(t_rr*1e9));
else
    %msgbox('zenerka se nemeri');
    set(handles.zavzotdob, 'String', 'neměřeno');
    cas_zavzot = 0;
end

set(handles.spustitmereni, 'Enable', 'on');
set(handles.premerit_va, 'Enable', 'on');
set(handles.premerit_propzot, 'Enable', 'on');
set(handles.premerit_zavzot, 'Enable', 'on');
set(handles.probihamereni, 'String', '0');
% konec celeho mereni

nastavene_veliciny = 0;
cas = 0;
% va char. -- Umax, Imax, krok, vzorku
% prop. zot. dob. -- amp1, frek1
% zav. zot. dob. -- amp2, frek2

msgbox('Měření je hotovo', 'Konec měření');

zadane_hodnoty = [Umax Imax krok vzorku amp1 frek1 amp2 frek2];
zjistene_hodnoty = [Uprahove Uzener Izbyt polarizace t_fr t_rr];
casove_hodnoty = [cas_va cas_propzot cas_zavzot];

% zjisteni typu diody a vygenerovani reportu
typ_diody(handles);



function maxnapeti_Callback(hObject, eventdata, handles)
% hObject    handle to maxnapeti (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 maxnapeti as text
%        str2double(get(hObject,'String')) returns contents of maxnapeti as a double
hodnota = str2double(get(handles.maxnapeti, 'String'));
if ~(hodnota <= 10 && hodnota > 0)
    errordlg('Hodnota musí spadat do intervalu (0, 10> V.', 'Neplatná hodnota');
    set(handles.maxnapeti, 'String', '10');
end


% --- Executes during object creation, after setting all properties.
function maxnapeti_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxnapeti (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 maxproud_Callback(hObject, eventdata, handles)
% hObject    handle to maxproud (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 maxproud as text
%        str2double(get(hObject,'String')) returns contents of maxproud as a double
hodnota = str2double(get(handles.maxproud, 'String'));
if ~(hodnota <= 100 && hodnota > 0)
    errordlg('Hodnota musí spadat do intervalu (0, 100> mA.', 'Neplatná hodnota');
    set(handles.maxproud, 'String', '20');
end


% --- Executes during object creation, after setting all properties.
function maxproud_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxproud (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 premerit_va.
function premerit_va_Callback(hObject, eventdata, handles)
% hObject    handle to premerit_va (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty

set(handles.probihamereni, 'String', '1');
% vymazani predeslych hodnot
set(handles.Uprahove, 'String', '');
set(handles.Uzener, 'String', '');
set(handles.Izbyt, 'String', '');
set(handles.pol, 'String', '');
set(handles.vysledek, 'String', '');

if isempty(dev)
    msgbox('Nutno vybrat zařízení, se kterým se bude pracovat', 'Výběr zařízení');
    set(handles.probihamereni, 'String', '0');
    return;
end

% nejdrive test zapnute prototypovaci desky
if ~prototypovaci_deska
    errordlg('Pravděpodobně není zapnuta prototypovací deska. Měření nemůže proběhnout.', 'Stav prototypovací desky');
    set(handles.probihamereni, 'String', '0');
    return;
end

Umax = str2num(get(handles.maxnapeti, 'String'));
Imax = str2num(get(handles.maxproud, 'String'))/1000;
krok = str2num(get(handles.krok_napeti, 'String'));
vzorku = str2num(get(handles.vzorku, 'String'));

ID_cas_va = tic;
[Uprahove Uzener Izbyt pol] = zmerstaticke(handles.graf_va, Umax, Imax, krok, vzorku);
cas_va = toc(ID_cas_va);
casove_hodnoty(1) = cas_va;

% ulozit nove hodnoty do globalni promenne zadane_hodnoty
zadane_hodnoty(1) = Umax;
zadane_hodnoty(2) = Imax;
zadane_hodnoty(3) = krok;
zadane_hodnoty(4) = vzorku;
zjistene_hodnoty(1) = Uprahove;
zjistene_hodnoty(2) = Uzener;
zjistene_hodnoty(3) = Izbyt;
zjistene_hodnoty(4) = pol;

% nastaveni novych hodnot
set(handles.Uprahove, 'String', num2str(Uprahove));
if isnan(Uzener)
    set(handles.Uzener, 'String', '-');
else
    set(handles.Uzener, 'String', num2str(Uzener));
end
if isnan(Izbyt)
    set(handles.Izbyt, 'String', '-');
else
    set(handles.Izbyt, 'String', num2str(round(Izbyt * 1e9)));
end
if pol == 1
    set(handles.pol, 'String', 'propustná');
else
    set(handles.pol, 'String', 'závěrná');
end
set(handles.pol_hl, 'String', num2str(pol));
if isnan(Uzener)
    set(handles.jezener, 'String', '0');
else
    set(handles.jezener, 'String', '1');
end

set(handles.probihamereni, 'String', '0');

% zjisteni typu diody
%typ_diody(handles, Uprahove, Uzener, Izbyt, polarizace, t_fr, t_rr);

msgbox('Měření je hotovo', 'Konec měření');

% zjisteni typu diody a vygenerovani reportu
typ_diody(handles);


% --- Executes on button press in zarizeni_obnova.
function zarizeni_obnova_Callback(hObject, eventdata, handles)
% hObject    handle to zarizeni_obnova (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global dev s fgen vps
set(handles.vyberzarizeni, 'Enable', 'on');
daq.reset;
zar = daq.getDevices;
set(handles.vyberzarizeni, 'Value', 1);
if length(zar) == 1
	% pouze 1 zarizeni, ktere je automaticky vybrano
    dev = 1;
    set(handles.vyberzarizeni, 'String', [zar(1).ID ' (' zar(1).Description ')']);
    % nutno take vytvorit promenne s referencemi na HW
    try
        dev = get(handles.vyberzarizeni, 'Value');
        fgen = elvis.Fgen;
        s = daq.createSession('ni');
        vps = elvis.VarPowSupply;
    catch chyba
        errordlg('Nastala chyba při konfiguraci zařízení. Opravdu je zařízení připojeno?', 'Chyba konfigurace');
    end
else
    seznam_zarizeni = cell(length(zar), 1);
    seznam_zarizeni(1) = num2cell('Vybrat zařízení...', 2);
    for i=1:length(zar)
        seznam_zarizeni(i+1) = num2cell([zar(i).ID ' (' zar(i).Description ')'], 2);
    end
    if isempty(zar)
        set(handles.vyberzarizeni, 'Enable', 'off');
        seznam_zarizeni = num2cell('Nenalezeno žádné zařízení', 2);
    end
    set(handles.vyberzarizeni, 'String', seznam_zarizeni);
    dev = 0;
end



function krok_napeti_Callback(hObject, eventdata, handles)
% hObject    handle to krok_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 krok_napeti as text
%        str2double(get(hObject,'String')) returns contents of krok_napeti as a double
hodnota = str2double(get(handles.krok_napeti, 'String'));
if ~(hodnota <= 5 && hodnota > 0)
    errordlg('Hodnota musí spadat do intervalu (0, 5> V.', 'Neplatná hodnota');
    set(handles.krok_napeti, 'String', '0.5');
end


% --- Executes during object creation, after setting all properties.
function krok_napeti_CreateFcn(hObject, eventdata, handles)
% hObject    handle to krok_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 vzorku_Callback(hObject, eventdata, handles)
% hObject    handle to vzorku (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 vzorku as text
%        str2double(get(hObject,'String')) returns contents of vzorku as a double
hodnota = str2double(get(handles.vzorku, 'String'));
if ~(hodnota <= 100 && hodnota > 0)
    errordlg('Hodnota musí spadat do intervalu (1, 100>.', 'Neplatná hodnota');
    set(handles.vzorku, 'String', '10');
end


% --- Executes during object creation, after setting all properties.
function vzorku_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vzorku (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 Uprahove_Callback(hObject, eventdata, handles)
% hObject    handle to Uprahove (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 Uprahove as text
%        str2double(get(hObject,'String')) returns contents of Uprahove as a double


% --- Executes during object creation, after setting all properties.
function Uprahove_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Uprahove (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 Uzener_Callback(hObject, eventdata, handles)
% hObject    handle to Uzener (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 Uzener as text
%        str2double(get(hObject,'String')) returns contents of Uzener as a double


% --- Executes during object creation, after setting all properties.
function Uzener_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Uzener (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 Izbyt_Callback(hObject, eventdata, handles)
% hObject    handle to Izbyt (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 Izbyt as text
%        str2double(get(hObject,'String')) returns contents of Izbyt as a double


% --- Executes during object creation, after setting all properties.
function Izbyt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Izbyt (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 pol_Callback(hObject, eventdata, handles)
% hObject    handle to pol (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 pol as text
%        str2double(get(hObject,'String')) returns contents of pol as a double


% --- Executes during object creation, after setting all properties.
function pol_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pol (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 premerit_propzot.
function premerit_propzot_Callback(hObject, eventdata, handles)
% hObject    handle to premerit_propzot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty

set(handles.probihamereni, 'String', '1');
% vymazani predeslych hodnot
set(handles.propzotdob, 'String', '');
%set(handles.fgen_prop_frek, 'String', '');
%set(handles.rozdilper, 'String', '');
set(handles.propzot_spatna, 'String', '');
set(handles.vysledek, 'String', '');

axes(handles.graf_propzot);
cla;

if isempty(dev)
    msgbox('Nutno vybrat zařízení, se kterým se bude pracovat', 'Výběr zařízení');
    set(handles.probihamereni, 'String', '0');
    return;
end

if str2num(get(handles.pol_hl, 'String')) == -1
    errordlg('Před tímto měřením je nutno zjistit polarizaci pomocí měření VA charakteristik.', 'Neznámá polarizace');
    set(handles.probihamereni, 'String', '0');
    return;
end

% nejdrive test zapnute prototypovaci desky
if ~prototypovaci_deska
    errordlg('Pravděpodobně není zapnuta prototypovací deska. Měření nemůže proběhnout.', 'Stav prototypovací desky');
    set(handles.probihamereni, 'String', '0');
    return;
end

amp = str2num(get(handles.fgen_prop_amp, 'String'));
ind = get(handles.fgen_prop_frek, 'Value');
volby=get(handles.fgen_prop_frek, 'String');
frek=str2num(cell2mat(volby(ind))) * 1e3;
polarizace = str2num(get(handles.pol_hl, 'String'));
zadane_hodnoty(5) = amp;
zadane_hodnoty(6) = frek;

set(handles.propzotdob, 'String', '');

ID_cas_propzot = tic;
[t_fr] = zmerpropzot(handles.graf_propzot, amp, frek, 10, 1.25e6, polarizace);
cas_propzot = toc(ID_cas_propzot);
casove_hodnoty(2) = cas_propzot;

zjistene_hodnoty(5) = t_fr;

set(handles.propzotdob, 'String', num2str(t_fr*1e9));
if t_fr > 1e-6
    set(handles.propzot_spatna, 'String', 'Tato hodnota je nejspíš nepřesná.');
end

set(handles.probihamereni, 'String', '0');
msgbox('Měření je hotovo', 'Konec měření');

% zjisteni typu diody a vygenerovani reportu
typ_diody(handles);



function fgen_prop_amp_Callback(hObject, eventdata, handles)
% hObject    handle to fgen_prop_amp (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 fgen_prop_amp as text
%        str2double(get(hObject,'String')) returns contents of fgen_prop_amp as a double
Ud = str2double(get(handles.Uprahove, 'String'));
amp = str2double(get(handles.fgen_prop_amp, 'String'));
%offset = str2double(get(handles.offset_prop, 'String'));
if isnan(Ud) || isnan(amp)
    set(handles.fgen_prop_amp, 'String', '2.5');
    set(handles.offset_prop, 'String', '2.5');
    return;
end
nova = (Ud+0.5)/2;
nova = ceil(2*nova+0.01)/2;
% vybere se vetsi z hodnot (spocitana minimalni a uzivatelem zadana)
if amp < nova
    amp = nova;
end
if amp > 2.5
    amp = 2.5;
end
set(handles.fgen_prop_amp, 'String', num2str(amp));
set(handles.offset_prop, 'String', num2str(amp));



% --- Executes during object creation, after setting all properties.
function fgen_prop_amp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fgen_prop_amp (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 fgen_prop_frek_Callback(hObject, eventdata, handles)
% hObject    handle to fgen_prop_frek (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 fgen_prop_frek as text
%        str2double(get(hObject,'String')) returns contents of fgen_prop_frek as a double


% --- Executes during object creation, after setting all properties.
function fgen_prop_frek_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fgen_prop_frek (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 pol_hl_Callback(hObject, eventdata, handles)
% hObject    handle to pol_hl (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 pol_hl as text
%        str2double(get(hObject,'String')) returns contents of pol_hl as a double


% --- Executes during object creation, after setting all properties.
function pol_hl_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pol_hl (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 propzotdob_Callback(hObject, eventdata, handles)
% hObject    handle to propzotdob (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 propzotdob as text
%        str2double(get(hObject,'String')) returns contents of propzotdob as a double


% --- Executes during object creation, after setting all properties.
function propzotdob_CreateFcn(hObject, eventdata, handles)
% hObject    handle to propzotdob (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 rozdilper_Callback(hObject, eventdata, handles)
% hObject    handle to rozdilper (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 rozdilper as text
%        str2double(get(hObject,'String')) returns contents of rozdilper as a double


% --- Executes during object creation, after setting all properties.
function rozdilper_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rozdilper (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 jezener_Callback(hObject, eventdata, handles)
% hObject    handle to jezener (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 jezener as text
%        str2double(get(hObject,'String')) returns contents of jezener as a double


% --- Executes during object creation, after setting all properties.
function jezener_CreateFcn(hObject, eventdata, handles)
% hObject    handle to jezener (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 premerit_zavzot.
function premerit_zavzot_Callback(hObject, eventdata, handles)
% hObject    handle to premerit_zavzot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global dev s vps fgen zadane_hodnoty zjistene_hodnoty casove_hodnoty

set(handles.probihamereni, 'String', '1');
% vymazani predeslych hodnot
set(handles.zavzotdob, 'String', '');
set(handles.vysledek, 'String', '');

axes(handles.graf_zavzot);
cla;

if isempty(dev)
    msgbox('Nutno vybrat zařízení, se kterým se bude pracovat', 'Výběr zařízení');
    set(handles.probihamereni, 'String', '0');
    return;
end

if str2num(get(handles.pol_hl, 'String')) == -1
    errordlg('Před měřením je nutno zjistit polarizaci pomocí měření VA charakteristik.', 'Neznámá polarizace');
    set(handles.probihamereni, 'String', '0');
    return;
end

if str2num(get(handles.jezener, 'String')) == 1
    errordlg('Závěrnou zotavovací dobu nelze na Zenerových diodách měřit.', 'Měření na Zenerových diodách');
    set(handles.probihamereni, 'String', '0');
    return;
end

% nejdrive test zapnute prototypovaci desky
if ~prototypovaci_deska
    errordlg('Pravděpodobně není zapnuta prototypovací deska. Měření nemůže proběhnout.', 'Stav prototypovací desky');
    set(handles.probihamereni, 'String', '0');
    return;
end

amp = str2num(get(handles.fgen_zav_amp, 'String'));
ind = get(handles.fgen_zav_frek, 'Value');
volby=get(handles.fgen_zav_frek, 'String');
frek=str2num(cell2mat(volby(ind))) * 1e3;
polarizace = str2num(get(handles.pol_hl, 'String'));
zadane_hodnoty(7) = amp;
zadane_hodnoty(8) = frek;

ID_cas_zavzot = tic;
[t_rr] = zmerzavzot(handles.graf_zavzot, amp, frek, 10, 1.25e6, polarizace);
cas_zavzot = toc(ID_cas_zavzot);
casove_hodnoty(3) = cas_zavzot;

zjistene_hodnoty(6) = t_rr;

if t_rr == -1
    set(handles.zavzotdob, 'String', 'nezjištěno');
else
    set(handles.zavzotdob, 'String', num2str(t_rr*1e9));
end

set(handles.probihamereni, 'String', '0');
msgbox('Měření je hotovo', 'Konec měření');

% zjisteni typu diody a vygenerovani reportu
typ_diody(handles);



function fgen_zav_amp_Callback(hObject, eventdata, handles)
% hObject    handle to fgen_zav_amp (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 fgen_zav_amp as text
%        str2double(get(hObject,'String')) returns contents of fgen_zav_amp as a double
amp = str2num(get(handles.fgen_zav_amp, 'String'));
if amp < 1 || amp > 5
    errordlg('Amplituda musí být v intervalu <Ud, 5> V.', 'Hodnota amplitudy');
    set(handles.fgen_zav_amp, 'String', '5');
end

% --- Executes during object creation, after setting all properties.
function fgen_zav_amp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fgen_zav_amp (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 fgen_zav_frek_Callback(hObject, eventdata, handles)
% hObject    handle to fgen_zav_frek (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 fgen_zav_frek as text
%        str2double(get(hObject,'String')) returns contents of fgen_zav_frek as a double


% --- Executes during object creation, after setting all properties.
function fgen_zav_frek_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fgen_zav_frek (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 rozdilper2_Callback(hObject, eventdata, handles)
% hObject    handle to rozdilper2 (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 rozdilper2 as text
%        str2double(get(hObject,'String')) returns contents of rozdilper2 as a double


% --- Executes during object creation, after setting all properties.
function rozdilper2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rozdilper2 (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 zavzotdob_Callback(hObject, eventdata, handles)
% hObject    handle to zavzotdob (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 zavzotdob as text
%        str2double(get(hObject,'String')) returns contents of zavzotdob as a double


% --- Executes during object creation, after setting all properties.
function zavzotdob_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zavzotdob (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 offset_prop_Callback(hObject, eventdata, handles)
% hObject    handle to offset_prop (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 offset_prop as text
%        str2double(get(hObject,'String')) returns contents of offset_prop as a double


% --- Executes during object creation, after setting all properties.
function offset_prop_CreateFcn(hObject, eventdata, handles)
% hObject    handle to offset_prop (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 probihamereni_Callback(hObject, eventdata, handles)
% hObject    handle to probihamereni (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 probihamereni as text
%        str2double(get(hObject,'String')) returns contents of probihamereni as a double


% --- Executes during object creation, after setting all properties.
function probihamereni_CreateFcn(hObject, eventdata, handles)
% hObject    handle to probihamereni (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 vysledek_Callback(hObject, eventdata, handles)
% hObject    handle to vysledek (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 vysledek as text
%        str2double(get(hObject,'String')) returns contents of vysledek as a double


% --- Executes during object creation, after setting all properties.
function vysledek_CreateFcn(hObject, eventdata, handles)
% hObject    handle to vysledek (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
