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

% Last Modified by GUIDE v2.5 04-Sep-2013 19:18:22

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

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

set(handles.Init, 'ForegroundColor', 'r');
set(handles.Zero_ch_1, 'Enable', 'Off');
set(handles.Zero_ch_2, 'Enable', 'Off');
set(handles.Apply_V_ch1, 'Enable', 'Off');
set(handles.Apply_V_ch2, 'Enable', 'Off');

set(handles.ch_2_V_mag_slider, 'Min', 0);
set(handles.ch_2_V_mag_slider, 'Max', 20);
set(handles.ch_2_V_mag_slider, 'Value', 0);
set(handles.ch_2_V_mag_slider, 'SliderStep', [.1/20, 1/20]);

disp_str = [num2str(round(100*get(handles.ch_2_V_mag_slider, 'Value'))/100), ' V'];
set(handles.ch_2_V_mag, 'String', disp_str);

set(handles.ch_1_V_mag_slider, 'Min', 0);
set(handles.ch_1_V_mag_slider, 'Max', 20);
set(handles.ch_1_V_mag_slider, 'Value', 0);
set(handles.ch_1_V_mag_slider, 'SliderStep', [.1/20, 1/20]);

disp_str = [num2str(round(100*get(handles.ch_1_V_mag_slider, 'Value'))/100), ' V'];
set(handles.ch_1_V_mag, 'String', disp_str);

set(handles.slider_dur_ch1, 'Min', 10);
set(handles.slider_dur_ch1, 'Max', 60000);
set(handles.slider_dur_ch1, 'Value', 1000);
set(handles.slider_dur_ch1, 'SliderStep', [100/(60000-1), 1000/(60000-1)]);

disp_str = [num2str(round(get(handles.slider_dur_ch1, 'Value'))), ' ms'];
set(handles.t_dur_ch1, 'String', disp_str);

set(handles.slider_dur_ch2, 'Min', 10);
set(handles.slider_dur_ch2, 'Max', 60000);
set(handles.slider_dur_ch2, 'Value', 1000);
set(handles.slider_dur_ch2, 'SliderStep', [100/(60000-1), 1000/(60000-1)]);

disp_str = [num2str(round(get(handles.slider_dur_ch2, 'Value'))), ' ms'];
set(handles.t_dur_ch2, 'String', disp_str);

set(handles.slider_dur_ch2_off, 'Min', 10);
set(handles.slider_dur_ch2_off, 'Max', 60000);
set(handles.slider_dur_ch2_off, 'Value', 1000);
set(handles.slider_dur_ch2_off, 'SliderStep', [100/(60000-1), 1000/(60000-1)]);

disp_str = [num2str(round(get(handles.slider_dur_ch2_off, 'Value'))), ' ms'];
set(handles.t_dur_ch2_off, 'String', disp_str);

set(handles.slider_pulse_num_ch2, 'Min', 1);
set(handles.slider_pulse_num_ch2, 'Max', 20);
set(handles.slider_pulse_num_ch2, 'Value', 1);
set(handles.slider_pulse_num_ch2, 'SliderStep', [1/19, 3/19]);

disp_str = num2str(round(get(handles.slider_pulse_num_ch2, 'Value')));
set(handles.pulse_num_ch2, 'String', disp_str);

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Magnet_GUI_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 Zero_ch_2.
function Zero_ch_2_Callback(hObject, eventdata, handles)
% hObject    handle to Zero_ch_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

stop(handles.A1);
putdata(handles.A1, 0); start(handles.A1);stop(handles.A1);

guidata(hObject, handles);

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

set(handles.Init, 'ForegroundColor', 'g');
set(handles.Zero_ch_1, 'Enable', 'On');
set(handles.Zero_ch_2, 'Enable', 'On');
set(handles.Apply_V_ch1, 'Enable', 'On');
set(handles.Apply_V_ch2, 'Enable', 'On');

handles.A0 = analogoutput('mcc');
handles.A1 = analogoutput('mcc');

addchannel(handles.A0, 0);
addchannel(handles.A1, 1);

guidata(hObject, handles);


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

T_duration = round(get(handles.slider_dur_ch2, 'Value'));
V_out_mag = get(handles.ch_2_V_mag_slider, 'Value');

T = linspace(0, 4, T_duration)';

V_out = 0.0958*V_out_mag*ones(size(T));
%V_out = 0.0958*V_out_mag*sin(2*T).^2;

stop(handles.A1);
putdata(handles.A1, V_out);
start(handles.A1);
wait(handles.A1, 100);

