20_FRM_IN.pm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ##############################################
  2. # $Id: 20_FRM_IN.pm 5927 2014-05-21 21:56:37Z ntruchsess $
  3. ##############################################
  4. package main;
  5. use strict;
  6. use warnings;
  7. #add FHEM/lib to @INC if it's not allready included. Should rather be in fhem.pl than here though...
  8. BEGIN {
  9. if (!grep(/FHEM\/lib$/,@INC)) {
  10. foreach my $inc (grep(/FHEM$/,@INC)) {
  11. push @INC,$inc."/lib";
  12. };
  13. };
  14. };
  15. use Device::Firmata::Constants qw/ :all /;
  16. #####################################
  17. my %sets = (
  18. "alarm" => "",
  19. "count" => 0,
  20. );
  21. my %gets = (
  22. "reading" => "",
  23. "state" => "",
  24. "count" => 0,
  25. "alarm" => "off"
  26. );
  27. sub
  28. FRM_IN_Initialize($)
  29. {
  30. my ($hash) = @_;
  31. $hash->{SetFn} = "FRM_IN_Set";
  32. $hash->{GetFn} = "FRM_IN_Get";
  33. $hash->{AttrFn} = "FRM_IN_Attr";
  34. $hash->{DefFn} = "FRM_Client_Define";
  35. $hash->{InitFn} = "FRM_IN_Init";
  36. $hash->{UndefFn} = "FRM_Client_Undef";
  37. $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";
  38. main::LoadModule("FRM");
  39. }
  40. sub
  41. FRM_IN_Init($$)
  42. {
  43. my ($hash,$args) = @_;
  44. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_INPUT);
  45. return $ret if (defined $ret);
  46. eval {
  47. my $firmata = FRM_Client_FirmataDevice($hash);
  48. my $pin = $hash->{PIN};
  49. if (defined (my $pullup = AttrVal($hash->{NAME},"internal-pullup",undef))) {
  50. $firmata->digital_write($pin,$pullup eq "on" ? 1 : 0);
  51. }
  52. $firmata->observe_digital($pin,\&FRM_IN_observer,$hash);
  53. };
  54. return FRM_Catch($@) if $@;
  55. if (! (defined AttrVal($hash->{NAME},"stateFormat",undef))) {
  56. $main::attr{$hash->{NAME}}{"stateFormat"} = "reading";
  57. }
  58. main::readingsSingleUpdate($hash,"state","Initialized",1);
  59. return undef;
  60. }
  61. sub
  62. FRM_IN_observer
  63. {
  64. my ($pin,$old,$new,$hash) = @_;
  65. my $name = $hash->{NAME};
  66. Log3 $name,5,"onDigitalMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--");
  67. if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
  68. $old = $old == PIN_LOW ? PIN_HIGH : PIN_LOW if (defined $old);
  69. $new = $new == PIN_LOW ? PIN_HIGH : PIN_LOW;
  70. }
  71. my $changed = ((!(defined $old)) or ($old != $new));
  72. main::readingsBeginUpdate($hash);
  73. if ($changed) {
  74. if (defined (my $mode = main::AttrVal($name,"count-mode",undef))) {
  75. if (($mode eq "both")
  76. or (($mode eq "rising") and ($new == PIN_HIGH))
  77. or (($mode eq "falling") and ($new == PIN_LOW))) {
  78. my $count = main::ReadingsVal($name,"count",0);
  79. $count++;
  80. if (defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
  81. if ( $count > $threshold ) {
  82. if (AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
  83. $count=0;
  84. main::readingsBulkUpdate($hash,"alarm","on",1);
  85. } elsif ( main::ReadingsVal($name,"alarm","off") ne "on" ) {
  86. main::readingsBulkUpdate($hash,"alarm","on",1);
  87. }
  88. }
  89. }
  90. main::readingsBulkUpdate($hash,"count",$count,1);
  91. }
  92. };
  93. }
  94. main::readingsBulkUpdate($hash,"reading",$new == PIN_HIGH ? "on" : "off", $changed);
  95. main::readingsEndUpdate($hash,1);
  96. }
  97. sub
  98. FRM_IN_Set
  99. {
  100. my ($hash, @a) = @_;
  101. return "Need at least one parameters" if(@a < 2);
  102. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %sets)
  103. if(!defined($sets{$a[1]}));
  104. my $command = $a[1];
  105. my $value = $a[2];
  106. COMMAND_HANDLER: {
  107. $command eq "alarm" and do {
  108. return undef if (!($value eq "off" or $value eq "on"));
  109. main::readingsSingleUpdate($hash,"alarm",$value,1);
  110. last;
  111. };
  112. $command eq "count" and do {
  113. main::readingsSingleUpdate($hash,"count",$value,1);
  114. last;
  115. };
  116. }
  117. }
  118. sub
  119. FRM_IN_Get($)
  120. {
  121. my ($hash, @a) = @_;
  122. return "Need at least one parameters" if(@a < 2);
  123. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %gets)
  124. if(!defined($gets{$a[1]}));
  125. my $name = shift @a;
  126. my $cmd = shift @a;
  127. ARGUMENT_HANDLER: {
  128. $cmd eq "reading" and do {
  129. eval {
  130. return FRM_Client_FirmataDevice($hash)->digital_read($hash->{PIN}) == PIN_HIGH ? "on" : "off";
  131. };
  132. return $@;
  133. };
  134. ( $cmd eq "count" or $cmd eq "alarm" or $cmd eq "state" ) and do {
  135. return main::ReadingsVal($name,"count",$gets{$cmd});
  136. };
  137. }
  138. return undef;
  139. }
  140. sub
  141. FRM_IN_Attr($$$$) {
  142. my ($command,$name,$attribute,$value) = @_;
  143. my $hash = $main::defs{$name};
  144. my $pin = $hash->{PIN};
  145. eval {
  146. if ($command eq "set") {
  147. ARGUMENT_HANDLER: {
  148. $attribute eq "IODev" and do {
  149. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  150. FRM_Client_AssignIOPort($hash,$value);
  151. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  152. }
  153. last;
  154. };
  155. $attribute eq "count-mode" and do {
  156. if ($value ne "none" and !defined main::ReadingsVal($name,"count",undef)) {
  157. main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
  158. }
  159. last;
  160. };
  161. $attribute eq "reset-on-threshold-reached" and do {
  162. if ($value eq "yes"
  163. and defined (my $threshold = main::AttrVal($name,"count-threshold",undef))) {
  164. if (main::ReadingsVal($name,"count",0) > $threshold) {
  165. main::readingsSingleUpdate($main::defs{$name},"count",$sets{count},1);
  166. }
  167. }
  168. last;
  169. };
  170. $attribute eq "count-threshold" and do {
  171. if (main::ReadingsVal($name,"count",0) > $value) {
  172. main::readingsBeginUpdate($hash);
  173. if (main::ReadingsVal($name,"alarm","off") ne "on") {
  174. main::readingsBulkUpdate($hash,"alarm","on",1);
  175. }
  176. if (main::AttrVal($name,"reset-on-threshold-reached","no") eq "yes") {
  177. main::readingsBulkUpdate($main::defs{$name},"count",0,1);
  178. }
  179. main::readingsEndUpdate($hash,1);
  180. }
  181. last;
  182. };
  183. $attribute eq "internal-pullup" and do {
  184. if ($main::init_done) {
  185. my $firmata = FRM_Client_FirmataDevice($hash);
  186. $firmata->digital_write($pin,$value eq "on" ? 1 : 0);
  187. #ignore any errors here, the attribute-value will be applied next time FRM_IN_init() is called.
  188. }
  189. last;
  190. };
  191. $attribute eq "activeLow" and do {
  192. my $oldval = AttrVal($hash->{NAME},"activeLow","no");
  193. if ($oldval ne $value) {
  194. $main::attr{$hash->{NAME}}{activeLow} = $value;
  195. if ($main::init_done) {
  196. my $firmata = FRM_Client_FirmataDevice($hash);
  197. FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
  198. }
  199. };
  200. last;
  201. };
  202. }
  203. } elsif ($command eq "del") {
  204. ARGUMENT_HANDLER: {
  205. $attribute eq "internal-pullup" and do {
  206. my $firmata = FRM_Client_FirmataDevice($hash);
  207. $firmata->digital_write($pin,0);
  208. last;
  209. };
  210. $attribute eq "activeLow" and do {
  211. if (AttrVal($hash->{NAME},"activeLow","no") eq "yes") {
  212. delete $main::attr{$hash->{NAME}}{activeLow};
  213. my $firmata = FRM_Client_FirmataDevice($hash);
  214. FRM_IN_observer($pin,undef,$firmata->digital_read($pin),$hash);
  215. };
  216. last;
  217. };
  218. }
  219. }
  220. };
  221. if (my $error = FRM_Catch($@)) {
  222. $hash->{STATE} = "error setting $attribute to $value: ".$error;
  223. return "cannot $command attribute $attribute to $value for $name: ".$error;
  224. }
  225. }
  226. 1;
  227. =pod
  228. =begin html
  229. <a name="FRM_IN"></a>
  230. <h3>FRM_IN</h3>
  231. <ul>
  232. represents a pin of an <a href="http://www.arduino.cc">Arduino</a> running <a href="http://www.firmata.org">Firmata</a>
  233. configured for digital input.<br>
  234. The current state of the arduino-pin is stored in reading 'state'. Values are 'on' and 'off'.<br>
  235. Requires a defined <a href="#FRM">FRM</a>-device to work.<br><br>
  236. <a name="FRM_INdefine"></a>
  237. <b>Define</b>
  238. <ul>
  239. <code>define &lt;name&gt; FRM_IN &lt;pin&gt;</code> <br>
  240. Defines the FRM_IN device. &lt;pin&gt> is the arduino-pin to use.
  241. </ul>
  242. <br>
  243. <a name="FRM_INset"></a>
  244. <b>Set</b><br>
  245. <ul>
  246. <li>alarm on|off<br>
  247. set the alarm to on or off. Used to clear the alarm.<br>
  248. The alarm is set to 'on' whenever the count reaches the threshold and doesn't clear itself.</li>
  249. </ul>
  250. <a name="FRM_INget"></a>
  251. <b>Get</b>
  252. <ul>
  253. <li>reading<br>
  254. returns the logical state of the arduino-pin. Values are 'on' and 'off'.<br></li>
  255. <li>count<br>
  256. returns the current count. Contains the number of toggles of the arduino-pin.<br>
  257. Depending on the attribute 'count-mode' every rising or falling edge (or both) is counted.</li>
  258. <li>alarm<br>
  259. returns the current state of 'alarm'. Values are 'on' and 'off' (Defaults to 'off')<br>
  260. 'alarm' doesn't clear itself, has to be set to 'off' eplicitly.</li>
  261. <li>state<br>
  262. returns the 'state' reading</li>
  263. </ul><br>
  264. <a name="FRM_INattr"></a>
  265. <b>Attributes</b><br>
  266. <ul>
  267. <li>activeLow &lt;yes|no&gt;</li>
  268. <li>count-mode none|rising|falling|both<br>
  269. Determines whether 'rising' (transitions from 'off' to 'on') of falling (transitions from 'on' to 'off')<br>
  270. edges (or 'both') are counted. Defaults to 'none'</li>
  271. <li>count-threshold &lt;number&gt;<br>
  272. sets the theshold-value for the counter. Whenever 'count' reaches the 'count-threshold' 'alarm' is<br>
  273. set to 'on'. Use 'set alarm off' to clear the alarm.</li>
  274. <li>reset-on-threshold-reached yes|no<br>
  275. if set to 'yes' reset the counter to 0 when the threshold is reached (defaults to 'no').
  276. </li>
  277. <li>internal-pullup on|off<br>
  278. allows to switch the internal pullup resistor of arduino to be en-/disabled. Defaults to off.
  279. </li>
  280. <li><a href="#IODev">IODev</a><br>
  281. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  282. than one FRM-device defined.)
  283. </li>
  284. <li><a href="#eventMap">eventMap</a><br></li>
  285. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  286. </ul>
  287. </ul>
  288. <br>
  289. =end html
  290. =cut