Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Friday, July 22, 2011

Ceton InifiniTV 4 in MythTV 0.24 - Part 3 (Mythbuntu)

In Part 2 of this article, I provided all of the instructions for setting up MythTV 0.24 to use a Ceton InfiniTV 4 cablecard tuner. Today, I'm going to provide a simple step by step that should get a Mythbuntu 11.04 user up and running with Ceton support. However, before continuing, please first read Part 2 to see all of the disclaimers and important info. I'm going to assume you've read it, and thus won't be repeating all of the important details here.

Instructions

mkdir /usr/src/mythtv
cd /usr/src/mythtv

apt-get build-dep mythtv
apt-get source mythtv
apt-get install devscripts

cd mythtv-0.24.0+fixes.20110416.9ba3ece

dch -i

This will take you to edit a file containing a changelog. At the top of the file, it will have already filled in the framework for you to enter your change info. There will be a line with a single asterisk * about 3-4 lines down. Next to the asterisk, just put a brief summery like "applying ron's ceton patch"

For the following, I'm going to assume you've put the patch file (which you can get here) in your home directory. Also, substitute the XXX in the filename with the appropriate version you've downloaded.

cp ~/ceton_mythbuntu_verXXX.patch debian/patches/
patch --dry-run -p1 < debian/patches/ceton_mythbuntu_verXXX.patch

Make sure you got no error from the above. Also please double check the output to make sure it doesn't say anything about applying the patch with fuzz. Although patching with fuzz will work at this step, when you go to rebuild the package, it won't allow fuzz.

pico debian/patches/series

Add ceton_mythbuntu_verXXX.patch at the end of the mythtv section.

debuild -us -uc -iFFmpeg

In the above command -us and -uc are necessary so that we can build the packages even though we aren't setup to sign them. The -iFFmpeg is to tell the packager not to include the FFmpeg stuff in the package. I had to do this because the build process started complaining about the binary file being different. I couldn't find any good explanations on google, but since I didn't modify anything in ffmpeg, we can just leave it out and it will use the old version.


Now at this step you are going to need to install the packages to replace the binary files with your new version. First, I want you to run the following command and make note of the timestamps on the existing files (copy the output to a text editor and hold onto it for a few moments).

ls -l /usr/bin/mythbackend /usr/bin/mythfrontend* /usr/lib/libmythtv-0.24.so* /usr/bin/mythtv-setup*

Now lets install the packages. There are a ton of packages here, and you can try to install them all if you like. However, I only bothered with the following, and everything seemed to work

cd ..
dpkg -i mythtv_0.24.0+fixes.20110416.9ba3ece-0ubuntu2_all.deb
dpkg -i mythtv-common_0.24.0+fixes.20110416.9ba3ece-0ubuntu2_i386.deb
dpkg -i libmyth-0.24-0_0.24.0+fixes.20110416.9ba3ece-0ubuntu2_i386.deb
dpkg -i mythtv-backend_0.24.0+fixes.20110416.9ba3ece-0ubuntu2_i386.deb
dpkg -i mythtv-frontend_0.24.0+fixes.20110416.9ba3ece-0ubuntu2_i386.deb


Now, let's once again look at the timestamps on the files:

ls -l /usr/bin/mythbackend /usr/bin/mythfrontend* /usr/lib/libmythtv-0.24.so* /usr/bin/mythtv-setup*

Everything should have a new timestamp on it


At this point, all of the Mythbuntu specific stuff should be setup. Now go to Part 2 of this article and continue on from step 8.


...click here to read more!

Thursday, July 21, 2011

Ceton InifiniTV 4 in MythTV 0.24 - Part 2

In Part 1 of this article, I discussed my decision to get a Ceton InfiniTV 4 card instead of a HD Homerun Prime, how it didn't initially go as planned, and how I eventually went through the process to write my own code to utilize the card in MythTV 0.24. Now I'd like to share the fruits of my labor...the necessary code which you can compile into MythTV 0.24-fixes to get Ceton support for yourself. In the upcomming Part 3 (hopefully posted tomorrow), I'll post a patch and instructions for Mythbuntu 11.04 users


Standard disclaimers

I'd be absolutely thrilled if you wanted to use my code to get your Ceton card working in MythTV. However, before I unleash all of this on you, I need to be clear about a number of things. I wouldn't want you to not realize what you are getting into and get mad at me if something goes terribly wrong (like you miss a bunch of recordings). So here are a few things you'll need to know before getting started

1) This isn't just a distribution you can download and install, or run apt-get on. We are talking about custom compiled code. You will be downloading the mythtv source code from the git tree, applying some patches, and custom compiling it on your own. If you've never done this before, then I'd prefer that you get comfortable with that process before trying to add in my code. So I'd suggest downloading 0.24-fixes from git (see the instructions at code.mythtv.org), compiling it, installing it, and making sure everything works. If that all works fine, only then should you think about proceeding

