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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/ioctl.h>
32 #define TRIES 25 /* get 25 samples */
33 #define LOCKTIME 400000 /* wait 400ms for card to lock on */
34 #define SAMPLEDELAY 15000 /* wait 15ms between samples */
35 #define THRESHOLD .5 /* minimum acceptable signal % */
39 printf("fmtools fmscan version %s\n\n", FMT_VERSION
);
40 printf("usage: %s [-h] [-d <dev>] [-s <freq>] [-e <freq>] [-i <freq>] [-t <%%>]\n\n", prog
);
42 printf("Auxiliary program to scan a frequency band for radio stations.\n\n");
44 printf(" -h - display this help\n");
45 printf(" -d <dev> - select device (default: /dev/radio0)\n");
46 printf(" -s <freq> - set start of scanning range to <freq>\n");
47 printf(" -e <freq> - set end of scanning range to <freq>\n");
48 printf(" -i <freq> - set increment value between channels to <freq>\n");
49 printf(" -t <%%> - set signal strength percentage to lock onto <%%>\n");
50 printf(" <freq> - a value in the format nnn.nn (MHz)\n");
55 int main(int argc
, char **argv
)
57 int fd
, ret
, i
, tries
= TRIES
;
58 struct video_tuner vt
;
59 float perc
, begval
, incval
, endval
, threshold
;
60 long lowf
, highf
, freq
, totsig
, incr
, fact
;
61 char *progname
, *dev
= NULL
;
63 progname
= argv
[0]; /* getopt munges argv[] later */
66 begval
= 87.9; /* start at 87.9 MHz */
67 incval
= 0.20; /* increment 0.2 MHz */
68 endval
= 107.9; /* stop at 107.9 MHz */
70 threshold
= THRESHOLD
;
72 while ((i
= getopt(argc
, argv
, "+e:hi:s:d:t:")) != EOF
) {
78 endval
= atof(optarg
);
81 incval
= atof(optarg
);
84 begval
= atof(optarg
);
87 threshold
= atof(optarg
)/100.;
97 dev
= strdup("/dev/radio0"); /* default */
99 fd
= open(dev
, O_RDONLY
);
101 fprintf(stderr
, "Unable to open %s: %s\n", dev
, strerror(errno
));
106 ret
= ioctl(fd
, VIDIOCGTUNER
, &vt
); /* get initial info */
108 perror("ioctl VIDIOCGTUNER");
112 if ((vt
.flags
& VIDEO_TUNER_LOW
) == 0)
117 /* cope with bizarre things from atof() like 95.099998 */
118 lowf
= fact
* (ceil(rint(begval
* 10)) / 10);
119 highf
= fact
* (ceil(rint(endval
* 10)) / 10);
121 incr
= fact
* incval
;
123 printf("Scanning range: %2.1f - %2.1f MHz (%2.1f MHz increments)...\n",
124 begval
, endval
, incval
);
126 for (freq
= lowf
; freq
<= highf
; freq
+= incr
) {
127 ret
= ioctl(fd
, VIDIOCSFREQ
, &freq
); /* tune */
130 perror("ioctl VIDIOCSFREQ");
134 printf("%2.1f:\r", (freq
/ (double) fact
));
136 usleep(LOCKTIME
); /* let it lock on */
139 for (i
= 1; i
< tries
+1; i
++) {
141 ret
= ioctl(fd
, VIDIOCGTUNER
, &vt
); /* get info */
143 perror("ioctl VIDIOCGTUNER");
148 perc
= (totsig
/ (65535.0 * i
));
150 printf("%2.1f: checking: %3.1f%% (%d/%d) \r",
151 (freq
/ (double) fact
), perc
* 100.0, i
, tries
);
156 /* clean up the display */
159 perc
= (totsig
/ (65535.0 * tries
));
161 if (perc
> threshold
)
162 printf("%2.1f: %3.1f%% \n",
163 (freq
/ (double) fact
), perc
* 100.0);