jump to navigation

Infrared in Linux: LIRC July 15, 2008

Posted by idebian in Debian, GNU/Linux, Hardware.
Tags: ,
trackback

LIRC is the Linux Infrared Remote Control framework: it’s widely used to control the computer with an IR remote (connected either to a USB or a Serial Port) and to send IR commands, if the hardware supports this feature – unfortunately mine don’t 😦

In this post I’ll show how to configure the iMac Apple Remote to work in Linux: this will let us control our favorite media player (Totem, Xine, MPlayer, Rhythmbox) comfortably sitting on our sofà 😉

Introduction

From a hardware point of view, the Apple iMac IR receiver is connected via the USB bus.
Linux offers mainly two different approaches to manage the Apple IR remote:

  1. HID device (raw) – usbhid manages the receiver and LIRC handles remote signals with the macmini driver
  2. input device (event) – using the appleir kernel module, the receiver acts as a USB input device (as a small USB 6 keys keyboard); LIRC is no necessary here, inputlirc can handle the remote events generated by the kernel

inputlirc is a small daemon that acts like a LIRC daemon, reading /dev/input/eventX (i.e. device managed by the input module) and translating kernel’s key events directly to useful symbolic names, without any further configuration necessary (i.e. lircd.conf).

Since it seems from here that the first method is the most recent and the way to go, I choose that one.

LIRC

LIRC decodes IR signals received by the device drivers and provides the information to the higher software level. The whole process involves:

  • a kernel driver that manage the hardware receiver
  • the lirc daemon (lircd) which decodes the driver IR commands
  • the software clients that translate IR commands to specific actions

A detailed diagram of the different components is represented in the figure below:

LIRC Architecture

In the Apple iMac specific case, the IR receiver is connected to the USB bus and supported by the stock kernel through the usbhid module; the received signals are accessible through /dev/usb/hiddevX (blue block’s of the figure above).

Depending on the specific receiver driver, the infrared signals, captured by the receiver, are passed to the lirc daemon (lircd) on a specific /dev file.
lircd translates the received signal in a specific key-pressed event with the aims of its configuration file (/etc/lircd.conf) and notifies this event via socket (/dev/lircd) and/or TCP/IP (port 8765).

The upper layer software can manage LIRC events in two different ways:

  • natively, with the aims of a configuration file (~/.lircrc) that map key-pressed events to specific actions
  • through irxevent, which is a LIRC native client, that send the key-pressed event to the software as if were pressed on the keyboard

Obviously, if a certain program support LIRC natively that’s the way to go!

Now, the whole process to get a functional remote involves the following steps:

  • LIRC installation
  • lircd configuration (lircd.conf file) and verification
  • key-action definition (~/.lircrc configuration file)

Hands on keyboard

As usual after a bit of theory of operations let’s start the funniest part 😀

Let’s start from the hardware identification:

$ lsusb
...
Bus 005 Device 004: ID 05ac:8242 Apple, Inc.
...
$ more /proc/bus/usb/devices
...
T: Bus=05 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=1.5 MxCh= 0
D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=05ac ProdID=8242 Rev= 0.16
S: Manufacturer=Apple Computer, Inc.
S: Product=IR Receiver
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=usbhid
E: Ad=82(I) Atr=03(Int.) MxPS= 8 Ivl=10ms
...
$ ls -l /dev/usb/
total 0
crw-rw---- 1 root root 180, 96 2008-07-13 07:48 hiddev0

LIRC installation

First of all, let’s install lirc and all its dependant packages; using the command line, this is as easy as type:

$ sudo apt-get install lirc

lircd configuration and verification

In Debian the lircd configuration files are located in /etc/lirc directory; here two files need to be edited:

  • hardware.conf, contains hardware options (kernel & lirc modules to load, device file, etc)
  • lircd.conf, the main lircd configuration file, contains remote definition in terms of key scan codes

For the hardware.conf file I just inserted macmini as the lirc driver and /dev/usb/hiddev0 as the device file to listen on. No kernel modules need to be loaded.
For sake of completeness, here is my hardware.conf:

$ more /etc/lirc/hardware.conf
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS=""
#Don't start lircmd even if there seems to be a good config file
#START_LIRCMD=false
#Don't start irexec, even if a good config file seems to exist.
#START_IREXEC=false
#Try to load appropriate kernel modules
LOAD_MODULES=true
# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="macmini"
# If DEVICE is set to /dev/lirc and udev is in use /dev/lirc0 will be
# automatically used instead
DEVICE="/dev/usb/hiddev0"
MODULES=""
# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""

Let’s generate the /etc/lircd.conf file directly scanning the key codes from the receiver using the LIRC irrecord tool (or you can look for a proper pre-built file for your remote):

