From f498753fba1d4d4bf84014e0f84aa0bb39581a58 Mon Sep 17 00:00:00 2001 From: ntruchsess <> Date: Sun, 16 Mar 2014 12:47:53 +0000 Subject: [PATCH] FRM_IN: fix attribute 'internal-pullup' and 'activeLow' if device is not connected at startup git-svn-id: https://svn.fhem.de/fhem/trunk@5234 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/10_FRM.pm | 9 +++++++++ fhem/FHEM/20_FRM_IN.pm | 22 +++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/fhem/FHEM/10_FRM.pm b/fhem/FHEM/10_FRM.pm index 51613a34b..bcda36ee7 100755 --- a/fhem/FHEM/10_FRM.pm +++ b/fhem/FHEM/10_FRM.pm @@ -567,6 +567,15 @@ sub FRM_Client_FirmataDevice($) { return $iodev->{FirmataDevice}; } +sub FRM_Catch($) { + my $exception = shift; + if ($exception) { + $exception =~ /^(.*)( at.*FHEM.*)$/; + return $1; + } + return undef; +} + package Firmata_IO; sub new { diff --git a/fhem/FHEM/20_FRM_IN.pm b/fhem/FHEM/20_FRM_IN.pm index ab6ab416b..dc6a8f0df 100755 --- a/fhem/FHEM/20_FRM_IN.pm +++ b/fhem/FHEM/20_FRM_IN.pm @@ -59,6 +59,7 @@ FRM_IN_Init($$) } $firmata->observe_digital($pin,\&FRM_IN_observer,$hash); }; + return FRM_Catch($@) if $@; if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) { $main::attr{$hash->{NAME}}{"stateFormat"} = "reading"; } @@ -192,17 +193,21 @@ FRM_IN_Attr($$$$) { last; }; $attribute eq "internal-pullup" and do { - my $firmata = FRM_Client_FirmataDevice($hash); - $firmata->digital_write($pin,$value eq "on" ? 1 : 0); - #ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called. + if ($main::init_done) { + my $firmata = FRM_Client_FirmataDevice($hash); + $firmata->digital_write($pin,$value eq "on" ? 1 : 0); + #ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called. + } last; }; $attribute eq "activeLow" and do { my $oldval = AttrVal($hash->{NAME},"activeLow","no"); if ($oldval ne $value) { $main::attr{$hash->{NAME}}{activeLow} = $value; - my $firmata = FRM_Client_FirmataDevice($hash); - FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash); + if ($main::init_done) { + my $firmata = FRM_Client_FirmataDevice($hash); + FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash); + } }; last; }; @@ -225,10 +230,9 @@ FRM_IN_Attr($$$$) { } } }; - if ($@) { - $@ =~ /^(.*)( at.*FHEM.*)$/; - $hash->{STATE} = "error setting $attribute to $value: ".$1; - return "cannot $command attribute $attribute to $value for $name: ".$1; + if (my $error = FRM_Catch($@)) { + $hash->{STATE} = "error setting $attribute to $value: ".$error; + return "cannot $command attribute $attribute to $value for $name: ".$error; } }