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