Wednesday, March 19, 2008

Mythdora - Setting up a USB IR-Blaster

Finally got around to ordering an IR-Blaster to use with my Mythdora installation.

The specific one I purchased can be found at www.usbuirt.com. I actually ordered it on-line from NCIX (Vancouver Canada source) on Monday afternoon, and I had it in my hands (Toronto) on Wednesday.

What I wanted to do was to use the IR-Blaster to give me the ability to first; have the Mythtv front-end be controlled by one of my many remote controls, instead of my wireless keyboard; and then to also change the channels on my STB as controlled from MythTV.

So, here's what I had to work with: My MythTV box, the USB IR-Blaster, My STB, the remote for my STB, and for now the remote for my MediaMVP box.

First thing I did was read, read, and read some more, and got myself totally confused ... that is until I read somewhere that the USB-UIRT was directly supported bu lirc 8.2 ... brillaint, because that's exactly what Mythdora comes with.

So I take the first big step; I plug the USB IR-Blaster into the USB port of my MythDora box ... suddenly I see some new device files in my /dev directly, the most important one being /dev/ttyUSB0 which it turns out is the recognized USB IR-Blaster.

Next, as a test I run the "irrecord" command ... first time through it doesn't work, but luckily I make a type on the driver argument, and it obligingly responds with a list of valid drivers, one of them being the "usb_uirt_raw"driver (I was trying to use some other driver, but types it in wrong, which was lucky, as had I typde it in correctly it just would have failed and I would have probably spent hurs more trying to google the reasons and corrections.)

Anyway, after I realize my mistake I correct it and type:

irrecord -H usb_uirt_raw -d /dev/ttyUSB0 myremote.conf

This brings up some information and a few prompts, and asks me to start hitting keys on my remote, which the program starts to recognize. so I know that I am getting somewhere with this little project.

So, let's step back a little bit here now that I've got the basics up and running, and do a quick overview of the steps that are going to be needed in order to accomplish what I want to do.

Step1: Get MythDora to recognize the USB IR-Blaster hardware.

Step 2: Create an lirc configuration file of the remote I want to use so the lirc system can both recognize and simulate these devices.

Step 3: Merge these files into one file and place it in the /etc/lircd.conf file.

Step 4: Configure the lircd to start properly and to start on a reboot.

Step 5: Configure MythTV to recognize the output from the lirc system

Step 6: Configure MythTV to send the proper IR codes out to the IR-Blaster to simulate the STB's remote control.

So, let's put some detail on some of those steps.

