20_FRM_IN.pm: added Firmata 2.5 feature PIN_MODE_PULLUP (requires perl-firmata 0.64) (Forum #81815)

git-svn-id: https://svn.fhem.de/fhem/trunk@16012 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
jensb 2018-01-27 20:11:34 +00:00
parent 81e287f292
commit 98ea153a79
3 changed files with 118 additions and 31 deletions

View File

@ -1,5 +1,7 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- feature: 20_FRM_IN: added Firmata 2.5 feature PIN_MODE_PULLUP
note: requires perl-firmata 0.64
- change: 74_Unifi: SSIDs-Readings and drop-downs use goodReadingName() - change: 74_Unifi: SSIDs-Readings and drop-downs use goodReadingName()
- feature: new module 37_NotifyAndroidTV.pm to send notifications - feature: new module 37_NotifyAndroidTV.pm to send notifications
to Notifications for Android TV/Fire TV apps (justme1968) to Notifications for Android TV/Fire TV apps (justme1968)

View File

@ -1,12 +1,43 @@
############################################## ########################################################################################
#
# $Id$ # $Id$
############################################## #
# FHEM module for one Firmata digial input pin
#
########################################################################################
#
# 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.
#
# The GNU General Public License can be found at
# http://www.gnu.org/copyleft/gpl.html.
# A copy is found in the textfile GPL.txt and important notices to the license
# from the author is found in LICENSE.txt distributed with these scripts.
#
# 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.
#
# This copyright notice MUST APPEAR in all copies of the script!
#
########################################################################################
package main; package main;
use strict; use strict;
use warnings; use warnings;
#add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though... #add FHEM/lib to @INC if it's not already included. Should rather be in fhem.pl than here though...
BEGIN { BEGIN {
if (!grep(/FHEM\/lib$/,@INC)) { if (!grep(/FHEM\/lib$/,@INC)) {
foreach my $inc (grep(/FHEM$/,@INC)) { foreach my $inc (grep(/FHEM$/,@INC)) {
@ -47,21 +78,43 @@ FRM_IN_Initialize($)
main::LoadModule("FRM"); 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 sub
FRM_IN_Init($$) FRM_IN_Init($$)
{ {
my ($hash,$args) = @_; my ($hash,$args) = @_;
my $ret = FRM_Init_Pin_Client($hash,$args,PIN_INPUT); if (FRM_IN_PinModePullupSupported($hash)) {
return $ret if (defined $ret); my $pullup = AttrVal($hash->{NAME},"internal-pullup","off");
eval { my $ret = FRM_Init_Pin_Client($hash,$args,defined($pullup) && ($pullup eq "on")? PIN_PULLUP : PIN_INPUT);
my $firmata = FRM_Client_FirmataDevice($hash); return $ret if (defined $ret);
my $pin = $hash->{PIN}; eval {
if (defined (my $pullup = AttrVal($hash->{NAME},"internal-pullup",undef))) { my $firmata = FRM_Client_FirmataDevice($hash);
$firmata->digital_write($pin,$pullup eq "on" ? 1 : 0); my $pin = $hash->{PIN};
} $firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
$firmata->observe_digital($pin,\&FRM_IN_observer,$hash); };
}; return FRM_Catch($@) if $@;
return FRM_Catch($@) if $@; } else {
my $ret = FRM_Init_Pin_Client($hash,$args,PIN_INPUT);
return $ret if (defined $ret);
eval {
my $firmata = FRM_Client_FirmataDevice($hash);
my $pin = $hash->{PIN};
if (defined (my $pullup = AttrVal($hash->{NAME},"internal-pullup",undef))) {
$firmata->digital_write($pin,$pullup eq "on" ? 1 : 0);
}
$firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
};
return FRM_Catch($@) if $@;
}
if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) { if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) {
$main::attr{$hash->{NAME}}{"stateFormat"} = "reading"; $main::attr{$hash->{NAME}}{"stateFormat"} = "reading";
} }
@ -197,8 +250,12 @@ FRM_IN_Attr($$$$) {
$attribute eq "internal-pullup" and do { $attribute eq "internal-pullup" and do {
if ($main::init_done) { if ($main::init_done) {
my $firmata = FRM_Client_FirmataDevice($hash); my $firmata = FRM_Client_FirmataDevice($hash);
$firmata->digital_write($pin,$value eq "on" ? 1 : 0); if (FRM_IN_PinModePullupSupported($hash)) {
#ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called. $firmata->pin_mode($pin,$value eq "on"? PIN_PULLUP : 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; last;
}; };
@ -218,7 +275,11 @@ FRM_IN_Attr($$$$) {
ARGUMENT_HANDLER: { ARGUMENT_HANDLER: {
$attribute eq "internal-pullup" and do { $attribute eq "internal-pullup" and do {
my $firmata = FRM_Client_FirmataDevice($hash); my $firmata = FRM_Client_FirmataDevice($hash);
$firmata->digital_write($pin,0); if (FRM_IN_PinModePullupSupported($hash)) {
$firmata->pin_mode($pin,PIN_INPUT);
} else {
$firmata->digital_write($pin,0);
}
last; last;
}; };
$attribute eq "activeLow" and do { $attribute eq "activeLow" and do {
@ -241,15 +302,29 @@ FRM_IN_Attr($$$$) {
1; 1;
=pod =pod
CHANGES
03.01.2018 jensb
o implemented Firmata 2.5 feature PIN_MODE_PULLUP (requires perl-firmata 0.64 or higher)
=cut
=pod
=item device
=item summary Firmata: digital input
=item summary_DE Firmata: digitaler Eingang
=begin html =begin html
<a name="FRM_IN"></a> <a name="FRM_IN"></a>
<h3>FRM_IN</h3> <h3>FRM_IN</h3>
<ul> <ul>
represents a pin of an <a href="http://www.arduino.cc">Arduino</a> running <a href="http://www.firmata.org">Firmata</a> This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
configured for digital input.<br> that should be configured as a digital input.<br><br>
The current state of the arduino-pin is stored in reading 'state'. Values are 'on' and 'off'.<br>
Requires a defined <a href="#FRM">FRM</a>-device to work.<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><br>
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"></a> <a name="FRM_INdefine"></a>
<b>Define</b> <b>Define</b>
@ -265,7 +340,8 @@ FRM_IN_Attr($$$$) {
<li>alarm on|off<br> <li>alarm on|off<br>
set the alarm to on or off. Used to clear the alarm.<br> set the alarm to on or off. Used to clear the alarm.<br>
The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li> The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li>
</ul> </ul><br>
<a name="FRM_INget"></a> <a name="FRM_INget"></a>
<b>Get</b> <b>Get</b>
<ul> <ul>
@ -276,10 +352,11 @@ FRM_IN_Attr($$$$) {
Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li> Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li>
<li>alarm<br> <li>alarm<br>
returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')<br> returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')<br>
'alarm' doesn't clear itself, has to be set to 'off' eplicitly.</li> 'alarm' doesn't clear itself, has to be set to 'off' explicitly.</li>
<li>state<br> <li>state<br>
returns the 'state' reading</li> returns the 'state' reading</li>
</ul><br> </ul><br>
<a name="FRM_INattr"></a> <a name="FRM_INattr"></a>
<b>Attributes</b><br> <b>Attributes</b><br>
<ul> <ul>
@ -302,9 +379,17 @@ FRM_IN_Attr($$$$) {
</li> </li>
<li><a href="#eventMap">eventMap</a><br></li> <li><a href="#eventMap">eventMap</a><br></li>
<li><a href="#readingFnAttributes">readingFnAttributes</a><br></li> <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
</ul> </ul><br>
<a name="FRM_INnotes"></a>
<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>
</ul> </ul>
<br> </ul><br>
=end html =end html
=cut =cut

View File

@ -103,7 +103,7 @@ FHEM/19_VBUSIF.pm Tobias/pejonp Sonstige Systeme
FHEM/20_FRM_AD.pm jensb Sonstige Systeme FHEM/20_FRM_AD.pm jensb Sonstige Systeme
FHEM/20_FRM_ROTENC.pm ntruchsess Sonstige Systeme FHEM/20_FRM_ROTENC.pm ntruchsess Sonstige Systeme
FHEM/20_FRM_I2C.pm ntruchsess Sonstige Systeme FHEM/20_FRM_I2C.pm ntruchsess Sonstige Systeme
FHEM/20_FRM_IN.pm ntruchsess Sonstige Systeme FHEM/20_FRM_IN.pm jensb Sonstige Systeme
FHEM/20_FRM_LCD.pm ntruchsess Sonstige Systeme (deprecated) FHEM/20_FRM_LCD.pm ntruchsess Sonstige Systeme (deprecated)
FHEM/20_FRM_OUT.pm jensb Sonstige Systeme FHEM/20_FRM_OUT.pm jensb Sonstige Systeme
FHEM/20_FRM_PWM.pm jensb Sonstige Systeme FHEM/20_FRM_PWM.pm jensb Sonstige Systeme