-
Notifications
You must be signed in to change notification settings - Fork 6
/
20_FRM_IN.pm
516 lines (429 loc) · 16.5 KB
/
20_FRM_IN.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
########################################################################################
# $Id: 20_FRM_IN.pm 23054 2020-10-30 18:16:24Z jensb $
########################################################################################
=encoding UTF-8
=head1 NAME
FHEM module for one Firmata digial input pin
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2013 ntruchess
Copyright (C) 2018 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 already 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";
};
};
};
#####################################
# default values for Attr
my %sets = (
"alarm" => "",
"count" => 0,
);
# default values for Get
my %gets = (
"reading" => "",
"state" => "",
"count" => 0,
"alarm" => "off"
);
sub FRM_IN_Initialize
{
my ($hash) = @_;
$hash->{SetFn} = "FRM_IN_Set";
$hash->{GetFn} = "FRM_IN_Get";
$hash->{AttrFn} = "FRM_IN_Attr";
$hash->{DefFn} = "FRM_Client_Define";
$hash->{InitFn} = "FRM_IN_Init";
$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 activeLow:yes,no $main::readingFnAttributes";
main::LoadModule("FRM");
}
sub FRM_IN_PinModePullupSupported
{
my ($hash) = @_;
my $iodev = $hash->{IODev};
my $pullupPins = defined($iodev)? $iodev->{pullup_pins} : undef;
return defined($pullupPins);
}
sub FRM_IN_Init
{
my ($hash,$args) = @_;
my $name = $hash->{NAME};
if (defined($main::defs{$name}{IODev_ERROR})) {
return 'Perl module Device::Firmata not properly installed';
}
if (FRM_IN_PinModePullupSupported($hash)) {
my $pullup = AttrVal($name, "internal-pullup", "off");
my $ret = FRM_Init_Pin_Client($hash,$args,defined($pullup) && ($pullup eq "on")? Device::Firmata::Constants->PIN_PULLUP : Device::Firmata::Constants->PIN_INPUT);
return $ret if (defined $ret);
eval {
my $firmata = FRM_Client_FirmataDevice($hash);
my $pin = $hash->{PIN};
$firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
};
if ($@) {
my $ret = FRM_Catch($@);
readingsSingleUpdate($hash, 'state', "error initializing: $ret", 1);
return $ret;
}
} else {
my $ret = FRM_Init_Pin_Client($hash, $args, Device::Firmata::Constants->PIN_INPUT);
return $ret if (defined $ret);
eval {
my $firmata = FRM_Client_FirmataDevice($hash);
my $pin = $hash->{PIN};
if (defined(my $pullup = AttrVal($name, "internal-pullup", undef))) {
$firmata->digital_write($pin,$pullup eq "on" ? 1 : 0);
}
$firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
};
if ($@) {
my $ret = FRM_Catch($@);
readingsSingleUpdate($hash, 'state', "error initializing: $ret", 1);
return $ret;
}
}
if (!(defined AttrVal($name, "stateFormat", undef))) {
$main::attr{$name}{"stateFormat"} = "reading";
}
main::readingsSingleUpdate($hash,"state","Initialized",1);
return undef;
}
sub FRM_IN_observer
{
my ($pin,$last,$new,$hash) = @_;
my $name = $hash->{NAME};
my $old = ReadingsVal($name, "reading", undef);
if (defined($old)) {
$old = $old eq "on" ? Device::Firmata::Constants->PIN_HIGH : Device::Firmata::Constants->PIN_LOW;
}
if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
$new = $new == Device::Firmata::Constants->PIN_LOW ? Device::Firmata::Constants->PIN_HIGH : Device::Firmata::Constants->PIN_LOW;
}
Log3 $name, 5, "$name: observer pin: $pin, old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--");
my $changed = !defined($old) || $old != $new;
if ($changed) {
main::readingsBeginUpdate($hash);
if (defined (my $mode = main::AttrVal($name,"count-mode",undef))) {
if (($mode eq "both")
or (($mode eq "rising") and ($new == Device::Firmata::Constants->PIN_HIGH))
or (($mode eq "falling") and ($new == Device::Firmata::Constants->PIN_LOW))) {
my $count = main::ReadingsVal($name,"count",0);
$count++;
if (defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
if ( $count > $threshold ) {
if (AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
$count=0;
main::readingsBulkUpdate($hash,"alarm","on",1);
} elsif ( main::ReadingsVal($name,"alarm","off") ne "on" ) {
main::readingsBulkUpdate($hash,"alarm","on",1);
}
}
}
main::readingsBulkUpdate($hash,"count",$count,1);
}
};
main::readingsBulkUpdate($hash, "reading", $new == Device::Firmata::Constants->PIN_HIGH ? "on" : "off", 1);
main::readingsEndUpdate($hash,1);
}
}
sub FRM_IN_Set
{
my ($hash, $name, $cmd, @a) = @_;
return "set command missing" if(!defined($cmd));
return "unknown set command '$cmd', choose one of " . join(" ", sort keys %sets) if(!defined($sets{$cmd}));
return "$cmd requires 1 argument" unless (@a == 1);
my $value = shift @a;
COMMAND_HANDLER: {
$cmd eq "alarm" and do {
return undef if (!($value eq "off" or $value eq "on"));
main::readingsSingleUpdate($hash,"alarm",$value,1);
last;
};
$cmd eq "count" and do {
main::readingsSingleUpdate($hash,"count",$value,1);
last;
};
}
}
sub FRM_IN_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}));
ARGUMENT_HANDLER: {
$cmd eq "reading" and do {
my $last;
eval {
if (defined($main::defs{$name}{IODev_ERROR})) {
die 'Perl module Device::Firmata not properly installed';
}
$last = FRM_Client_FirmataDevice($hash)->digital_read($hash->{PIN});
if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
$last = $last == Device::Firmata::Constants->PIN_LOW ? Device::Firmata::Constants->PIN_HIGH : Device::Firmata::Constants->PIN_LOW;
}
};
if ($@) {
my $ret = FRM_Catch($@);
$hash->{STATE} = "get $cmd error: " . $ret;
return $hash->{STATE};
}
return $last == Device::Firmata::Constants->PIN_HIGH ? "on" : "off";
};
($cmd eq "count" or $cmd eq "alarm" or $cmd eq "state") and do {
return main::ReadingsVal($name,$cmd,$gets{$cmd});
};
}
return undef;
}
sub FRM_IN_Attr
{
my ($command,$name,$attribute,$value) = @_;
my $hash = $main::defs{$name};
my $pin = $hash->{PIN};
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;
};
$attribute eq "count-mode" and do {
if ($value ne "none" and !defined main::ReadingsVal($name,"count",undef)) {
main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
}
last;
};
$attribute eq "reset-on-threshold-reached" and do {
if ($value eq "yes"
and defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
if (main::ReadingsVal($name,"count",0) > $threshold) {
main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
}
}
last;
};
$attribute eq "count-threshold" and do {
if (main::ReadingsVal($name,"count",0) > $value) {
main::readingsBeginUpdate($hash);
if (main::ReadingsVal($name,"alarm","off") ne "on") {
main::readingsBulkUpdate($hash,"alarm","on",1);
}
if (main::AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
main::readingsBulkUpdate($main::defs{$name},"count",0,1);
}
main::readingsEndUpdate($hash,1);
}
last;
};
$attribute eq "internal-pullup" and do {
if ($main::init_done) {
if (defined($main::defs{$name}{IODev_ERROR})) {
die 'Perl module Device::Firmata not properly installed';
}
my $firmata = FRM_Client_FirmataDevice($hash);
if (FRM_IN_PinModePullupSupported($hash)) {
$firmata->pin_mode($pin, $value eq "on"? Device::Firmata::Constants->PIN_PULLUP : Device::Firmata::Constants->PIN_INPUT);
} else {
$firmata->digital_write($pin,$value eq "on" ? 1 : 0);
#ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called.
}
}
last;
};
$attribute eq "activeLow" and do {
my $oldval = AttrVal($hash->{NAME},"activeLow","no");
if ($oldval ne $value) {
$main::attr{$hash->{NAME}}{activeLow} = $value;
if ($main::init_done) {
if (defined($main::defs{$name}{IODev_ERROR})) {
die 'Perl module Device::Firmata not properly installed';
}
my $firmata = FRM_Client_FirmataDevice($hash);
FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
}
};
last;
};
}
} elsif ($command eq "del") {
ARGUMENT_HANDLER: {
$attribute eq "internal-pullup" and do {
if (defined($main::defs{$name}{IODev_ERROR})) {
die 'Perl module Device::Firmata not properly installed';
}
my $firmata = FRM_Client_FirmataDevice($hash);
if (FRM_IN_PinModePullupSupported($hash)) {
$firmata->pin_mode($pin, Device::Firmata::Constants->PIN_INPUT);
} else {
$firmata->digital_write($pin,0);
}
last;
};
$attribute eq "activeLow" and do {
if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
if (defined($main::defs{$name}{IODev_ERROR})) {
die 'Perl module Device::Firmata not properly installed';
}
delete $main::attr{$hash->{NAME}}{activeLow};
my $firmata = FRM_Client_FirmataDevice($hash);
FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
};
last;
};
}
}
};
if ($@) {
my $ret = FRM_Catch($@);
$hash->{STATE} = "$command $attribute error: " . $ret;
return $hash->{STATE};
}
}
1;
=pod
=head1 CHANGES
15.02.2019 jensb
o bugfix: change detection no longer assumes that reading "reading" is defined
04.11.2018 jensb
o bugfix: get alarm/reading/state
o feature: remove unused FHEMWEB input field from all get commands
o feature: @see https://forum.fhem.de/index.php/topic,81815.msg842557.html#msg842557
- use current FHEM reading instead of perl-firmata "old" value to improve change detection
- only update reading on change to filter updates by other pins on same Firmata digital port
03.01.2018 jensb
o implemented Firmata 2.5 feature PIN_MODE_PULLUP (requires perl-firmata 0.64 or higher)
24.08.2020 jensb
o check for IODev install error in Init, Get and Attr
o prototypes removed
o set argument verifier improved
19.10.2020 jensb
o annotaded module help of attributes for FHEMWEB
=cut
=pod
=head1 FHEM COMMANDREF METADATA
=over
=item device
=item summary Firmata: digital input
=item summary_DE Firmata: digitaler Eingang
=back
=head1 INSTALLATION AND CONFIGURATION
=begin html
<a name="FRM_IN"/>
<h3>FRM_IN</h3>
<ul>
This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
that should be configured as a digital input.<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">"input_pins" or "pullup_pins"</a>
of the FRM device (after connecting to the Firmata device) to be used as digital input with or without pullup.<br><br>
<a name="FRM_INdefine"/>
<b>Define</b>
<ul>
<code>define <name> FRM_IN <pin></code> <br>
Defines the FRM_IN device. <pin>> is the Firmata pin to use.
</ul>
<br>
<a name="FRM_INset"/>
<b>Set</b><br>
<ul>
<li>alarm on|off<br>
set the 'alarm' reading to on or off. Typically used to clear the alarm.
The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li>
<li>count <number><br>
set the 'count' reading to a specific value.
The counter is incremented depending on the attribute 'count-mode'.</li>
</ul><br>
<a name="FRM_INget"/>
<b>Get</b>
<ul>
<li>reading<br>
returns the logical state of the input pin last received from the Firmata device depending on the attribute 'activeLow'.
Values are 'on' and 'off'.<br></li>
<li>count<br>
returns the current counter value. Contains the number of toggles reported by the Fimata device on this input pin.
Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li>
<li>alarm<br>
returns the 'alarm' reading. Values are 'on' and 'off' (Defaults to 'off').
The 'alarm' reading doesn't clear itself, it has to be set to 'off' explicitly.</li>
<li>state<br>
returns the 'state' reading</li>
</ul><br>
<a name="FRM_INattr"/>
<b>Attributes</b><br>
<ul>
<a name="activeLow"/>
<li>activeLow yes|no<br>
inverts the logical state of the pin reading if set to yes (defaults to 'no').
</li>
<a name="count-mode"/>
<li>count-mode none|rising|falling|both<br>
Determines whether 'rising' (transitions from 'off' to 'on') of falling (transitions from 'on' to 'off')
edges (or 'both') are counted (defaults to 'none').
</li>
<a name="count-threshold"/>
<li>count-threshold <number><br>
sets the threshold-value for the counter - if defined whenever 'count' exceeds the 'count-threshold'
the 'alarm' reading is set to 'on' (defaults to undefined). Use 'set alarm off' to clear the alarm.
</li>
<a name="reset-on-threshold-reached"/>
<li>reset-on-threshold-reached yes|no<br>
if set to 'yes' reset the counter to 0 when the threshold is reached (defaults to 'no').
</li>
<a name="internal-pullup"/>
<li>internal-pullup on|off<br>
enables/disables the internal pullup resistor of the Firmata pin (defaults to 'off'). Requires hardware
and firmware support.
</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_INnotes"/>
<b>Notes</b><br>
<ul>
<li>attribute <i>stateFormat</i><br>
In most cases it is a good idea to assign "reading" to the attribute <i>stateFormat</i>. This will show the state
of the pin in the web interface.
</li>
<li>attribute <i>count-mode</i><br>
The count-mode does not depended on hardware or firmware of the Firmata device because it is implemented in FHEM. The counter will not be updated while the Firmata device is not connected to FHEM. Any changes of the pin state during this time will be lost.
</li>
</ul>
</ul><br>
=end html
=begin html_DE
<a name="FRM_IN"/>
<h3>FRM_IN</h3>
<ul>
Die Modulbeschreibung von FRM_IN gibt es nur auf <a href="commandref.html#FRM_IN">Englisch</a>. <br>
</ul> <br>
=end html_DE
=cut