20_FRM_AD.pm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ##############################################
  2. # $Id: 20_FRM_AD.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 %gets = (
  18. "reading" => "",
  19. "state" => "",
  20. "alarm-upper-threshold" => "off",
  21. "alarm-lower-threshold" => "off",
  22. );
  23. sub
  24. FRM_AD_Initialize($)
  25. {
  26. my ($hash) = @_;
  27. $hash->{AttrFn} = "FRM_AD_Attr";
  28. $hash->{GetFn} = "FRM_AD_Get";
  29. $hash->{DefFn} = "FRM_Client_Define";
  30. $hash->{InitFn} = "FRM_AD_Init";
  31. $hash->{AttrList} = "IODev upper-threshold lower-threshold $main::readingFnAttributes";
  32. main::LoadModule("FRM");
  33. }
  34. sub
  35. FRM_AD_Init($$)
  36. {
  37. my ($hash,$args) = @_;
  38. my $ret = FRM_Init_Pin_Client($hash,$args,PIN_ANALOG);
  39. return $ret if (defined $ret);
  40. my $firmata = $hash->{IODev}->{FirmataDevice};
  41. my $name = $hash->{NAME};
  42. $firmata->observe_analog($hash->{PIN},\&FRM_AD_observer,$hash);
  43. $main::defs{$name}{resolution}=$firmata->{metadata}{analog_resolutions}{$hash->{PIN}} if (defined $firmata->{metadata}{analog_resolutions});
  44. if (! (defined AttrVal($name,"stateFormat",undef))) {
  45. $main::attr{$name}{"stateFormat"} = "reading";
  46. }
  47. if (! (defined AttrVal($name,"event-min-interval",undef))) {
  48. $main::attr{$name}{"event-min-interval"} = 5;
  49. }
  50. main::readingsSingleUpdate($hash,"state","Initialized",1);
  51. return undef;
  52. }
  53. sub
  54. FRM_AD_observer
  55. {
  56. my ($pin,$old,$new,$hash) = @_;
  57. my $name = $hash->{NAME};
  58. Log3 $name,6,"onAnalogMessage for pin ".$pin.", old: ".(defined $old ? $old : "--").", new: ".(defined $new ? $new : "--");
  59. main::readingsBeginUpdate($hash);
  60. main::readingsBulkUpdate($hash,"reading",$new,1);
  61. my $upperthresholdalarm = ReadingsVal($name,"alarm-upper-threshold","off");
  62. if ( $new < AttrVal($name,"upper-threshold",1024) ) {
  63. if ( $upperthresholdalarm eq "on" ) {
  64. main::readingsBulkUpdate($hash,"alarm-upper-threshold","off",1);
  65. }
  66. my $lowerthresholdalarm = ReadingsVal($name,"alarm-lower-threshold","off");
  67. if ( $new > AttrVal($name,"lower-threshold",-1) ) {
  68. if ( $lowerthresholdalarm eq "on" ) {
  69. main::readingsBulkUpdate($hash,"alarm-lower-threshold","off",1);
  70. }
  71. } else {
  72. if ( $lowerthresholdalarm eq "off" ) {
  73. main::readingsBulkUpdate($hash,"alarm-lower-threshold","on",1);
  74. }
  75. }
  76. } else {
  77. if ( $upperthresholdalarm eq "off" ) {
  78. main::readingsBulkUpdate($hash,"alarm-upper-threshold","on",1);
  79. }
  80. };
  81. main::readingsEndUpdate($hash,1);
  82. }
  83. sub
  84. FRM_AD_Get($)
  85. {
  86. my ($hash,@a) = @_;
  87. my $name = shift @a;
  88. my $cmd = shift @a;
  89. my $ret;
  90. ARGUMENT_HANDLER: {
  91. $cmd eq "reading" and do {
  92. eval {
  93. return FRM_Client_FirmataDevice($hash)->analog_read($hash->{PIN});
  94. };
  95. return $@;
  96. };
  97. ( $cmd eq "alarm-upper-threshold" or $cmd eq "alarm-lower-threshold" or $cmd eq "state" ) and do {
  98. return main::ReadingsVal($name,"count",$gets{$cmd});
  99. };
  100. }
  101. return undef;
  102. }
  103. sub
  104. FRM_AD_Attr($$$$) {
  105. my ($command,$name,$attribute,$value) = @_;
  106. my $hash = $main::defs{$name};
  107. eval {
  108. if ($command eq "set") {
  109. ARGUMENT_HANDLER: {
  110. $attribute eq "IODev" and do {
  111. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  112. FRM_Client_AssignIOPort($hash,$value);
  113. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  114. }
  115. last;
  116. };
  117. }
  118. }
  119. };
  120. if ($@) {
  121. $@ =~ /^(.*)( at.*FHEM.*)$/;
  122. $hash->{STATE} = "error setting $attribute to $value: ".$1;
  123. return "cannot $command attribute $attribute to $value for $name: ".$1;
  124. }
  125. }
  126. 1;
  127. =pod
  128. =begin html
  129. <a name="FRM_AD"></a>
  130. <h3>FRM_AD</h3>
  131. <ul>
  132. represents a pin of an <a href="http://www.arduino.cc">Arduino</a> running <a href="http://www.firmata.org">Firmata</a>
  133. configured for analog input.<br>
  134. The value read is stored in reading 'state'. Range is from 0 to 1023 (10 Bit)<br>
  135. Requires a defined <a href="#FRM">FRM</a>-device to work.<br><br>
  136. <a name="FRM_ADdefine"></a>
  137. <b>Define</b>
  138. <ul>
  139. <code>define &lt;name&gt; FRM_AD &lt;pin&gt;</code> <br>
  140. Defines the FRM_AD device. &lt;pin&gt; is the arduino-pin to use.
  141. </ul>
  142. <br>
  143. <a name="FRM_ADset"></a>
  144. <b>Set</b><br>
  145. <ul>
  146. N/A<br>
  147. </ul><br>
  148. <a name="FRM_ADget"></a>
  149. <b>Get</b><br>
  150. <ul>
  151. <li>reading<br>
  152. returns the voltage-level read on the arduino-pin. Values range from 0 to 1023.</li>
  153. <li>alarm-upper-threshold<br>
  154. returns the current state of 'alarm-upper-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
  155. 'alarm-upper-threshold' turns 'on' whenever the 'reading' is higher than the attribute 'upper-threshold'<br>
  156. it turns 'off' again as soon 'reading' falls below 'alarm-upper-threshold'</li>
  157. <li>alarm-lower-threshold<br>
  158. returns the current state of 'alarm-lower-threshold'. Values are 'on' and 'off' (Defaults to 'off')<br>
  159. 'alarm-lower-threshold' turns 'on' whenever the 'reading' is lower than the attribute 'lower-threshold'<br>
  160. it turns 'off' again as soon 'reading rises above 'alarm-lower-threshold'</li>
  161. <li>state<br>
  162. returns the 'state' reading</li>
  163. </ul><br>
  164. <a name="FRM_ADattr"></a>
  165. <b>Attributes</b><br>
  166. <ul>
  167. <li>upper-threshold<br>
  168. sets the 'upper-threshold'. Whenever the 'reading' exceeds this value 'alarm-upper-threshold' is set to 'on'<br>
  169. As soon 'reading' falls below the 'upper-threshold' 'alarm-upper-threshold' turns 'off' again<br>
  170. Defaults to 1024.</li>
  171. <li>lower-threshold<br>
  172. sets the 'lower-threshold'. Whenever the 'reading' falls below this value 'alarm-lower-threshold' is set to 'on'<br>
  173. As soon 'reading' rises above the 'lower-threshold' 'alarm-lower-threshold' turns 'off' again<br>
  174. Defaults to -1.</li>
  175. <li><a href="#IODev">IODev</a><br>
  176. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  177. than one FRM-device defined.)
  178. </li>
  179. <li><a href="#eventMap">eventMap</a><br></li>
  180. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  181. </ul>
  182. </ul>
  183. <br>
  184. =end html
  185. =cut