2) This isn't official code by any means. This is something I put together by myself, as a stopgap measure until 0.25 is eventually released with real Ceton support. So far, the only person to test this code is me. If you run into problems, the folks on the MythTV forums aren't gonna help you with this one. You'll be depending on me for help.

3) If you have problems with the code, I'll try to help. I take pride in what I do, and I'll be willing to work with you as much as I can to figure out your problems. That said, I've got a full time job and a 1 year old daughter, so don't expect 24 hour support and quick turnaround time. It may take me a while to figure out and get to the bottom of it, but I'll try my best.

4) If you do need help from me, then I'm gonna need help from you. I'm gonna ask you for logs, I'll probably send you some custom code, ask you to run it, and send me back the results. I may do this a number of times.

5) Realizing that this isn't the official software for running the Ceton card in MythTV (nor will it be the official code in 0.25 or ever), you are going to have to expect things to break when you eventually upgrade to 0.25. You're going to have to delete your tuners and repeat the setup process in 0.25 using the officially supported mechanism.

6) When 0.25 comes out, if by some chance it does NOT contain the official support for this card as expected, then this code won't work on 0.25. I may be a bit delayed in upgrading my systems to 0.25, so it may be a little while before I can update the code. That said, I should be able to do it relatively easily, as I already did some preliminary work at getting this working in 0.25 (and had it working in a limited capacity), so I've got a decent part of the code written. And some of the biggest parts (that deal with tuning and getting status info) are actually not tied to myth directly, so I should be able to port them with no problems.


Other important information

If the disclaimers didn't scare you off, then here are few of the little quirks you need to know about:

Importing QAM channels - I haven't written a channel scanner for this card, so if you want to run it in QAM mode without a cablecard installed, you are going to have to do some work to get your channel data setup properly. I can work with you to get this setup if necessary, and I can probably write some perl scripts to help you with the process. However, hopefully nobody is buying this card just for it's QAM support, so hopefully this won't be such an issue. But if you maybe want to test it in QAM mode before upgrading your cable service and ordering a cable card, then we'll have to setup some channels for you

Importing cable card channels - For cable card support, we just use myth's download option and let mythfilldatabase download the data from schedules direct and create the channels. This sets up the channels for the most part, but they are not yet usable by this code. You'll need to run an SQL query to update one of the fields in the database. I'll provide you with instructions below. Also, in the future, if new channels get added to your lineup automatically, they are going to need to be tweaked before they are usable, so you'll need to rerun that query whenever your lineup changes. In the future, I'll try to figure out the most appropriate way to automate this so you don't have to worry about it, but for now it's something you'll need to be aware of

Live TV channel changing - Typically this works very well...about 3 to 4 seconds per channel change. However, occasionally a tuning request may fail and the Ceton doesn't properly tune the channel. The good news is that I've got code to detect this condition (as least for every way I've experienced this or I anticipate it could happen) and re-initiate the channel change request automatically. However, when it does happen, it will take 5 seconds for the code to kick in, and then another few seconds for the channel change to occur. When this happens, you'll be staring at a black screen for several seconds. Back on the good news side, I've only had this happen to me once in my testing, so I don't expect it will be very common).

Startup error condition - I've started and stopped the mythtv-backend service hundreds of times almost entirely without issue. Once it's running, the code works great. However, one time I rebooted my computer and when it came up, it didn't initialize properly. As a result, the card wasn't usable for recording until I stopped and restarted mythtv-backend. When it happened, it was logging very obvious info to the log file, so this will be easy for you to detect. Sometime next week (after my daughter's 1st birthday party) I intend to take a better look at this, rebooting my system several times to see if I can recreate it. I'm pretty sure it was just a matter of mythtv-backend starting up before the Ceton card was ready. I've modified my card to get its IP through DHCP instead of using the static 192.168.200.1 that it has by default, so this may have been the cause of the problem.


One final disclaimer

It's important to know that cablecard support is going to depend on your cable provider. Unfortunately, cable labs has only certified Windows Media Center to support certain modes of DRM. This means in MythTV, you can only watch channels that are marked as Copy Freely, and it's up to your cable provider to decide this. If they mark a channel as Copy Once or Copy Never, you are out of luck and the only thing you can possibly do is complain to them (for all the good it will probably do). You can probably check with others on the MythTV forum to try and figure out what channels are marked Copy Freely for you (either someone that lives in your area may know, or someone may be able to show you how to retrieve that info from your cable box).

In general, WOW (wide open west) has said that they mark everything except premium channels (HBO, showtime, etc) and PPV as Copy Freely. This should hold true in all WOW markets. Comcast has the exact same arrangement in most of their markets, but I believe there are some places here and there that are configured differently. With other cable providers, results may vary.


Are you still here? OK, let's get this going

If I haven't scared you off with the disclaimers and quirks, then I guess you are serious about this and it's time to get to work. So here's what you need to do

1) Download myth 0.24-fixes and make sure you can compile it all fine.

