fmtools 0.2.5.
[fmtools.git] / fmscan.c
blob848aea738cf2d80bb6fdea3b131d286ecf13feb5
1 /* scan.c - v4l radio band scanner using signal strength
3 Copyright (C) 1999 Russell Kroll <rkroll@exploits.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <math.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <asm/types.h>
27 #include <sys/ioctl.h>
28 #include <linux/videodev.h>
30 void help (char *prog)
32 printf ("usage: %s [-h] [-s <freq>] [-e <freq>] [-i <freq>]\n\n", prog);
34 printf ("-h - display this help\n");
35 printf ("-s <freq> - set start of scanning range to <freq>\n");
36 printf ("-e <freq> - set end of scanning range to <freq>\n");
37 printf ("-i <freq> - set increment value between channels to <freq>\n");
38 printf ("<freq> - a value in the format nnn.nn (MHz)\n");
40 exit (1);
43 int main(int argc, char **argv)
45 int fd, ret, i, tries = 25;
46 struct video_tuner vt;
47 float perc, begval, incval, endval;
48 long lowf, highf, freq, totsig, incr, fact;
49 char *progname;
51 progname = argv[0]; /* getopt munges argv[] later */
53 fd = open ("/dev/radio0", O_RDONLY);
54 if (fd == -1) {
55 perror ("Unable to open /dev/radio0");
56 exit (1);
59 /* USA defaults */
60 begval = 87.9; /* start at 87.9 MHz */
61 incval = 0.20; /* increment 0.2 MHz */
62 endval = 107.9; /* stop at 107.9 MHz */
64 while ((i = getopt(argc, argv, "+e:hi:s:")) != EOF) {
65 switch (i) {
66 case 'e':
67 endval = atof (optarg);
68 break;
69 case 'i':
70 incval = atof (optarg);
71 break;
72 case 's':
73 begval = atof (optarg);
74 break;
75 case 'h':
76 default:
77 help(progname);
78 break;
82 vt.tuner = 0;
83 ret = ioctl(fd, VIDIOCGTUNER, &vt); /* get initial info */
85 if ((vt.flags & VIDEO_TUNER_LOW) == 0)
86 fact = 16;
87 else
88 fact = 16000;
90 /* cope with bizarre things from atof() like 95.099998 */
91 lowf = fact * (ceil(rint(begval * 10)) / 10);
92 highf = fact * (ceil(rint(endval * 10)) / 10);
94 incr = fact * incval;
96 printf ("Scanning range: %2.1f - %2.1f MHz (%2.1f MHz increments)...\n",
97 begval, endval, incval);
99 for (freq = lowf; freq <= highf; freq += incr) {
100 ioctl (fd, VIDIOCSFREQ, &freq); /* tune */
102 printf ("%2.1f: checking\r", (freq / (double) fact));
104 fflush (stdout);
105 usleep (400000); /* let it lock on */
107 totsig = 0;
108 for (i = 0; i < tries; i++) {
109 vt.tuner = 0;
110 ret = ioctl(fd, VIDIOCGTUNER, &vt); /* get info */
111 totsig += vt.signal;
112 fflush(stdout);
113 usleep (15000);
116 /* clean up the display */
117 printf (" \r");
119 perc = (totsig / (65535.0 * tries));
121 if (perc > .5)
122 printf ("%2.1f: %3.1f%% \n",
123 (freq / (double) fact), perc * 100.0);
126 close (fd);
128 return (0);