File: scanForArduinos.m

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (209 lines) | stat: -rw-r--r-- 6,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
## Copyright (C) 2018-2025 John Donoghue <john.donoghue@ieee.org>
## 
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {} {@var{retval} =} scanForArduinos ()
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{maxCount})
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{"debug"})
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{maxCount}, @var{type})
## @deftypefnx {} {@var{retval} =} scanForArduinos (@var{propertyname}, @var{propertvalue} ...)
## Scan system for programmed serial connected arduino boards.
##
## scanForArduinos will scan the system for programmed arduino boards 
## and return at most @var{maxCount} of them as a cell array 
## in @var{retval}.
##
## @subsubheading Inputs
## @var{maxCount} - max number of arduino boards to detect.
## if @var{maxCount} is not specified, or is a less than 1, the 
## function will return as many arduino boards as it can detect.
##
## @var{type} - optional board type to match. If specified, the board 
## type must match for the arduino to be added to the return list.
##
## @var{"debug"} - if single input parameter is "debug", the 
## scanForArduinos will display debug information as it scans
## all available ports for arduinos.
##
## @var{propertyname}, @var{propertyvalue} - property name/value pairs to match search with.
## @table @asis
## @item 'BaudRate'
## Numeric BaudRate to use when trying to scan for arduinos.
## @item 'MaxCount'
## Max number of arduinos to scan for.
## @item 'BoardType'
## Boardtype to match.
## @item 'Debug'
## Logical flag for debug mode.
## @end table
##
## @subsubheading Outputs
## @var{retval} structure cell array of matching detected arduino boards.
##
## Each cell value of the cell array will contain a structure with values of:
## @table @asis
## @item port 
## the serial port the arduino is connected to
## @item board
## the board type of the arduino
## @end table
##
## @seealso{arduino}
## @end deftypefn

function arduinos = scanForArduinos (varargin)

  # maxCount, typestr

  arduinos = {};
  debug_flag = false;

  maxCount = 0;
  typestr = "";
  baudrate = [];

  if nargin == 1
    typestr = "";
    if ischar(varargin{1})
      if strcmp(varargin{1}, "debug")
        debug_flag = 1; 
      else
        error ("scanForArduinos: invalid argument");
      endif
    elseif isnumeric(varargin{1})
      maxCount = int32(varargin{1});
    else
      error ("scanForArduinos: invalid argument");
    endif
  elseif nargin == 2 && isnumeric(varargin{1}) && ischar(varargin{2})
    # maxCount and boardtype
    maxCount = int32(varargin{1});
    typestr = varargin{2};
  elseif nargin >= 2
    # properties
    if mod (nargin, 2) != 0
        error ("scanForArduins: expected property name, value pairs");
    endif
    if !iscellstr (varargin (1:2:nargin))
      error ("scanForArduinos: expected property names to be strings");
    endif
 
    for i = 1:2:nargin
      propname = tolower (varargin{i});
      propvalue = varargin{i+1};

      if strcmp (propname,"debug")
        if propvalue
          debug_flag = 1;
        else
          debug_flag = 0;
        endif
      endif
      if strcmp (propname,"boardtype")
        boardstr = propvalue;
      endif
      if strcmp (propname,"baudrate")
        baudrate = propvalue;
      endif
      if strcmp (propname,"maxcount")
        maxCount = propvalue;
      endif
    endfor
  endif

  if ! isnumeric (maxCount) || maxCount < 0
    error ("scanForArduinos expected maxCount to be a number");
  endif
  if ! ischar (typestr) && !isempty (typestr)
    error ("scanForArduinos expected typestr to be a board type");
  elseif ischar (typestr)
    typestr = tolower (typestr);
  else
    typestr = "";
  endif

  if isempty(baudrate)
    baudrate = 9600;
    if !isempty(typestr)
      # get default baudrate for baud
      try
        c = arduinoio.getBoardConfig(typestr);
        baudrate = c.baudrate;
      catch err
        error ("scanForArduinos: unknown board type");
      end_try_catch
    endif
  endif

  if ! isnumeric (baudrate) || baudrate < 1200
    error ("scanForArduinos expected baudrate to be a number >= 1200");
  endif

  # get list of serial ports to try
  ports = __arduino_serialportlist__ (debug_flag);

  for i = 1:numel (ports)
    try
      s = {};
      unwind_protect
        portname = ports{i};

        if debug_flag
          printf("* trying comport %s\n", portname);	
        endif
        s = arduino(portname, "", "Debug", debug_flag, "BaudRate", baudrate, "_scan_only", 1);

        if isempty (typestr) || strcmpi(s.board, typestr)
          info = {};
          info.port = portname;
          info.board = s.board;
          arduinos{end+1} = info;
          
          if debug_flag
            printf(" ** found board %s\n", info.board);
          endif

          if numel (arduinos) == maxCount
            break;
          endif
        endif
	
      unwind_protect_cleanup
        if !isempty (s)
          delete(s);
        endif
      end_unwind_protect

    catch err
      % do nothing
      if debug_flag
        printf(" ** %s\n", err.message);
      endif
    end_try_catch
  endfor
endfunction

%!test
%! # assuming that to test, we have at least one board available
%! arduinos = scanForArduinos(1);
%! assert(numel(arduinos), 1);
%! assert(!isempty(arduinos{1}.port))
%! assert(!isempty(arduinos{1}.board))

%!test
%! a = scanForArduinos("BaudRate", 115200, "BoardType", "Uno"); 

%!error <scanForArduinos: unknown board type> scanForArduinos(1, "madeuparduinoname");