2) Download my ceton patch for 0.24-fixes.

3) cd to the mythtv/mythtv folder of the source code. This is the folder that contains the ./configure script

4) Clean up any previously compiled code by running:
make dist-clean

5) Test the patch first by running the following command:
patch --dry-run -p2 < /path/to/ceton_verXXX.patch

6) If the above test was successful and no failures occurred, then apply the patches by running:
patch -p2 < /path/to/ceton_verXXX.patch

7) Run ./configure, compile everything, and install it

8) Start mythtv-setup and add a Ceton tuner. The default IP address (if you haven't changed it) is 192.168.200.1, and the tuners are numbered 1 to 4. Repeat this process for all 4 tuners (or as many as you want to setup).

9) Create a brand new data source exclusively for the Ceton tuners (and don't let any other non-ceton tuners use it).

10) Click the button to fetch channel info (I don't recall the exact wording, but it's the next to the scan button in the Input Connections page

11) If you have a cablecard installed in your Ceton InfiniTV, perform step 11-A and then go to step 12. If you DO NOT have a cablecard installed (ie: you want to operate the card in QAM mode) perform step 11-B and then go to step 12.

11-A) Note: this step is no longer needed as of patch ver 008 (1/7/2012) Do this step ONLY if you are running the Ceton in CableCard mode (ie: you have a cablecard installed):
Download the channel mapping utility. Unzip/untar it and edit the perl script to adjust the config settings at the top of the file. You'll want to verify (and change when necessary) the path to wget, the ip address of your Ceton InfiniTV 4 card, and the mysql info (host/db/user/password). Then simply run the perl script to have it update the channel config.


11-B) Do this step ONLY if you are running the Ceton in QAM mode (ie: you do NOT have a cablecard installed):
Download the QAM channel mapping utility. Unzip/untar it and edit the perl script to adjust the config settings at the top of the file. You'll want to verify (and change when necessary) the path to wget, the ip address of your Ceton InfiniTV 4 card, and the mysql info (host/db/user/password). Then run the perl script to have it update the channel config.

Note: One limitation of this script is that it cannot determine which channels are available in unencrypted QAM. It will simply look at every channel in the videosource that you've assigned to the card in mythtv-setup, and then update the configuration of that channel accordingly. Therefore, in order to eliminate the encrypted channels so that myth doesn't have to tune them, you will need to delete those extra channels or mark them as not visible (either in mythtv-setup or using mythweb).

12) Startup the backend and the front end, and you should be good to go if I haven't forgot anything.


...click here to read more!

Tuesday, July 19, 2011

Ceton InifiniTV 4 in MythTV 0.24 - Part 1

With the pending release of the SiliconDust HD Homerun Prime, I was getting pretty excited about getting a cablecard tuner for MythTV. However, then something wonderful happened...Ceton decided to drop the price of the InfiniTV 4 card to $300. I hadn't been very interested in the card before simply because it was a bit out of my desired price range. However, at the new price, it was barely more expensive than the Prime, and it gave you a 4th tuner to run off the same cablecard. After thinking about it for a few weeks, I decided to cancel my HDHR Prime preorder and take a chance on the Ceton card.

I said take a chance, because I knew that the Ceton card wasn't yet supported in MythTV. However, I had some ideas how I could hack support into myth 0.24, and failing that, I was under the impression that the trunk code of mythtv (which will eventually be 0.25) already had the code necessary to work with the Ceton. So I figured one way or another, I was covered with the caveat that I wouldn't be working with a proven, tested system.

It turns out I was wrong on both counts. My plans for hacking support into 0.24 (using the Demo Recorder or Import Recorder) didn't pan out. So I tried the trunk support and couldn't get it working. After a post on the MythTV user forums, I learned that I was mistaken...that the trunk code only contained bits of the needed code, and wasn't at all functional. Oops. Big oops.

So here I was with a $300 capture card that didn't work in myth, and a wife who had been anxiously anticipating an upgrade to HD channels. What do I do? Tell her "sorry...you'll have to wait a while...maybe another 6 months"? Or do I sell the Ceton card and buy the Prime? Or is there a 3rd option....can I write my own code to get it working in MythTV 0.24?


Writing the code

When I first looked into the code, it was a huge mess of very mysterious things I didn't understand. There was a part of me that said I'd probably never get this all to work. But then I did what I always do...just try to break it down into chunks. I started looking at bits and pieces, figuring out what I did and didn't need. This allowed me to discard large sections of code and focus on a much smaller core set of functionality.

