Linux Infrared Control Part 4: Trigger Apps via IR

Would you like to use a remote control to control your computer? How about using your computer to send remote control signals to your TV or Stereo instead of using the remote?

Introduction

This is the fourth in a series of posts where I will describe how to use a Raspberry Pi, Raspbian and Linux Infrared Remote Control (Lirc) to receive and send infrared remote control signals. This post will be focused on how to trigger applications and depends on Linux Infrared Control Part 1: Receive IR and Linux Infrared Control Part 2: Send IR.

Ever wanted to use a remote to change a song on your computer, just like your tv? There are mobile phone apps, but they don’t have the same ability to find a button without looking and aren’t as durable as a remote.

There are other creative possibilities, including mapping one remote to another. That is to say, using a different remote with an existing device. Capture both remotes, the configure `irexec` to use the preferred remote to call `irsend` to control the device.

Lets start at the beginning, `irw` and `irexec`.

What are irw and irexec?

The Lirc `irexec` is an app that can be configured to launch an program or script when a remote button is press is received. For example you could stop or reboot the computer, change the volume or refresh the screen. `irexec` is dependent on correctly configured IR receiver and at least one remote.

The Lirc `irw` app is used to confirm the button presses are being sent through `/var/run/lirc/lircd` and will simply output the remote and buttons received.

Reference:
http://www.lirc.org/html/irw.html
http://www.lirc.org/html/irexec.html

Using irw

First, go through the buttons on your remote and confirm that they are correctly configured. If only a few buttons were configured, like in Linux Infrared Control Part 1: Receive IR, then take this time to re-run `irrecord` and create a complete lirc.conf configuration file for your remote.

As an example, here is a lirc.conf file for Microsoft’s Xbox One Media Remote:

# Please take the time to finish this file as described in
# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/
# and make it available to others by sending it to
# <lirc@bartelmus.de>
#
# This config file was automatically generated
# using lirc-0.9.4c(default) on Wed Nov 29 02:20:00 2017
# Command line used: -d /dev/lirc0
# Kernel version (uname -r): 4.9.58+
#
# Remote name (as of config file): xbox-one-media-remote
# Brand of remote device, the thing you hold in your hand:
# Remote device model nr:
# Remote device info url:
# Does remote device has a bundled capture device e. g., a
#     usb dongle? :
# For bundled USB devices: usb vendor id, product id
#     and device string (use dmesg or lsusb):
# Type of device controlled
#     (TV, VCR, Audio, DVD, Satellite, Cable, HTPC, ...) :
# Device(s) controlled by this remote:

begin remote

  name  xbox-one-media-remote
  bits           32
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9064  4498
  one           590  1667
  zero          590   539
  ptrail        588
  repeat       9063  2238
  gap          108705
  toggle_bit_mask 0x0
  frequency    38000

      begin codes
          KEY_POWER                0x011B26D9 
          KEY_CYCLEWINDOWS         0x011B7689
          KEY_MENU                 0x011BF609
          KEY_UP                   0x011B7887
          KEY_LEFT                 0x011B04FB
          KEY_SELECT               0x011B44BB
          KEY_RIGHT                0x011B847B
          KEY_DOWN                 0x011BF807
          KEY_BACK                 0x011BC43B
          KEY_INFO                 0x011B649B
          KEY_CHANNELUP            0x011B08F7
          KEY_CHANNELDOWN          0x011B8877
          KEY_MUTE                 0x011B708F
          KEY_VOLUMEUP             0x011B48B7
          KEY_VOLUMEDOWN           0x011BC837
          KEY_REWIND               0x011BA857
          KEY_PLAYPAUSE            0x011B0EF1
          KEY_FASTFORWARD          0x011B28D7
          KEY_LAST                 0x011BD827
          KEY_STOP                 0x011B9867
          KEY_NEXT                 0x011B58A7
      end codes

end remote
</lirc@bartelmus.de>

Next, copy this lirc.conf configuration file to `/etc/lirc/lircd.conf.d/`:

sudo cp xbox-one-media-remote.lircd.conf /etc/lirc/lircd.conf.d/

Test the configuration file by restarting the `lircd.service` and running `irw`:

sudo systemctl restart lircd.service
irw

