2 * Copyright (C) 2014 by Kyle Keen <keenerd@gmail.com>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /* a collection of user friendly tools
19 * todo: use strtol for more flexible int parsing
32 #define _USE_MATH_DEFINES
40 /* standard suffixes */
67 /* time suffixes, returns seconds */
93 /* percent suffixes */
112 int nearest_gain(rtlsdr_dev_t *dev, int target_gain)
114 int i, r, err1, err2, count, nearest;
116 r = rtlsdr_set_tuner_gain_mode(dev, 1);
118 fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
121 count = rtlsdr_get_tuner_gains(dev, NULL);
125 gains = malloc(sizeof(int) * count);
126 count = rtlsdr_get_tuner_gains(dev, gains);
128 for (i=0; i<count; i++) {
129 err1 = abs(target_gain - nearest);
130 err2 = abs(target_gain - gains[i]);
139 int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency)
142 r = rtlsdr_set_center_freq(dev, frequency);
144 fprintf(stderr, "WARNING: Failed to set center freq.\n");
146 fprintf(stderr, "Tuned to %u Hz.\n", frequency);
151 int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
154 r = rtlsdr_set_sample_rate(dev, samp_rate);
156 fprintf(stderr, "WARNING: Failed to set sample rate.\n");
158 fprintf(stderr, "Sampling at %u S/s.\n", samp_rate);
163 int verbose_direct_sampling(rtlsdr_dev_t *dev, int on)
166 r = rtlsdr_set_direct_sampling(dev, on);
168 fprintf(stderr, "WARNING: Failed to set direct sampling mode.\n");
172 fprintf(stderr, "Direct sampling mode disabled.\n");}
174 fprintf(stderr, "Enabled direct sampling mode, input 1/I.\n");}
176 fprintf(stderr, "Enabled direct sampling mode, input 2/Q.\n");}
180 int verbose_offset_tuning(rtlsdr_dev_t *dev)
183 r = rtlsdr_set_offset_tuning(dev, 1);
185 fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
187 fprintf(stderr, "Offset tuning mode enabled.\n");
192 int verbose_auto_gain(rtlsdr_dev_t *dev)
195 r = rtlsdr_set_tuner_gain_mode(dev, 0);
197 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
199 fprintf(stderr, "Tuner gain set to automatic.\n");
204 int verbose_gain_set(rtlsdr_dev_t *dev, int gain)
207 r = rtlsdr_set_tuner_gain_mode(dev, 1);
209 fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
212 r = rtlsdr_set_tuner_gain(dev, gain);
214 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
216 fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0);
221 int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error)
224 if (ppm_error == 0) {
226 r = rtlsdr_set_freq_correction(dev, ppm_error);
228 fprintf(stderr, "WARNING: Failed to set ppm error.\n");
230 fprintf(stderr, "Tuner error set to %i ppm.\n", ppm_error);
235 int verbose_reset_buffer(rtlsdr_dev_t *dev)
238 r = rtlsdr_reset_buffer(dev);
240 fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
244 int verbose_device_search(char *s)
246 int i, device_count, device, offset;
248 char vendor[256], product[256], serial[256];
249 device_count = rtlsdr_get_device_count();
251 fprintf(stderr, "No supported devices found.\n");
254 fprintf(stderr, "Found %d device(s):\n", device_count);
255 for (i = 0; i < device_count; i++) {
256 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
257 fprintf(stderr, " %d: %s, %s, SN: %s\n", i, vendor, product, serial);
259 fprintf(stderr, "\n");
260 /* does string look like raw id number */
261 device = (int)strtol(s, &s2, 0);
262 if (s2[0] == '\0' && device >= 0 && device < device_count) {
263 fprintf(stderr, "Using device %d: %s\n",
264 device, rtlsdr_get_device_name((uint32_t)device));
267 /* does string exact match a serial */
268 for (i = 0; i < device_count; i++) {
269 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
270 if (strcmp(s, serial) != 0) {
273 fprintf(stderr, "Using device %d: %s\n",
274 device, rtlsdr_get_device_name((uint32_t)device));
277 /* does string prefix match a serial */
278 for (i = 0; i < device_count; i++) {
279 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
280 if (strncmp(s, serial, strlen(s)) != 0) {
283 fprintf(stderr, "Using device %d: %s\n",
284 device, rtlsdr_get_device_name((uint32_t)device));
287 /* does string suffix match a serial */
288 for (i = 0; i < device_count; i++) {
289 rtlsdr_get_device_usb_strings(i, vendor, product, serial);
290 offset = strlen(serial) - strlen(s);
293 if (strncmp(s, serial+offset, strlen(s)) != 0) {
296 fprintf(stderr, "Using device %d: %s\n",
297 device, rtlsdr_get_device_name((uint32_t)device));
300 fprintf(stderr, "No matching devices found.\n");
304 // vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab