ButtonBox: Difference between revisions
Jump to navigation
Jump to search
Wiki-admin (talk | contribs) No edit summary |
Wiki-admin (talk | contribs) No edit summary |
||
| Line 339: | Line 339: | ||
% | % | ||
% to run: (receiving incoming data, check code for own purposes) | % to run: (receiving incoming data, check code for own purposes) | ||
% buttonbox('run' | % buttonbox('run'); | ||
% | % | ||
% or | % or | ||
| Line 353: | Line 353: | ||
% | % | ||
% to close the connection: | % to close the connection: | ||
% buttonbox('close' | % buttonbox('close'); | ||
persistent old_hdl % keep handle to COM object persistent | |||
% set defaults | % set defaults | ||
bb.Device = 'COM2'; | bb.Device = 'COM2'; | ||
| Line 362: | Line 361: | ||
bb.DataBits = 8; | bb.DataBits = 8; | ||
bb.StopBits = 1; | bb.StopBits = 1; | ||
bb.Parity = 'none'; | bb.Parity = 'none'; | ||
if nargin < 1 | if nargin < 1 | ||
cmd = 'open'; | cmd = 'open'; | ||
| Line 379: | Line 377: | ||
end | end | ||
if ~any(strcmp(cmd,{'open','close'})) | |||
if isempty(old_hdl) | |||
help serial_buttonbox_common | |||
error('Buttonbox not yet initialized'); | |||
end | |||
handle = old_hdl; | |||
end | |||
switch cmd | switch cmd | ||
case 'marker' | case 'marker' | ||
fwrite(handle, uint8(marker));%IOPort('Write', handle, uint8(marker), 1); % last argument: blocking | |||
WaitSecs(0.002); | |||
fwrite(handle, uint8(0));%IOPort('Write', handle, uint8(0), 0); % last argument: blocking | |||
IOPort('Write', handle, uint8(0), 0); % last argument: blocking | |||
return | return | ||
case 'clear' | case 'clear' | ||
while(handle.BytesAvailable) | |||
fread(handle, 1); | |||
end | |||
end | |||
ret = []; % meaningless | ret = []; % meaningless | ||
return | return | ||
| Line 409: | Line 403: | ||
return | return | ||
case 'close' | case 'close' | ||
if nargin > | if nargin > 1 | ||
handle = varargin{1}; | handle = varargin{1}; | ||
else | else | ||
| Line 415: | Line 409: | ||
end | end | ||
fclose(handle); | fclose(handle); | ||
ret = | delete(handle); | ||
ret = []; | |||
return | return | ||
case 'run' | case 'run' | ||
% read incoming data | % read incoming data | ||
% code proceeds below .... | % code proceeds below .... | ||
case 'wait_keypress' | case 'wait_keypress' | ||
% start polling for characters (indicating start of scan) | |||
while(1) | |||
data = []; | |||
while handle.BytesAvailable | |||
navailable = handle.BytesAvailable; | |||
% read incoming data | |||
[newdata, cnt] = fread(handle, navailable); | |||
% concatenate possible new data | |||
if cnt | |||
data = [data newdata(:)]; | |||
end | end | ||
end | |||
if ~isempty(data) | |||
ret = data; | ret = data; | ||
return | return | ||
end | end | ||
end | end | ||
otherwise | otherwise | ||
fprintf('Unknown option %s\n',cmd); | fprintf('Unknown option %s\n',cmd); | ||
ret = []; | ret = []; | ||
return | return | ||
end | |||
end | |||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
% only gets here when cmd = 'run' % | % only gets here when cmd = 'run' % | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
% Initialize output figure | % Initialize output figure | ||
win = list_output(' ',[]); | win = list_output(' ',[]); | ||
| Line 491: | Line 470: | ||
end | end | ||
pause(0.01); | pause(0.01); | ||
end %while 1 | end %while 1 | ||
function hdl = open_buttonbox(device) | function hdl = open_buttonbox(device) | ||
% open handle to serial device (mini buttonbox) | % open handle to serial device (mini buttonbox) WaitSecs(0.002); % just to load mex-file into memory | ||
try | try | ||
hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity); | hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity); | ||
| Line 506: | Line 487: | ||
fopen(hdl); | fopen(hdl); | ||
end | end | ||
old_hdl = hdl; | old_hdl = hdl; | ||
fprintf('Wait for device buttonbox....\n'); | fprintf('Wait for device buttonbox....\n'); | ||
tic | tic | ||
| Line 515: | Line 495: | ||
fread(hdl, navailable); | fread(hdl, navailable); | ||
end | end | ||
pause(0.5); | pause(0.5); | ||
end | |||
end | |||
function win = list_output(line,win) | function win = list_output(line,win) | ||
| Line 532: | Line 502: | ||
persistent lines | persistent lines | ||
persistent edt | persistent edt | ||
Maxlines = 40; | Maxlines = 40; | ||
if isempty(win) | if isempty(win) | ||
% initialize listbox output figure | % initialize listbox output figure | ||
| Line 540: | Line 509: | ||
ptr=Maxlines; | ptr=Maxlines; | ||
lines(ptr) = {'Buttonbox output:'}; | lines(ptr) = {'Buttonbox output:'}; | ||
idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1; | idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1; | ||
win = figure(); | win = figure(); | ||
% initialize figure to hold output text | % initialize figure to hold output text | ||
| Line 555: | Line 523: | ||
set(edt,'String',lines(idxs),'Value',Maxlines); | set(edt,'String',lines(idxs),'Value',Maxlines); | ||
drawnow; | drawnow; | ||
end | end | ||
end | |||
end</syntaxhighlight> | end</syntaxhighlight> | ||