It took me only a few days to figure out enough to hack my way through it and get my very first output...video from the Ceton on my TV screen. It was very basic..it didn't include things like channel changing yet, but it was a start. Then I discovered that a whole bunch of stuff wasn't working...seeking, commercial flagging, channel changing was incredibly slow, etc.

After looking into it, it turns out that a lot of that "unimportant" code I discarded early actually had important functionality that I didn't yet understand. Yet stripping it out still had value...it allowed me to focus on a smaller bit of functionality and become more familiar with it. I now understood a lot of code, and as I started to work back in those pieces I ripped out, one by one they were making sense, too.

Over the next few weeks, I got everything put back in and reworked it. I hit some snags along the way...a bug in the existing myth code, a VDPAU bug in an early release of Debian squeeze, and some peculiar mpeg data streams from my cable provider all simultaneously threw up obstacles and mislead me about why things weren't working. One by one I figured it out, and eventually I ended up with a fully functional recorder for the Ceton card. It could record, flag commercials, do the channel changing on it's own, etc.

However, I had a few issues with channel changing. I was changing channels through the Ceton's built in web interface (rather than the DRI UPnP interface), and apparently that isn't the most rock solid way to change channels. I thought I had dealt with the issue and everything seemed fine, so I set up my first big test...recording a dozen programs overnight. When I woke up in the morning, I found that a few of the recordings failed. However, that turned out to be my fault...I had setup the channel data with a channel I didn't actually subscribe to.

After fixing that, I ran a bigger test the next night....about 30 programs, using all 4 tuners at a time. When I woke up the next day, I found that several of those didn't record. Looking at my logs, the problem appeared to be that when multiple tuners tried to change the channel in rapid succession, not all of the requests would always succeed. So I spent a few days writing an entire new set of channel changing code, which would do 2 things...first, it would space out the request to ensure too many don't occur at once. Second, it would monitor the change until it saw that it was complete and successful, and if necessary, it would re-initiate the channel change until it finally succeeds.

With my new changes in place, I setup my box to run for 12 hours, recording 80 programs, all back to back, and often with 4 new program starting simultaneously. End result...everything worked perfectly.

With that final test completely successful, I put it all on the line. I took the Ceton card out of my dev box, put it in my newly-rebuilt production myth server, and set it up as the 4 highest priority tuners. It's been working great for a few days now.

I still have some more tweaks I want to make, but it's going to be a little while (I'm getting prepared for my daughters 1st birthday this weekend). However, everything is working well enough now, so I'll be posting my existing code very soon.
...click here to read more!

Saturday, January 2, 2010

IRWatch version 2 released

