2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-10 03:06:37 +00:00

rework assoziation of IODev to FRM clients

git-svn-id: https://svn.fhem.de/fhem/trunk@4132 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ntruchsess 2013-10-29 23:34:43 +00:00
parent e74cc2d176
commit b6de5baa16
9 changed files with 222 additions and 52 deletions

View File

@ -194,11 +194,8 @@ sub OWX_Define ($$) {
#-- check if we are connecting to Arduino (via FRM): #-- check if we are connecting to Arduino (via FRM):
} elsif ($dev =~ /^\d{1,2}$/) { } elsif ($dev =~ /^\d{1,2}$/) {
$hash->{INTERFACE} = "firmata"; $hash->{INTERFACE} = "firmata";
if (defined $main::modules{FRM}) { main::LoadModule("FRM");
FRM_Client_Define($hash,$def); FRM_Client_Define($hash,$def);
} else {
Log 1,"module FRM not yet loaded, please define an FRM device first.";
}
} else { } else {
$hash->{DeviceName} = $dev; $hash->{DeviceName} = $dev;
#-- Second step in case of CUNO: See if we can open it #-- Second step in case of CUNO: See if we can open it

View File

@ -387,7 +387,7 @@ FRM_forall_clients($$$)
} }
sub sub
FRM_Init_Client($$) { FRM_Init_Client($@) {
my ($hash,$args) = @_; my ($hash,$args) = @_;
if (!defined $args and defined $hash->{DEF}) { if (!defined $args and defined $hash->{DEF}) {
my @a = split("[ \t][ \t]*", $hash->{DEF}); my @a = split("[ \t][ \t]*", $hash->{DEF});
@ -406,25 +406,15 @@ FRM_Init_Pin_Client($$$) {
return $u unless defined $args and int(@$args) > 0; return $u unless defined $args and int(@$args) > 0;
my $pin = @$args[0]; my $pin = @$args[0];
foreach my $d ( sort keys %main::defs ) {
if ( defined( my $dev = $main::defs{$d} )) {
if ( $dev != $hash
&& defined( $dev->{IODev} )
&& defined( $dev->{PIN} )
&& $dev->{IODev} == $hash->{IODev}
&& $dev->{PIN} == $pin ) {
die "Device $main::defs{$d}{NAME} allready defined for pin $pin";
}
}
}
$hash->{PIN} = $pin; $hash->{PIN} = $pin;
eval { eval {
FRM_Client_AssignIOPort($hash);
FRM_Client_FirmataDevice($hash)->pin_mode($pin,$mode); FRM_Client_FirmataDevice($hash)->pin_mode($pin,$mode);
}; };
if ($@) { if ($@) {
main::Log(2,"FRM_Init error setting pin_mode: ".$@); $@ =~ /^(.*)( at.*FHEM.*)$/;
#FRM_Client_Unassign($hash); $hash->{STATE} = "error initializing: ".$1;
return "error setting ".$hash->{NAME}." pin_mode for pin ".$pin; return "error initializing '".$hash->{NAME}."': ".$1;
} }
return undef; return undef;
} }
@ -437,10 +427,6 @@ FRM_Client_Define($$)
$hash->{STATE}="defined"; $hash->{STATE}="defined";
AssignIoPort($hash);
if ( defined($hash->{IODev}) && defined($hash->{IODev}->{SNAME})) {
$hash->{IODev} = $main::defs{$hash->{IODev}->{SNAME}};
}
eval { eval {
FRM_Init_Client($hash,[@a[2..scalar(@a)-1]]); FRM_Init_Client($hash,[@a[2..scalar(@a)-1]]);
}; };
@ -474,6 +460,28 @@ FRM_Client_Unassign($)
$dev->{STATE}="defined"; $dev->{STATE}="defined";
} }
sub
FRM_Client_AssignIOPort($)
{
my $hash = shift;
AssignIoPort($main::defs{$hash->{NAME}});
die "unable to assign IODev to '$hash->{NAME}'" unless defined ($hash->{IODev});
$hash->{IODev} = $main::defs{$hash->{IODev}->{SNAME}} if (defined($hash->{IODev}->{SNAME}));
foreach my $d ( sort keys %main::defs ) {
if ( defined( my $dev = $main::defs{$d} )) {
if ( $dev != $hash
&& defined( $dev->{IODev} )
&& defined( $dev->{PIN} )
&& $dev->{IODev} == $hash->{IODev}
&& $dev->{PIN} == $hash->{PIN} ) {
die "Device $main::defs{$d}{NAME} allready defined for pin $hash->{PIN}";
}
}
}
}
sub FRM_Client_FirmataDevice($) { sub FRM_Client_FirmataDevice($) {
my $hash = shift; my $hash = shift;
my $iodev = $hash->{IODev}; my $iodev = $hash->{IODev};

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -20,11 +29,13 @@ FRM_AD_Initialize($)
{ {
my ($hash) = @_; my ($hash) = @_;
$hash->{AttrFn} = "FRM_AD_Attr";
$hash->{GetFn} = "FRM_AD_Get"; $hash->{GetFn} = "FRM_AD_Get";
$hash->{DefFn} = "FRM_Client_Define"; $hash->{DefFn} = "FRM_Client_Define";
$hash->{InitFn} = "FRM_AD_Init"; $hash->{InitFn} = "FRM_AD_Init";
$hash->{AttrList} = "IODev upper-threshold lower-threshold loglevel:0,1,2,3,4,5,6 $main::readingFnAttributes"; $hash->{AttrList} = "IODev upper-threshold lower-threshold loglevel:0,1,2,3,4,5,6 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -99,6 +110,23 @@ FRM_AD_Get($)
return undef; return undef;
} }
sub
FRM_AD_Attr($$$$) {
my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") {
ARGUMENT_HANDLER: {
$attribute eq "IODev" and do {
my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
}
}
}
1; 1;
=pod =pod

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -18,6 +27,7 @@ FRM_I2C_Initialize($)
$hash->{AttrFn} = "FRM_I2C_Attr"; $hash->{AttrFn} = "FRM_I2C_Attr";
$hash->{AttrList} = "IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -42,12 +52,22 @@ FRM_I2C_Init($)
return undef; return undef;
} }
sub FRM_I2C_Attr(@) { sub
my ($command,$name,$attribute,$value) = @_; FRM_I2C_Attr($$$$) {
my $hash = $main::defs{$name}; my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") { if ($command eq "set") {
$main::attr{$name}{$attribute}=$value; ARGUMENT_HANDLER: {
} $attribute eq "IODev" and do {
my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$main::attr{$name}{$attribute}=$value;
}
}
} }
1; 1;

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -33,6 +42,7 @@ FRM_IN_Initialize($)
$hash->{UndefFn} = "FRM_Client_Undef"; $hash->{UndefFn} = "FRM_Client_Undef";
$hash->{AttrList} = "IODev count-mode:none,rising,falling,both count-threshold reset-on-threshold-reached:yes,no internal-pullup:on,off loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "IODev count-mode:none,rising,falling,both count-threshold reset-on-threshold-reached:yes,no internal-pullup:on,off loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -138,6 +148,14 @@ FRM_IN_Attr($$$$) {
my ($command,$name,$attribute,$value) = @_; my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") { if ($command eq "set") {
ARGUMENT_HANDLER: { ARGUMENT_HANDLER: {
$attribute eq "IODev" and do {
my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$attribute eq "count-mode" and do { $attribute eq "count-mode" and do {
if ($value ne "none" and !defined main::ReadingsVal($name,"count",undef)) { if ($value ne "none" and !defined main::ReadingsVal($name,"count",undef)) {
main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1); main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -33,6 +42,7 @@ FRM_LCD_Initialize($)
$hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev model backLight:on,off blink:on,off autoClear:on,off autoBreak:on,off loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev model backLight:on,off blink:on,off autoClear:on,off autoBreak:on,off loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
# autoScroll:on,off direction:leftToRight,rightToLeft do not work reliably # autoScroll:on,off direction:leftToRight,rightToLeft do not work reliably
main::LoadModule("FRM");
} }
sub sub
@ -75,13 +85,23 @@ FRM_LCD_Init($)
return undef; return undef;
} }
sub FRM_LCD_Attr(@) { sub
my ($command,$name,$attribute,$value) = @_; FRM_LCD_Attr($$$$) {
my $hash = $main::defs{$name}; my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") { if ($command eq "set") {
$main::attr{$name}{$attribute}=$value; ARGUMENT_HANDLER: {
FRM_LCD_Apply_Attribute($name,$attribute); $attribute eq "IODev" and do {
} my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$main::attr{$name}{$attribute}=$value;
FRM_LCD_Apply_Attribute($name,$attribute);
}
}
} }
sub FRM_LCD_Apply_Attribute { sub FRM_LCD_Apply_Attribute {

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
use SetExtensions; use SetExtensions;
@ -17,9 +26,11 @@ FRM_OUT_Initialize($)
$hash->{DefFn} = "FRM_Client_Define"; $hash->{DefFn} = "FRM_Client_Define";
$hash->{InitFn} = "FRM_OUT_Init"; $hash->{InitFn} = "FRM_OUT_Init";
$hash->{UndefFn} = "FRM_Client_Undef"; $hash->{UndefFn} = "FRM_Client_Undef";
$hash->{AttrFn} = "FRM_OUT_Attr";
$hash->{StateFn} = "FRM_OUT_State"; $hash->{StateFn} = "FRM_OUT_State";
$hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -74,6 +85,24 @@ STATEHANDLER: {
} }
} }
sub
FRM_OUT_Attr($$$$) {
my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") {
ARGUMENT_HANDLER: {
$attribute eq "IODev" and do {
my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$main::attr{$name}{$attribute}=$value;
}
}
}
1; 1;
=pod =pod

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -21,9 +30,11 @@ FRM_PWM_Initialize($)
$hash->{DefFn} = "FRM_Client_Define"; $hash->{DefFn} = "FRM_Client_Define";
$hash->{InitFn} = "FRM_PWM_Init"; $hash->{InitFn} = "FRM_PWM_Init";
$hash->{UndefFn} = "FRM_Client_Undef"; $hash->{UndefFn} = "FRM_Client_Undef";
$hash->{AttrFn} = "FRM_PWM_Attr";
$hash->{StateFn} = "FRM_PWM_State"; $hash->{StateFn} = "FRM_PWM_State";
$hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -77,6 +88,24 @@ STATEHANDLER: {
} }
} }
sub
FRM_PWM_Attr($$$$) {
my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") {
ARGUMENT_HANDLER: {
$attribute eq "IODev" and do {
my $hash = $main::defs{$name};
if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$main::attr{$name}{$attribute}=$value;
}
}
}
1; 1;
=pod =pod

