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

% Last Modified by GUIDE v2.5 11-Apr-2017 17:27:42

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

% Choose default command line output for CCSDS_App
imshow('CCSDS.png','Parent',handles.axes1);
movegui(gcf,'center');
Deber = [];
set(handles.text_minsnr_d,'string','0');
set(handles.text_minsnr_d,'enable','inactive');
set(handles.text_maxsnr_d,'string','5');
set(handles.text_maxsnr_d,'enable','inactive');
set(handles.text_kroksnr_d,'string','0.5');
set(handles.text_kroksnr_d,'enable','inactive');
set(handles.text_data_d,'string','100000');
set(handles.text_data_d,'enable','inactive');
set(handles.popupmenu_blok,'value',1);
set(handles.popupmenu_blok,'enable','on');
set(handles.popupmenu_alg,'value',1);
set(handles.popupmenu_alg,'enable','on');
set(handles.popupmenu_iter,'value',1);
set(handles.popupmenu_iter,'enable','on');
set(handles.popupmenu_rate,'value',1);
set(handles.popupmenu_rate,'enable','on');
set(handles.checkbox_iter,'value',1);
set(handles.btn_ulozit,'enable','off');
set(handles.btn_zahodit,'enable','off');
set(handles.btn_simulovat,'enable','on');

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = CCSDS_App_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 btn_simulovat.
function btn_simulovat_Callback(hObject, eventdata, handles)

set(handles.btn_simulovat,'enable','off');
set(handles.text_minsnr_d,'enable','off');
set(handles.text_maxsnr_d,'enable','off');
set(handles.text_kroksnr_d,'enable','off');
set(handles.text_data_d,'enable','off');
set(handles.popupmenu_blok,'enable','off');
set(handles.popupmenu_alg,'enable','off');
set(handles.popupmenu_rate,'enable','off');
set(handles.popupmenu_iter,'enable','off');
min_snr = str2num(get(handles.text_minsnr_d,'string'));
max_snr = str2num(get(handles.text_maxsnr_d,'string'));
skok_snr = str2num(get(handles.text_kroksnr_d,'string'));
[r,s] = size(skok_snr);

if isempty(min_snr)
    warndlg('Nespravne zadana minimálna hodnota Eb/N0.','Error');
    return;
elseif isempty(max_snr)
    warndlg('Nespravne zadana maximálna hodnota Eb/N0.','Error');
    return;
elseif isempty(skok_snr)||r>1||s>1
    warndlg('Nespravne zadaná hodnota krok Eb/N0.','Error');
    return;
elseif max_snr<min_snr
    warndlg('Maximálna hodnota Eb/N0 musi byt vacsie alebo rovna ako minimálna hodnota.','Error');
    return;
else
    snr = min_snr:skok_snr:max_snr;
    handles.snr = snr;
    guidata(hObject,handles);
end

data_str = get(handles.text_data_d,'string');
data = str2num(data_str);
handles.data_str = data_str;
guidata(hObject,handles);

allbloks = get(handles.popupmenu_blok,'string');
blok_str = allbloks{get(handles.popupmenu_blok,'value')};
blok = str2num(blok_str);
handles.blok_str = blok_str;
guidata(hObject,handles);

alg = get(handles.popupmenu_alg,'value');
handles.alg = alg;
guidata(hObject,handles);

allrates = get(handles.popupmenu_rate,'string');
rate = allrates{get(handles.popupmenu_rate,'value')};
handles.rate = rate;
guidata(hObject,handles);

allit = get(handles.popupmenu_iter,'string');
it = str2num(allit{get(handles.popupmenu_iter,'value')});

it_bool = get(handles.checkbox_iter,'value');

[Deber,berU] = CCSDS(data,blok,it,it_bool,snr,rate,alg);
handles.Deber = Deber;
handles.berU = berU;
guidata(hObject,handles);

set(handles.btn_ulozit,'enable','on');
set(handles.btn_zahodit,'enable','on');
set(handles.btn_simulovat,'enable','off');

function text_minsnr_d_Callback(hObje6ct, eventdata, handles)

set(handles.text_minsnr_d,'enable','inactive');