Today, after a MUCH longer delay than anticipated, I've finally made available the newest version of my IRWatch utility. It's been rewritten from scratch for a much cleaner and more modular design. No longer are you required to go into the main perl script and enter all of the logic into the while loop (making sure not to mess any of it up). Now there are much simpler config files for you to edit (ok...technically you are still modifying perl code, but it's MUCH MUCH prettier than before). The special event handling logic has also been exported to a plugin format, which makes it much easier for you to design your own custom event handlers and then share that with other users

Configuration
First, you need to download the latest version of IRWatch and also the RFLibs libraries.

Next, you need to set them up in the right locations. I put both the irwatch and the RFLibs folders into /usr/local/scripts. If you want to place them somewhere else, you will need to modify the first line of irwatch.pl so that the perl include (-I) specifies the appropriate location.

Now you need to do some relatively simple configuration. You can probably just wing it by modifying the 000_general file in the config directory, but if you need more help, there is more extensive documentation in the README file. The important parts here are to make sure the values in the settings hash are correct, then define aliases for your remote(s) in the remotes hash, then go through the events hash and define just how you want the remote to behave.

You will notice the 500_aquos.disabled file in the config folder. The .disabled extension tells irwatch not to process that config file. For most users, that is what you want (otherwise, the config file will tell irwatch to load the aquos plugin, which will fail becuase it can't connect to the aquosserver, so irwatch will abort). However, if you are running a compatible Sharp Aquos TV with a serial port, and if you have successfully setup my aquosserver script to control the TV, then you will want to enable this functionality. Just rename the file to 500_aquos (ie: remove the .disabled extension) and edit the file to your liking.

Once again, check the README file for more details

Writing Plugins

Writing plugins is actually relatively easy. Most of the info you need is (once again) in the README file. Simply put, you need to create your plugin in the plugins folder, add a corresponding config file in the config folder, and make sure the config file indicates to load the newly created plugins. When writing a plugin, you want other users to be able to use it without modifying the plugin code at all. So, to that end, make you put configurable option like paths, ip addresses, ports, etc into the config file in the settings hash.

If you write any interesting plugins that you think others might find handy, please send me a copy via email. If it looks interesting, I'll include it on my website.

...click here to read more!

Monday, May 4, 2009

Controlling a Sharp Aquos TV via the serial port interface

Last year, I replaced my ancient CRT television with an HDTV LCD...a Sharp Aquos. When I got the TV, I was curious to find that it had a RS-232 compliant serial port onboard. I was wondering if I could setup my mythtv frontend to communicate with TV. A quick look through the user manual turned up something I found even more shocking than the existance of the serial port...the communication protocol for the TV's serial port was completely documented in the user manual, including things like port settings, command format, a list of commands, and their accepted parameters. This was exciting...it seemed like it wouldn't be too much trouble to get it all working.

Indeed, it was quite easy. I installed a Perl module called Device::SerialPort which made it a piece of cake to setup a serial port connection. In no time at all, I quickly issued my first command to the TV and had my response back. From there, it was just a simple matter of turning it into a script.

At first, I just wrote a simple standalone program to handle what I wanted. It would open a connection, send the command, and give back the response. However, the problem was that I intended to be interfacing with the TV from multiple different scripts and programs, and was concerned about cases where 2 scripts might try writing to the serial port at the same time, which would probably either fail or do something like intermix the 2 commands being written resulting in the TV getting corrupt data.

Instead, I decided the best way to handle it would be to write a simple server script. This server script would be the only script that interacts with the Aquos via serial port. All other programs would connect to the server, issue a request, and let the server handle the communication and pass back the result. If multiple clients connected to the server, the server would just handle the requests one at time, in the order received.

The end result of this effort is the Aquos Server, which is a simple perl script that handles requests via tcp/ip connections. You simply start the script running at bootup, connect to a port, issue a text command, and read back a text result. It's a very simple communication protocol. Each request is one line, and it is always responded to with a one line response.


Setting it up

First thing you will need to do is download my updated RFLibs (ver 2) library of Perl modules. Also note that even if you downloaded these libs in the last few months (since I posted the updated ones), you'll want to redownload them again. There was one remaining rare bug in the serial port communication that I was able to track down and fix. You can get the current version from here.

Then you need to download the aquosserver script. You can get that from here.

Finally, you need to make sure you have the Device::SerialPort module installed for Perl. Under Debian, you can do this using "apt-get install libdevice-serialport-perl", but the command or package name may be different for your distribution.

Once you've got it, you'll want to make sure the aquosserver script is in the same directory as the RFLibs directory. Then you may need to edit the aquosserver script and change 2 things:
1)The $listenport variable (set to 4684). This is the port that the aquose server listens for connections on. Change it to whatever you please.
2) The $serialport_device variable (set to '/dev/ttyS2'). This is the device name for the serial port that is connected to your Sharp Aquos TV.


Testing

Simply start the script running. Then, to test it out, open another ssh/telnet window and try connecting to it. The easiest program to use is nc:
nc localhost 4684

Then type "POWER" (without quotes) and hit enter. If everything went as planned, you should get back a response of either 0 or 1 (depending on if the TV is off or on). Next we want to make sure you can turn the TV on via serial port (by default, that capability was disabled on my TV). First, turn the TV on manually. Wait for it to power up completely, and then issue the "SERIALPOWERUP ENABLE" command to the aquosserver, which should give you an "OK" response.

Now you can try turning it on and off using "POWER ON", "POWER OFF", and "POWER TOGGLE". You can query the current volume with the "VOL" command, and you can adjust the volume using "VOL+" and "VOL-", or you can set it explicitly using "VOL 10". You can query the input using the "INPUT" command (which returns the input number, with input 0 being the tuner), and you can set it to input 2 by issuing "INPUT 2". The commands for muting are "MUTE", "MUTE ON", "MUTE OFF", and "MUTE TOGGLE". Finally, when you are done issuing commands, type "EXIT" and hit enter to disconnect.

This should cover most of what you want to do with the TV, but if you need to issue a command not listed here, you can issue a raw command code to the aquos server. First look up the command code and parameter in your TV user manual. You can then issue the command using the "CMD" command and pass it both the command code and the parameter value. For instance, to set the AV Mode to Game, you would use "CMD AVMD 3". Note that the parameter (3) does not need to be padded to 4 bytes as the Sharp protocol requires. My library handles the padding for you.

Also, note that this script was written around the D64U series of Aquos TVs. I've looked at a few other series and they use the same protocol and commands. However, if your TV has different protocol/commands you'll have a problem. If that's the case, let me know and I'll see if maybe I can find a way to handle other TVs in a future version.


Using

Now, how do you implement this into something useful? Well, first you need to make sure that the aquosserver script is always running in the background. You'll want to launch it from one of your init scripts. I'll let you figure out how to do that. Once it's running, you'll need to issue commands to it. You can do this from a script. For instance, if you want to power on the TV when your computer boots up, you can run a script (after the aquosserver has started up) which runs the following command:
echo -e 'POWER ON\nEXIT' | nc localhost 4684

