changed: new logic for reading ports state

git-svn-id: https://svn.fhem.de/fhem/trunk/fhem@4045 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
betateilchen 2013-10-15 13:08:03 +00:00
parent 43ea47bb83
commit 54a923f1d7

View File

@ -51,17 +51,16 @@ sub PIFACE_Initialize($){
$hash->{UndefFn} = "PIFACE_Undefine"; $hash->{UndefFn} = "PIFACE_Undefine";
$hash->{SetFn} = "PIFACE_Set"; $hash->{SetFn} = "PIFACE_Set";
$hash->{GetFn} = "PIFACE_Get"; $hash->{GetFn} = "PIFACE_Get";
$hash->{AttrList} = $readingFnAttributes; $hash->{AttrList} = "pifaceAutoPoll:0,1 ".
$readingFnAttributes;
} }
sub PIFACE_Define($$){ sub PIFACE_Define($$){
my ($hash, $def) = @_; my ($hash, $def) = @_;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
Log3($name, 3, "PIFACE $name: created"); Log3($name, 3, "PIFACE $name: created");
PI_read_allports($hash);
readingsSingleUpdate($hash, "state", "active",1); readingsSingleUpdate($hash, "state", "active",1);
PI_read_outports($hash);
PI_read_inports($hash,0);
PI_read_inports($hash,1);
return undef; return undef;
} }
@ -126,19 +125,17 @@ sub PIFACE_Get($@){
return $usage if $port eq "?"; return $usage if $port eq "?";
if ($port eq "0") { if ($port eq "0") {
PI_read_outports($hash); # read all inports and outports
PI_read_inports($hash,0); PI_read_allports($hash);
PI_read_inports($hash,1);
} elsif ($port ~~ [1..8]) { } elsif ($port ~~ [1..8]) {
# read inport # read single inport
$adr = $base + $port; $adr = $base + $port;
$cmd = '/usr/local/bin/gpio -p read '.$adr; $cmd = '/usr/local/bin/gpio -p read '.$adr;
$val = `$cmd`; $val = `$cmd`;
readingsSingleUpdate($hash, 'in'.$port, $val, 1); readingsSingleUpdate($hash, 'in'.$port, $val, 1);
} elsif ($port ~~ [11..18]) { } elsif ($port ~~ [11..18]) {
# read inport with pullup Log3($name, 3, "PIFACE $name: get inports with internal pullups is DEPRECATED and will be removed in further versions!");
# read single inport with pullup
$pin = $port - 10; $pin = $port - 10;
$adr = $base + $pin; $adr = $base + $pin;
$cmd = '/usr/local/bin/gpio -p mode '.$adr.' up'; $cmd = '/usr/local/bin/gpio -p mode '.$adr.' up';
@ -146,64 +143,40 @@ sub PIFACE_Get($@){
$cmd = '/usr/local/bin/gpio -p read '.$adr; $cmd = '/usr/local/bin/gpio -p read '.$adr;
$val = `$cmd`; $val = `$cmd`;
readingsSingleUpdate($hash, 'in'.$port, $val, 1); readingsSingleUpdate($hash, 'in'.$port, $val, 1);
} elsif ($port ~~ [21..28]) { } elsif ($port ~~ [21..28]) {
# read outport # read single outport
$pin = $port - 12; $pin = $port - 12;
$port -= 20; $port -= 20;
$adr = $base + $pin; $adr = $base + $pin;
$cmd = '/usr/local/bin/gpio -p read '.$adr; $cmd = '/usr/local/bin/gpio -p read '.$adr;
$val = `$cmd`; $val = `$cmd`;
readingsSingleUpdate($hash, 'out'.$port, $val, 1); readingsSingleUpdate($hash, 'out'.$port, $val, 1);
} }
return ""; return "";
} }
sub PI_read_outports($){ sub PI_read_allports($){
my ($hash) = @_; my ($hash) = @_;
my $name = $hash->{NAME};
my ($cmd, $val, $p, $pin, $v, $zeile, @ports);
my $val = '?'; $cmd = '/usr/local/bin/gpio -p readall';
my ($cmd, $i, $port); $val = `$cmd`;
@ports = split(/\n/, $val);
readingsBeginUpdate($hash); foreach (@ports){
for($i=1; $i<9; $i++){ $zeile = $_;
$port = $base + $i + 8; $p = substr($zeile, 3, 3);
$cmd = '/usr/local/bin/gpio -p read '.$port; $v = substr($zeile, 13, 1);
$val = `$cmd`; if (substr($p,0,1) eq '2' && $p ~~ [200..207]){
readingsBulkUpdate($hash, 'out'.$i, $val); $pin = $p - 199;
} readingsSingleUpdate($hash, 'in'.$pin, $v, 1) if(ReadingsVal($name, 'in'.$pin, '') ne $v);
readingsEndUpdate($hash, 0); } elsif (substr($p,0,1) eq '2' && $p ~~ [208..215]){
return $pin = $p - 207;
} readingsSingleUpdate($hash, 'out'.$pin, $v, 1) if(ReadingsVal($name, 'out'.$pin, '') ne $v);
sub PI_read_inports($;$){
my ($hash,$pull) = @_;
my $val = '?';
my ($cmd, $i, $j, $port);
readingsBeginUpdate($hash);
for($i=1; $i<9; $i++){
$port = $base + $i;
if($pull eq '1'){
$cmd = '/usr/local/bin/gpio -p mode '.$port.' up';
$val = `$cmd`;
$cmd = '/usr/local/bin/gpio -p read '.$port;
$val = `$cmd`;
$j = 10 + $i;
readingsBulkUpdate($hash, 'in'.$j, chomp($val));
} else {
$cmd = '/usr/local/bin/gpio -p mode '.$port.' tri';
$val = `$cmd`;
$cmd = '/usr/local/bin/gpio -p read '.$port;
$val = `$cmd`;
readingsBulkUpdate($hash, 'in'.$i, chomp($val));
} }
} }
readingsEndUpdate($hash, 1); return;
return
} }
1; 1;
@ -288,10 +261,10 @@ sub PI_read_inports($;$){
<li>get state of single output port with internal pullups <b>on</b><br/><br/> <li>get state of single output port with internal pullups <b>on</b><br/><br/>
Add 20 to port number!<br/><br/> Add 20 to port number!<br/><br/>
Example:<br/> Example:<br/>
get &lt;name&gt; 25 =&gt; get state of output port 5<br/></li> get &lt;name&gt; 25 =&gt; get state of output port 5<br/>
<br/> <b>Important:</b> reading with internal pullups is DEPRECATED and will be removed in further versions!<br/><br/></li>
<br/> <li>get state of all input AND output ports and update readings.<br/>
<li>get state of all input AND output ports and update readings<br/><br/> <b>Important:</b> in-ports are only read without pullup!<br/>
Example:<br/> Example:<br/>
get &lt;name&gt; 0 =&gt; get state of all ports<br/></li> get &lt;name&gt; 0 =&gt; get state of all ports<br/></li>
</ul> </ul>