Step 1: This step is actually very simple, as mentioned above. Ignore everything you read about setting up other kinds of IR Blasters on other version of lirc, simply make sure you have lirc 8.2 and plug the IR Blaster into your USB port (note: use "rpm -qa |grep lirc" to determine if you have lirc 8.2 ... and if you don't I would assume a command such as "yum install lirc" would quickly fix this problem ... but, as I stated already, MythDora should already come with this software.)

Step 2: This is where the "irrecord" command comes in. You run this command with the proper parameters, and it prompts you to start hitting keys on the remote, gives these keys some names, and it automatically builds the lirc configuration file for you.

For example, to create the lircd configuration file for my Hauppauge MediaMVP remote control I used the following command:

"irrecord -H usb_uirt_raw -d /dev/ttyUSB0 hauppauge"


The program walked me through the process where I hit all the keys on my remote, gave them some names, and it did all the work in creating the following file:


# Please make this file available to others
# by sending it to
#
# this config file was automatically generated
# using lirc-0.8.2-CVS(usb_uirt_raw) on Thu Mar 20 15:55:56 2008
#
# contributed by
#
# brand: hauppauge
# model no. of remote control:
# devices being controlled by this remote:
#

begin remote

name hauppauge
bits 13
flags RC5|CONST_LENGTH
eps 30
aeps 100

one 917 775
zero 917 775
plead 961
gap 109396
min_repeat 1
toggle_bit_mask 0x800

begin codes
1 0x1781
2 0x1782
3 0x1783
4 0x1784
5 0x1785
6 0x1786
7 0x1787
8 0x1788
9 0x1789
0 0x1780
Text 0x178A
Sub/CC 0x178E
Red 0x178B
Green 0x17AE
Yellow 0x17B8
Blue 0x17A9
Skip- 0x17A4
Pause 0x17B0
Skip+ 0x179E
Rewind 0x17B2
Play 0x17B5
Forward 0x17B4
Record 0x17B7
Pause 0x17B6
Vol- 0x1791
Vol+ 0x1790
Mute 0x178F
Previous 0x1792
CH- 0x17A1
Ch+ 0x17A0
Exit 0x179F
Down 0x1795
Menu 0x178D
Left 0x1796
Right 0x1797
Up 0x1794
Guide 0x179B
Radio 0x178C
TV 0x179C
Videos 0x1798
Music 0x1799
Pictures 0x179A
Power 0x17BD
OK 0x17A5
Go 0x17BB
end codes

end remote
Step 3: I also did the same thing with my STB's remote control, merged the two files ("cat file1 >> file2") and copied that merged file to /etc/lircd.conf.

I then had a file that allows the lirc system to both recognize and simulate both of those remote controls.

Note: I have several older remotes, especially a couple of RCA universal remotes, but neither of these remotes were properly recognized by the irrecord program ... so be warned, not all remotes are created euqally, so YMMV.

Step 4: Next we want to set up the licrd program to start properly, and to start up automatically on reboot.

First we need to edit the /etc/sysconfig/licrd file to contain the line:

LIRCD_OPTIONS="--driver=usb_uirt_raw --device=/dev/ttyUSB0"
This will you to start the lircd deamon using the command:

/etc/rc.d/init.d/lircd start
In order to make sure the lircd program starts up after a system reboot you can enter the following command:

chkconfig lircd on
Step 5: Mythtv doesn't actually do the recognizing of the remote control codes ... lircd does that and then looks up what to do with these codes in your /home/mythtv/.mythtv/lircrc file (also "soft linked" to /home/mythtv/.lircrc, so if you edit one of them you actually edit both.)

The lircrc file format (along with other useful information about the lirc system) can be found here.

To understand better what's goin gon here, let's step through a basic key press from my MediaMVP remote control and trace what happens from the actual key press to the action on the screen within MythTV.

OK, so let's say I press the "menu" button on my MediaMVP remote.

The IR-Blaster receives the IR signal "0x178D" and passes it on to the lircd program.

The lircd program checks in the /etc/litcd.conf file and finds the "0x178D" code (see bolded code above) in the hauppauge section of the file, which tells it that this code corresponds to the "menu" button of the hauppauge remote control.

So, now the lircd knows that the "menu" button on the hauppauge remote was pressed .. now what does it do whit it?

First thing the lircd does is to check what program has the "focus" of the system ... in this case since we are supposedly running MythTV, the lircd program finds that "mythtv" has the focus.

Lircd now looks in the /home/mythtv/.mythtv/lircrc file.

It looks through all the file for any entry that refers to the "hauppauge" remote, for a program called "mythtv", and a reference to a "menu" button .

In doing so it finds something like this:

begin
remote=hauppauge
prog = mythtv
button = Menu
repeat = 3
config = M
end

This tells the lircd that as the "mythtv" program has focus, and the "Menu" button was pressed, then send the key-press "M" to the mythtv program.

A key-press of "M", assuming you're watching live TV, will bring up the live tv menu.

The only hard part about this whole process is that you will have to go through each entry in the /home/mythtv/.mythtv/lircrc file and make sure the "button" entry matches what you have listed in the /etc/lircd file (for both "remote" and "button" at least ... also "repeat" and "delay" might need to be adjusted for your particular system needs.)

Step 6: So now we have MythTV recognizing the signals from your remote control, now we need to have Mythtv send out the proper remote codes when it wants to change the channel, so that the STB thinks the codes are coming from it's own remote control.

Again, let's step through a sample case. Let's say you are watching a show on channel 100 and want to change to channel 200 ... so you enter "2-0-0" and hit "ok" on the remote.

Mythtv recognizes these remote control signals just as it would in Step 5 above ... but this time, since it is a string of numbers and an "ok", it interprets this as a request to change the channel (and rightfully so.)

When MythTV recognizes a channel change request it can do one of two things; If you have an internal cable or satellite card installed, it sends that request directly to the card and lets the card handle the channel change process. If you are using an external Set Top Box (STB) then you need to configure MythTV to use an "external channel change command".

You config this in the MythTV setup program under "Input connections".

Run "MythTV Setup", go to the "Input connections" section and select the appropriate connection ... in my case it's the "[MPEG : /dev/video0 ] (Composite 1)" connection as I have my composite output from my STB connected to the composite input of my hauppauge PVR-150 card.

In this Connection I have entered "/usr/local/bin/change_chan.sh" into the filed called "External channel change commands:".

This tells MythTV that when ever a channel change is requested (by entering "###ok" on your remote, or via an automated request from the recording system) to take all the numbers and send them as parameters to a program called change_chan.sh in the /etc/local/bin directory.

So, since as we stated abve we are changing from channel 100 to channel 200 by entering "200ok" on the remote, MythTV automatically runs the Linux command:

"/usr/local/bin/change_chan.sh 200"
Next let's have a look at the /usr/local/bin/change_chan.sh shell script:


#!/bin/sh
REMOTE_NAME=STB-REMOTE
for digit in $(echo $1 | sed -e 's/./& /g'); do
irsend SEND_ONCE $REMOTE_NAME $digit
sleep 0.2 # note, you may have to tweak the interdigit delay up a bit
done
irsend SEND_ONCE $REMOTE_NAME OK
The most important parts of the script are the "irsend" commands. This is the lirc command that tells the ir blaster to send an ir signal out to some other device.

You'll need to understand a little bit of shell scripting to figure out the details of this script, but essentially it takes the channel number passed to it by Mythtv, let's say 200, and takes each individual digit of that number, looks up the proper remote code in the /etc/lircd.conf file, and sends it individually out the ir port of the ir blaster ... repeats for each digit, and then finally sends an "OK" ir signal for that remote.

You will have to modify this script to suit your own remote and the signals it requires:

Change REMOTE_NAME=STB-REMOTE to REMOTE_NAME=
You will find your remote name in the /etc/lircd.conf file in the "name" section of the remote that controls your STB.

You may also have to change the "
irsend SEND_ONCE $REMOTE_NAME OK"
line as well to change "OK" to whatever you called the button that your remote uses to signal that you've stopped entering digits and to take whatever number you've entered as the complete channel number you are attempting to change to.


And that should be it .... you may need to restart MythTV, or even reboot your system (I'd recommend a complete reboot to make sure everything starts as it should.


My Mythtv now is controlled by my old MediaMVP remote control, and when I enter a channel number (or Mythtv automatically tries to change channels; for a recording for example) it sends the channel number to the lirc system which emulates the IR Remote of my set top box.

My next little project is to modify the /usr/local/bin/change_chan.sh to look for special numbers, which I will then cause to run other scripts to kick off and send other ir codes that will automatically configure all my A/V equipment ... for example let's say I enter the channel number "99966" (9-9-9-o-n) that could be recognized by the change_chan.sh script, which would then run another script that uses irsend to send ir codes to the TV, HTS, & STB to turn them all on and set them to the proper volumn levels, inputs and outputs. 999644 (9-9-9-o-f-f) could trigger a scrpt that turns everything off.

First though I need to figure out how to get the proper licrd.conf file for my RCA remote that came with y old HTS, as while it is an RCA universal remote, it doesn't seem to be like any official RCA universal remote.


For those attempting this project, I would first look in http://lirc.sourceforge.net/remotes/ to see if your remote already has an lircd.conf file ... if not, then you're going to have to use the lirc tools such as irrecord, mode2, and such to try and build your own ... good luck.