View File

@ -3,7 +3,16 @@ package main;
use strict; use strict;
use warnings; use warnings;
use Device::Firmata;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) {
push @INC,$inc."/lib";
};
};
};
use Device::Firmata::Constants qw/ :all /; use Device::Firmata::Constants qw/ :all /;
##################################### #####################################
@ -24,6 +33,7 @@ FRM_SERVO_Initialize($)
$hash->{AttrFn} = "FRM_SERVO_Attr"; $hash->{AttrFn} = "FRM_SERVO_Attr";
$hash->{AttrList} = "min-pulse max-pulse IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes"; $hash->{AttrList} = "min-pulse max-pulse IODev loglevel:0,1,2,3,4,5 $main::readingFnAttributes";
main::LoadModule("FRM");
} }
sub sub
@ -39,14 +49,25 @@ FRM_SERVO_Init($$)
return undef; return undef;
} }
sub FRM_SERVO_Attr(@) { sub
my ($command,$name,$attribute,$value) = @_; FRM_SERVO_Attr($$$$) {
if ($command eq "set") { my ($command,$name,$attribute,$value) = @_;
$main::attr{$name}{$attribute}=$value; if ($command eq "set") {
if ( $attribute eq "min-pulse" || $attribute eq "max-pulse" ) { ARGUMENT_HANDLER: {
FRM_SERVO_apply_attribute($main::defs{$name},$attribute); $attribute eq "IODev" and do {
} my $hash = $main::defs{$name};
} if (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value) {
$hash->{IODev} = $defs{$value};
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
$main::attr{$name}{$attribute}=$value;
if ( $attribute eq "min-pulse" || $attribute eq "max-pulse" ) {
FRM_SERVO_apply_attribute($main::defs{$name},$attribute);
}
}
}
} }
sub FRM_SERVO_apply_attribute { sub FRM_SERVO_apply_attribute {