| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- ##############################################
- # $Id: 00_LIRC.pm 11984 2016-08-19 12:47:50Z rudolfkoenig $
- package main;
- use strict;
- use warnings;
- use Time::HiRes qw(gettimeofday);
- use Lirc::Client;
- use IO::Select;
- #####################################
- # Note: we are a data provider _and_ a consumer at the same time
- sub
- LIRC_Initialize($)
- {
- my ($hash) = @_;
- # Provider
- $hash->{ReadFn} = "LIRC_Read";
- $hash->{ReadyFn} = "LIRC_Ready";
- $hash->{Clients} = ":LIRC:";
- # Consumer
- $hash->{DefFn} = "LIRC_Define";
- $hash->{UndefFn} = "LIRC_Undef";
- $hash->{AttrList}= "";
- }
- #####################################
- sub
- LIRC_Define($$)
- {
- my ($hash, $def) = @_;
- my @a = split("[ \t][ \t]*", $def);
- $hash->{STATE} = "Initialized";
- $hash->{LircObj}->clean_up() if($hash->{LircObj});
- delete $hash->{LircObj};
- delete $hash->{FD};
- my $name = $a[0];
- my $config = $a[2];
- Log3 $name, 3, "LIRC opening $name device $config";
- my $lirc = Lirc::Client->new({
- prog => 'fhem',
- rcfile => "$config",
- debug => 0,
- fake => 0,
- });
- return "Can't open $config: $!\n" if(!$lirc);
- Log3 $name, 3, "LIRC opened $name device $config";
- my $select = IO::Select->new();
- $select->add( $lirc->sock );
- $hash->{LircObj} = $lirc;
-
- $hash->{FD} = $lirc->{sock}->fileno; # is not working and sets timeout to undefined
- $selectlist{"$name"} = $hash; #
- #$readyfnlist{"$name"} = $hash; # thats why we start polling
- $hash->{SelectObj} = $select;
- $hash->{DeviceName} = $name;
- $hash->{STATE} = "Opened";
- return undef;
- }
- #####################################
- sub
- LIRC_Undef($$)
- {
- my ($hash, $arg) = @_;
- $hash->{LircObj}->clean_up() if($hash->{LircObj});
- delete $hash->{LircObj};
- delete $hash->{FD};
- return undef;
- }
- #####################################
- sub
- LIRC_Read($)
- {
- my ($hash) = @_;
- my $lirc= $hash->{LircObj};
- my $select= $hash->{SelectObj};
- if( my @ready = $select->can_read(0) ){
- # an ir event has been received (if you are tracking other filehandles, you need to make sure it is lirc)
- my @codes = $lirc->next_codes; # should not block
- my $name = $hash->{NAME};
- for my $code (@codes){
- Log3 $name, 3, "LIRC $name $code";
- DoTrigger($name, $code);
- }
- }
- }
- #####################################
- sub
- LIRC_Ready($)
- {
- my ($hash) = @_;
- my $select= $hash->{SelectObj};
- return $select->can_read(0);
- }
- 1;
- =pod
- =item summary connection to the Linux Infrared Server (lirc)
- =item summary_DE Anbindung der Linux Infrared (lirc) Servers
- =begin html
- <a name="LIRC"></a>
- <h3>LIRC</h3>
- <ul>
- Generate FHEM-events when an LIRC device receives infrared signals.
- <br><br>
- Note: this module needs the Lirc::Client perl module.
- <br><br>
- <a name="LIRCdefine"></a>
- <b>Define</b>
- <ul>
- define <name> LIRC <lircrc_file><br>
- Example:<br>
- <ul>
- define Lirc LIRC /etc/lirc/lircrc
- </ul>
- Note: In the lirc configuration file you have to define each possible event.
- If you have this configuration
- <pre>
- begin
- prog = fhem
- button = pwr
- config = IrPower
- end</pre>
- and you press the pwr button the IrPower toggle event occures at fhem.
- <pre>
- define IrPower01 notify Lirc:IrPower set lamp toggle</pre>
- turns the lamp on and off.
- If you want a faster reaction to keypresses you have to change the
- defaultvalue of readytimeout from 5 seconds to e.g. 1 second in fhem.pl
- </ul>
- <br>
- <a name="LIRCset"></a>
- <b>Set</b> <ul>N/A</ul><br>
- <a name="LIRCget"></a>
- <b>Get</b> <ul>N/A</ul><br>
- <a name="LIRCattr"></a>
- <b>Attributes</b>
- <ul>
- </ul><br>
- </ul>
- =end html
- =cut
|