From fb20c32fd1a0f22c84e7799fdc1410cbd6b01837 Mon Sep 17 00:00:00 2001 From: ntruchsess Date: Mon, 17 Mar 2014 10:36:13 +0000 Subject: [PATCH] FRM_LCD,FRM_SERVO: cleanup exceptionhandling for initialization git-svn-id: https://svn.fhem.de/fhem/trunk@5242 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/20_FRM_LCD.pm | 212 +++++++++++++++++++------------------- fhem/FHEM/20_FRM_SERVO.pm | 53 +++++----- 2 files changed, 134 insertions(+), 131 deletions(-) diff --git a/fhem/FHEM/20_FRM_LCD.pm b/fhem/FHEM/20_FRM_LCD.pm index 13f5b6960..071a77584 100755 --- a/fhem/FHEM/20_FRM_LCD.pm +++ b/fhem/FHEM/20_FRM_LCD.pm @@ -77,11 +77,7 @@ FRM_LCD_Init($) # FRM_LCD_Apply_Attribute($name,"direction"); FRM_LCD_Apply_Attribute($name,"blink"); }; - if ($@) { - $@ =~ /^(.*)( at.*FHEM.*)$/; - $hash->{STATE} = "error initializing: ".$1; - return "error initializing '".$hash->{NAME}."': ".$1; - } + return FRM_Catch($@) if $@; } if (! (defined AttrVal($name,"stateFormat",undef))) { $main::attr{$name}{"stateFormat"} = "text"; @@ -90,8 +86,6 @@ FRM_LCD_Init($) if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") { FRM_LCD_Set($hash,$name,"text",$value); } - - return undef; } @@ -114,17 +108,17 @@ FRM_LCD_Attr($$$$) { } } }; - if ($@) { - $@ =~ /^(.*)( at.*FHEM.*)$/; - $hash->{STATE} = "error setting $attribute to $value: ".$1; - return "cannot $command attribute $attribute to $value for $name: ".$1; + my $ret = FRM_Catch($@) if $@; + if ($ret) { + $hash->{STATE} = "error setting $attribute to $value: ".$ret; + return "cannot $command attribute $attribute to $value for $name: ".$ret; } } sub FRM_LCD_Apply_Attribute { my ($name,$attribute) = @_; my $lcd = $main::defs{$name}{lcd}; - if (defined $lcd) { + if ($main::init_done and defined $lcd) { ATTRIBUTE_HANDLER: { $attribute eq "backLight" and do { if (AttrVal($name,"backLight","on") eq "on") { @@ -176,101 +170,105 @@ sub FRM_LCD_Set(@) { } my $lcd = $hash->{lcd}; return unless defined $lcd; - COMMAND_HANDLER: { - $command eq "text" and do { - shift @a; - shift @a; - $value = join(" ", @a); - if (AttrVal($hash->{NAME},"autoClear","on") eq "on") { - $lcd->clear(); - } - if (AttrVal($hash->{NAME},"autoBreak","on") eq "on") { - my $sizex = $hash->{sizex}; - my $sizey = $hash->{sizey}; - my $start = 0; - my $len = length $value; - for (my $line = 0;$line<$sizey;$line++) { - $lcd->setCursor(0,$line); - if ($start<$len) { - $lcd->print(substr $value, $start, $sizex); - } else { - last; - } - $start+=$sizex; - } - } else { - $lcd->print($value); - } - main::readingsSingleUpdate($hash,"text",$value,1); - last; - }; - $command eq "home" and do { - $lcd->home(); - last; - }; - $command eq "reset" and do { - $lcd->init(); -# $hash->{lcd} = $lcd; - last; - }; - $command eq "clear" and do { - $lcd->clear(); - main::readingsSingleUpdate($hash,"text","",1); - last; - }; - $command eq "display" and do { - if ($value ne "off") { - $lcd->display(); - } else { - $lcd->noDisplay(); - } - last; - }; - $command eq "cursor" and do { - my ($x,$y) = split ",",$value; - $lcd->setCursor($x,$y); - last; - }; - $command eq "scroll" and do { - if ($value eq "left") { - $lcd->scrollDisplayLeft(); - } else { - $lcd->scrollDisplayRight(); - } - last; - }; - $command eq "backlight" and do { - if ($value eq "on") { - $lcd->backlight(); - } else { - $lcd->noBacklight(); - } - last; - }; - $command eq "writeXY" and do { - my ($x,$y,$l,$al) = split(",",$value); - $lcd->setCursor($x,$y); - shift @a; shift @a; shift @a; - my $t = join(" ", @a); - my %umlaute = ("ä" => "ae", "Ä" => "Ae", "ü" => "ue", "Ü" => "Ue", "ö" => "oe", "Ö" => "Oe", "ß" => "ss" ," - " => " " ,"©"=>"@"); - my $umlautkeys = join ("|", keys(%umlaute)); - $t =~ s/($umlautkeys)/$umlaute{$1}/g; - my $sl = length $t; - if ($sl > $l) { - $t = substr($t,0,$l); - } - if ($sl < $l) { - my $dif = ""; - for (my $i=$sl; $i<$l; $i++) { - $dif .= " "; - } - $t = ($al eq "l") ? $t.$dif : $dif.$t; - } - $lcd->print($t); - readingsSingleUpdate($hash,"state",$t,1); - last; #"X=$x|Y=$y|L=$l|Text=$t"; - }; - } + eval { + COMMAND_HANDLER: { + $command eq "text" and do { + shift @a; + shift @a; + $value = join(" ", @a); + if (AttrVal($hash->{NAME},"autoClear","on") eq "on") { + $lcd->clear(); + } + if (AttrVal($hash->{NAME},"autoBreak","on") eq "on") { + my $sizex = $hash->{sizex}; + my $sizey = $hash->{sizey}; + my $start = 0; + my $len = length $value; + for (my $line = 0;$line<$sizey;$line++) { + $lcd->setCursor(0,$line); + if ($start<$len) { + $lcd->print(substr $value, $start, $sizex); + } else { + last; + } + $start+=$sizex; + } + } else { + $lcd->print($value); + } + main::readingsSingleUpdate($hash,"text",$value,1); + last; + }; + $command eq "home" and do { + $lcd->home(); + last; + }; + $command eq "reset" and do { + $lcd->init(); +# $hash->{lcd} = $lcd; + last; + }; + $command eq "clear" and do { + $lcd->clear(); + main::readingsSingleUpdate($hash,"text","",1); + last; + }; + $command eq "display" and do { + if ($value ne "off") { + $lcd->display(); + } else { + $lcd->noDisplay(); + } + last; + }; + $command eq "cursor" and do { + my ($x,$y) = split ",",$value; + $lcd->setCursor($x,$y); + last; + }; + $command eq "scroll" and do { + if ($value eq "left") { + $lcd->scrollDisplayLeft(); + } else { + $lcd->scrollDisplayRight(); + } + last; + }; + $command eq "backlight" and do { + if ($value eq "on") { + $lcd->backlight(); + } else { + $lcd->noBacklight(); + } + last; + }; + $command eq "writeXY" and do { + my ($x,$y,$l,$al) = split(",",$value); + $lcd->setCursor($x,$y); + shift @a; shift @a; shift @a; + my $t = join(" ", @a); + my %umlaute = ("ä" => "ae", "Ä" => "Ae", "ü" => "ue", "Ü" => "Ue", "ö" => "oe", "Ö" => "Oe", "ß" => "ss" ," - " => " " ,"©"=>"@"); + my $umlautkeys = join ("|", keys(%umlaute)); + $t =~ s/($umlautkeys)/$umlaute{$1}/g; + my $sl = length $t; + if ($sl > $l) { + $t = substr($t,0,$l); + } + if ($sl < $l) { + my $dif = ""; + for (my $i=$sl; $i<$l; $i++) { + $dif .= " "; + } + $t = ($al eq "l") ? $t.$dif : $dif.$t; + } + $lcd->print($t); + readingsSingleUpdate($hash,"state",$t,1); + last; #"X=$x|Y=$y|L=$l|Text=$t"; + }; + } + }; + return FRM_Catch($@) if $@; + return undef; } sub FRM_LCD_State($$$$) diff --git a/fhem/FHEM/20_FRM_SERVO.pm b/fhem/FHEM/20_FRM_SERVO.pm index 30f08efbc..eb3b0311d 100755 --- a/fhem/FHEM/20_FRM_SERVO.pm +++ b/fhem/FHEM/20_FRM_SERVO.pm @@ -39,14 +39,17 @@ FRM_SERVO_Initialize($) sub FRM_SERVO_Init($$) { - my ($hash,$args) = @_; - my $ret = FRM_Init_Pin_Client($hash,$args,PIN_SERVO); - return $ret if (defined $ret); - my $firmata = $hash->{IODev}->{FirmataDevice}; - $main::defs{$hash->{NAME}}{resolution}=$firmata->{metadata}{servo_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{servo_resolutions}); - FRM_SERVO_apply_attribute($hash,"max-pulse"); #sets min-pulse as well - main::readingsSingleUpdate($hash,"state","Initialized",1); - return undef; + my ($hash,$args) = @_; + my $ret = FRM_Init_Pin_Client($hash,$args,PIN_SERVO); + return $ret if (defined $ret); + eval { + my $firmata = FRM_Client_FirmataDevice($hash); + $hash->{resolution}=$firmata->{metadata}{servo_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{servo_resolutions}); + FRM_SERVO_apply_attribute($hash,"max-pulse"); #sets min-pulse as well + }; + return FRM_Catch($@) if $@; + main::readingsSingleUpdate($hash,"state","Initialized",1); + return undef; } sub @@ -63,29 +66,31 @@ FRM_SERVO_Attr($$$$) { } last; }; - $main::attr{$name}{$attribute}=$value; - if ( $attribute eq "min-pulse" || $attribute eq "max-pulse" ) { - FRM_SERVO_apply_attribute($main::defs{$name},$attribute); - } + ($attribute eq "min-pulse" || $attribute eq "max-pulse") and do { + if ($main::init_done) { + $main::attr{$name}{$attribute}=$value; + FRM_SERVO_apply_attribute($hash,$attribute); + } + last; + }; } } }; - if ($@) { - $@ =~ /^(.*)( at.*FHEM.*)$/; - $hash->{STATE} = "error setting $attribute to $value: ".$1; - return "cannot $command attribute $attribute to $value for $name: ".$1; + my $ret = FRM_Catch($@) if $@; + if ($ret) { + $hash->{STATE} = "error setting $attribute to $value: ".$ret; + return "cannot $command attribute $attribute to $value for $name: ".$ret; } + return undef; } sub FRM_SERVO_apply_attribute { - my ($hash,$attribute) = @_; - return unless (defined $hash->{IODev} and defined $hash->{IODev}->{FirmataDevice}); - my $firmata = $hash->{IODev}->{FirmataDevice}; - my $name = $hash->{NAME}; - if ( $attribute eq "min-pulse" || $attribute eq "max-pulse" ) { - # defaults are taken from: http://arduino.cc/en/Reference/ServoAttach - $firmata->servo_config($hash->{PIN},{min_pulse => main::AttrVal($name,"min-pulse",544), max_pulse => main::AttrVal($name,"max-pulse",2400)}); - } + my ($hash,$attribute) = @_; + if ( $attribute eq "min-pulse" || $attribute eq "max-pulse" ) { + my $name = $hash->{NAME}; + # defaults are taken from: http://arduino.cc/en/Reference/ServoAttach + FRM_Client_FirmataDevice($hash)->servo_config($hash->{PIN},{min_pulse => main::AttrVal($name,"min-pulse",544), max_pulse => main::AttrVal($name,"max-pulse",2400)}); + } } sub