function text_minsnr_d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_minsnr_d (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 text_maxsnr_d_Callback(hObject, eventdata, handles)

set(handles.text_maxsnr_d,'enable','inactive');

function text_maxsnr_d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_maxsnr_d (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 text_kroksnr_d_Callback(hObject, eventdata, handles)

set(handles.text_kroksnr_d,'enable','inactive');

function text_kroksnr_d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_kroksnr_d (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 text_data_d_Callback(hObject, eventdata, handles)

set(handles.text_data_d,'enable','inactive');

% --- Executes during object creation, after setting all properties.
function text_data_d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text_data_d (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 popupmenu_blok.
function popupmenu_blok_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_blok (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 popupmenu_blok contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_blok


% --- Executes during object creation, after setting all properties.
function popupmenu_blok_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_blok (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 popupmenu_alg.
function popupmenu_alg_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_alg (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 popupmenu_alg contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_alg


% --- Executes during object creation, after setting all properties.
function popupmenu_alg_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_alg (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 popupmenu_iter.
function popupmenu_iter_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_iter (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 popupmenu_iter contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_iter


% --- Executes during object creation, after setting all properties.
function popupmenu_iter_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_iter (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 popupmenu_rate.
function popupmenu_rate_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_rate (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 popupmenu_rate contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_rate


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


% --- Executes on button press in btn_helpminsnr.
function btn_helpminsnr_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Urcuje minimalnu hodnotu Eb/N0 od ktorej zacne simulacia.\n\nODPORUCANA HODNOTA: 0'),'HELP');

% --- Executes on button press in btn_helpdata.
function btn_helpdata_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Pocet generovanych dat (bitov). Cim vyssia hodnota tym je simulacia presnejsia avsak vyrazne predlzuje cas simulacie.\n\nODPORUCANA HODNOTA: 1 000 000'),'HELP');

% --- Executes on button press in btn_helpblok.
function btn_helpblok_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Hodnota do ktorych su data rozdelene pri kodovani/dekodovani.'),'HELP');

% --- Executes on button press in btn_helpalg.
function btn_helpalg_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Algoritmus zvoleny pri dekodovani.'),'HELP');

% --- Executes on button press in btn_helprate.
function btn_helprate_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Oznacuje kodovy pomer kodera.'),'HELP');

% --- Executes on button press in btn_helpiterch.
function btn_helpiterch_Callback(hObject, eventdata, handles)

helpdlg(sprintf('V pripade zaskrtnutia sa zobrazia vysledky pre vsetky iteracie v druhom pripade iba posledna.'),'HELP');

% --- Executes on button press in btn_helpiter.
function btn_helpiter_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Urcuje pocet dekodovacich iteracii.'),'HELP');

% --- Executes on button press in btn_helpskoksnr.
function btn_helpskoksnr_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Urcuje krok v ktorych bude prebiehat simulacia medzi min a max Eb/N0.\n\nODPORUCANA HODNOTA: 0.5'),'HELP');

% --- Executes on button press in btn_helpmaxsnr.
function btn_helpmaxsnr_Callback(hObject, eventdata, handles)

helpdlg(sprintf('Urcuje maximalnu hodnotu Eb/N0 do ktorej bude simulacia prebiehat.\n\nODPORUCANA HODNOTA: 5'),'HELP');

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text_kroksnr_d.
function text_kroksnr_d_ButtonDownFcn(hObject, eventdata, handles)

if strcmp(get(handles.text_kroksnr_d,'enable'),'off')
else
    set(handles.text_kroksnr_d,'enable','on');
    set(handles.text_kroksnr_d,'string','');
end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text_maxsnr_d.
function text_maxsnr_d_ButtonDownFcn(hObject, eventdata, handles)

if strcmp(get(handles.text_maxsnr_d,'enable'),'off')
else
    set(handles.text_maxsnr_d,'enable','on');
    set(handles.text_maxsnr_d,'string','');
end

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text_minsnr_d.
function text_minsnr_d_ButtonDownFcn(hObject, eventdata, handles)

if strcmp(get(handles.text_minsnr_d,'enable'),'off')
else
    set(handles.text_minsnr_d,'enable','on');
    set(handles.text_minsnr_d,'string','');
end

% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)

addpath('..');
Application;
delete(hObject);


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text_data_d.
function text_data_d_ButtonDownFcn(hObject, eventdata, handles)

if strcmp(get(handles.text_data_d,'enable'),'off')
else
    set(handles.text_data_d,'enable','on');
    set(handles.text_data_d,'string','');
end

% --- Executes on button press in btn_ulozit.
function btn_ulozit_Callback(hObject, eventdata, handles)

[data,alg] = CCSDS_convert(1000,handles.alg);
str = sprintf('CCSDS_%s.txt',alg);
[fname, path] = uiputfile(str);
if fname == 0
    return;
else
    path_file = fullfile(path,fname);
    a = fopen(path_file,'wt');
    fprintf(a,'CCSDS\n%s\n%s\n%s\n%s\n%s\n%s\n%s',alg,mat2str(handles.snr),...
        handles.data_str,handles.blok_str,handles.rate,mat2str(handles.Deber),mat2str(handles.berU));
    fclose('all');
end


% --- Executes on button press in btn_zahodit.
function btn_zahodit_Callback(hObject, eventdata, handles)

set(handles.text_minsnr_d,'enable','inactive');
set(handles.text_maxsnr_d,'enable','inactive');
set(handles.text_kroksnr_d,'enable','inactive');
set(handles.text_data_d,'enable','inactive');
set(handles.popupmenu_blok,'enable','on');
set(handles.popupmenu_alg,'enable','on');
set(handles.popupmenu_rate,'enable','on');
set(handles.popupmenu_iter,'enable','on');
set(handles.btn_ulozit,'enable','off');
set(handles.btn_zahodit,'enable','off');
set(handles.btn_simulovat,'enable','on');
Deber = [];
