20_FRM_I2C.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ##############################################
  2. # $Id: 20_FRM_I2C.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. sub
  18. FRM_I2C_Initialize($)
  19. {
  20. my ($hash) = @_;
  21. $hash->{DefFn} = "FRM_Client_Define";
  22. $hash->{InitFn} = "FRM_I2C_Init";
  23. $hash->{UndefFn} = "FRM_Client_Undef";
  24. $hash->{AttrFn} = "FRM_I2C_Attr";
  25. $hash->{AttrList} = "IODev $main::readingFnAttributes";
  26. main::LoadModule("FRM");
  27. }
  28. sub
  29. FRM_I2C_Init($)
  30. {
  31. my ($hash,$args) = @_;
  32. my $u = "wrong syntax: define <name> FRM_I2C address register numbytes";
  33. return $u if(int(@$args) < 3);
  34. $hash->{"i2c-address"} = @$args[0];
  35. $hash->{"i2c-register"} = @$args[1];
  36. $hash->{"i2c-bytestoread"} = @$args[2];
  37. eval {
  38. FRM_Client_AssignIOPort($hash);
  39. FRM_Client_FirmataDevice($hash)->i2c_read(@$args[0],@$args[1],@$args[2]);
  40. };
  41. if ($@) {
  42. $@ =~ /^(.*)( at.*FHEM.*)$/;
  43. $hash->{STATE} = "error initializing: ".$1;
  44. return "error initializing '".$hash->{NAME}."': ".$1;
  45. }
  46. return "error calling i2c_read: ".$@ if ($@);
  47. if (! (defined AttrVal($hash->{NAME},"event-min-interval",undef))) {
  48. $main::attr{$hash->{NAME}}{"event-min-interval"} = 5;
  49. }
  50. return undef;
  51. }
  52. sub
  53. FRM_I2C_Attr($$$$) {
  54. my ($command,$name,$attribute,$value) = @_;
  55. my $hash = $main::defs{$name};
  56. eval {
  57. if ($command eq "set") {
  58. ARGUMENT_HANDLER: {
  59. $attribute eq "IODev" and do {
  60. if ($main::init_done and (!defined ($hash->{IODev}) or $hash->{IODev}->{NAME} ne $value)) {
  61. FRM_Client_AssignIOPort($hash,$value);
  62. FRM_Init_Client($hash) if (defined ($hash->{IODev}));
  63. }
  64. last;
  65. };
  66. }
  67. }
  68. };
  69. if ($@) {
  70. $@ =~ /^(.*)( at.*FHEM.*)$/;
  71. $hash->{STATE} = "error setting $attribute to $value: ".$1;
  72. return "cannot $command attribute $attribute to $value for $name: ".$1;
  73. }
  74. }
  75. 1;
  76. =pod
  77. =begin html
  78. <a name="FRM_I2C"></a>
  79. <h3>FRM_I2C</h3>
  80. <ul>
  81. represents an integrated curcuit connected to the i2c-pins of an <a href="http://www.arduino.cc">Arduino</a>
  82. running <a href="http://www.firmata.org">Firmata</a><br>
  83. Requires a defined <a href="#FRM">FRM</a>-device to work.<br>
  84. this FRM-device has to be configures for i2c by setting attr 'i2c-config' on the FRM-device<br>
  85. it reads out the ic-internal storage in intervals of 'sampling-interval' as set on the FRM-device<br><br>
  86. <a name="FRM_I2Cdefine"></a>
  87. <b>Define</b>
  88. <ul>
  89. <code>define &lt;name&gt; FRM_I2C &lt;i2c-address&gt; &lt;register&gt; &lt;bytes-to-read&gt;</code> <br>
  90. Specifies the FRM_I2C device.<br>
  91. <li>i2c-address is the (device-specific) address of the ic on the i2c-bus</li>
  92. <li>register is the (device-internal) address to start reading bytes from.</li>
  93. <li>bytes-to-read is the number of bytes read from the ic</li>
  94. </ul>
  95. <br>
  96. <a name="FRM_I2Cset"></a>
  97. <b>Set</b><br>
  98. <ul>
  99. N/A<br>
  100. </ul>
  101. <a name="FRM_I2Cget"></a>
  102. <b>Get</b><br>
  103. <ul>
  104. N/A<br>
  105. </ul><br>
  106. <a name="FRM_I2Cattr"></a>
  107. <b>Attributes</b><br>
  108. <ul>
  109. <li><a href="#IODev">IODev</a><br>
  110. Specify which <a href="#FRM">FRM</a> to use. (Optional, only required if there is more
  111. than one FRM-device defined.)
  112. </li>
  113. <li><a href="#eventMap">eventMap</a><br></li>
  114. <li><a href="#readingFnAttributes">readingFnAttributes</a><br></li>
  115. </ul>
  116. </ul>
  117. <br>
  118. =end html
  119. =cut