From aaf763784a806ea7d0121f7cfe63c8d0620e717f Mon Sep 17 00:00:00 2001
From: ntruchsess <>
Date: Sat, 2 Feb 2013 22:48:54 +0000
Subject: [PATCH] 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
---
fhem/FHEM/20_FRM_AD.pm | 78 +++++++++++++++++++++++++++------
fhem/FHEM/20_FRM_IN.pm | 98 ++++++++++++++++++++++++++++++++++++------
2 files changed, 150 insertions(+), 26 deletions(-)
diff --git a/fhem/FHEM/20_FRM_AD.pm b/fhem/FHEM/20_FRM_AD.pm
index 9ea9be56a..4bdc51f6a 100755
--- a/fhem/FHEM/20_FRM_AD.pm
+++ b/fhem/FHEM/20_FRM_AD.pm
@@ -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
@@ -92,17 +128,35 @@ FRM_AD_Undef($$)
Set
Get
- - reading
- returns the voltage-level read on the arduino-pin. Values range from 0 to 1023.
+ - reading
+ returns the voltage-level read on the arduino-pin. Values range from 0 to 1023.
+ - alarm-upper-threshold
+ returns the current state of 'alarm-upper-threshold'. Values are 'on' and 'off' (Defaults to 'off')
+ 'alarm-upper-threshold' turns 'on' whenever the 'reading' is higher than the attribute 'upper-threshold'
+ it turns 'off' again as soon 'reading' falls below 'alarm-upper-threshold'
+ - alarm-lower-threshold
+ returns the current state of 'alarm-lower-threshold'. Values are 'on' and 'off' (Defaults to 'off')
+ 'alarm-lower-threshold' turns 'on' whenever the 'reading' is lower than the attribute 'lower-threshold'
+ it turns 'off' again as soon 'reading rises above 'alarm-lower-threshold'
+ - state
+ returns the 'state' reading
Attributes
+ - upper-threshold
+ sets the 'upper-threshold'. Whenever the 'reading' exceeds this value 'alarm-upper-threshold' is set to 'on'
+ As soon 'reading' falls below the 'upper-threshold' 'alarm-upper-threshold' turns 'off' again
+ Defaults to 1024.
+ - lower-threshold
+ sets the 'lower-threshold'. Whenever the 'reading' falls below this value 'alarm-lower-threshold' is set to 'on'
+ As soon 'reading' rises above the 'lower-threshold' 'alarm-lower-threshold' turns 'off' again
+ Defaults to -1.
- IODev
Specify which FRM to use. (Optional, only required if there is more
than one FRM-device defined.)
diff --git a/fhem/FHEM/20_FRM_IN.pm b/fhem/FHEM/20_FRM_IN.pm
index 4cc588207..a765f7404 100755
--- a/fhem/FHEM/20_FRM_IN.pm
+++ b/fhem/FHEM/20_FRM_IN.pm
@@ -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};
+ my ($hash, @a) = @_;
+ 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($$)
Set
- N/A
+ - alarm on|off
+ set the alarm to on or off. Used to clear the alarm.
+ The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.
Get
- - reading
- returns the state of the arduino-pin. Values are 'on' and 'off'.
+ - reading
+ returns the logical state of the arduino-pin. Values are 'on' and 'off'.
+ - count
+ returns the current count. Contains the number of toggles of the arduino-pin.
+ Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.
+ - alarm
+ returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')
+ 'alarm' doesn't clear itself, has to be set to 'off' eplicitly./li>
+ - state
+ returns the 'state' reading
Attributes
+ - count-mode rising|falling|both
+ Determines whether 'rising' (transitions from 'off' to 'on') of falling (transitions from 'on' to 'off')
+ edges (or 'both') are counted. Defaults to 'rising'
+ - count-threshold <number>
+ sets the theshold-value for the counter. Whenever 'count' reaches the 'count-threshold' 'alarm' is
+ set to 'on' and count is reset to 0. Use 'set alarm off' to clear the alarm.
- IODev
Specify which FRM to use. (Optional, only required if there is more
than one FRM-device defined.)