00_LIRC.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ##############################################
  2. # $Id: 00_LIRC.pm 11984 2016-08-19 12:47:50Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Time::HiRes qw(gettimeofday);
  7. use Lirc::Client;
  8. use IO::Select;
  9. #####################################
  10. # Note: we are a data provider _and_ a consumer at the same time
  11. sub
  12. LIRC_Initialize($)
  13. {
  14. my ($hash) = @_;
  15. # Provider
  16. $hash->{ReadFn} = "LIRC_Read";
  17. $hash->{ReadyFn} = "LIRC_Ready";
  18. $hash->{Clients} = ":LIRC:";
  19. # Consumer
  20. $hash->{DefFn} = "LIRC_Define";
  21. $hash->{UndefFn} = "LIRC_Undef";
  22. $hash->{AttrList}= "";
  23. }
  24. #####################################
  25. sub
  26. LIRC_Define($$)
  27. {
  28. my ($hash, $def) = @_;
  29. my @a = split("[ \t][ \t]*", $def);
  30. $hash->{STATE} = "Initialized";
  31. $hash->{LircObj}->clean_up() if($hash->{LircObj});
  32. delete $hash->{LircObj};
  33. delete $hash->{FD};
  34. my $name = $a[0];
  35. my $config = $a[2];
  36. Log3 $name, 3, "LIRC opening $name device $config";
  37. my $lirc = Lirc::Client->new({
  38. prog => 'fhem',
  39. rcfile => "$config",
  40. debug => 0,
  41. fake => 0,
  42. });
  43. return "Can't open $config: $!\n" if(!$lirc);
  44. Log3 $name, 3, "LIRC opened $name device $config";
  45. my $select = IO::Select->new();
  46. $select->add( $lirc->sock );
  47. $hash->{LircObj} = $lirc;
  48. $hash->{FD} = $lirc->{sock}->fileno; # is not working and sets timeout to undefined
  49. $selectlist{"$name"} = $hash; #
  50. #$readyfnlist{"$name"} = $hash; # thats why we start polling
  51. $hash->{SelectObj} = $select;
  52. $hash->{DeviceName} = $name;
  53. $hash->{STATE} = "Opened";
  54. return undef;
  55. }
  56. #####################################
  57. sub
  58. LIRC_Undef($$)
  59. {
  60. my ($hash, $arg) = @_;
  61. $hash->{LircObj}->clean_up() if($hash->{LircObj});
  62. delete $hash->{LircObj};
  63. delete $hash->{FD};
  64. return undef;
  65. }
  66. #####################################
  67. sub
  68. LIRC_Read($)
  69. {
  70. my ($hash) = @_;
  71. my $lirc= $hash->{LircObj};
  72. my $select= $hash->{SelectObj};
  73. if( my @ready = $select->can_read(0) ){
  74. # an ir event has been received (if you are tracking other filehandles, you need to make sure it is lirc)
  75. my @codes = $lirc->next_codes; # should not block
  76. my $name = $hash->{NAME};
  77. for my $code (@codes){
  78. Log3 $name, 3, "LIRC $name $code";
  79. DoTrigger($name, $code);
  80. }
  81. }
  82. }
  83. #####################################
  84. sub
  85. LIRC_Ready($)
  86. {
  87. my ($hash) = @_;
  88. my $select= $hash->{SelectObj};
  89. return $select->can_read(0);
  90. }
  91. 1;
  92. =pod
  93. =item summary connection to the Linux Infrared Server (lirc)
  94. =item summary_DE Anbindung der Linux Infrared (lirc) Servers
  95. =begin html
  96. <a name="LIRC"></a>
  97. <h3>LIRC</h3>
  98. <ul>
  99. Generate FHEM-events when an LIRC device receives infrared signals.
  100. <br><br>
  101. Note: this module needs the Lirc::Client perl module.
  102. <br><br>
  103. <a name="LIRCdefine"></a>
  104. <b>Define</b>
  105. <ul>
  106. define &lt;name&gt; LIRC &lt;lircrc_file&gt;<br>
  107. Example:<br>
  108. <ul>
  109. define Lirc LIRC /etc/lirc/lircrc
  110. </ul>
  111. Note: In the lirc configuration file you have to define each possible event.
  112. If you have this configuration
  113. <pre>
  114. begin
  115. prog = fhem
  116. button = pwr
  117. config = IrPower
  118. end</pre>
  119. and you press the pwr button the IrPower toggle event occures at fhem.
  120. <pre>
  121. define IrPower01 notify Lirc:IrPower set lamp toggle</pre>
  122. turns the lamp on and off.
  123. If you want a faster reaction to keypresses you have to change the
  124. defaultvalue of readytimeout from 5 seconds to e.g. 1 second in fhem.pl
  125. </ul>
  126. <br>
  127. <a name="LIRCset"></a>
  128. <b>Set</b> <ul>N/A</ul><br>
  129. <a name="LIRCget"></a>
  130. <b>Get</b> <ul>N/A</ul><br>
  131. <a name="LIRCattr"></a>
  132. <b>Attributes</b>
  133. <ul>
  134. </ul><br>
  135. </ul>
  136. =end html
  137. =cut