Skip to content

Commit b418878

Browse files
committed
* inst/scanForArduinos.m: add property name/value inputs
1 parent ad50814 commit b418878

File tree

1 file changed

+97
-59
lines changed

1 file changed

+97
-59
lines changed

inst/scanForArduinos.m

+97-59
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
## <https://www.gnu.org/licenses/>.
1616

1717
## -*- texinfo -*-
18-
## @deftypefn {} {@var{retval} =} scanForArduinos (@var{maxCount})
18+
## @deftypefn {} {@var{retval} =} scanForArduinos ()
19+
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{maxCount})
1920
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{"debug"})
2021
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{maxCount}, @var{type})
21-
## Scan system for programmed arduino boards.
22+
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{propertyname}, @var{propertvalue} ...)
23+
## Scan system for programmed serial connected arduino boards.
2224
##
23-
## scanForArduinos will scan the system for programmed arduino boards
25+
## scanForArduinos will scan the system for programmed arduino boards
2426
## and return at most @var{maxCount} of them as a cell array
2527
## in @var{retval}.
2628
##
@@ -36,6 +38,18 @@
3638
## scanForArduinos will display debug information as it scans
3739
## all available ports for arduinos.
3840
##
41+
## @var{propertyname}, @var{propertyvalue} - property name/value pairs to match search with.
42+
## @table @asis
43+
## @item 'BaudRate'
44+
## Numeric BaudRate to use when trying to scan for arduinos.
45+
## @item 'MaxCount'
46+
## Max number of arduinos to scan for.
47+
## @item 'BoardType'
48+
## Boardtype to match.
49+
## @item 'Debug'
50+
## Logical flag for debug mode.
51+
## @end table
52+
##
3953
## @subsubheading Outputs
4054
## @var{retval} structure cell array of matching detected arduino boards.
4155
##
@@ -50,30 +64,67 @@
5064
## @seealso{arduino}
5165
## @end deftypefn
5266

53-
function arduinos = scanForArduinos (maxCount, typestr)
67+
function arduinos = scanForArduinos (varargin)
68+
69+
# maxCount, typestr
5470

5571
arduinos = {};
56-
ARDUINO_INIT_COMMAND = 1;
5772
debug_flag = false;
5873

59-
if nargin > 2
60-
print_usage ();
61-
elseif nargin == 1
62-
typestr = "";
63-
elseif nargin == 0
64-
maxCount = 0;
65-
typestr = "";
66-
endif
74+
maxCount = 0;
75+
typestr = "";
76+
baudrate = [];
6777

68-
if ischar (maxCount) && strcmpi(maxCount, "debug")
69-
if nargin > 1
70-
error ("scanForArduinos allows no additional arguments for 'debug'");
78+
if nargin == 1
79+
typestr = "";
80+
if ischar(varargin{1})
81+
if strcmp(varargin{1}, "debug")
82+
debug_flag = 1;
83+
else
84+
error ("scanForArduinos: invalid argument");
85+
endif
86+
elseif isnumeric(varargin{1})
87+
maxCount = int32(varargin{1});
88+
else
89+
error ("scanForArduinos: invalid argument");
90+
endif
91+
elseif nargin == 2 && isnumeric(varargin{1}) && ischar(varargin{2})
92+
# maxCount and boardtype
93+
maxCount = int32(varargin{1});
94+
typestr = varargin{2};
95+
elseif nargin >= 2
96+
# properties
97+
if mod (nargin, 2) != 0
98+
error ("scanForArduins: expected property name, value pairs");
7199
endif
72-
maxCount = 0;
73-
debug_flag = true;
100+
if !iscellstr (varargin (1:2:nargin))
101+
error ("scanForArduinos: expected property names to be strings");
102+
endif
103+
104+
for i = 1:2:nargin
105+
propname = tolower (varargin{i});
106+
propvalue = varargin{i+1};
107+
108+
if strcmp (propname,"debug")
109+
if propvalue
110+
debug_flag = 1;
111+
else
112+
debug_flag = 0;
113+
endif
114+
endif
115+
if strcmp (propname,"boardtype")
116+
boardstr = propvalue;
117+
endif
118+
if strcmp (propname,"baudrate")
119+
baudrate = propvalue;
120+
endif
121+
if strcmp (propname,"maxcount")
122+
maxCount = propvalue;
123+
endif
124+
endfor
74125
endif
75126