Then start pressing buttons and if everything is working you should see something like this:

00000000011b847b 00 KEY_RIGHT xbox-one-media-remote
00000000011b847b 01 KEY_RIGHT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011b44bb 01 KEY_SELECT xbox-one-media-remote
00000000011b44bb 02 KEY_SELECT xbox-one-media-remote
00000000011b44bb 03 KEY_SELECT xbox-one-media-remote
00000000011b44bb 04 KEY_SELECT xbox-one-media-remote
00000000011b44bb 05 KEY_SELECT xbox-one-media-remote
00000000011b44bb 06 KEY_SELECT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011bc43b 00 KEY_BACK xbox-one-media-remote
00000000011bc43b 01 KEY_BACK xbox-one-media-remote
00000000011b7887 00 KEY_UP xbox-one-media-remote
00000000011b7887 01 KEY_UP xbox-one-media-remote
00000000011bf807 00 KEY_DOWN xbox-one-media-remote
00000000011bf807 01 KEY_DOWN xbox-one-media-remote
00000000011b04fb 00 KEY_LEFT xbox-one-media-remote
00000000011b04fb 01 KEY_LEFT xbox-one-media-remote
00000000011b847b 00 KEY_RIGHT xbox-one-media-remote
00000000011b847b 01 KEY_RIGHT xbox-one-media-remote
00000000011b44bb 00 KEY_SELECT xbox-one-media-remote
00000000011b44bb 01 KEY_SELECT xbox-one-media-remote
00000000011b708f 00 KEY_MUTE xbox-one-media-remote
00000000011b08f7 00 KEY_CHANNELUP xbox-one-media-remote
00000000011b8877 00 KEY_CHANNELDOWN xbox-one-media-remote
00000000011b48b7 00 KEY_VOLUMEUP xbox-one-media-remote

Configure irexec

With all the buttons configured, next step is to configure `irexec` by editing `/etc/lirc/irexec.lircrc` and make an entry for as many of the buttons as you want.

For example, the following will echo the navigation buttons (up, down, left, right) when pressed:

sudo vi /etc/lirc/irexec.lircrc
begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_UP
    config = echo "KEY_UP"
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_LEFT
    config = echo "KEY_LEFT"
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_RIGHT
    config = echo "KEY_RIGHT"
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_DOWN
    config = echo "KEY_DOWN"
end

Restart the irexec service to load the changes:

sudo systemctl restart irexec.service

To see the `echo` statements above, tail the log for the `irexec.service`:

journalctl -u irexec.service -f

Start pressing buttons to confirm your configuration.

Configure irexec to Refresh Screen and Control Volume

With `irexec` working now the fun can begin. Below is an example `irexec.lircrc` that has 5 buttons configured.

The first configuration maps `KEY_POWER` with the command to shutdown the computer, `shutdown -h now`. As by default `irexec` runs as root, sudo is not needed.

The second configuration will refresh the screen for the Chromium browser. This could be useful if there is a browser running fullscreen with no keyboard. It sets the `DISPLAY` environment variable, uses `xdotool` look for a window called ‘chromium’ and sends a control R key combination to refresh the screen. This requires ‘xdotool’ tool to be installed.

The last three configurations use `amixer` to set the volume levels and mute the audio. Similarly, they require amixer to be installed.

sudo vi /etc/lirc/irexec.lircrc
begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_POWER
    config = shutdown -h now
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_CYCLEWINDOWS
    config = export DISPLAY=':0.0' && xdotool search "chromium" windowactivate --sync key --clearmodifiers "ctrl+R"
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_VOLUMEUP
    config = amixer set PCM 5dB+
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_VOLUMEDOWN
    config = amixer set PCM 5dB-
end

begin
    prog   = irexec
    remote = xbox-one-media-remote
    repeat = 0
    button = KEY_MUTE
    config = amixer set PCM toggle
end

Save the `irexec.lircrc` file and restart the irexec service to load the changes:

sudo systemctl restart irexec.service

To watch for any errors, tail the log for the `irexec.service`:

journalctl -u irexec.service -f

Conclusion

Hopefully this post gives you an idea of the power of `irexec`, anything that can be run on the command line can be controlled by a remote.

Comments are closed.