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
|
## Copyright (C) 2019 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{val} = } subsref (@var{dev}, @var{sub})
## subref for device
##
## @seealso{device}
## @end deftypefn
function val = subsref (this, s)
if isempty(s)
error ("device.subsref missing index");
endif
if strcmp(this.interface, "SPI")
if s(1).type == "."
fld = tolower(s(1).subs);
switch (fld)
case "pins"
val = {};
for i = 1:numel(this.pins)
val{end+1} = this.pins{i}.name;
endfor
case "interface"
val = this.interface;
case "parent"
val = this.parent;
case "spimode"
val = this.devinfo.mode;
case "bitrate"
val = this.devinfo.bitrate;
case "bitorder"
val = this.devinfo.bitorder;
case "spichipselectpin"
val = this.devinfo.chipselectpin;
case "misopin"
val = get_func_pin(this.pins, "miso");
case "mosipin"
val = get_func_pin(this.pins, "mosi");
case "sckpin"
val = get_func_pin(this.pins, "sck");
otherwise
error ("device.subsref invalid property '%s'", fld);
endswitch
else
error("unimplemented device.subsref type");
endif
elseif strcmp(this.interface, "Serial")
if s(1).type == "."
fld = tolower(s(1).subs);
switch (fld)
case "pins"
val = {};
for i = 1:numel(this.pins)
val{end+1} = this.pins{i}.name;
endfor
case "interface"
val = this.interface;
case "parent"
val = this.parent;
case "baudrate"
val = this.devinfo.baudrate;
case "databits"
val = this.devinfo.databits;
case "stopbits"
val = this.devinfo.stopbits;
case "parity"
val = this.devinfo.parity;
case "timeout"
val = this.devinfo.timeout;
case "numbytesavailable"
val = __getBytesAvailable__(this);
case "serialport"
val = this.devinfo.id;
otherwise
error ("device.subsref invalid property '%s'", fld);
endswitch
else
error("unimplemented device.subsref type");
endif
else # I2C
if s(1).type == "."
fld = tolower(s(1).subs);
switch (fld)
case "pins"
val = {};
for i = 1:numel(this.pins)
val{end+1} = this.pins{i}.name;
endfor
case "interface"
val = this.interface;
case "parent"
val = this.parent;
case "bus"
val = this.devinfo.bus;
case "i2caddress"
val = this.devinfo.address;
case "bitrate"
val = this.devinfo.bitrate;
case "sdapin"
val = get_func_pin(this.pins, "sda");
case "sclpin"
val = get_func_pin(this.pins, "scl");
otherwise
error ("device.subsref invalid property '%s'", fld);
endswitch
else
error("unimplemented device.subsref type");
endif
endif
if (numel (s) > 1)
val = subsref (val, s(2:end));
endif
endfunction
function val = get_func_pin(pins, func)
val = [];
for i=1:numel(pins)
pin = pins{i};
if strcmp(pin.func, func)
val = pin.name;
endif
endfor
endfunction
%!test
%! ar = arduino();
%! spi = device (ar, "spichipselectpin", "d10");
%! assert (spi.spichipselectpin, "d10")
%! assert (isarduino(spi.parent))
%! assert (ar.port, spi.parent.port)
%! assert (spi.spimode, 0)
%! assert (spi.bitorder, "msbfirst")
%! assert (spi.interface, "SPI")
%! assert (numel(spi.pins) >= 4)
%! fail ("spi.invalid")
%! delete(spi)
|