76-
if ! isnumeric (maxCount)
127+
if ! isnumeric (maxCount) || maxCount < 0
77128
error ("scanForArduinos expected maxCount to be a number");
78129
endif
79130
if ! ischar (typestr) && !isempty (typestr)
@@ -84,6 +135,20 @@
84135
typestr = "";
85136
endif
86137

138+
if isempty(baudrate)
139+
if !isempty(typestr)
140+
# get default baudrate for baud
141+
c = arduinoio.getBoardConfig(typestr);
142+
baudrate = c.baudrate;
143+
else
144+
baudrate = 9600;
145+
endif
146+
endif
147+
148+
if ! isnumeric (baudrate) || baudrate < 1200
149+
error ("scanForArduinos expected baudrate to be a number >= 1200");
150+
endif
151+
87152
# get list of serial ports to try
88153
ports = instrhwinfo ('serial');
89154

@@ -105,54 +170,27 @@
105170
if debug_flag
106171
printf("* trying comport %s\n", portname);
107172
endif
108-
s = serial (portname, 9600, 1);
109-
pause(2);
110-
111-
hdr = uint8 ([ hex2dec("A5") 0 ARDUINO_INIT_COMMAND 0]);
112-
if debug_flag
113-
printf(" >> "); printf("%02X ", hdr); printf("\n");
114-
endif
115-
len = srl_write (s, hdr);
116-
[tmpdataOut, tmpdataSize] = srl_read (s, 4);
117173

118-
if debug_flag
119-
printf(" << "); printf("%02X ", tmpdataOut); printf("\n");
120-
endif
121-
122-
if tmpdataSize == 4 && tmpdataOut(1) == hex2dec("A5") && tmpdataOut(2) == 0 && tmpdataOut(3) == ARDUINO_INIT_COMMAND && tmpdataOut(4) >= 5
123-
expectlen = tmpdataOut(4);
124-
125-
[dataout, datasize] = srl_read (s, expectlen);
174+
s = arduino(portname, "", "Debug", debug_flag, "BaudRate", 9600);
126175

176+
if isempty (typestr) || strcmpi(s.board, typestr)
177+
info = {};
178+
info.port = portname;
179+
info.board = s.board;
180+
arduinos{end+1} = info;
181+
127182
if debug_flag
128-
printf(" << "); printf("%02X ", dataout); printf("\n");
183+
printf(" ** found board %s\n", info.board);
129184
endif
130185

131-
if datasize == expectlen
132-
# init returns the following info
133-
sig = (uint32 (dataout(1))*256*256) + (uint32 (dataout(2))*256) + uint32 (dataout(3));
134-
board = dataout(4);
135-
voltref = double (dataout(5))/10.0;
136-
if isempty (typestr) || strcmpi(arduinoio.boardTypeString (board), typestr)
137-
info = {};
138-
info.port = portname;
139-
info.board = arduinoio.boardTypeString (board);
140-
arduinos{end+1} = info;
141-
142-
if debug_flag
143-
printf(" ** found board %s\n", info.board);
144-
endif
145-
146-
if numel (arduinos) == maxCount
147-
break;
148-
endif
149-
endif
150-
endif
186+
if numel (arduinos) == maxCount
187+
break;
188+
endif
151189
endif
152-
190+
153191
unwind_protect_cleanup
154192
if !isempty (s)
155-
srl_close (s);
193+
clear s
156194
endif
157195
end_unwind_protect
158196

0 commit comments

Comments
 (0)