This issues the command to turn on the TV and then disconnects from the aquosserver (without paying attention to the result).

Another thing you may want to do is issue commands in response to pressing buttons on the remote. I believe there is a way to do that via the lircrc file, but I can't instruct you on how. Myself, I do it using the latest version of my irwatch script. That will be posted about in my next blog entry, so for now, you are on your own. Stay tuned...



...click here to read more!

Tuesday, March 17, 2009

Updated RFLibs & new scripts coming soon

It's been a while since I posted anything here, but I have been working here and there on getting some work done on mythtv. Over the next few weeks, I'm hoping to post some updates and new scripts. A few things I've got coming include an updated version of irwatch, a new and improved alpha release of the mythimon client, and some stuff for controlling a Sharp Aquos TV via the serial port interface.


All of these scripts are based in part around my RFLibs set of perl modules. I posted a release of RFLibs last year. Since then, I've made a lot of changes to those scripts. While some of the modules only experienced minor changes or the addition of new functions only, other modules (especially the imon releated modules) experienced a considerable redesign.

As a result of these changes, if you had previously made use of my RFLibs modules in some of your own scripts, you might find that they no longer work with the new versions. I wish I could have avoided doing so, but many of the changes were necessary. I'm hoping things are in a much more stable state now, but still no guarantees.

So for now, I'm going to make the new versions of the modules available for download. However, note that if you are currently using the old mythimon (alpha 1) or irwatch (version 1) clients, you cannot use these new versions with those scripts (you need to wait for the upcoming announcment of new versions of those scripts), so don't put these in place just yet.

Download version 2 of the RFLibs perl module pack here



...click here to read more!

Tuesday, April 29, 2008

MythTV iMON server - alpha release 1

As I mentioned before, I've been working on an LCD server for mythtv in my spare time with the intention of making full use of the iMON LCD's icons. I'm happy to say that I'm ready to release the first alpha version. Its a little rough around the edges, and very basic in functionality at the moment, but it's a decent start.

Note: this application is specifically for the iMON LCD...NOT the VFD. If your display doesn't look like the photos at the bottom of this page, you have the VFD, and this program won't do you a bit of good.

How to get it
First, you will need to download 2 separate archives. The first is the application specific archive, and the second is an archive of perl libraries I've written.

mythimon:
http://www.ronfrazier.net/mythtv/downloads/mythimon.tar.gz

RFLibs:
http://www.ronfrazier.net/mythtv/downloads/RFLibs.tar.gz

Make sure the RFLibs folder exists as a subdirectory of the folder where you put the mythimon files.


Running it

The first thing you want to do is to disable any other software that is outputting to the lcd (including LCDproc). The only thing you want to keep loaded is the lirc imon lcd driver.

Next, the simpler test is to try running testAnimations.pl and make sure you get output on the LCD. If you have the older version of the iMON LCD, you will need to edit the code and change '15c2:0038' to '15c2:ffdc'. Also, if your device is setup somewhere besides /dev/lcd0, modify that setting too. If it works, you are good to go for the next test. If not, check for things like perl libraries, and make sure you are using at least perl 5.8 (needed for the thread support).

Next, you can try the mythimon.pl application. First you need to open it up and setup a few config variables. Specifically, the port numbers for the lcd server and the network control, the location of the frontend log file, the location of the lcd devices, and the iMON version.

If you don't have logging enabled for your frontend, you will need to add logging by adding the necessary paramters to the script that launches mythfrontend (ex "-l /var/log/mythtv/mythfrontend.log"). You will also need to have enabled both the network control port (found under setup->general) and the lcd port (under setup->appearance). You may need to restart the frontend for these changes to take effect.

Once that's all setup, you want to kill the existing instance of the mythlcdserver (so you can steal the port from it) and quickly start up the application. You can do this with the command:

killall mythlcdserver; ./mythimon.pl

You should now be looking at a screen with a clock. Next, you need to wait up to 10 seconds for mythfrontend to connect to the lcdserver port. When it does, you will see the output "FIRST COMMAND RECIEVED FROM MYTHLCDSERVER" appear on the command line (not on the LCD).

Now, when you go to watch TV or listen to music, you should get info on the screen and icons lit up. If not, again, check for perl libraries. Make sure you have the mythtv perl bindings installed.


Known Limitations

Being a work in progress, there is (by definition) a lot of stuff to be added or improved upon. Here are some of the known limitations, and what I plan to do:

Menu Support - This will be added most likely in the next version. I plan to integrate it with the front panel controls of the case via lirc. This way the menu's won't pop up whenever there is a menu change in myth (as mythlcdserver does). Instead, it won't display the menus until you hit the specified button on the front panel to enter menu mode. Then the menu display will override everything else until you hit the specified exit key or you let the menu timeout.

