File: getPinInfo.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 (141 lines) | stat: -rw-r--r-- 3,656 bytes parent folder | download | duplicates (4)
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
## Copyright (C) 2018 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.

## -*- texinfo -*- 
## @deftypefn {} {@var{pininfo} =} getPinInfo (@var{ar}, @var{pin})
## @deftypefnx {} {@var{pininfoarray} =} getPinInfo (@var{ar}, @var{pinarray})
## Get the pin information from the input pins values.
##
## getPinInfo (@var{ar}, @var{pin}) will get information for a single pin.
##
## getPinInfo (@var{ar}, @var{pinarray}) will get a cell array of pin information
## 
## @subsubheading Inputs
## @var{ar} - the connected arduino object.
##
## @var{pin} - a pin number or pin name.
##
## @var{pinarray} - the array of pin numbers or names
##
## The pininfo struct contains the following fields:
## @table @asis
## @item terminal
## Terminal number of the pin
## @item name
## String name of the pin
## @item owner
## Current item owner of the pin
## @item mode
## Current configured mode for the pin
## @end table
##
## @subsubheading Outputs
## @var{pininfo} - struct on pin information.
##
## @var{pininfolist} - cell array of pin info
##
## @seealso{arduino, configurePinResource, getResourceOwner}
## @end deftypefn

function retval = getPinInfo (obj, pins)

  if nargin != 2
    print_usage ()
  endif

  if iscell (pins)
    retval = {};
    for i=1:numel (pins)
      p = obj.get_pin(pins{i});
      inf = {};
      inf.name = p.name;
      inf.terminal = p.id;
      inf.owner = p.owner;
      inf.mode = p.mode;
      retval{end+1} = inf;
    endfor
  elseif ischar(pins)
    p = obj.get_pin(pins);
    inf = {};
    inf.name = p.name;
    inf.terminal = p.id;
    inf.owner = p.owner;
    inf.mode = p.mode;
 
    retval = inf;
  elseif isvector (pins) && numel (pins) == 1
    p = obj.get_pin(pins);
    inf = {};
    inf.name = p.name;
    inf.terminal = p.id;
    inf.owner = p.owner;
    inf.mode = p.mode;
 
    retval = inf;
  elseif isvector (pins)
    retval = {};
    for i=1:numel (pins)
      p = obj.get_pin(pins(i));
      inf = {};
      inf.name = p.name;
      inf.terminal = p.id;
      inf.owner = p.owner;
      inf.mode = p.mode;
 
      retval{end+1} = inf;
    endfor
  elseif isnumeric (pins)
    p = obj.get_pin(pins);
    inf = {};
    inf.name = p.name;
    inf.terminal = p.id;
    inf.owner = p.owner;
    inf.mode = p.mode;
    retval = inf;
  else
    error ("@arduino.getPinInfo: expected pins a array of numbers or names");
  endif
endfunction

%!shared ar
%! ar = arduino();

%!test
%! info = getPinInfo(ar, 0);
%! # terminal 0 is alwars D0 ?
%! assert (isstruct (info));
%! assert (!iscell (info));
%! assert (toupper (info.name), "D0");
%! assert (info.terminal, 0);

%!test
%! info = getPinInfo(ar, "d0");
%! # terminal 0 is alwars D0 ?
%! assert (isstruct (info));
%! assert (!iscell (info));
%! assert (toupper (info.name), "D0");
%! assert (info.terminal, 0);

%!test
%! info = getPinInfo(ar, [0 2]);
%! assert(numel(info), 2);
%! assert(iscell(info));
%! assert (toupper (info{1}.name), "D0");
%! assert (toupper (info{2}.name), "D2");

%!test
%! info = getPinInfo(ar, {"d4", 5});
%! assert(numel(info), 2);
%! assert(iscell(info));
%! assert (toupper (info{1}.name), "D4");
%! assert (toupper (info{2}.name), "D5");