-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path20_FRM_PWM.pm
486 lines (409 loc) · 13.1 KB
/
20_FRM_PWM.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
########################################################################################
# $Id: 20_FRM_PWM.pm 23054 2020-10-30 18:16:24Z jensb $
########################################################################################
=encoding UTF-8
=head1 NAME
FHEM module for one Firmata PWM output pin
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2013 ntruchess
Copyright (C) 2016 jensb
All rights reserved
This script is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This script is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this script; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
A copy of the GNU General Public License, Version 2 can also be found at
http://www.gnu.org/licenses/old-licenses/gpl-2.0.
This copyright notice MUST APPEAR in all copies of the script!
=cut
package main;
use strict;
use warnings;
#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 SetExtensions qw/ :all /;
#####################################
my %gets = (
"dim" => "",
"value" => "",
);
# number of arguments
my %sets = (
"on:noArg" => 0,
"off:noArg" => 0,
"toggle:noArg" => 0,
"value" => 1,
"dim:slider,0,1,100" => 1,
"dimUp:noArg" => 0,
"dimDown:noArg" => 0,
);
sub 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";
$hash->{AttrFn} = "FRM_PWM_Attr";
$hash->{StateFn} = "FRM_PWM_State";
$hash->{AttrList} = "restoreOnReconnect:on,off restoreOnStartup:on,off IODev $main::readingFnAttributes";
main::LoadModule("FRM");
}
sub FRM_PWM_Init
{
my ($hash,$args) = @_;
my $name = $hash->{NAME};
if (defined($main::defs{$name}{IODev_ERROR})) {
return 'Perl module Device::Firmata not properly installed';
}
my $ret = FRM_Init_Pin_Client($hash, $args, Device::Firmata::Constants->PIN_PWM);
if (defined($ret)) {
readingsSingleUpdate($hash, 'state', "error initializing: $ret", 1);
return $ret;
}
eval {
my $firmata = FRM_Client_FirmataDevice($hash);
my $resolution = 8;
if (defined $firmata->{metadata}{pwm_resolutions}) {
$resolution = $firmata->{metadata}{pwm_resolutions}{$hash->{PIN}}
}
$hash->{resolution} = $resolution;
$hash->{".max"} = defined $resolution ? (1<<$resolution)-1 : 255;
$hash->{".dim"} = 0;
$hash->{".toggle"} = "off";
};
if ($@) {
my $ret = FRM_Catch($@);
readingsSingleUpdate($hash, 'state', "error initializing: $ret", 1);
return $ret;
}
if (!(defined AttrVal($name,"stateFormat",undef))) {
$main::attr{$name}{"stateFormat"} = "value";
}
my $value = ReadingsVal($name,"value",undef);
if (defined $value and AttrVal($hash->{NAME},"restoreOnReconnect","on") eq "on") {
FRM_PWM_Set($hash,$name,"value",$value);
}
main::readingsSingleUpdate($hash,"state","Initialized",1);
return undef;
}
sub FRM_PWM_Set
{
my ($hash, $name, $cmd, @a) = @_;
return "set command missing" if(!defined($cmd));
my @match = grep( $_ =~ /^$cmd($|:)/, keys %sets );
return SetExtensions($hash, join(" ", keys %sets), $name, $cmd, @a) unless @match == 1;
return "$cmd requires $sets{$match[0]} argument(s)" unless (@a == $sets{$match[0]});
if (defined($main::defs{$name}{IODev_ERROR})) {
return 'Perl module Device::Firmata not properly installed';
}
my $value = shift @a;
eval {
SETHANDLER: {
$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 "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 ($@) {
my $ret = FRM_Catch($@);
$hash->{STATE} = "set $cmd error: " . $ret;
return $hash->{STATE};
}
return undef;
}
sub 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 "get command missing" if(!defined($cmd));
return "unknown get command '$cmd', choose one of " . join(":noArg ", sort keys %gets) . ":noArg" if(!defined($gets{$cmd}));
GETHANDLER: {
$cmd eq 'dim' and do {
return ReadingsVal($name,"dim",undef);
};
$cmd eq 'value' and do {
return ReadingsVal($name,"value",undef);
};
}
}
sub FRM_PWM_State
{
my ($hash, $tim, $sname, $sval) = @_;
my $name = $hash->{NAME};
if ($sname eq "value") {
# depending on the FHEM startup timing and the Arduino connection type, FHEM statefile restore and Arduino connect take place in arbitrary order
if (AttrVal($name, "restoreOnStartup", "on") eq "on") {
$hash->{READINGS}{$sname}{VAL} = $sval;
$hash->{READINGS}{$sname}{TIME} = $tim;
if (defined($hash->{IODev}) && defined($hash->{IODev}->{FirmataDevice} && $hash->{IODev}->{FirmataDevice}->{state} eq "Initialized")) {
FRM_PWM_Set($hash, $name, "value", $sval);
}
} else {
$hash->{READINGS}{$sname}{VAL} = undef;
$hash->{READINGS}{$sname}{TIME} = gettimeofday();
}
}
return 0; # default processing by fhem.pl
}
sub FRM_PWM_Attr
{
my ($command,$name,$attribute,$value) = @_;
my $hash = $main::defs{$name};
eval {
if ($command eq "set") {
ARGUMENT_HANDLER: {
$attribute eq "IODev" and do {
if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
FRM_Client_AssignIOPort($hash,$value);
FRM_Init_Client($hash) if (defined ($hash->{IODev}));
}
last;
};
}
}
};
if ($@) {
my $ret = FRM_Catch($@);
$hash->{STATE} = "$command $attribute error: " . $ret;
return $hash->{STATE};
}
}
1;
=pod
=head1 CHANGES
2016 jensb
o modified subs FRM_PWM_Init and FRM_PWM_State to support attribute "restoreOnStartup"
24.08.2020 jensb
o check for IODev install error in Init and Set
o prototypes removed
o set argument metadata added
o get/set argument verifier improved
o module help updated
22.10.2020 jensb
o annotaded module help of attributes for FHEMWEB
=cut
=pod
=head1 FHEM COMMANDREF METADATA
=over
=item device
=item summary Firmata: PWM output
=item summary_DE Firmata: PWM Ausgang
=back
=head1 INSTALLATION AND CONFIGURATION
=begin html
<a name="FRM_PWM"/>
<h3>FRM_PWM</h3>
<ul>
This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
that should be configured as a pulse width modulated output (PWM).<br><br>
Requires a defined <a href="#FRM">FRM</a> device to work. The pin must be listed in the internal reading
"<a href="#FRMinternals">pwm_pins</a>" of the FRM device (after connecting to the Firmata device) to be
used as PWM output.<br><br>
<a name="FRM_PWMdefine"/>
<b>Define</b>
<ul>
<code>define <name> FRM_PWM <pin></code><br><br>
Defines the FRM_PWM device. <pin>> is the arduino-pin to use.
</ul><br>
<a name="FRM_PWMset"/>
<b>Set</b>
<ul>
<li><code>set <name> on</code><br>
sets the pulse-width to 100%<br>
</li>
<li>
<code>set <name> off</code><br>
sets the pulse-width to 0%<br>
</li>
<li>
<a href="#setExtensions">set extensions</a> are supported<br>
</li>
<li>
<code>set <name> toggle</code><br>
toggles the pulse-width in between to the last value set by 'value' or 'dim' and 0 respectivly 100%<br>
</li>
<li>
<code>set <name> value <value></code><br>
sets the pulse-width to the value specified<br>
The min value is zero and the max value depends on the Firmata device (see internal reading<br>
"<a href="#FRMinternals">pwm_resolutions</a>" of the FRM device). For 8 bits resolution the range
is 0 to 255 (also see <a href="http://arduino.cc/en/Reference/AnalogWrite">analogWrite()</a> for details)<br>
</li>
<li>
<code>set <name> dim <value></code><br>
sets the pulse-width to the value specified in percent<br>
Range is from 0 to 100<br>
</li>
<li>
<code>set <name> dimUp</code><br>
increases the pulse-width by 10%<br>
</li>
<li>
<code>set <name> dimDown</code><br>
decreases the pulse-width by 10%<br>
</li>
</ul><br>
<a name="FRM_PWMget"/>
<b>Get</b><br>
<ul>
<li>
<code>get <dim></code><br>
returns current dim setting in percent, see description for set command for more details<br>
</li>
<li>
<code>get <value></code><br>
returns current dim setting, see description for set command for more details<br>
</li>
</ul><br>
<a name="FRM_PWMattr"/>
<b>Attributes</b><br>
<ul>
<a name="restoreOnStartup"/>
<li>restoreOnStartup <on|off></li>
<a name="restoreOnReconnect"/>
<li>restoreOnReconnect <on|off></li>
<a name="IODev"/>
<li><a href="#IODev">IODev</a><br>
Specify which <a href="#FRM">FRM</a> to use. Only required if there is more than one FRM-device defined.
</li>
<li><a href="#attributes">global attributes</a></li>
<li><a href="#readingFnAttributes">readingFnAttributes</a></li>
</ul><br>
<a name="FRM_PWMnotes"/>
<b>Notes</b><br>
<ul>
<li>attribute <i>stateFormat</i><br>
In most cases it is a good idea to assign "value" to the attribute <i>stateFormat</i>. This will show the
current value of the pin in the web interface.
</li>
</ul>
</ul><br>
=end html
=begin html_DE
<a name="FRM_PWM"/>
<h3>FRM_PWM</h3>
<ul>
Die Modulbeschreibung von FRM_PWM gibt es nur auf <a href="commandref.html#FRM_PWM">Englisch</a>. <br>
</ul><br>
=end html_DE
=cut