Volume Support - This will be added eventually. I plan to add a bit of configurability here...perhaps with plugin modules. One of the things I want to do for my specific setup is to integrate it with my TV, so it controls the volume via the serial port. I prefer this to adjusting the volume within mythtv. However, that's not going to work for most people, so I'll make it work both ways. With a simple plugin architecture, I'm thinking people could customize it to work with their setup (such as controlling an AV receiver).

Generic Info - This is info like photo names in the photo gallery, etc. Hopefully this will be in the next verison also.

Pausing/Stopping - There are times when myth doesn't send progress updates (like when playing music in the background). As a result, I've taken it upon myself to continuing calculating the progress on the assumption that playback is proceeding as normal. However, since myth sends no pause events, I have no way to track them. Thus, when you pause, my script doesn't know it, and keeps going. A similar problem exists with not knowing when music playback has stopped. I haven't yet figured out an ideal way to handle this. Probably some hack of monitoring the IR codes, cross reference that will the current location as reported by the network control port, etc.


Ideas for future expansion

I welcome feedback on ideas for what other types of things to do. There are some things I'm not sure about:

Uses for icons/display features - Anything interesting I can do with the top progress bar, other than just duplicating the bottom progress bar? When I add menu support at a later time, I was thinking I could use it to track progress through the menu (like a scrollbar on the side of your browser). How about the spinning disk, and the tray icon right below it? And the vol and time icons (one use is obvious, but also kind of pointless)? How about the source icons? I've taken advantage of TV and HDTV, but not SRC/SRC1/SRC2/FIT (and while we are at it...what the heck is FIT for anyway?).

Any good way to get the recording status out of the backend for the rec icon? I could also try monitoring the backend log, but that won't always be available on the remote frontend.

Anyone know a way to get codec data out of the music player to light up those icons?

How about getting weather data out of mythweather?

Is there any easy way to get spectrum analyzer data out of myth or the linux audio system?


Custom Fonts

I've created 2 different fonts for the application. The first is a normal font, 16 pixel tall (currently used for the clock). The other is an 8 pixel tall font with small caps instead of lowercase letters. OK...technically I made a 3rd font, but it only consists of 1 charachter (space), and is only meant as a vertical spacer in the display.


If you would like to make your own fonts, you can use this utility
http://www.ronfrazier.net/mythtv/imon_fontedit.html

If you come up with some good fonts, please feel free to share them with me.


Screenshots



























...click here to read more!

Tuesday, April 22, 2008

Developng an iMON client for MythTV

Over the last few weeks I haven't posted anything here. I've been using whatever time I could to work on developing an iMON client that can interface with MythTV. I've been making some decent process, and although I have a lot of features I still want to implement, I think I might be ready to release a limited-functionality alpha version in a few days. For now, I just want to discuss why I'm developing it and what it has to offer over the standard mythlcdserver.


Why not use mythlcdserver

The iMON LCD device has a lot of new capabilities. It's not just a standard 16x2 (or whatever) text display. It goes way beyond that. It's actually a 96x16 pixel display (1-bit each). In addition to displaying text, it can display arbitrary graphics. This gives it a lot of options. You can use custom bitmapped fonts of any size. You can draw symbols on the display. Really, anything you can imagine. The only real limitation (other than size and bit depth) is that, like a lot of inexpensive LCDs, the response time is a bit slow, so animations don't work well (it kind of blurs together).

In addition, the iMON also supports custom icons. You can turn on icons for different a/v formats (mp3, divx, dts, etc), icons to represent the number of audio channel, and a bunch of others.

