2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-02-07 16:59:18 +00:00

FRM implement dimmer interface for FRM_PWM

git-svn-id: https://svn.fhem.de/fhem/trunk@4195 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
ntruchsess 2013-11-09 20:38:55 +00:00
parent ae5c97a662
commit 27934dd7fb
3 changed files with 265 additions and 82 deletions

View File

@ -128,6 +128,9 @@ FRM_OUT_Attr($$$$) {
<ul>
<code>set &lt;name&gt; on|off</code><br><br>
</ul>
<ul>
<a href="#setExtensions">set extensions</a> are supported<br>
</ul>
<a name="FRM_OUTget"></a>
<b>Get</b><br>
<ul>

View File

@ -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($$$$) {
<a name="FRM_PWMset"></a>
<b>Set</b><br>
<ul>
<code>set &lt;name&gt; on</code><br>
sets the pulse-width to 100%<br>
</ul>
<ul>
<code>set &lt;name&gt; off</code><br>
sets the pulse-width to 0%<br>
</ul>
<ul>
<a href="#setExtensions">set extensions</a> are supported<br>
</ul>
<ul>
<code>set &lt;name&gt; toggle</code><br>
toggles the pulse-width in between to the last value set by 'value' or 'dim' and 0 respectivly 100%<br>
</ul>
<ul>
<code>set &lt;name&gt; value &lt;value&gt;</code><br>
sets the pulse-width of the signal that is output on the configured arduino pin<br>
Range is from 0 to 255 (see <a href="http://arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> for details)
sets the pulse-width to the value specified<br>
Range is from 0 to 255 (for 8-bit resolution) (see <a href="http://arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> for details)<br>
</ul>
<ul>
<code>set &lt;name&gt; dim &lt;value&gt;</code><br>
sets the pulse-width to the value specified in percent<br>
Range is from 0 to 100<br>
</ul>
<ul>
<code>set &lt;name&gt; dimUp</code><br>
increases the pulse-width by 10%<br>
</ul>
<ul>
<code>set &lt;name&gt; dimDown</code><br>
decreases the pulse-width by 10%<br>
</ul>
<a name="FRM_PWMget"></a>
<b>Get</b><br>

View File

@ -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($$$$) {
<a name="FRM_RGBdefine"></a>
<b>Define</b>
<ul>
<code>define &lt;name&gt; FRM_PWM &lt;pin&gt; &lt;pin&gt; &lt;pin&gt; [pin...]</code> <br>
<code>define &lt;name&gt; FRM_RGB &lt;pin&gt; &lt;pin&gt; &lt;pin&gt; [pin...]</code> <br>
Defines the FRM_RGB device. &lt;pin&gt> are the arduino-pin to use.<br>
For rgb-controlled devices first pin drives red, second pin green and third pin blue.
</ul>
@ -304,6 +300,8 @@ FRM_RGB_Attr($$$$) {
<code>set &lt;name&gt; off</code><br>
sets the pulse-width of all configured pins to 0%</ul><br>
<ul>
<a href="#setExtensions">set extensions</a> are supported</ul><br>
<ul>
<code>set &lt;name&gt; toggle</code><br>
toggles in between the last dimmed value, 0% and 100%. If no dimmed value was set before defaults to pulsewidth 50% on all channels</ul><br>
<ul>
@ -320,7 +318,6 @@ FRM_RGB_Attr($$$$) {
<ul>
<code>set &lt;name&gt; dimDown</code><br>
dims down by 10%</ul><br>
<a name="FRM_RGBget"></a>
<b>Get</b><br>
<ul>