close all
figure(1)
ioaddr='0x240';
interval=0.000001;
datnum=1024;
channel=1;
mininterval=channel/100000;
if(interval < mininterval) interval=mininterval;end
data=adpcmcia(interval,datnum,channel,ioaddr,0);
pp=plot((1:datnum)*interval,data);
axis([0 datnum*interval -10 10]);
cgca=gca;
set(cgca,'position',[0.2 .12 .75 0.815]);
ui(1)=uicontrol('style','pushbutton','unit','normalized','position',[0 0.9 0.07 0.05],'string','start');
ui(2)=uicontrol('style','pushbutton','unit','normalized','position',[0.1 0.9 0.07 0.05],'string','stop');
ui(3)=uicontrol('style','checkbox','unit','normalized','position',[0.00 0.85 0.11 0.05],'string','oneshot');
ui(4)=uicontrol('style','checkbox','unit','normalized','position',[0.00 0.80 0.11 0.05],'string','spectrum');
ui(5)=uicontrol('style','text','unit','normalized','position',[0 0.76 0.08 0.03],'string','interval');
ui(6)=uicontrol('style','slider','unit','normalized','position',[0 0.73 0.17 0.03]);
ui(7)=uicontrol('style','edit','unit','normalized','position',[0 0.68 0.17 0.05]);
ui(8)=uicontrol('style','text','unit','normalized','position',[0 0.64 0.08 0.03],'string','datanum');
ui(9)=uicontrol('style','slider','unit','normalized','position',[0 0.61 0.17 0.03]);
ui(10)=uicontrol('style','edit','unit','normalized','position',[0 0.56 0.17 0.05]);
ui(11)=uicontrol('style','text','unit','normalized','position',[0 0.32-0.04 0.08 0.03],'string','channel');
ui(12)=uicontrol('style','popupmenu','unit','normalized','position',[0 0.32-0.09 0.17 0.05],'string',[{'0'},{'1'},{'2'},{'3'},{'4'},{'5'},{'6'},{'7'}]);
ui(13)=uicontrol('style','text','unit','normalized','position',[0 0.32-0.12 0.08 0.03],'string','I/O Addr');
ui(14)=uicontrol('style','text','unit','normalized','position',[0 0.32-0.17 0.17 0.05],'string',ioaddr);
ui(15)=uicontrol('style','text','unit','normalized','position',[0.01 0.32-0.22 0.12 0.05],'string','Gerox(c) 1997');
ui(16)=uicontrol('style','text','unit','normalized','position',[0 0.52 0.08 0.03],'string','gain');
ui(17)=uicontrol('style','slider','unit','normalized','position',[0 0.49 0.17 0.03]);
ui(18)=uicontrol('style','edit','unit','normalized','position',[0 0.44 0.17 0.05]);
ui(19)=uicontrol('style','radiobutton','unit','normalized','position',[0 0.4 0.08 0.03],'string','DC');
ui(20)=uicontrol('style','radiobutton','unit','normalized','position',[0.09 0.4 0.08 0.03],'string','AC');
%ui(20)=uicontrol('style','slider','unit','normalized','position',[0 0.44-0.07 0.17 0.03]);
%ui(21)=uicontrol('style','edit','unit','normalized','position',[0 0.44-.12 0.17 0.05]);

set(cgca,'drawmode','fast');
set(pp,'erasemode','xor');
estop = 0;
freqmode=0;
% set start button
set(ui(1),'callback',[...
      'estop=get(ui(3),''value'');while 1;',...
      'data = adpcmcia(interval,datnum,channel,ioaddr,0);',...
      'if(freqmode==1); ',...
      'data=abs(fft(data))/datnum*2;',...
      'for j=1:channel;',...
      'set(pp(j),''ydata'',data(1+ac_dc:round(datnum/2),j));',...
      'end;else ',...
      'for j=1:channel;',...
      'set(pp(j),''ydata'',(data(:,j)-ac_dc*mean(data(:,j))));',...
      'end;end;',...
      'if estop==1, break;end;',...
      'drawnow;',...
      'end']);
% set one shot checkbox
set(ui(3),'callback','estop = 1;');
% set stop button
set(ui(2),'callback','estop = 1;');
%  set spectrum mode
set(ui(4),'callback','adcdraw;');
% set interval ui(6) slider ui(7) edit
set(ui(6),'max',1048576/1e7);
set(ui(6),'min',mininterval);
set(ui(6),'value',interval);
set(ui(7),'string',num2str(interval));
set(ui(6),'callback','adcdraw;');
set(ui(7),'callback',[...
      'temp=str2num(get(ui(7),''string''));',...
      'if(temp < mininterval) temp=mininterval;end;',...
      'if(temp >= 1048576/1e7) temp = 1048576/1e7;end;',...
      'set(ui(6),''value'',temp);',...
      'adcdraw;']);
% set datanum ui(9),ui(10)
maxdatnum = round(16384/channel);
set(ui(9),'max',maxdatnum);
set(ui(9),'min',1);
set(ui(9),'value',datnum);
set(ui(10),'string',num2str(datnum));
set(ui(9),'callback','adcdraw;');
set(ui(10),'callback',[...
        'temp=round(str2num(get(ui(10),''string'')));',...
        'if(temp >= 1 & temp <= 16384/get(ui(12),''value'')) datnum=temp;end;',...
        'set(ui(9),''value'',datnum);',...
        'adcdraw;']);
% set channel ui(12)
set(ui(12),'value',channel);
set(ui(12),'callback','adcdraw');
  %set I/O addr ui(14)
  % set gain ui(17),ui(18)
  gain=1;
set(ui(17),'max',200);
set(ui(17),'min',1);
set(ui(17),'value',gain);
set(ui(18),'string',num2str(gain));
set(ui(17),'callback','adcdraw');
set(ui(18),'callback',[...
        'temp=str2num(get(ui(18),''string''));',...
        'if(temp >= 1 & temp <= 200) temp = round(temp);end;',...
        'set(ui(17),''value'',temp);',...
        'adcdraw;']);
  % set ac_dc ui(20),ui(21)
  ac_dc=0;
set(ui(19),'value',1);
set(ui(20),'value',0);
set(ui(19),'callback',[...
        'set(ui(19),''value'',1);',...
        'set(ui(20),''value'',0);',....
        'ac_dc=0;adcdraw;']);
  set(ui(20),'callback',[...
        'set(ui(20),''value'',1);',...
        'set(ui(19),''value'',0);',...
        'ac_dc=1;adcdraw;']);
