|
15 | 15 | ## <https://www.gnu.org/licenses/>.
|
16 | 16 |
|
17 | 17 | ## -*- texinfo -*-
|
18 |
| -## @deftypefn {} {@var{retval} =} scanForArduinos (@var{maxCount}) |
| 18 | +## @deftypefn {} {@var{retval} =} scanForArduinos () |
| 19 | +## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{maxCount}) |
19 | 20 | ## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{"debug"})
|
20 | 21 | ## @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. |
22 | 24 | ##
|
23 |
| -## scanForArduinos will scan the system for programmed arduino boards |
| 25 | +## scanForArduinos will scan the system for programmed arduino boards |
24 | 26 | ## and return at most @var{maxCount} of them as a cell array
|
25 | 27 | ## in @var{retval}.
|
26 | 28 | ##
|
|
36 | 38 | ## scanForArduinos will display debug information as it scans
|
37 | 39 | ## all available ports for arduinos.
|
38 | 40 | ##
|
| 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 | +## |
39 | 53 | ## @subsubheading Outputs
|
40 | 54 | ## @var{retval} structure cell array of matching detected arduino boards.
|
41 | 55 | ##
|
|
50 | 64 | ## @seealso{arduino}
|
51 | 65 | ## @end deftypefn
|
52 | 66 |
|
53 |
| -function arduinos = scanForArduinos (maxCount, typestr) |
| 67 | +function arduinos = scanForArduinos (varargin) |
| 68 | + |
| 69 | + # maxCount, typestr |
54 | 70 |
|
55 | 71 | arduinos = {};
|
56 |
| - ARDUINO_INIT_COMMAND = 1; |
57 | 72 | debug_flag = false;
|
58 | 73 |
|
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 = []; |
67 | 77 |
|
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"); |
71 | 99 | 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 |
74 | 125 | endif
|
75 | 126 |
|
76 |
| - if ! isnumeric (maxCount) |
| 127 | + if ! isnumeric (maxCount) || maxCount < 0 |
77 | 128 | error ("scanForArduinos expected maxCount to be a number");
|
78 | 129 | endif
|
79 | 130 | if ! ischar (typestr) && !isempty (typestr)
|
|
84 | 135 | typestr = "";
|
85 | 136 | endif
|
86 | 137 |
|
| 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 | + |
87 | 152 | # get list of serial ports to try
|
88 | 153 | ports = instrhwinfo ('serial');
|
89 | 154 |
|
|
105 | 170 | if debug_flag
|
106 | 171 | printf("* trying comport %s\n", portname);
|
107 | 172 | 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); |
117 | 173 |
|
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); |
126 | 175 |
|
| 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 | + |
127 | 182 | if debug_flag
|
128 |
| - printf(" << "); printf("%02X ", dataout); printf("\n"); |
| 183 | + printf(" ** found board %s\n", info.board); |
129 | 184 | endif
|
130 | 185 |
|
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 |
151 | 189 | endif
|
152 |
| - |
| 190 | + |
153 | 191 | unwind_protect_cleanup
|
154 | 192 | if !isempty (s)
|
155 |
| - srl_close (s); |
| 193 | + clear s |
156 | 194 | endif
|
157 | 195 | end_unwind_protect
|
158 | 196 |
|
|
0 commit comments