% Stimulator GUI %clean up any leftover serial connections clear all fclose(instrfind) %open a window figure('position',[700 550 280 128],... 'name','Stimulator Control',... 'numbertitle','off') clf %open a serial connection s = serial('COM2',... 'baudrate',9600); fopen(s) %define the quit button x=200; y=0; w=50; h=20; exit=0; quitbutton=uicontrol('style','pushbutton',... 'string','Quit', ... 'fontsize',12, ... 'position',[x,y,w,h], ... 'tag','stimcntl',... 'callback','exit=1;close;'); %define the amplitude editable text field x=10; y=100; w=50; h=20; ampnow=0; ampCtl=uicontrol('style','edit',... 'string','100', ... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x,y,w,h],... 'callback',[... 'v=str2num(get(ampCtl,''string''));'... 'if (v<1);set(ampCtl,''string'',num2str(1,''%6.1f''));end;'... 'if (v>100);set(ampCtl,''string'',num2str(100,''%6.1f''));end;'... 'ampnow=1;'... ]); uicontrol('style','text',... 'string','Amplitude (%)',... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x+w,y,125,h]); %define the duration editable text field x=10; y=75; w=50; h=20; durCtl=uicontrol('style','edit',... 'string','1', ... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x,y,w,h],... 'callback',[... 'v=str2num(get(durCtl,''string''));'... 'if (v<.1);set(durCtl,''string'',num2str(.1,''%6.1f''));end;'... 'if (v>4000);set(durCtl,''string'',num2str(4000,''%6.1f''));end;'... ]); uicontrol('style','text',... 'string','Duration (mSec)',... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x+w,y,125,h]); %define the delay editable text field x=10; y=50; w=50; h=20; delayCtl=uicontrol('style','edit',... 'string','1', ... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x,y,w,h],... 'callback',[... 'v=str2num(get(delayCtl,''string''));'... 'if (v<.1);set(delayCtl,''string'',num2str(.1,''%6.1f''));end;'... 'if (v>4000);set(delayCtl,''string'',num2str(4000,''%6.1f''));end;'... ]); uicontrol('style','text',... 'string','Delay (mSec)',... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x+w,y,125,h]); %define the repeat time editable text field x=10; y=25; w=50; h=20; repCtl=uicontrol('style','edit',... 'string','10', ... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x,y,w,h],... 'callback',[... 'v=str2num(get(repCtl,''string''));'... 'if (v<.1);set(repCtl,''string'',num2str(.1,''%6.1f''));end;'... 'if (v>4000);set(repCtl,''string'',num2str(4000,''%6.1f''));end;'... ]); uicontrol('style','text',... 'string','RepeatTime (mSec)',... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x+w,y,125,h]); %define the number of pulses editable text field x=10; y=0; w=50; h=20; numCtl=uicontrol('style','edit',... 'string','2', ... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x,y,w,h],... 'callback',[... 'v=str2num(get(numCtl,''string''));'... 'if (v<1);set(numCtl,''string'',num2str(1,''%6.1f''));end;'... 'if (v>1000);set(numCtl,''string'',num2str(1000,''%6.1f''));end;'... ]); uicontrol('style','text',... 'string','Number of pulses',... 'fontsize',12, ... 'tag','stimcntl',... 'position',[x+w,y,125,h]); %define the manual/repeat radiobuttons x=200; y=50; w=75; h=20; radionow=0; mode='m'; mode1button=uicontrol('style','radiobutton',... 'string','Manual', ... 'fontsize',12, ... 'value',1,... 'position',[x,y,w,h], ... 'tag','stimcntl',... 'callback','radionow=1;'); mode2button=uicontrol('style','radiobutton',... 'string','Repeat', ... 'fontsize',12, ... 'position',[x,y-25,w,h], ... 'tag','stimcntl',... 'callback','radionow=1;'); %define the start train button x=200; y=100; w=50; h=20; gonow=0; quitbutton=uicontrol('style','pushbutton',... 'string','Start', ... 'fontsize',12, ... 'position',[x,y,w,h], ... 'tag','stimcntl',... 'callback','gonow=1;'); %define the stop train button x=200; y=75; w=50; h=20; stopnow=0; quitbutton=uicontrol('style','pushbutton',... 'string','Stop', ... 'fontsize',12, ... 'position',[x,y,w,h], ... 'callback','stopnow=1;'); %Now start handling events while (exit==0) %If the start button was pushed if(gonow) gonow=0; dur=num2str(fix(10*str2num(get(durCtl,'string')))); amp=num2str(fix(1.28*str2num(get(ampCtl,'string')))); delay=num2str(fix(10*str2num(get(delayCtl,'string')))); rep=num2str(fix(10*str2num(get(repCtl,'string')))); num=num2str(fix(str2num(get(numCtl,'string')))); fprintf(s,['m' mode]) fprintf(s,['p ' num ' ' amp ' ' dur ' ' delay ' ' rep]) fprintf(s,'g') set(findobj('tag','stimcntl'),'enable','off') end %If the amplitude control was changed if(ampnow) ampnow=0; dur=num2str(fix(10*str2num(get(durCtl,'string')))); amp=num2str(fix(1.28*str2num(get(ampCtl,'string')))); delay=num2str(fix(10*str2num(get(delayCtl,'string')))); rep=num2str(fix(10*str2num(get(repCtl,'string')))); num=num2str(fix(str2num(get(numCtl,'string')))); fprintf(s,['p ' num ' ' amp ' ' dur ' ' delay ' ' rep]) end %If the stop button was pushed if(stopnow) stopnow=0; fprintf(s,'s') %sends the stop command fprintf(s,'mr') %cancels the pushbutton set(findobj('tag','stimcntl'),'enable','on') end %If either of the radio buttons was pushed if(radionow) radionow=0; if (gco==mode1button) set(mode2button,'value',0) mode='m'; else set(mode1button,'value',0) mode='r'; end end drawnow %force a window redrawx end %close the serial port fclose(s)