20_FRM_IN.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. ########################################################################################
  2. #
  3. # $Id: 20_FRM_IN.pm 16012 2018-01-27 20:11:34Z jensb $
  4. #
  5. # FHEM module for one Firmata digial input pin
  6. #
  7. ########################################################################################
  8. #
  9. # LICENSE AND COPYRIGHT
  10. #
  11. # Copyright (C) 2013 ntruchess
  12. # Copyright (C) 2018 jensb
  13. #
  14. # All rights reserved
  15. #
  16. # This script is free software; you can redistribute it and/or modify
  17. # it under the terms of the GNU General Public License as published by
  18. # the Free Software Foundation; either version 2 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # The GNU General Public License can be found at
  22. # http://www.gnu.org/copyleft/gpl.html.
  23. # A copy is found in the textfile GPL.txt and important notices to the license
  24. # from the author is found in LICENSE.txt distributed with these scripts.
  25. #
  26. # This script is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU General Public License for more details.
  30. #
  31. # This copyright notice MUST APPEAR in all copies of the script!
  32. #
  33. ########################################################################################
  34. package main;
  35. use strict;
  36. use warnings;
  37. #add FHEM/lib to @INC if it's not already included. Should rather be in fhem.pl than here though...
  38. BEGIN {
  39. if (!grep(/FHEM\/lib$/,@INC)) {
  40. foreach my $inc (grep(/FHEM$/,@INC)) {
  41. push @INC,$inc."/lib";
  42. };
  43. };
  44. };
  45. use Device::Firmata::Constants qw/ :all /;
  46. #####################################
  47. my %sets = (
  48. "alarm" => "",
  49. "count" => 0,
  50. );
  51. my %gets = (
  52. "reading" => "",
  53. "state" => "",
  54. "count" => 0,
  55. "alarm" => "off"
  56. );
  57. sub
  58. FRM_IN_Initialize($)
  59. {
  60. my ($hash) = @_;
  61. $hash->{SetFn} = "FRM_IN_Set";
  62. $hash->{GetFn} = "FRM_IN_Get";
  63. $hash->{AttrFn} = "FRM_IN_Attr";
  64. $hash->{DefFn} = "FRM_Client_Define";
  65. $hash->{InitFn} = "FRM_IN_Init";
  66. $hash->{UndefFn} = "FRM_Client_Undef";
  67. $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";
  68. main::LoadModule("FRM");
  69. }
  70. sub
  71. FRM_IN_PinModePullupSupported($)
  72. {
  73. my ($hash) = @_;
  74. my $iodev = $hash->{IODev};
  75. my $pullupPins = defined($iodev)? $iodev->{pullup_pins} : undef;
  76. return defined($pullupPins);
  77. }
  78. sub
  79. FRM_IN_Init($$)
  80. {
  81. my ($hash,$args) = @_;
  82. if (FRM_IN_PinModePullupSupported($hash)) {
  83. my $pullup = AttrVal($hash->{NAME},"internal-pullup","off");
  84. my $ret = FRM_Init_Pin_Client($hash,$args,defined($pullup) && ($pullup eq "on")? PIN_PULLUP : PIN_INPUT);
  85. return $ret if (defined $ret);
  86. eval {
  87. my $firmata = FRM_Client_FirmataDevice($hash);
  88. my $pin = $hash->{PIN};
  89. $firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
  90. };
  91. return FRM_Catch($@) if $@;
  92. } else {
  93. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_INPUT);
  94. return $ret if (defined $ret);
  95. eval {
  96. my $firmata = FRM_Client_FirmataDevice($hash);
  97. my $pin = $hash->{PIN};
  98. if (defined (my $pullup = AttrVal($hash->{NAME},"internal-pullup",undef))) {
  99. $firmata->digital_write($pin,$pullup eq "on" ? 1 : 0);
  100. }
  101. $firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
  102. };
  103. return FRM_Catch($@) if $@;
  104. }
  105. if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) {
  106. $main::attr{$hash->{NAME}}{"stateFormat"} = "reading";
  107. }
  108. main::readingsSingleUpdate($hash,"state","Initialized",1);
  109. return undef;
  110. }
  111. sub
  112. FRM_IN_observer
  113. {
  114. my ($pin,$old,$new,$hash) = @_;
  115. my $name = $hash->{NAME};
  116. Log3 $name,5,"onDigitalMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--");
  117. if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
  118. $old = $old == PIN_LOW ? PIN_HIGH : PIN_LOW if (defined $old);
  119. $new = $new == PIN_LOW ? PIN_HIGH : PIN_LOW;
  120. }
  121. my $changed = ((!(defined $old)) or ($old != $new));
  122. main::readingsBeginUpdate($hash);
  123. if ($changed) {
  124. if (defined (my $mode = main::AttrVal($name,"count-mode",undef))) {
  125. if (($mode eq "both")
  126. or (($mode eq "rising") and ($new == PIN_HIGH))
  127. or (($mode eq "falling") and ($new == PIN_LOW))) {
  128. my $count = main::ReadingsVal($name,"count",0);
  129. $count++;
  130. if (defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
  131. if ( $count > $threshold ) {
  132. if (AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
  133. $count=0;
  134. main::readingsBulkUpdate($hash,"alarm","on",1);
  135. } elsif ( main::ReadingsVal($name,"alarm","off") ne "on" ) {
  136. main::readingsBulkUpdate($hash,"alarm","on",1);
  137. }
  138. }
  139. }
  140. main::readingsBulkUpdate($hash,"count",$count,1);
  141. }
  142. };
  143. }
  144. main::readingsBulkUpdate($hash,"reading",$new == PIN_HIGH ? "on" : "off", $changed);
  145. main::readingsEndUpdate($hash,1);
  146. }
  147. sub
  148. FRM_IN_Set
  149. {
  150. my ($hash, @a) = @_;
  151. return "Need at least one parameters" if(@a < 2);
  152. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %sets)
  153. if(!defined($sets{$a[1]}));
  154. my $command = $a[1];
  155. my $value = $a[2];
  156. COMMAND_HANDLER: {
  157. $command eq "alarm" and do {
  158. return undef if (!($value eq "off" or $value eq "on"));
  159. main::readingsSingleUpdate($hash,"alarm",$value,1);
  160. last;
  161. };
  162. $command eq "count" and do {
  163. main::readingsSingleUpdate($hash,"count",$value,1);
  164. last;
  165. };
  166. }
  167. }
  168. sub
  169. FRM_IN_Get($)
  170. {
  171. my ($hash, @a) = @_;
  172. return "Need at least one parameters" if(@a < 2);
  173. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %gets)
  174. if(!defined($gets{$a[1]}));
  175. my $name = shift @a;
  176. my $cmd = shift @a;
  177. ARGUMENT_HANDLER: {
  178. $cmd eq "reading" and do {
  179. eval {
  180. return FRM_Client_FirmataDevice($hash)->digital_read($hash->{PIN}) == PIN_HIGH ? "on" : "off";
  181. };
  182. return $@;
  183. };
  184. ( $cmd eq "count" or $cmd eq "alarm" or $cmd eq "state" ) and do {
  185. return main::ReadingsVal($name,"count",$gets{$cmd});
  186. };
  187. }
  188. return undef;
  189. }
  190. sub
  191. FRM_IN_Attr($$$$) {
  192. my ($command,$name,$attribute,$value) = @_;
  193. my $hash = $main::defs{$name};
  194. my $pin = $hash->{PIN};
  195. eval {
  196. if ($command eq "set") {
  197. ARGUMENT_HANDLER: {
  198. $attribute eq "IODev" and do {
  199. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  200. FRM_Client_AssignIOPort($hash,$value);
  201. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  202. }
  203. last;
  204. };
  205. $attribute eq "count-mode" and do {
  206. if ($value ne "none" and !defined main::ReadingsVal($name,"count",undef)) {
  207. main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
  208. }
  209. last;
  210. };
  211. $attribute eq "reset-on-threshold-reached" and do {
  212. if ($value eq "yes"
  213. and defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
  214. if (main::ReadingsVal($name,"count",0) > $threshold) {
  215. main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
  216. }
  217. }
  218. last;
  219. };
  220. $attribute eq "count-threshold" and do {
  221. if (main::ReadingsVal($name,"count",0) > $value) {
  222. main::readingsBeginUpdate($hash);
  223. if (main::ReadingsVal($name,"alarm","off") ne "on") {
  224. main::readingsBulkUpdate($hash,"alarm","on",1);
  225. }
  226. if (main::AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
  227. main::readingsBulkUpdate($main::defs{$name},"count",0,1);
  228. }
  229. main::readingsEndUpdate($hash,1);
  230. }
  231. last;
  232. };
  233. $attribute eq "internal-pullup" and do {
  234. if ($main::init_done) {
  235. my $firmata = FRM_Client_FirmataDevice($hash);
  236. if (FRM_IN_PinModePullupSupported($hash)) {
  237. $firmata->pin_mode($pin,$value eq "on"? PIN_PULLUP : PIN_INPUT);
  238. } else {
  239. $firmata->digital_write($pin,$value eq "on" ? 1 : 0);
  240. #ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called.
  241. }
  242. }
  243. last;
  244. };
  245. $attribute eq "activeLow" and do {
  246. my $oldval = AttrVal($hash->{NAME},"activeLow","no");
  247. if ($oldval ne $value) {
  248. $main::attr{$hash->{NAME}}{activeLow} = $value;
  249. if ($main::init_done) {
  250. my $firmata = FRM_Client_FirmataDevice($hash);
  251. FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
  252. }
  253. };
  254. last;
  255. };
  256. }
  257. } elsif ($command eq "del") {
  258. ARGUMENT_HANDLER: {
  259. $attribute eq "internal-pullup" and do {
  260. my $firmata = FRM_Client_FirmataDevice($hash);
  261. if (FRM_IN_PinModePullupSupported($hash)) {
  262. $firmata->pin_mode($pin,PIN_INPUT);
  263. } else {
  264. $firmata->digital_write($pin,0);
  265. }
  266. last;
  267. };
  268. $attribute eq "activeLow" and do {
  269. if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
  270. delete $main::attr{$hash->{NAME}}{activeLow};
  271. my $firmata = FRM_Client_FirmataDevice($hash);
  272. FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
  273. };
  274. last;
  275. };
  276. }
  277. }
  278. };
  279. if (my $error = FRM_Catch($@)) {
  280. $hash->{STATE} = "error setting $attribute to $value: ".$error;
  281. return "cannot $command attribute $attribute to $value for $name: ".$error;
  282. }
  283. }
  284. 1;
  285. =pod
  286. CHANGES
  287. 03.01.2018 jensb
  288. o implemented Firmata 2.5 feature PIN_MODE_PULLUP (requires perl-firmata 0.64 or higher)
  289. =cut
  290. =pod
  291. =item device
  292. =item summary Firmata: digital input
  293. =item summary_DE Firmata: digitaler Eingang
  294. =begin html
  295. <a name="FRM_IN"></a>
  296. <h3>FRM_IN</h3>
  297. <ul>
  298. This module represents a pin of a <a href="http://www.firmata.org">Firmata device</a>
  299. that should be configured as a digital input.<br><br>
  300. Requires a defined <a href="#FRM">FRM</a> device to work. The pin must be listed in
  301. the internal reading <a href="#FRMinternals">"input_pins" or "pullup_pins"</a><br>
  302. of the FRM device (after connecting to the Firmata device) to be used as digital input with or without pullup.<br><br>
  303. <a name="FRM_INdefine"></a>
  304. <b>Define</b>
  305. <ul>
  306. <code>define &lt;name&gt; FRM_IN &lt;pin&gt;</code> <br>
  307. Defines the FRM_IN device. &lt;pin&gt> is the arduino-pin to use.
  308. </ul>
  309. <br>
  310. <a name="FRM_INset"></a>
  311. <b>Set</b><br>
  312. <ul>
  313. <li>alarm on|off<br>
  314. set the alarm to on or off. Used to clear the alarm.<br>
  315. The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li>
  316. </ul><br>
  317. <a name="FRM_INget"></a>
  318. <b>Get</b>
  319. <ul>
  320. <li>reading<br>
  321. returns the logical state of the arduino-pin. Values are 'on' and 'off'.<br></li>
  322. <li>count<br>
  323. returns the current count. Contains the number of toggles of the arduino-pin.<br>
  324. Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li>
  325. <li>alarm<br>
  326. returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')<br>
  327. 'alarm' doesn't clear itself, has to be set to 'off' explicitly.</li>
  328. <li>state<br>
  329. returns the 'state' reading</li>
  330. </ul><br>
  331. <a name="FRM_INattr"></a>
  332. <b>Attributes</b><br>
  333. <ul>
  334. <li>activeLow &lt;yes|no&gt;</li>
  335. <li>count-mode none|rising|falling|both<br>
  336. Determines whether 'rising' (transitions from 'off' to 'on') of falling (transitions from 'on' to 'off')<br>
  337. edges (or 'both') are counted. Defaults to 'none'</li>
  338. <li>count-threshold &lt;number&gt;<br>
  339. sets the theshold-value for the counter. Whenever 'count' reaches the 'count-threshold' 'alarm' is<br>
  340. set to 'on'. Use 'set alarm off' to clear the alarm.</li>
  341. <li>reset-on-threshold-reached yes|no<br>
  342. if set to 'yes' reset the counter to 0 when the threshold is reached (defaults to 'no').
  343. </li>
  344. <li>internal-pullup on|off<br>
  345. allows to switch the internal pullup resistor of arduino to be en-/disabled. Defaults to off.
  346. </li>
  347. <li><a href="#IODev">IODev</a><br>
  348. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  349. than one FRM-device defined.)
  350. </li>
  351. <li><a href="#eventMap">eventMap</a><br></li>
  352. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  353. </ul><br>
  354. <a name="FRM_INnotes"></a>
  355. <b>Notes</b><br>
  356. <ul>
  357. <li>attribute <i>stateFormat</i><br>
  358. In most cases it is a good idea to assign "reading" to the attribute <i>stateFormat</i>. This will show the state
  359. of the pin in the web interface.
  360. </li>
  361. </ul>
  362. </ul><br>
  363. =end html
  364. =cut