From 120cb5bd16a2d86aa353d335d793a2cf3f646816 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Mon, 1 Dec 2014 14:13:26 +0000 Subject: [PATCH] FRM_ROTENC: add statefull offset git-svn-id: https://svn.fhem.de/fhem/trunk@7111 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/20_FRM_ROTENC.pm | 57 +++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/fhem/FHEM/20_FRM_ROTENC.pm b/fhem/FHEM/20_FRM_ROTENC.pm index 5f7e38ec5..753316185 100755 --- a/fhem/FHEM/20_FRM_ROTENC.pm +++ b/fhem/FHEM/20_FRM_ROTENC.pm @@ -21,10 +21,13 @@ use Device::Firmata::Constants qw/ :all /; my %sets = ( "reset" => "noArg", + "offset"=> "", ); my %gets = ( "position" => "noArg", + "offset" => "noArg", + "value" => "noArg", ); sub @@ -38,7 +41,8 @@ FRM_ROTENC_Initialize($) $hash->{DefFn} = "FRM_Client_Define"; $hash->{InitFn} = "FRM_ROTENC_Init"; $hash->{UndefFn} = "FRM_ROTENC_Undef"; - + $hash->{StateFn} = "FRM_ROTENC_State"; + $hash->{AttrList} = "IODev $main::readingFnAttributes"; main::LoadModule("FRM"); } @@ -53,6 +57,7 @@ FRM_ROTENC_Init($$) my $pinA = @$args[0]; my $pinB = @$args[1]; my $encoder = defined @$args[2] ? @$args[2] : 0; + my $name = $hash->{NAME}; $hash->{PINA} = $pinA; $hash->{PINB} = $pinB; @@ -68,12 +73,15 @@ FRM_ROTENC_Init($$) if ($@) { $@ =~ /^(.*)( at.*FHEM.*)$/; $hash->{STATE} = "error initializing: ".$1; - return "error initializing '".$hash->{NAME}."': ".$1; + return "error initializing '$name': $1"; } - if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) { - $main::attr{$hash->{NAME}}{"stateFormat"} = "position"; + if (! (defined AttrVal($name,"stateFormat",undef))) { + $main::attr{$name}{"stateFormat"} = "position"; } + + $hash->{offset} = ReadingsVal($name,"position",0); + main::readingsSingleUpdate($hash,"state","Initialized",1); return undef; } @@ -83,9 +91,10 @@ FRM_ROTENC_observer { my ( $encoder, $value, $hash ) = @_; my $name = $hash->{NAME}; - Log3 $name,5,"onEncoderMessage for pins ".$hash->{PINA}.",".$hash->{PINB}." encoder: ".$encoder." position: ".$value."\n"; + Log3 ($name,5,"onEncoderMessage for pins ".$hash->{PINA}.",".$hash->{PINB}." encoder: ".$encoder." position: ".$value."\n"); main::readingsBeginUpdate($hash); - main::readingsBulkUpdate($hash,"position",$value, 1); + main::readingsBulkUpdate($hash,"position",$value+$hash->{offset}, 1); + main::readingsBulkUpdate($hash,"value",$value, 1); main::readingsEndUpdate($hash,1); } @@ -108,6 +117,15 @@ FRM_ROTENC_Set eval { FRM_Client_FirmataDevice($hash)->encoder_reset_position($hash->{ENCODERNUM}); }; + main::readingsBeginUpdate($hash); + main::readingsBulkUpdate($hash,"position",$hash->{offset},1); + main::readingsBulkUpdate($hash,"value",0,1); + main::readingsEndUpdate($hash,1); + last; + }; + $command eq "offset" and do { + $hash->{offset} = $value; + readingsSingleUpdate($hash,"position",ReadingsVal($hash->{NAME},"value",0)+$value,1); last; }; } @@ -133,6 +151,12 @@ FRM_ROTENC_Get($) $cmd eq "position" and do { return ReadingsVal($hash->{NAME},"position","0"); }; + $cmd eq "offset" and do { + return $hash->{offset}; + }; + $cmd eq "value" and do { + return ReadingsVal($hash->{NAME},"value","0"); + }; } return undef; } @@ -185,6 +209,16 @@ FRM_ROTENC_Undef($$) return undef; } +sub +FRM_ROTENC_State($$$$) +{ + my ($hash, $tim, $sname, $sval) = @_; + if ($sname eq "position") { + $hash->{offset} = $sval; + } + return undef; +} + 1; =pod @@ -209,11 +243,20 @@ FRM_ROTENC_Undef($$) Set
  • reset
    resets to value of 'position' to 0
  • +
  • offset <value>
    + set offset value of 'position'
  • Get
    Attributes