% Zero the Voltage
putdata(handles.A1, 0);
start(handles.A1);

guidata(hObject, handles);

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

T_duration = round(get(handles.slider_dur_ch1, 'Value'));
V_out_mag = get(handles.ch_1_V_mag_slider, 'Value');

T = linspace(0, 4, T_duration)';

V_out = 0.0958*V_out_mag*ones(size(T));
%V_out = 0.0958*V_out_mag*sin(2*T).^2;

stop(handles.A0);
putdata(handles.A0, V_out);
start(handles.A0);
wait(handles.A0, 100);

% Zero the Voltage
putdata(handles.A0, 0);
start(handles.A0);

guidata(hObject, handles);

% --- Executes on slider movement.
function ch_2_V_mag_slider_Callback(hObject, eventdata, handles)
% hObject    handle to ch_2_V_mag_slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

disp_str = [num2str(round(100*get(handles.ch_2_V_mag_slider, 'Value'))/100), ' V'];
set(handles.ch_2_V_mag, 'String', disp_str);

guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


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

stop(handles.A0);
putdata(handles.A0, 0); start(handles.A0);stop(handles.A0);

guidata(hObject, handles);



% --- Executes on slider movement.
function ch_1_V_mag_slider_Callback(hObject, eventdata, handles)
% hObject    handle to ch_1_V_mag_slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

disp_str = [num2str(round(100*get(handles.ch_1_V_mag_slider, 'Value'))/100), ' V'];
set(handles.ch_1_V_mag, 'String', disp_str);

guidata(hObject, handles);

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_dur_ch2_Callback(hObject, eventdata, handles)
% hObject    handle to slider_dur_ch2 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
disp_str = [num2str(round(get(handles.slider_dur_ch2, 'Value')/100)*100), ' ms'];
set(handles.t_dur_ch2, 'String', disp_str);
set(handles.slider_dur_ch2, 'Value', round(get(handles.slider_dur_ch2, 'Value'))); 

guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_dur_ch1_Callback(hObject, eventdata, handles)
% hObject    handle to slider_dur_ch1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

disp_str = [num2str(round(get(handles.slider_dur_ch1, 'Value')/100)*100), ' ms'];
set(handles.t_dur_ch1, 'String', disp_str);
set(handles.slider_dur_ch1, 'Value', round(get(handles.slider_dur_ch1, 'Value'))); 

guidata(hObject, handles);


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_dur_ch2_off_Callback(hObject, eventdata, handles)
% hObject    handle to slider_dur_ch2_off (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

disp_str = [num2str(round(get(handles.slider_dur_ch2_off, 'Value')/100)*100), ' ms'];
set(handles.t_dur_ch2_off, 'String', disp_str);
set(handles.slider_dur_ch2_off, 'Value', round(get(handles.slider_dur_ch2_off, 'Value'))); 

guidata(hObject, handles);

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_pulse_num_ch2_Callback(hObject, eventdata, handles)
% hObject    handle to slider_pulse_num_ch2 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

disp_str = num2str(round(get(handles.slider_pulse_num_ch2, 'Value')));
set(handles.pulse_num_ch2, 'String', disp_str);
set(handles.slider_pulse_num_ch2, 'Value', round(get(handles.slider_pulse_num_ch2, 'Value'))); 

guidata(hObject, handles);

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


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

T_dur_on = round(get(handles.slider_dur_ch2, 'Value'));
T_dur_off = round(get(handles.slider_dur_ch2_off, 'Value'));
P_num = round(get(handles.slider_pulse_num_ch2, 'Value'));
V_out_mag = get(handles.ch_2_V_mag_slider, 'Value');


V_out_tmp = 0.0958*V_out_mag*ones(T_dur_on + T_dur_off, 1);
V_out_tmp((T_dur_on+1):end) = 0;
%V_out = 0.0958*V_out_mag*sin(2*T).^2;

%{
V_out = repmat(V_out_tmp, [P_num, 1]);
stop(handles.A1);
putdata(handles.A1, V_out);
start(handles.A1);
wait(handles.A1, length(V_out)+50);
%}

for i=1:P_num,
    stop(handles.A1);
    putdata(handles.A1, V_out_tmp);
    start(handles.A1);
    wait(handles.A1, length(V_out_tmp)+50);
    disp(['Done with pulse number ', num2str(i)]);
end
% Zero the Voltage
putdata(handles.A1, 0);
start(handles.A1);

guidata(hObject, handles);
