Log in Register FAQ Memberlist Search pcHDTV Forum Index
pcHDTV Forum

pcHDTV Forum Index -> General pcHDTV topics -> new utility: tune
Post new topic  This topic is locked: you cannot edit posts or make replies. View previous topic :: View next topic 
new utility: tune
PostPosted: Fri Jan 09, 2004 7:16 am Reply with quote
WaxyLemon
Guest
 




I just modified signal.c to make a simple utility that tunes a channel and puts the card in either NTSC or ATSC mode. It doesn't do anything that signal doesn't already do, it's just designed more for scripting and less for testing/troubleshooting.

The code is pretty clear, but here are the options:

tune [-d device] [-q] [-n|-a] channel

Default device is /dev/dtv0
-q is quiet (don't print signal strength), also results in faster execution
-n sets NTSC mode
-a sets ATSC mode

The 1 second sleep is just something I figured would give the card a better chance to lock the signal before trying to get the strength.

Compile with:
gcc -o tune tune.c

Since I can't attach files here I'll just quote the code. Copy and paste to compile. Enjoy.

Code:

/*
 * tune.c
 * Copyright (C) 2004 Chris Eldredge
 * Based on signal.c Copyright (C) Jack Kelliher
 *
 * Compile with 'gcc -Wall -O tune.c -o tune'
 *
 * Use:
 * tune [-d /dev/dtv0] 42
 * tune takes a dtv device and dtv station channel and prints out the signal
 * strength for that channel to stdout
 *
 * tune is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * tune is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <sys/ioctl.h>

#include <inttypes.h>

#include <linux/videodev.h>

#define DEFAULT_DEVICE "/dev/dtv0"

struct CHANLIST {
    char *name;
    int   freq;
};

/* US broadcast */
static struct CHANLIST ntsc_bcast[] = {
    { "2",    55250 },
    { "3",    61250 },
    { "4",    67250 },
    { "5",    77250 },
    { "6",    83250 },
    { "7",   175250 },
    { "8",   181250 },
    { "9",   187250 },
    { "10",   193250 },
    { "11",   199250 },
    { "12",   205250 },
    { "13",   211250 },
    { "14",   471250 },
    { "15",   477250 },
    { "16",   483250 },
    { "17",   489250 },
    { "18",   495250 },
    { "19",   501250 },
    { "20",   507250 },
    { "21",   513250 },
    { "22",   519250 },
    { "23",   525250 },
    { "24",   531250 },
    { "25",   537250 },
    { "26",   543250 },
    { "27",   549250 },
    { "28",   555250 },
    { "29",   561250 },
    { "30",   567250 },
    { "31",   573250 },
    { "32",   579250 },
    { "33",   585250 },
    { "34",   591250 },
    { "35",   597250 },
    { "36",   603250 },
    { "37",   609250 },
    { "38",   615250 },
    { "39",   621250 },
    { "40",   627250 },
    { "41",   633250 },
    { "42",   639250 },
    { "43",   645250 },
    { "44",   651250 },
    { "45",   657250 },
    { "46",   663250 },
    { "47",   669250 },
    { "48",   675250 },
    { "49",   681250 },
    { "50",   687250 },
    { "51",   693250 },
    { "52",   699250 },
    { "53",   705250 },
    { "54",   711250 },
    { "55",   717250 },
    { "56",   723250 },
    { "57",   729250 },
    { "58",   735250 },
    { "59",   741250 },
    { "60",   747250 },
    { "61",   753250 },
    { "62",   759250 },
    { "63",   765250 },
    { "64",   771250 },
    { "65",   777250 },
    { "66",   783250 },
    { "67",   789250 },
    { "68",   795250 },
    { "69",   801250 },
 
    { "70",   807250 },

    { "71",   813250 },
    { "72",   819250 },
    { "73",   825250 },
    { "74",   831250 },
    { "75",   837250 },
    { "76",   843250 },
    { "77",   849250 },
    { "78",   855250 },
    { "79",   861250 },
    { "80",   867250 },
    { "81",   873250 },
    { "82",   879250 },
    { "83",   885250 },
};

int main (int argc, char *argv[])
{
    struct video_channel vc;
    struct video_signal vsig;
    unsigned long freq = 884;
    char *devname=NULL;
    int channel=2, quiet=0, ntsc=0, devfd=-1;
    int  ioctlval, signal;
    int i;

    for (i=1; i<argc; i++) {
        if (strcmp(argv[i], "-d")==0) {
            devname = argv[i+1];
            i++;
        } else if (strcmp(argv[i], "-a")==0) {
            ntsc=0;
        } else if (strcmp(argv[i], "-n")==0) {
            ntsc=1;
        } else if (strcmp(argv[i], "-q")==0) {
            quiet++;
        } else {
            channel = atoi(argv[i]);
        }
    }

    if (devname==NULL) {
        devname = DEFAULT_DEVICE;
    }

    devfd = open(devname, O_RDWR);
    if (devfd < 0) {
        fprintf(stderr, "%s: cannot open %s: %s\n", argv[0], devname, strerror(errno));
        exit(1);
    }

    memset(&vc, 0, sizeof(vc));
    ioctlval = ioctl(devfd, VIDIOCGCHAN, &vc);
    if (ioctlval<0) {
        fprintf(stderr, "%s: ioctl: %s\n", argv[0], strerror(errno));
    }

    if (ntsc) {
        vc.norm = VIDEO_MODE_NTSC;
    } else {
        vc.norm = VIDEO_MODE_ATSC;
    }

    ioctlval = ioctl(devfd,VIDIOCSCHAN,&vc);
    if (ioctlval<0) {
        fprintf(stderr, "%s: ioctl: %s\n", argv[0], strerror(errno));
    }

    channel = channel - 2;
    if(channel < 0) channel = 0;
   
    freq =  ((ntsc_bcast[channel].freq/10)*16)/100;

    ioctlval = ioctl(devfd,VIDIOCSFREQ,&freq);
    if (ioctlval<0) {
        fprintf(stderr, "%s: ioctl: %s\n", argv[0], strerror(errno));
    }

    if (!quiet) {
        sleep(1);

        ioctlval = ioctl(devfd,VIDIOCGSIGNAL,&vsig);
        if (ioctlval<0) {
            fprintf(stderr, "%s: ioctl: %s\n", argv[0], strerror(errno));
        }

        signal = (0xCB - (vsig.strength >> 8)) /2;

        printf("Channel %d (signal strength %d%%)\n", channel+2, signal);
    }

    close(devfd);

    return 0;
}
PostPosted: Fri Jan 09, 2004 9:02 am Reply with quote
elwood
 
Joined: 26 Sep 2003
Posts: 67
Location: Phoenix, AZ




This utility could potentially be used with mythtv as an external tuner. If we could still get around the virtual card problem, this could be used to alleviate the "V4L has only one tuner" problem.

Brad
View user's profile Send private message
PostPosted: Fri Jan 09, 2004 1:17 pm Reply with quote
WaxyLemon
Guest
 




Yeah, that's an interesting idea. There's just one major issue at the moment- this utility only has bcast frequencies. So if you want to tune using us-cable, or something else, at the moment you'd be SOL.
PostPosted: Fri Jan 09, 2004 2:06 pm Reply with quote
elwood
 
Joined: 26 Sep 2003
Posts: 67
Location: Phoenix, AZ




If we let Myth tune the NTSC sources, and use "tune" as the external tuner for the ATSC sources, we'd be golden.

Brad
View user's profile Send private message
Re: new utility: tune
PostPosted: Sun Jan 18, 2004 11:30 pm Reply with quote
Ulmo
 
Joined: 06 Nov 2003
Posts: 95
Location: Aptos,CA,USA




WaxyLemon wrote:
I just modified signal.c to make a simple utility that tunes a channel and puts the card in either NTSC or ATSC mode. It doesn't do anything that signal doesn't already do, it's just designed more for scripting and less for testing/troubleshooting.

The code is pretty clear, but here are the options:

tune [-d device] [-q] [-n|-a] channel

Default device is /dev/dtv0
-q is quiet (don't print signal strength), also results in faster execution
-n sets NTSC mode
-a sets ATSC mode

The 1 second sleep is just something I figured would give the card a better chance to lock the signal before trying to get the strength.


Yes I think it does, although dtvsignal does some other thing while that's happening that I don't yet understand.

Here's my patch to your "tune.c" which adds input support using what I think is still the current driver:

The options become:

tune [-d device] [-i input] [-q] [-n|-a] channel

Default device is /dev/dtv0
Default input seems to be 0
-q is quiet (don't print signal strength), also results in faster execution
-n sets NTSC mode
-a sets ATSC mode

The only two inputs that make sense are "0" and "1".

Code:

--- tune.c.~1~   2004-01-18 21:50:07.000000000 -0800
+++ tune.c   2004-01-18 21:56:32.000000000 -0800
@@ -6,7 +6,7 @@
  * Compile with 'gcc -Wall -O tune.c -o tune'
  *
  * Use:
- * tune [-d /dev/dtv0] 42
+ * tune [-d /dev/dtv0] [-i 0] 42
  * tune takes a dtv device and dtv station channel and prints out the signal
  * strength for that channel to stdout
  *
@@ -140,16 +140,17 @@
     struct video_signal vsig;
     unsigned long freq = 884;
     char *devname=NULL;
-    int channel=2, quiet=0, ntsc=0, devfd=-1;
+    int channel=2, quiet=0, ntsc=0, devfd=-1, input=-1;
     int  ioctlval, signal;
     int i;
 
     for (i=1; i<argc; i++) {
         if (strcmp(argv[i], "-d")==0) {
-            devname = argv[i+1];
-            i++;
+            devname = argv[++i];
         } else if (strcmp(argv[i], "-a")==0) {
             ntsc=0;
+        } else if (strcmp(argv[i], "-i")==0) {
+            input = atoi(argv[++i]);
         } else if (strcmp(argv[i], "-n")==0) {
             ntsc=1;
         } else if (strcmp(argv[i], "-q")==0) {
@@ -181,6 +182,12 @@
         vc.norm = VIDEO_MODE_ATSC;
     }
 
+    if(input<0) {
+       input = !!(vc.flags&VIDEO_RF_AUX_INPUT);
+    } else {
+       vc.flags|=(input?VIDEO_RF_AUX_INPUT:0);
+    }
+
     ioctlval = ioctl(devfd,VIDIOCSCHAN,&vc);
     if (ioctlval<0) {
         fprintf(stderr, "%s: ioctl: %s\n", argv[0], strerror(errno));
@@ -206,7 +213,8 @@
 
         signal = (0xCB - (vsig.strength >> 8)) /2;
 
-        printf("Channel %d (signal strength %d%%)\n", channel+2, signal);
+        printf("Channel %d input %d (signal strength %d%%)\n",
+          channel+2, input, signal);
     }
 
     close(devfd);


Also, I wrote one called "dtvchan.c" a little bit ago, for similar reasons, basically by modifying dtvsignal.c:

See:
ftp://ftp.armory.com/pub/user/ulmo/HW/pcHDTV/HD-2000/
tools/tools.patch.bz2

It includes dtvchan.c, which you can also get individually from the same directory.

Mine does not print signal strength on exit.

Yours does, but I still don't think % makes sense:

Code:
$ ./tune 13 -i 0
Channel 13 (signal strength -10%)
$ ./tune 13 -i 0
Channel 13 input 0 (signal strength 4%)
$ ./tune 13 -i 0
Channel 13 input 0 (signal strength 10%)
$ ./tune 13     
Channel 13 input 0 (signal strength 20%)
$ ./tune 13 -i 1
Channel 2 input 1 (signal strength 0%)
$ ./tune 13 -i 1
Channel 13 input 1 (signal strength 49%)


What's -10%? And how high does it go? % of what?

Anyway, to test, I put two antennas on the above. I thought I'd be able to tell the second antenna was much worse, but actually I found a better spot with it. Smile

I already know it works from previous tests, anyway.


Cable broadcast frequencies are in MythTV's libs/libmythtv/frequencies.c.

Let me check a little bit to see if it makes sense:

tune.c:
Code:
/* US broadcast */
static struct CHANLIST ntsc_bcast[] = {
    { "2",    55250 },
    { "3",    61250 },
    { "4",    67250 },
[...]
    { "42",   639250 },
    { "43",   645250 },
    { "44",   651250 },


13 & below should be = cable & broadcast; however, others should not:

mythtv frequencies.c:
Code:
/* US cable */
static struct CHANLIST ntsc_cable[] = {
    { "1",       73250 },
    { "2",       55250 },
    { "3",       61250 },
[Above are equal; so far so good ... BTW, there is a freq for "channel 0", but Myth doesn't seem to know that]
    { "42",     331250 },
    { "43",     337250 },
    { "44",     343250 },
[Good.  They're there.]


So, you could add another option for antenna or broadcast (can't use -a, since you're using that already, so I suppose -b) and cable (-c for cable?). Also, you could do this table for HRC since some complexes use that here in ATSC land:

mythtv frequencies.c:
Code:
/* US HRC */
static struct CHANLIST ntsc_hrc[] = {
[...]

You could use an option like -h.

There are lists on the Internet of all these frequencies. I have a bookmark someplace that actually tells the whole history of it (it might be on FCC.GOV) for USA.
View user's profile Send private message Send e-mail
Re: new utility: tune
PostPosted: Mon Jan 19, 2004 12:03 am Reply with quote
Ulmo
 
Joined: 06 Nov 2003
Posts: 95
Location: Aptos,CA,USA




Ok. I spent half of this night writing this. The first time I posted this message, it had a buggy patch. Now, it's all done, and seems to test OK. Please tell me if anything doesn't work as designed in these modified tools, since I'd like to fix them. Get the tools.tar.bz2 file:

ftp://ftp.armory.com/pub/user/ulmo/HW/pcHDTV/HD-2000/tools/
tools.tar.bz2
tools.tar.bz2.sig

Ulmo wrote:
Cable broadcast frequencies are in MythTV's libs/libmythtv/frequencies.c.
[...]
So, you could add another option for antenna or broadcast.


I modified your tune.c again to also include frequency table selection. You have to copy frequencies.c and frequencies.h from MythTV or somewhere else that uses the same format (I guess Myth is not the first one to use that file; HD-2000 tools also use the same format), and then compile them in too. Instructions are in tune.c as before.

The option is "-f <tablename>". Use frequencies.c as a guide. The three that make the most sense are:

struct CHANLISTS chanlists[] = {
{ "us-bcast", ntsc_bcast, CHAN_COUNT(ntsc_bcast) },
{ "us-cable", ntsc_cable, CHAN_COUNT(ntsc_cable) },
{ "us-cable-hrc", ntsc_hrc, CHAN_COUNT(ntsc_hrc) }, [...]

i.e., us-bcast, us-cable, and us-cable-hrc.

Here's all the tools I've collected and/or modified in one tar:

ftp://ftp.armory.com/pub/user/ulmo/HW/pcHDTV/HD-2000/
tools/tools.tar.bz2

Also, in the same directory is a signature for that, and a patch file for CD tools.

Earlier, I thought I wrote a patch for the above, and posted it in this message, but it was wrong, so I redid it, with all my other modifications to tools. Now they all take input and frequency table arguments. Arguments to modified pcHDTV tools are still positional, whereas for tune.c they aren't.
View user's profile Send private message Send e-mail
PostPosted: Wed Jan 28, 2004 1:02 pm Reply with quote
WaxyLemon
Guest
 




Looks good. The negative percentages don't happen in dtvsignal because it prints 0 if it is negative. I didn't bother because I was lazy.
new utility: tune
  pcHDTV Forum Index -> General pcHDTV topics
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 7 Hours  
Page 1 of 1  

  
  
 Post new topic  This topic is locked: you cannot edit posts or make replies.  


Powered by phpBB © 2001-2003 phpBB Group
Theme created by Vjacheslav Trushkin