Line 328: |
Line 328: |
| | | |
| <syntaxhighlight lang="matlab" line style="height:50em; overflow:auto;"> | | <syntaxhighlight lang="matlab" line style="height:50em; overflow:auto;"> |
− | function handle = buttonbox(cmd,varargin) | + | function ret = buttonbox(cmd,varargin) |
− | % to initialize connection: | + | % to initialize connection: (omit 2nd argument if defaults apply) |
| % define settings as structure with fields: | | % define settings as structure with fields: |
| % bb.Device = 'COM2'; | | % bb.Device = 'COM2'; |
Line 338: |
Line 338: |
| % handle = buttonbox('open',bb) | | % handle = buttonbox('open',bb) |
| % | | % |
− | % to run: | + | % to run: (receiving incoming data, check code for own purposes) |
| % buttonbox('run',handle); | | % buttonbox('run',handle); |
| + | % |
| + | % or |
| + | % |
| + | % to send a marker: (marker: a numeric value) |
| + | % buttonbox(marker) |
| + | % |
| + | % or |
| + | % |
| + | % to wait for a buttonpress: |
| + | % key = buttonbox('wait_keypress') |
| % | | % |
| % to close the connection: | | % to close the connection: |
Line 354: |
Line 364: |
| | | |
| if nargin < 1 | | if nargin < 1 |
− | cmd = 'open';
| + | cmd = 'open'; |
| end | | end |
| if nargin > 1 | | if nargin > 1 |
− | % user overwrites default settings
| + | % user overwrites default settings |
− | flds = fields(varargin{1});
| + | flds = fields(varargin{1}); |
− | for n = 1 : numel(flds)
| + | for n = 1 : numel(flds) |
− | bb.(flds{n}) = varargin{1}.(flds{n});
| + | bb.(flds{n}) = varargin{1}.(flds{n}); |
− | end
| + | end |
| + | end |
| + | if nargin==1 && isnumeric(cmd) |
| + | marker = cmd; |
| + | cmd = 'marker'; |
| end | | end |
| | | |
| | | |
| switch cmd | | switch cmd |
− | case 'open'
| + | case 'marker' |
− | % get handle to serial device
| + | if isempty(old_hdl) |
− | handle = open_buttonbox(bb);
| + | help serial_buttonbox_common |
− | return
| + | error('Buttonbox not yet initialized'); |
− | case 'close'
| + | end |
− | if nargin > 0
| + | handle = old_hdl; |
− | handle = varargin{1};
| + | ret = IOPort('Write', handle, uint8(marker), 1); % last argument: blocking |
− | else
| + | java.lang.Thread.sleep(10); |
− | handle = old_hdl;
| + | IOPort('Write', handle, uint8(0), 0); % last argument: blocking |
− | end
| + | if ret < 1 |
− | fclose(handle);
| + | disp('Marker might not have been written to button box, please verify setup....?'); |
− | return
| + | end |
− | case 'run'
| + | return |
− | % read incoming data
| + | case 'open' |
− | if isempty(old_hdl)
| + | % get handle to serial device |
− | help serial_buttonbox_common
| + | handle = open_buttonbox(bb); |
− | error('Buttonbox not yet initialized');
| + | ret = handle; |
− | end
| + | return |
− | handle = old_hdl;
| + | case 'close' |
− | otherwise
| + | if nargin > 0 |
− | fprintf('Unknown option %s\n',cmd);
| + | handle = varargin{1}; |
− | return
| + | else |
| + | handle = old_hdl; |
| + | end |
| + | fclose(handle); |
| + | ret = handle; |
| + | return |
| + | case 'run' |
| + | % read incoming data |
| + | if isempty(old_hdl) |
| + | help serial_buttonbox_common |
| + | error('Buttonbox not yet initialized'); |
| + | end |
| + | handle = old_hdl; |
| + | % code proceeds below .... |
| + | case 'wait_keypress' |
| + | % read incoming data |
| + | if isempty(old_hdl) |
| + | help serial_buttonbox_common |
| + | error('Buttonbox not yet initialized'); |
| + | end |
| + | handle = old_hdl; |
| + | while 1 |
| + | % start polling for characters (indicating start of scan) |
| + | navailable = handle.BytesAvailable; |
| + | if navailable |
| + | data = []; |
| + | while navailable |
| + | % read incoming data |
| + | [newdata, cnt] = fread(handle, navailable); |
| + | % concatenate possible new data |
| + | if cnt |
| + | data = [data newdata(:)]; |
| + | end |
| + | % check if any more data left |
| + | navailable = handle.BytesAvailable; |
| + | end |
| + | ret = data; |
| + | return |
| + | end |
| + | end %while 1 |
| + | otherwise |
| + | fprintf('Unknown option %s\n',cmd); |
| + | ret = []; |
| + | 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(' ',[]); |
| while 1 | | while 1 |
− | % Exit if user closed output figure
| + | % Exit if user closed output figure |
− | if ~ishandle(win)
| + | if ~ishandle(win) |
− | return
| + | return |
− | end
| + | end |
− | % start polling for characters (indicating start of scan)
| + | % start polling for characters (indicating start of scan) |
− | navailable = handle.BytesAvailable;
| + | navailable = handle.BytesAvailable; |
− | if navailable
| + | if navailable |
− | data = [];
| + | data = []; |
− | while navailable
| + | while navailable |
− | % read incoming data
| + | % read incoming data |
− | [newdata, cnt] = fread(handle, navailable);
| + | [newdata, cnt] = fread(handle, navailable); |
− | % concatenate possible new data
| + | % concatenate possible new data |
− | if cnt
| + | if cnt |
− | data = [data newdata(:)];
| + | data = [data newdata(:)]; |
− | end
| + | end |
− | % check if any more data left
| + | % check if any more data left |
− | navailable = handle.BytesAvailable;
| + | navailable = handle.BytesAvailable; |
− | end
| + | end |
− | % output info about which button was pressed
| + | % output info about which button was pressed |
− | for n = 1 : numel(data)
| + | for n = 1 : numel(data) |
− | line = sprintf('incoming: %03d %s',data(n),char(data(n)));
| + | line = sprintf('incoming: %03d %s',data(n),char(data(n))); |
− | list_output(line,win);
| + | list_output(line,win); |
− | end
| + | end |
− | 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) |
− | 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); |
− | fopen(hdl);
| + | fopen(hdl); |
− | catch
| + | catch |
− | if ~isempty(old_hdl)
| + | if ~isempty(old_hdl) |
− | fclose(old_hdl);
| + | fclose(old_hdl); |
− | delete(old_hdl);
| + | delete(old_hdl); |
− | end
| + | end |
− | 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); |
− | 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 |
− | while hdl.BytesAvailable && toc<10
| + | while hdl.BytesAvailable && toc<10 |
− | navailable = bbox.BytesAvailable;
| + | navailable = bbox.BytesAvailable; |
− | % wait for welcome message device
| + | % wait for welcome message device |
− | fread(hdl, navailable);
| + | fread(hdl, navailable); |
− | end
| + | end |
− | pause(0.5);
| + | pause(0.5); |
− | end
| + | |
− | | + | % while ~IOPort('BytesAvailable', hdl) && toc<10 |
− | function win = list_output(line,win)
| + | % % wait for welcome message device |
− | persistent ptr
| + | % end |
− | persistent lines
| + | % pause(0.5); |
− | persistent edt
| + | |
− | Maxlines = 40;
| + | % clear buffer |
− |
| + | %IOPort('flush', hdl); |
− | if isempty(win)
| + | IOPort('purge', hdl); |
− | % initialize listbox output figure
| + | |
− | lines = cell(1,Maxlines);
| + | end |
− | [lines(1:end)]=deal({''});
| |
− | ptr=Maxlines;
| |
− | lines(ptr) = {'Buttonbox output:'};
| |
− | idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
| |
| | | |
− | win = figure();
| + | function win = list_output(line,win) |
− | % initialize figure to hold output text
| + | persistent ptr |
− | edt = uicontrol('Parent',win,'Style','ListBox','HorizontalAlignment','left', ...
| + | persistent lines |
| + | persistent edt |
| + | Maxlines = 40; |
| + | |
| + | if isempty(win) |
| + | % initialize listbox output figure |
| + | lines = cell(1,Maxlines); |
| + | [lines(1:end)]=deal({''}); |
| + | ptr=Maxlines; |
| + | lines(ptr) = {'Buttonbox output:'}; |
| + | idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1; |
| + | |
| + | win = figure(); |
| + | % initialize figure to hold output text |
| + | edt = uicontrol('Parent',win,'Style','ListBox','HorizontalAlignment','left', ... |
| 'Max',Maxlines,'BackgroundColor',[1 1 1],'Visible','on','String',lines(idxs), ... | | 'Max',Maxlines,'BackgroundColor',[1 1 1],'Visible','on','String',lines(idxs), ... |
| 'FontSize',12,'Value',Maxlines); | | 'FontSize',12,'Value',Maxlines); |
− | pos = get(win,'Position');
| + | pos = get(win,'Position'); |
− | set(edt,'Position',[1 1 pos(3) pos(4)]);
| + | set(edt,'Position',[1 1 pos(3) pos(4)]); |
− | end
| + | end |
− | ptr = mod(ptr,Maxlines)+1; % start
| + | ptr = mod(ptr,Maxlines)+1; % start |
− | lines{ptr} = line;
| + | lines{ptr} = line; |
− | idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
| + | idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1; |
− | set(edt,'String',lines(idxs),'Value',Maxlines);
| + | set(edt,'String',lines(idxs),'Value',Maxlines); |
− | drawnow;
| + | drawnow; |
− | end
| + | end |
− | | + | end</syntaxhighlight> |
− | end | |
− | </syntaxhighlight> | |