function varargout = timer_explorer(varargin)
% TIMER_EXPLORER MATLAB code 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 handle 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
% Copyright 2007 - 2010 The MathWorks, Inc.
% Begin initialization code - DO NOT EDIT
gui_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{:});
else
gui_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_explorer
handles.output = hObject;
handles.myradio = 'random';
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes timer_explorer wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% set up the state of the relevant user elements
set_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 structure
varargout{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 first
if 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 later
handles.AI = AI;
% start the input, but don't trigger
start(AI)
end
% attempt to create a timer object
timerstr = handles.timercommand;
eval(timerstr); % creates timer object, t
t.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 boxes
set(handles.popup_execmode,'enable','off');
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 boxes
set(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 buttons
set(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 object
t = 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 controls
set(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 boxes
set(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 buttons
set(handles.button_timercreate,'enable','on');
set(handles.button_timerstart,'enable','off');
set(handles.button_timerstop,'enable','off');
set(handles.button_timerremove,'enable','off');
% --- 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)
tic
try
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 elements
set_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)
% TODO: check that this is a valid value
% update other user elements
set_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 elements
set_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,'defaultUicontrolBackgroundColor'))
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
% --- 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 function
function radio_Callback(hObject, eventdata, handles)
% Work out which radio button is lit, then call to update GUI
theTag = get(hObject,'tag');
switch theTag
case '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';
end
set_elements(hObject, eventdata, handles)
guidata(hObject,handles)
% --- Function to set the display elements, based on the user input
function set_elements(hObject, eventdata, handles)
% get the relevant strings for the timer mode etc. from the user controls
strs = 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 string
t_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)];
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-box
switch 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 timer 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');
otherwise
mystring = 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 again
guidata(hObject,handles)
% --- Timer Callback Function
function MyTimerFcn(obj, event)
persistent d
persistent 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 timer
if ~exist('d')
d=[];
end
if exist('needreset')
if needreset==1
d=[];
needreset=0;
end
end
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 x
d = [d x];
plot(handles.axes1,d)
drawnow
case 'plotacq'
trigger(handles.AI);
acq = getdata(handles.AI,800);
plot(handles.axes1,acq);
drawnow
needreset=1;
end
% --- Timer Stop Function for automatic callback...
function MyTimerStopFcn(obj, event)
% print up some information
interval = 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))