$ sudo irrecord -H macmini -d /dev/usb/hiddev0 /etc/lirc/lircd.conf
$ more /etc/lirc/lircd.conf
begin remote
name Apple_A1156
bits 8
eps 30
aeps 100
one 0 0
zero 0 0
pre_data_bits 24
pre_data 0x87EECF
gap 211981
toggle_bit_mask 0x0
begin codes
MENU 0x02
PLAY 0x04
NEXT 0x07
PREV 0x08
PLUS 0x0B
MINUS 0x0D
end codes
end remote

Now restart lircd and verify that the lircd is receiving remote inputs using LIRC irw tool:

$ sudo invoke-rc.d lirc restart
Stopping lirc daemon: irexec lircmd lircd.
Starting lirc daemon: lircd.
$ irw
0000000087eecf04 00 PLAY Apple_A1156
0000000087eecf07 00 NEXT Apple_A1156
0000000087eecf08 00 PREV Apple_A1156
0000000087eecf0d 00 MINUS Apple_A1156
0000000087eecf0b 00 PLUS Apple_A1156
0000000087eecf02 00 MENU Apple_A1156
0000000087eecf04 00 PLAY Apple_A1156
0000000087eecf04 00 PLAY Apple_A1156
^C

Clients configuration: lircrc file

We now have to set up the “client” side of lirc, by setting up an ~/.lircrc where we bind the key-pressed event to a specific action for each client. The format is really easy: this is an example for Totem

begin
prog = Totem
button = PLAY
config = play_pause
end
begin
prog = Totem
button = MENU
config = fullscreen
end
begin
prog = Totem
button = NEXT
config = next
end
begin
prog = Totem
button = PREV
config = previous
end
begin
prog = Totem
button = PLUS
config = volume_up
repeat = 1
end
begin
prog = Totem
button = MINUS
config = volume_down
repeat = 1
end

The repeat option:

tells the program what shall happen if a key is repeated. A value of zero tells the program to ignore repeated keys. Any other positive value ‘n’ tells the program to pass the config string every ‘n’-th time to the according application, when a key is repeated. The default for repeat is zero.

Obviously, the client should be made aware of the LIRC presence in order to receive events from the remote: this is usually done by means of plugins. For example in Totem go to Edit->Plugins and activate the ‘Infrared Remote Control’ plugin.

irxevent

For all the clients that do not natively support LIRC, irxevent sends key presses to X applications triggered by a LIRC driven remote control.
irxevent configuration is straightforward and follows the same rules as the previous case. For example here follows a couple of bindings for the PLUS and MINUS keys:

begin
prog = irxevent
button = PLUS
repeat = 1
config = Key Up CurrentWindow
end
begin
prog = irxevent
button = MINUS
repeat = 1
config = Key Down CurrentWindow
end

ENJOY 😀

References

  1. LIRC homepage
  2. LIRC page on MythTV wiki
  3. AppleMacBookProV3 on PmWiki
  4. Setting up Apple Remote Control with F9
Advertisements

Comments»

1. cippo - July 16, 2008

Very, interesting!
thanks for this useful how to!
🙂

2. viablepanic - May 20, 2010

This worked for me in Ubuntu 10.04,

For example instead of NEXT and PREV I used FORWARD and BACKWARD

begin
remote = Apple_A1156
prog = totem
button = VOLUP
config = volume_up
repeat = 1
delay = 0
end

begin
remote = Apple_A1156
prog = totem
button = VOLDOWN
config = volume_down
repeat = 1
delay = 0
end

begin
remote = Apple_A1156
prog = totem
button = BACKWARD
config = previous
repeat = 0
delay = 0
end

begin
remote = Apple_A1156
prog = totem
button = FORWARD
config = next
repeat = 0
delay = 0
end

begin
remote = Apple_A1156
prog = totem
button = PLAY
config = play_pause
repeat = 0
delay = 0
end

begin
remote = Apple_A1156
prog = totem
button = MENU
config = fullscreen
repeat = 0
delay = 0
end

3. Sebastià Matas - February 12, 2011

Hi! In case anyone out there needs the config file for their MacBookPro2,2, here it is.
# Please make this file available to others
# by sending it to
#
# this config file was automatically generated
# using lirc-0.8.3(macmini) on Sat Feb 12 17:22:19 2011
#
# contributed by
#
# brand: Aple_MacBookPro2,2
# model no. of remote control:
# devices being controlled by this remote:
#

begin remote

name dffd
bits 8
eps 30
aeps 100

one 0 0
zero 0 0
pre_data_bits 24
pre_data 0x87EE56
gap 211973
toggle_bit_mask 0x0

begin codes
MENU 0x02
FORWARD 0x07
REWIND 0x08
VOLUMEUP 0x0B
VOLUMEDOWN 0x0D
PLAYPAUSE 0x04
end codes

end remote

Kind regards
Sebastià

4. raj - February 9, 2014

Hi,

I am new to Infrared.

Could you please share sample code(java) to capture the IR signals received, if you have it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: