mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-03-10 03:06:37 +00:00
add counter to FRM_IN, add thresholds and alarms to FRM_IN and FRM_AD
git-svn-id: https://svn.fhem.de/fhem/trunk@2638 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
9bf9111cf5
commit
aaf763784a
@ -7,6 +7,14 @@ use Device::Firmata;
|
||||
use Device::Firmata::Constants qw/ :all /;
|
||||
|
||||
#####################################
|
||||
|
||||
my %gets = (
|
||||
"reading" => "",
|
||||
"state" => "",
|
||||
"alarm-upper-threshold" => "off",
|
||||
"alarm-lower-threshold" => "off",
|
||||
);
|
||||
|
||||
sub
|
||||
FRM_AD_Initialize($)
|
||||
{
|
||||
@ -17,7 +25,7 @@ FRM_AD_Initialize($)
|
||||
$hash->{InitFn} = "FRM_AD_Init";
|
||||
$hash->{UndefFn} = "FRM_AD_Undef";
|
||||
|
||||
$hash->{AttrList} = "IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
|
||||
$hash->{AttrList} = "IODev upper-threshold lower-threshold loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
|
||||
}
|
||||
|
||||
sub
|
||||
@ -28,6 +36,9 @@ FRM_AD_Init($$)
|
||||
my $firmata = $hash->{IODev}->{FirmataDevice};
|
||||
$firmata->observe_analog($hash->{PIN},\&FRM_AD_observer,$hash);
|
||||
$main::defs{$hash->{NAME}}{resolution}=$firmata->{metadata}{analog_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{analog_resolutions});
|
||||
if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) {
|
||||
$main::attr{$hash->{NAME}}{"stateFormat"} = "reading";
|
||||
}
|
||||
main::readingsSingleUpdate($hash,"state","Initialized",1);
|
||||
return undef;
|
||||
}
|
||||
@ -39,27 +50,52 @@ FRM_AD_observer
|
||||
{
|
||||
my ($pin,$old,$new,$hash) = @_;
|
||||
main::Log(6,"onAnalogMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--"));
|
||||
main::readingsSingleUpdate($hash,"state",$new, 1);
|
||||
main::readingsBeginUpdate($hash);
|
||||
main::readingsBulkUpdate($hash,"reading",$new,1);
|
||||
my $name = $hash->{NAME};
|
||||
my $upperthresholdalarm = ReadingsVal($name,"alarm-upper-threshold","off");
|
||||
if ( $new < AttrVal($name,"upper-threshold",1024) ) {
|
||||
if ( $upperthresholdalarm eq "on" ) {
|
||||
main::readingsBulkUpdate($hash,"alarm-upper-threshold","off",1);
|
||||
}
|
||||
my $lowerthresholdalarm = ReadingsVal($name,"alarm-lower-threshold","off");
|
||||
if ( $new > AttrVal($name,"lower-threshold",-1) ) {
|
||||
if ( $lowerthresholdalarm eq "on" ) {
|
||||
main::readingsBulkUpdate($hash,"alarm-lower-threshold","off",1);
|
||||
}
|
||||
} else {
|
||||
if ( $lowerthresholdalarm eq "off" ) {
|
||||
main::readingsBulkUpdate($hash,"alarm-lower-threshold","on",1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ( $upperthresholdalarm eq "off" ) {
|
||||
main::readingsBulkUpdate($hash,"alarm-upper-threshold","on",1);
|
||||
}
|
||||
};
|
||||
main::readingsBulkUpdate($hash,"reading",$new, 1);
|
||||
main::readingsEndUpdate($hash,0);
|
||||
}
|
||||
|
||||
sub
|
||||
FRM_AD_Get($)
|
||||
{
|
||||
my ($hash,@a) = @_;
|
||||
my $iodev = $hash->{IODev};
|
||||
my $name = shift @a;
|
||||
return $name." no IODev assigned" if (!defined $iodev);
|
||||
return $name.", ".$iodev->{NAME}." is not connected" if (!(defined $iodev->{FirmataDevice} and defined $iodev->{FD}));
|
||||
my $cmd = shift @a;
|
||||
my $ret;
|
||||
ARGUMENT_HANDLER: {
|
||||
$cmd eq "reading" and do {
|
||||
$ret = $iodev->{FirmataDevice}->analog_read($hash->{PIN});
|
||||
last;
|
||||
my $iodev = $hash->{IODev};
|
||||
return $name." no IODev assigned" if (!defined $iodev);
|
||||
return $name.", ".$iodev->{NAME}." is not connected" if (!(defined $iodev->{FirmataDevice} and defined $iodev->{FD}));
|
||||
return $iodev->{FirmataDevice}->analog_read($hash->{PIN});
|
||||
};
|
||||
( $cmd eq "alarm-upper-threshold" or $cmd eq "alarm-lower-threshold" or $cmd eq "state" ) and do {
|
||||
return main::ReadingsVal($name,"count",$gets{$cmd});
|
||||
};
|
||||
$ret = "unknown command ".$cmd;
|
||||
}
|
||||
return $ret;
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub
|
||||
@ -98,11 +134,29 @@ FRM_AD_Undef($$)
|
||||
<b>Get</b><br>
|
||||
<ul>
|
||||
<li>reading<br>
|
||||
returns the voltage-level read on the arduino-pin. Values range from 0 to 1023.<br></li>
|
||||
returns the voltage-level read on the arduino-pin. Values range from 0 to 1023.</li>
|
||||
<li>alarm-upper-threshold<br>
|
||||
returns the current state of 'alarm-upper-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
|
||||
'alarm-upper-threshold' turns 'on' whenever the 'reading' is higher than the attribute 'upper-threshold'<br>
|
||||
it turns 'off' again as soon 'reading' falls below 'alarm-upper-threshold'</li>
|
||||
<li>alarm-lower-threshold<br>
|
||||
returns the current state of 'alarm-lower-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
|
||||
'alarm-lower-threshold' turns 'on' whenever the 'reading' is lower than the attribute 'lower-threshold'<br>
|
||||
it turns 'off' again as soon 'reading rises above 'alarm-lower-threshold'</li>
|
||||
<li>state<br>
|
||||
returns the 'state' reading</li>
|
||||
</ul><br>
|
||||
<a name="FRM_ADattr"></a>
|
||||
<b>Attributes</b><br>
|
||||
<ul>
|
||||
<li>upper-threshold<br>
|
||||
sets the 'upper-threshold'. Whenever the 'reading' exceeds this value 'alarm-upper-threshold' is set to 'on'<br>
|
||||
As soon 'reading' falls below the 'upper-threshold' 'alarm-upper-threshold' turns 'off' again<br>
|
||||
Defaults to 1024.</li>
|
||||
<li>lower-threshold<br>
|
||||
sets the 'lower-threshold'. Whenever the 'reading' falls below this value 'alarm-lower-threshold' is set to 'on'<br>
|
||||
As soon 'reading' rises above the 'lower-threshold' 'alarm-lower-threshold' turns 'off' again<br>
|
||||
Defaults to -1.</li>
|
||||
<li><a href="#IODev">IODev</a><br>
|
||||
Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
|
||||
than one FRM-device defined.)
|
||||
|
@ -7,17 +7,30 @@ use Device::Firmata;
|
||||
use Device::Firmata::Constants qw/ :all /;
|
||||
|
||||
#####################################
|
||||
|
||||
my %sets = (
|
||||
"alarm" => "",
|
||||
);
|
||||
|
||||
my %gets = (
|
||||
"reading" => "",
|
||||
"state" => "",
|
||||
"count" => 0,
|
||||
"alarm" => "off"
|
||||
);
|
||||
|
||||
sub
|
||||
FRM_IN_Initialize($)
|
||||
{
|
||||
my ($hash) = @_;
|
||||
|
||||
$hash->{SetFn} = "FRM_IN_Set";
|
||||
$hash->{GetFn} = "FRM_IN_Get";
|
||||
$hash->{DefFn} = "FRM_Client_Define";
|
||||
$hash->{InitFn} = "FRM_IN_Init";
|
||||
$hash->{UndefFn} = "FRM_IN_Undef";
|
||||
|
||||
$hash->{AttrList} = "IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
|
||||
$hash->{AttrList} = "IODev count-mode count-threshold loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
|
||||
}
|
||||
|
||||
sub
|
||||
@ -27,6 +40,9 @@ FRM_IN_Init($$)
|
||||
if (FRM_Init_Pin_Client($hash,$args,PIN_INPUT)) {
|
||||
my $firmata = $hash->{IODev}->{FirmataDevice};
|
||||
$firmata->observe_digital($hash->{PIN},\&FRM_IN_observer,$hash);
|
||||
if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) {
|
||||
$main::attr{$hash->{NAME}}{"stateFormat"} = "reading";
|
||||
}
|
||||
main::readingsSingleUpdate($hash,"state","Initialized",1);
|
||||
return undef;
|
||||
}
|
||||
@ -39,27 +55,65 @@ FRM_IN_observer
|
||||
{
|
||||
my ($pin,$old,$new,$hash) = @_;
|
||||
main::Log(6,"onDigitalMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--"));
|
||||
main::readingsSingleUpdate($hash,"state",$new == PIN_HIGH ? "on" : "off", 1);
|
||||
my $name = $hash->{NAME};
|
||||
my $mode = AttrVal($name,"count-mode","rising");
|
||||
my $count = ReadingsVal($name,"count",0);
|
||||
main::readingsBeginUpdate($hash);
|
||||
if ( ($old != $new)
|
||||
and (($mode eq "rising" and $old == PIN_LOW)
|
||||
or ($mode eq "falling" and $old == PIN_HIGH)
|
||||
or ($mode eq "both"))) {
|
||||
$count++;
|
||||
my $threshold = AttrVal($name,"count-threshold",0);
|
||||
if ( $count >= $threshold ) {
|
||||
main::readingsBulkUpdate($hash,"alarm","on",1);
|
||||
$count=0;
|
||||
}
|
||||
main::readingsBulkUpdate($hash,"count",$count,1);
|
||||
};
|
||||
main::readingsBulkUpdate($hash,"reading",$new == PIN_HIGH ? "on" : "off", 1);
|
||||
main::readingsEndUpdate($hash,0);
|
||||
}
|
||||
|
||||
sub
|
||||
FRM_IN_Set
|
||||
{
|
||||
my ($hash, @a) = @_;
|
||||
return "Need at least one parameters" if(@a < 2);
|
||||
return "Unknown argument $a[1], choose one of " . join(" ", sort keys %sets)
|
||||
if(!defined($sets{$a[1]}));
|
||||
my $command = $a[1];
|
||||
my $value = $a[2];
|
||||
COMMAND_HANDLER: {
|
||||
$command eq "alarm" and do {
|
||||
return undef if (!($value eq "off" or $value eq "on"));
|
||||
main::readingsSingleUpdate($hash,"alarm",$value,1);
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub
|
||||
FRM_IN_Get($)
|
||||
{
|
||||
my ($hash, @a) = @_;
|
||||
my $iodev = $hash->{IODev};
|
||||
return "Need at least one parameters" if(@a < 2);
|
||||
return "Unknown argument $a[1], choose one of " . join(" ", sort keys %gets)
|
||||
if(!defined($gets{$a[1]}));
|
||||
my $name = shift @a;
|
||||
return $name." no IODev assigned" if (!defined $iodev);
|
||||
return $name.", ".$iodev->{NAME}." is not connected" if (!(defined $iodev->{FirmataDevice} and defined $iodev->{FD}));
|
||||
my $cmd = shift @a;
|
||||
my $ret;
|
||||
ARGUMENT_HANDLER: {
|
||||
$cmd eq "reading" and do {
|
||||
my $ret = $iodev->{FirmataDevice}->digital_read($hash->{PIN});
|
||||
return $ret == PIN_HIGH ? "on" : "off";
|
||||
my $iodev = $hash->{IODev};
|
||||
return $name." no IODev assigned" if (!defined $iodev);
|
||||
return $name.", ".$iodev->{NAME}." is not connected" if (!(defined $iodev->{FirmataDevice} and defined $iodev->{FD}));
|
||||
return $iodev->{FirmataDevice}->digital_read($hash->{PIN}) == PIN_HIGH ? "on" : "off";
|
||||
};
|
||||
( $cmd eq "count" or $cmd eq "alarm" or $cmd eq "state" ) and do {
|
||||
return main::ReadingsVal($name,"count",$gets{$cmd});
|
||||
};
|
||||
$ret = "unknown command ".$cmd;
|
||||
}
|
||||
return $ret;
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub
|
||||
@ -92,17 +146,33 @@ FRM_IN_Undef($$)
|
||||
<a name="FRM_INset"></a>
|
||||
<b>Set</b><br>
|
||||
<ul>
|
||||
N/A<br>
|
||||
<li>alarm on|off<br>
|
||||
set the alarm to on or off. Used to clear the alarm.<br>
|
||||
The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li>
|
||||
</ul>
|
||||
<a name="FRM_INget"></a>
|
||||
<b>Get</b>
|
||||
<ul>
|
||||
<li>reading<br>
|
||||
returns the state of the arduino-pin. Values are 'on' and 'off'.<br></li>
|
||||
returns the logical state of the arduino-pin. Values are 'on' and 'off'.<br></li>
|
||||
<li>count<br>
|
||||
returns the current count. Contains the number of toggles of the arduino-pin.<br>
|
||||
Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li>
|
||||
<li>alarm<br>
|
||||
returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')<br>
|
||||
'alarm' doesn't clear itself, has to be set to 'off' eplicitly./li>
|
||||
<li>state<br>
|
||||
returns the 'state' reading</li>
|
||||
</ul><br>
|
||||
<a name="FRM_INattr"></a>
|
||||
<b>Attributes</b><br>
|
||||
<ul>
|
||||
<li>count-mode rising|falling|both<br>
|
||||
Determines whether 'rising' (transitions from 'off' to 'on') of falling (transitions from 'on' to 'off')<br>
|
||||
edges (or 'both') are counted. Defaults to 'rising'</li>
|
||||
<li>count-threshold <number><br>
|
||||
sets the theshold-value for the counter. Whenever 'count' reaches the 'count-threshold' 'alarm' is<br>
|
||||
set to 'on' and count is reset to 0. Use 'set alarm off' to clear the alarm.</li>
|
||||
<li><a href="#IODev">IODev</a><br>
|
||||
Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
|
||||
than one FRM-device defined.)
|
||||
|
Loading…
x
Reference in New Issue
Block a user