These sort of features are completely absent from mythlcdserver. They aren't something that would work with a small tweak. They would require a complete overhaul of the application. It would also require some changes to the way myth itself works, since currently myth doesn't provide any of this data to mythlcdserver. In the end, it would be a huge change, which presents 2 obstacles. First, getting a change that major into myth would be a bit difficult (I've had significantly smaller changes in there for months without anybody accepting/rejecting or even commenting on it). The second is that, even if it were to get included, it would require a myth recompile for people, or would make people wait until the changes made it into the next release of mythtv (or mythbuntu, knoppmyth, etc).

In summary, it just seemed to me to be better to do it in a new application.


What cool things can be done?

The first obvious thing that can be done is to support the iMON's 40+ icons. The next thing you can do is to use the 4 horizontal lines as progress bars, instead of wasting an entire row of the display on the progress bar. This gives you more space for display info while still having the progress bar visible at all times.

As I already mentioned, the bitmapped LCD gives a lot of options for bitmapped fonts. Not only can you do half height text (8 pixel) or full height (16 pixel), but you can do any size you want. Perhaps a slightly bigger 11 pixel font on top and a tiny 5 pixel font on the bottom. Or instead of the 5 pixel font, you can display icons across the bottom for the functionality of the front panel buttons (pause, rewind, etc).


How does it get the data

The correct way to get the data would be explicitly from myth. However, as I already stated, this data isn't readily available, and making it available would require a wide range of modifications to mythtv. Instead, I'm going the quick and dirty route. I collect the data from a variety of sources. I pretend to be the mythlcdserver program and monitor port 6545. I scrape data from log files. I gather data from the network control on port 6546. I watch for IR commands. I then weave all of this data together to get a coherent picture of what is going on in myth. It's not 100% perfect, but it works well enough to make me happy.

...click here to read more!

Sunday, March 30, 2008

Writing my first linux driver - Part 1

I've always wanted to write a driver for a piece of hardware, but hadn't the slightest clue how to go about it. It seems like such a complex process...interfacing with the system at the kernel level, dealing with the necessary subsystems (ex: USB), reverse engineering the device's communication protocol. Where does a driver writing noob even begin?

Getting my inspiration

Luckily, I found a very insightful and well written series of articles by Greg Kroah-Hartman, who is the official maintainer of (among other things) the linux USB subsystem code. In his series, he covers all kinds of topics dealing with driver writing. Theres a lot of content, and it's not exactly laid out in step-by-step tutorial form. Still, it is by far the best resource out there that I've seen so far.

A list of the articles is located here. Lots of those articles are useful, but the most enlightening for someone getting started were the articles on writng usb drivers, writing your first kernel space driver, then rewriting the driver as a user space driver. In addition, the article on snooping the usb stream was helpful in figuring out how to reverse engineer a devices protocol from the working Windows driver that almost every usb device has.


Choosing a device for my first driver

So now that the series of articles had inspired me, I needed a piece of hardware to serve as my first driver. I quickly figured out a way to kill 2 birds with one stone.

I've been having some success getting my LCD working, but I've been having some trouble with the IR part of it. It seems that when the IR sensor receives certain signals, it locks up and stops responding. However, it works fine under Windows, so theres some issue in the current linux driver for this device.

The device seemed to be simple enough. I didn't envision a lot of back and forth communication. I envision a one way stream of incoming data for the IR sensor, and another of outgoing data to write to the LCD. The hardware didn't appear to have a lot of conditional states built into it. That meant there probably woulnd't be a lot of tricky reverse engineering such as "this command does X if the device is doing A, but Y if it's doing C, or Z if it's doing C or D" etc. Nope...it appeared it would (hopefully) be fairly straight forward.


Reverse engineering the protocol

If I needed to, I could always try to strip some protocol information out of the existing partially working driver. However, I figured it would be best to start out from scratch (that would be the most rewarding experience for my learning process).

I downloaded a copy the Snoopy program for Windows, hooked the LCD up to my Windows system, installed the drivers, and started monitoring the stream with Snoopy. Snoopy has it's pluses and minuses. It's very simple to use, but it doesn't let you monitor the stream in real time. It records it all to a log file, which you can later look through. The entries are all timestamped (relative to the start of the log), so you can tell what happened and when.

So first thing I did way was start up the log and watch the event counter grow. After a couple seconds, the counter stopped. I waited 10 second and the count remained steady. Great...that means I can look at that bunch and treat it as the initialization code. I then pressed a single button, waited for the event counter to stabilize, and then waited another 10. Then I started doing the same with other things (turn the volume knob, hit the menu key, switch the driver into graphic eq mode, etc).

At the same time I was doing this, I was writing down on paper a list of what actions I took at each step, and what type of feedback I got for each action (text on the LCD changed, an icon blinked, etc). After doing this for about a dozen different actions, I stopped and saved the log file.

Now, all I had to do was go through the list and start picking out some common things. I saw that every event had one pair of command codes in common. Looking at my notes, the one thing that every event had in common was that it blinked a particular icon. I now figured out the first code was to turn the icon on, and the other to turn it off.

Comparing them, they were very similar codes. Almost all zeros, except for the last byte (which was identical), and one of the other bytes had a single bit set in one of the codes but not the other. I quickly figured out that the last byte indicated that we were toggling the icons, and which bits were set in the other bytes indicated which icons to turn on.

I then looked at my written list. One of the other action had toggled a different icon. I suspected that If I looked in the corresponding series of events, I'd find an event where the command code had the same last byte, but a different set of bits turned on in the other bytes. Indeed, there was. I had now figured out how to toggle the icons. All that was left was to determine which bits handle which icons...a trivial task.


Comming Attractions!

Before I got much further in the debugging, I decided I needed to test my theory about the icon command. It was time to start writing a very basic driver. In my next post, I'll cover writing the first bits of driver code and putting my theories about the protocol to the test.

...click here to read more!