diff --git a/FHEM/20_FRM_OUT.pm b/FHEM/20_FRM_OUT.pm
index 37fc07353..3ac93ab0f 100755
--- a/FHEM/20_FRM_OUT.pm
+++ b/FHEM/20_FRM_OUT.pm
@@ -128,6 +128,9 @@ FRM_OUT_Attr($$$$) {
+
Get
diff --git a/FHEM/20_FRM_PWM.pm b/FHEM/20_FRM_PWM.pm
index 3cc197782..219531d8c 100755
--- a/FHEM/20_FRM_PWM.pm
+++ b/FHEM/20_FRM_PWM.pm
@@ -14,11 +14,25 @@ BEGIN {
};
use Device::Firmata::Constants qw/ :all /;
+use SetExtensions qw/ :all /;
#####################################
+my %gets = (
+ "dim" => 0,
+ "value" => 0,
+ "devStateIcon" => 0,
+);
+
my %sets = (
- "value" => "",
+ "on" => 0,
+ "off" => 0,
+ "toggle" => 0,
+ "value" => 1,
+ "dim:slider,0,1,100" => 1,
+ "fadeTo" => 2,
+ "dimUp" => 0,
+ "dimDown" => 0,
);
sub
@@ -27,6 +41,7 @@ FRM_PWM_Initialize($)
my ($hash) = @_;
$hash->{SetFn} = "FRM_PWM_Set";
+ $hash->{GetFn} = "FRM_PWM_Get";
$hash->{DefFn} = "FRM_Client_Define";
$hash->{InitFn} = "FRM_PWM_Init";
$hash->{UndefFn} = "FRM_Client_Undef";
@@ -45,7 +60,10 @@ FRM_PWM_Init($$)
return $ret if (defined $ret);
my $firmata = $hash->{IODev}->{FirmataDevice};
my $name = $hash->{NAME};
- $main::defs{$name}{resolution}=$firmata->{metadata}{pwm_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{pwm_resolutions});
+ my $resolution = $firmata->{metadata}{pwm_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{pwm_resolutions});
+ $hash->{".max"} = defined $resolution ? (1<<$resolution)-1 : 255;
+ $hash->{".dim"} = 0;
+ $hash->{".toggle"} = "off";
if (! (defined AttrVal($name,"stateFormat",undef))) {
$main::attr{$name}{"stateFormat"} = "value";
}
@@ -60,36 +78,173 @@ FRM_PWM_Init($$)
sub
FRM_PWM_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];
- my $iodev = $hash->{IODev};
- eval {
- FRM_Client_FirmataDevice($hash)->analog_write($hash->{PIN},$value);
- main::readingsSingleUpdate($hash,"value",$value, 1);
- };
- return $@;
-}
+ my ($hash, $name, $cmd, @a) = @_;
+
+ my @match = grep( $_ =~ /^$cmd($|:)/, keys %sets );
+ #-- check argument
+ return SetExtensions($hash, join(" ", keys %sets), $name, $cmd, @a) unless @match == 1;
+ return "$cmd expects $sets{$match[0]} parameters" unless (@a eq $sets{$match[0]});
-sub FRM_PWM_State($$$$)
-{
- my ($hash, $tim, $sname, $sval) = @_;
-
-STATEHANDLER: {
- $sname eq "value" and do {
- if (AttrVal($hash->{NAME},"restoreOnStartup","on") eq "on") {
- FRM_PWM_Set($hash,$hash->{NAME},$sval);
- }
- last;
- }
+ eval {
+ SETHANDLER: {
+ my $value = $a[0] if @a;
+ $cmd eq "on" and do {
+ FRM_PWM_writeOut($hash,$hash->{".max"});
+ $hash->{".toggle"} = "on";
+ last;
+ };
+ $cmd eq "off" and do {
+ FRM_PWM_writeOut($hash,0);
+ $hash->{".toggle"} = "off";
+ last;
+ };
+ $cmd eq "toggle" and do {
+ my $toggle = $hash->{".toggle"};
+ TOGGLEHANDLER: {
+ $toggle eq "off" and do {
+ FRM_PWM_writeOut($hash,$hash->{".dim"});
+ $hash->{".toggle"} = "up";
+ last;
+ };
+ $toggle eq "up" and do {
+ FRM_PWM_writeOut($hash,$hash->{".max"});
+ $hash->{".toggle"} = "on";
+ last;
+ };
+ $toggle eq "on" and do {
+ FRM_PWM_writeOut($hash,$hash->{".dim"});
+ $hash->{".toggle"} = "down";
+ last;
+ };
+ $toggle eq "down" and do {
+ FRM_PWM_writeOut($hash,0);
+ $hash->{".toggle"} = "off";
+ last;
+ };
+ };
+ last;
+ };
+ $cmd eq "value" and do {
+ my $max = $hash->{".max"};
+ die "maximum value of $max exceeded: $value" if ($value > $max);
+ FRM_PWM_writeOut($hash,$value);
+ TOGGLEHANDLER: {
+ $value == $max and do {
+ $hash->{".toggle"} = "on";
+ last;
+ };
+ $value == 0 and do {
+ $hash->{".toggle"} = "off";
+ last;
+ };
+ $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
+ $hash->{".dim"} = $value;
+ };
+ last;
+ };
+ $cmd eq "dim" and do {
+ die "maximum value of 100 exceeded: $value" if ($value > 100);
+ my $dim = int($hash->{".max"}*$value/100);
+ FRM_PWM_writeOut($hash,$dim);
+ TOGGLEHANDLER: {
+ $value == 100 and do {
+ $hash->{".toggle"} = "on";
+ last;
+ };
+ $value == 0 and do {
+ $hash->{".toggle"} = "off";
+ last;
+ };
+ $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
+ $hash->{".dim"} = $dim;
+ };
+ last;
+ };
+ $cmd eq "fadeTo" and do {
+ die "fadeTo not implemented yet";
+ };
+ $cmd eq "dimUp" and do {
+ my $dim = $hash->{".dim"};
+ my $max = $hash->{".max"};
+ if ($dim > $max * 0.9) {
+ $dim = $max;
+ $hash->{".toggle"} = "on";
+ } else {
+ $dim = $dim + $max / 10;
+ $hash->{".toggle"} = "up" unless $hash->{".toggle"} eq "down";
+ }
+ FRM_PWM_writeOut($hash,$dim);
+ $hash->{".dim"} = $dim;
+ last;
+ };
+ $cmd eq "dimDown" and do {
+ my $step = $hash->{".max"} / 10;
+ my $dim = $hash->{".dim"};
+ if ($dim < $step) {
+ $dim = 0;
+ $hash->{".toggle"} = "off";
+ } else {
+ $dim = $dim - $step;
+ $hash->{".toggle"} = "down" unless $hash->{".toggle"} eq "up";
+ }
+ FRM_PWM_writeOut($hash,$dim);
+ $hash->{".dim"} = $dim;
+ last;
+ };
+ }
+ };
+ if ($@) {
+ $@ =~ /^(.*)( at.*FHEM.*)$/;
+ $hash->{STATE} = "error setting '$cmd': ".(defined $1 ? $1 : $@);
+ return "error setting '$hash->{NAME} $cmd': ".(defined $1 ? $1 : $@);
}
+ return undef;
}
sub
-FRM_PWM_Attr($$$$) {
+FRM_PWM_writeOut($$)
+{
+ my ($hash,$value) = @_;
+ FRM_Client_FirmataDevice($hash)->analog_write($hash->{PIN},$value);
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"value",$value, 1);
+ readingsBulkUpdate($hash,"dim",int($value*100/$hash->{".max"}), 1);
+ readingsEndUpdate($hash, 1);
+}
+
+sub
+FRM_PWM_Get($@)
+{
+ my ($hash, $name, $cmd, @a) = @_;
+
+ return "FRM_PWM: Get with unknown argument $cmd, choose one of ".join(" ", sort keys %gets)
+ unless defined($gets{$cmd});
+
+ GETHANDLER: {
+ $cmd eq 'dim' and do {
+ return ReadingsVal($name,"dim",undef);
+ };
+ $cmd eq 'value' and do {
+ return ReadingsVal($name,"value",undef);
+ };
+ $cmd eq 'devStateIcon' and do {
+ return return "not implemented yet";
+ };
+ }
+}
+
+sub
+FRM_PWM_State($$$$)
+{
+ my ($hash, $tim, $sname, $sval) = @_;
+ if ($sname eq "value") {
+ FRM_PWM_Set($hash,$hash->{NAME},$sname,$sval);
+ }
+}
+
+sub
+FRM_PWM_Attr($$$$)
+{
my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") {
ARGUMENT_HANDLER: {
@@ -130,9 +285,37 @@ FRM_PWM_Attr($$$$) {
Set
+ set <name> on
+ sets the pulse-width to 100%
+
+
+ set <name> off
+ sets the pulse-width to 0%
+
+
+
+ set <name> toggle
+ toggles the pulse-width in between to the last value set by 'value' or 'dim' and 0 respectivly 100%
+
+
set <name> value <value>
- sets the pulse-width of the signal that is output on the configured arduino pin
- Range is from 0 to 255 (see analogWrite() for details)
+ sets the pulse-width to the value specified
+ Range is from 0 to 255 (for 8-bit resolution) (see analogWrite() for details)
+
+
+ set <name> dim <value>
+ sets the pulse-width to the value specified in percent
+ Range is from 0 to 100
+
+
+ set <name> dimUp
+ increases the pulse-width by 10%
+
+
+ set <name> dimDown
+ decreases the pulse-width by 10%
Get
diff --git a/FHEM/20_FRM_RGB.pm b/FHEM/20_FRM_RGB.pm
index ef648b405..14fe3740e 100644
--- a/FHEM/20_FRM_RGB.pm
+++ b/FHEM/20_FRM_RGB.pm
@@ -73,29 +73,26 @@ FRM_RGB_Init($$)
my $ret = FRM_Init_Pin_Client($hash,$args,PIN_PWM);
return $ret if (defined $ret);
my @pins = ();
- eval {
- my $firmata = FRM_Client_FirmataDevice($hash);
- $hash->{PIN} = "";
- foreach my $pin (@{$args}) {
- $firmata->pin_mode($pin,PIN_PWM);
- push @pins,{
- pin => $pin,
- "shift" => defined $firmata->{metadata}{pwm_resolutions} ? $firmata->{metadata}{pwm_resolutions}{$pin}-8 : 0,
- };
- $hash->{PIN} .= $hash->{PIN} eq "" ? $pin : " $pin";
- }
- $hash->{PINS} = \@pins;
- if (! (defined AttrVal($name,"stateFormat",undef))) {
- $attr{$name}{"stateFormat"} = "rgb";
- }
- my $value = ReadingsVal($name,"rgb",undef);
- if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") {
- FRM_RGB_Set($hash,$name,"rgb",$value);
- }
- };
- return $@ if $@;
+ my $firmata = FRM_Client_FirmataDevice($hash);
+ $hash->{PIN} = "";
+ foreach my $pin (@{$args}) {
+ $firmata->pin_mode($pin,PIN_PWM);
+ push @pins,{
+ pin => $pin,
+ "shift" => defined $firmata->{metadata}{pwm_resolutions} ? $firmata->{metadata}{pwm_resolutions}{$pin}-8 : 0,
+ };
+ $hash->{PIN} .= $hash->{PIN} eq "" ? $pin : " $pin";
+ }
+ $hash->{PINS} = \@pins;
+ if (! (defined AttrVal($name,"stateFormat",undef))) {
+ $attr{$name}{"stateFormat"} = "rgb";
+ }
+ my $value = ReadingsVal($name,"rgb",undef);
+ if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") {
+ FRM_RGB_Set($hash,$name,"rgb",$value);
+ }
$hash->{toggle} = "off";
- $hash->{dim} = {
+ $hash->{".dim"} = {
bri => 50,
channels => [(255) x @{$hash->{PINS}}],
};
@@ -130,7 +127,7 @@ FRM_RGB_Set($@)
TOGGLEHANDLER: {
$toggle eq "off" and do {
$hash->{toggle} = "up";
- FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{dim}));
+ FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{".dim"}));
last;
};
$toggle eq "up" and do {
@@ -140,7 +137,7 @@ FRM_RGB_Set($@)
};
$toggle eq "on" and do {
$hash->{toggle} = "down";
- FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{dim}));
+ FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{".dim"}));
last;
};
$toggle eq "down" and do {
@@ -169,30 +166,30 @@ FRM_RGB_Set($@)
};
$hash->{toggle} = "up";
};
- $hash->{dim} = ChannelsToBrightness(@channels);
+ $hash->{".dim"} = ChannelsToBrightness(@channels);
last;
};
$cmd eq "pct" and do {
- $hash->{dim}->{bri} = $a[0];
- FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{dim}));
+ $hash->{".dim"}->{bri} = $a[0];
+ FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{".dim"}));
last;
};
$cmd eq "dimUp" and do {
- $hash->{dim}->{bri} = $hash->{dim}->{bri} > 90 ? 100 : $hash->{dim}->{bri}+10;
- FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{dim}));
+ $hash->{".dim"}->{bri} = $hash->{".dim"}->{bri} > 90 ? 100 : $hash->{".dim"}->{bri}+10;
+ FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{".dim"}));
last;
};
$cmd eq "dimDown" and do {
- $hash->{dim}->{bri} = $hash->{dim}->{bri} < 10 ? 0 : $hash->{dim}->{bri}-10;
- FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{dim}));
+ $hash->{".dim"}->{bri} = $hash->{".dim"}->{bri} < 10 ? 0 : $hash->{".dim"}->{bri}-10;
+ FRM_RGB_SetChannels($hash,BrightnessToChannels($hash->{".dim"}));
last;
};
}
};
if ($@) {
$@ =~ /^(.*)( at.*FHEM.*)$/;
- $hash->{STATE} = "error setting '$cmd': ".$1;
- return "error setting '$hash->{NAME} $cmd': ".$1;
+ $hash->{STATE} = "error setting '$cmd': ".(defined $1 ? $1 : $@);
+ return "error setting '$hash->{NAME} $cmd': ".(defined $1 ? $1 : $@);
}
return undef;
}
@@ -210,10 +207,10 @@ FRM_RGB_Get($@)
return ReadingsVal($name,"rgb",undef);
};
$cmd eq 'RGB' and do {
- return ChannelsToRgb(@{$hash->{dim}->{channels}});
+ return ChannelsToRgb(@{$hash->{".dim"}->{channels}});
};
$cmd eq 'pct' and do {
- return $hash->{dim}->{bri};
+ return $hash->{".dim"}->{bri};
return undef;
};
}
@@ -238,25 +235,24 @@ FRM_RGB_SetChannels($$)
}
$firmata->analog_write($pin->{pin},$value);
};
- readingsSingleUpdate($hash,"rgb",ChannelsToRgb(@channels),1);
-}
-
-sub FRM_RGB_State($$$$)
-{
- my ($hash, $tim, $sname, $sval) = @_;
-
-STATEHANDLER: {
- $sname eq "value" and do {
- if (AttrVal($hash->{NAME},"restoreOnStartup","on") eq "on") {
- FRM_RGB_Set($hash,$hash->{NAME},$sval);
- }
- last;
- }
- }
+ readingsBeginUpdate($hash);
+ readingsBulkUpdate($hash,"rgb",ChannelsToRgb(@channels),1);
+ readingsBulkUpdate($hash,"pct",(ChannelsToBrightness(@channels))->{bri},1);
+ readingsEndUpdate($hash, 1);
}
sub
-FRM_RGB_Attr($$$$) {
+FRM_RGB_State($$$$)
+{
+ my ($hash, $tim, $sname, $sval) = @_;
+ if ($sname eq "rgb") {
+ FRM_RGB_Set($hash,$hash->{NAME},$sname,$sval);
+ }
+}
+
+sub
+FRM_RGB_Attr($$$$)
+{
my ($command,$name,$attribute,$value) = @_;
if ($command eq "set") {
ARGUMENT_HANDLER: {
@@ -289,7 +285,7 @@ FRM_RGB_Attr($$$$) {
Define
- define <name> FRM_PWM <pin> <pin> <pin> [pin...]
+ define <name> FRM_RGB <pin> <pin> <pin> [pin...]
Defines the FRM_RGB device. <pin>> are the arduino-pin to use.
For rgb-controlled devices first pin drives red, second pin green and third pin blue.
@@ -304,6 +300,8 @@ FRM_RGB_Attr($$$$) {
set <name> off
sets the pulse-width of all configured pins to 0%
+
set <name> toggle
toggles in between the last dimmed value, 0% and 100%. If no dimmed value was set before defaults to pulsewidth 50% on all channels
@@ -320,7 +318,6 @@ FRM_RGB_Attr($$$$) {
set <name> dimDown
dims down by 10%
-
Get