timer_explorer
-
Upload
tan-yu-ang -
Category
Documents
-
view
217 -
download
0
Transcript of timer_explorer
-
8/7/2019 timer_explorer
1/8
function varargout = timer_explorer(varargin)% TIMER_EXPLORER M-file for timer_explorer.fig% TIMER_EXPLORER, by itself, creates a new TIMER_EXPLORER or raises the existing% singleton*.%% H = TIMER_EXPLORER returns the handle to a new TIMER_EXPLORER or the hand
le to% the existing singleton*.%% TIMER_EXPLORER('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in TIMER_EXPLORER.M with the given input arguments.%% TIMER_EXPLORER('Property','Value',...) creates a new TIMER_EXPLORER or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before timer_explorer_OpeningFunction gets called. An% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to timer_explorer_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 timer_explorer
% Last Modified by GUIDE v2.5 08-Aug-2005 10:12:02
% Begin initialization code - DO NOT EDITgui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @timer_explorer_OpeningFcn, ...'gui_OutputFcn', @timer_explorer_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{:});
elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT
% --- Executes just before timer_explorer is made visible.function timer_explorer_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for timer_explorerhandles.output = hObject;handles.myradio = 'random';
-
8/7/2019 timer_explorer
2/8
% Update handles structureguidata(hObject, handles);
% UIWAIT makes timer_explorer wait for user response (see UIRESUME)% uiwait(handles.figure1);
% set up the state of the relevant user elementsset_elements(hObject, eventdata, handles)plot(handles.axes1,0,0)
% --- Outputs from this function are returned to the command line.function varargout = timer_explorer_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structurevarargout{1} = handles.output;
% --- Executes on button press in button_timercreate.function button_timercreate_Callback(hObject, eventdata, handles)
try% if we are going to do DAQ input, try and create an analogue input
% object firstif strcmp(handles.myradio,'plotacq')
AI = analoginput('winsound');AI.SampleRate = 8000;AI.SamplesPerTrigger = 800;AI.TriggerType = 'Manual';AI.TriggerRepeat = Inf;chan = addchannel(AI,1);
% store in handles, for use laterhandles.AI = AI;
% start the input, but don't triggerstart(AI)
end
% attempt to create a timer objecttimerstr = handles.timercommand;eval(timerstr); % creates timer object, tt.userdata = handles;t.StopFcn = @MyTimerStopFcn;
set(handles.txt_timername,'String',t.name)handles.mytimer = t;guidata(hObject,handles)
% if the timer is created OK, then disable the settings boxesset(handles.popup_execmode,'enable','off');
-
8/7/2019 timer_explorer
3/8
set(handles.popup_busymode,'enable','off');set(handles.edit_startdelay,'enable','off');set(handles.edit_period,'enable','off');set(handles.edit_tasks,'enable','off');
% disable radio boxesset(handles.radio_nothing,'enable','off');
set(handles.radio_beep,'enable','off');set(handles.radio_random,'enable','off');set(handles.radio_plotacq,'enable','off');set(handles.radio_plotworkspace,'enable','off');set(handles.radio_message,'enable','off');
% and dis/enable the other timer buttonsset(handles.button_timercreate,'enable','off');set(handles.button_timerstart,'enable','on');set(handles.button_timerstop,'enable','on');set(handles.button_timerremove,'enable','on');
end
% --- Executes on button press in button_timerremove.function button_timerremove_Callback(hObject, eventdata, handles)
% remove timer objectt = handles.mytimer;delete(t)
set(handles.txt_timername,'String','none')
% get rid of the AI object if its been used.if strcmp(handles.myradio,'plotacq')
stop(handles.AI)delete(handles.AI)
end
% re-enable the user controlsset(handles.popup_execmode,'enable','on');set(handles.popup_busymode,'enable','on');set(handles.edit_startdelay,'enable','on');set(handles.edit_period,'enable','on');set(handles.edit_tasks,'enable','on');
% disable radio boxesset(handles.radio_nothing,'enable','on');set(handles.radio_beep,'enable','on');set(handles.radio_random,'enable','on');set(handles.radio_plotacq,'enable','on');set(handles.radio_plotworkspace,'enable','on');set(handles.radio_message,'enable','on');
% and dis/enable the other timer buttonsset(handles.button_timercreate,'enable','on');
set(handles.button_timerstart,'enable','off');set(handles.button_timerstop,'enable','off');set(handles.button_timerremove,'enable','off');
-
8/7/2019 timer_explorer
4/8
% --- Executes on button press in button_timerstart.
function button_timerstart_Callback(hObject, eventdata, handles)
set(handles.txt_timercalls,'string','0')t = handles.mytimer;set(handles.txt_starttime,'string',datestr(now))set(handles.txt_finishtime,'string','')set(handles.txt_duration,'string','')guidata(hObject,handles)tictry
start(t)end
% --- Executes on button press in button_timerstop.function button_timerstop_Callback(hObject, eventdata, handles)
t = handles.mytimer;if strcmp(t.running,'on')
stop(t)MyTimerStopFcn(handles.mytimer,[])
end
function edit_period_Callback(hObject, eventdata, handles)
% TODO: check that this is a valid value
% update other user elementsset_elements(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.function edit_period_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');end
function edit_startdelay_Callback(hObject, eventdata, handles)
-
8/7/2019 timer_explorer
5/8
% TODO: check that this is a valid value
% update other user elementsset_elements(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.function edit_startdelay_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');end
function edit_tasks_Callback(hObject, eventdata, handles)
% TODO: check that this is a valid value
% update other user elementsset_elements(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.function edit_tasks_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgr
oundColor'))set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.function popup_execmode_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');end
% --- Executes during object creation, after setting all properties.function txt_callback_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');end
-
8/7/2019 timer_explorer
6/8
% --- Executes during object creation, after setting all properties.function popup_busymode_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');end
% --- Callback for all the radio buttons which choose the timer functionfunction radio_Callback(hObject, eventdata, handles)
% Work out which radio button is lit, then call to update GUI
theTag = get(hObject,'tag');switch theTagcase 'radio_random'
handles.myradio = 'random';case 'radio_message'
handles.myradio = 'message';case 'radio_plotacq'
handles.myradio = 'plotacq';case 'radio_plotworkspace'
handles.myradio = 'plotworkspace';case 'radio_nothing'
handles.myradio = 'nothing';case 'radio_beep'
handles.myradio = 'beep';endset_elements(hObject, eventdata, handles)guidata(hObject,handles)
% --- Function to set the display elements, based on the user inputfunction set_elements(hObject, eventdata, handles)
% get the relevant strings for the timer mode etc. from the user controlsstrs = get(handles.popup_execmode,'String');execmode_str = strs{get(handles.popup_execmode,'Value')};strs = get(handles.popup_busymode,'String');busymode_str = strs{get(handles.popup_busymode,'Value')};startdelay = get(handles.edit_startdelay,'string');period = get(handles.edit_period,'string');tasks = get(handles.edit_tasks,'string');
% set the timer creation stringt_string = ['t = timer(' sprintf('\''TimerFcn\'', @MyTimerFcn, ')];t_string = [t_string sprintf('\''StartDelay\'', %s, ',startdelay)];
t_string = [t_string sprintf('\''Period\'', %s, ',period)];t_string = [t_string sprintf('\''TasksToExecute\'', %s, ',tasks)];t_string = [t_string sprintf('\''ExecutionMode\'', \''%s\'', ',execmode_str)];
-
8/7/2019 timer_explorer
7/8
t_string = [t_string sprintf('\''BusyMode\'', \''%s\'' );',busymode_str)];set(handles.text_timerfunc,'string',['>> ' t_string])handles.timercommand = t_string;
% set the callback function edit-boxswitch handles.myradio
case 'nothing'
mystring = sprintf('%% do nothing callback code');case 'beep'
mystring = sprintf('%% beep only callback code\nbeep');case 'random'
mystring = sprintf('%% plot random numbers callback code\ndata = [data rand(1)]\nplot(hAx,data)\ndrawnow');
case 'message'mystring = sprintf('%% print message callback code\ndisp (\''Executing t
imer callback...\'')');case 'plotworkspace'
mystring = sprintf('%% plot variable x from main workspace\n%% try: \"global x, x=1\" from the cmd window while running\nglobal x\ndata = [data x]\nplot
(hAx,data)\ndrawnow');otherwisemystring = sprintf('%% wait for 0.1s data from soundcard\n%% (AI is DAQ
toolbox object, created earlier)\ntrigger(AI)\nacq = getdata(AI,800);\nplot(hAx,data)\ndrawnow');end
mystring = [sprintf('function MyTimerFcn(obj, event)\n\n') mystring];set(handles.txt_callback,'string',mystring)
% store the modified handles data back againguidata(hObject,handles)
% --- Timer Callback Functionfunction MyTimerFcn(obj, event)
persistent dpersistent needreset
% increment the number of counts...handles = obj.Userdata;
if ~strcmp(handles.myradio,'nothing')set(handles.txt_timercalls,'string',num2str(obj.TasksExecuted))
end
% do whatever we are supposed to do in the timerif ~exist('d')
d=[];endif exist('needreset')
if needreset==1d=[];
needreset=0;end
end
-
8/7/2019 timer_explorer
8/8
switch handles.myradio
case 'random'd = [d rand(1)];plot(handles.axes1,d)drawnow
case 'beep'beep
case 'message'disp ('Executing timer callback...');
case 'plotworkspace'global xd = [d x];plot(handles.axes1,d)drawnow
case 'plotacq'trigger(handles.AI);acq = getdata(handles.AI,800);
plot(handles.axes1,acq);drawnowneedreset=1;
end
% --- Timer Stop Function for automatic callback...function MyTimerStopFcn(obj, event)
% print up some informationinterval = toc;handles = obj.Userdata;set(handles.txt_finishtime,'string',datestr(now))set(handles.txt_duration,'string',num2str(interval))set(handles.txt_timercalls,'string',num2str(obj.TasksExecuted))