Add sound manager initial source code
[staging/soundmanager.git] / sample / radio / binding / convenience / convenience.c
1 /*
2  * Copyright (C) 2014 by Kyle Keen <keenerd@gmail.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 /* a collection of user friendly tools
19  * todo: use strtol for more flexible int parsing
20  * */
21
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #ifndef _WIN32
27 #include <unistd.h>
28 #else
29 #include <windows.h>
30 #include <fcntl.h>
31 #include <io.h>
32 #define _USE_MATH_DEFINES
33 #endif
34
35 #include <math.h>
36
37 #include "rtl-sdr.h"
38
39 double atofs(char *s)
40 /* standard suffixes */
41 {
42         char last;
43         int len;
44         double suff = 1.0;
45         len = strlen(s);
46         last = s[len-1];
47         s[len-1] = '\0';
48         switch (last) {
49                 case 'g':
50                 case 'G':
51                         suff *= 1e3;
52                 case 'm':
53                 case 'M':
54                         suff *= 1e3;
55                 case 'k':
56                 case 'K':
57                         suff *= 1e3;
58                         suff *= atof(s);
59                         s[len-1] = last;
60                         return suff;
61         }
62         s[len-1] = last;
63         return atof(s);
64 }
65
66 double atoft(char *s)
67 /* time suffixes, returns seconds */
68 {
69         char last;
70         int len;
71         double suff = 1.0;
72         len = strlen(s);
73         last = s[len-1];
74         s[len-1] = '\0';
75         switch (last) {
76                 case 'h':
77                 case 'H':
78                         suff *= 60;
79                 case 'm':
80                 case 'M':
81                         suff *= 60;
82                 case 's':
83                 case 'S':
84                         suff *= atof(s);
85                         s[len-1] = last;
86                         return suff;
87         }
88         s[len-1] = last;
89         return atof(s);
90 }
91
92 double atofp(char *s)
93 /* percent suffixes */
94 {
95         char last;
96         int len;
97         double suff = 1.0;
98         len = strlen(s);
99         last = s[len-1];
100         s[len-1] = '\0';
101         switch (last) {
102                 case '%':
103                         suff *= 0.01;
104                         suff *= atof(s);
105                         s[len-1] = last;
106                         return suff;
107         }
108         s[len-1] = last;
109         return atof(s);
110 }
111
112 int nearest_gain(rtlsdr_dev_t *dev, int target_gain)
113 {
114         int i, r, err1, err2, count, nearest;
115         int* gains;
116         r = rtlsdr_set_tuner_gain_mode(dev, 1);
117         if (r < 0) {
118                 fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
119                 return r;
120         }
121         count = rtlsdr_get_tuner_gains(dev, NULL);
122         if (count <= 0) {
123                 return 0;
124         }
125         gains = malloc(sizeof(int) * count);
126         count = rtlsdr_get_tuner_gains(dev, gains);
127         nearest = gains[0];
128         for (i=0; i<count; i++) {
129                 err1 = abs(target_gain - nearest);
130                 err2 = abs(target_gain - gains[i]);
131                 if (err2 < err1) {
132                         nearest = gains[i];
133                 }
134         }
135         free(gains);
136         return nearest;
137 }
138
139 int verbose_set_frequency(rtlsdr_dev_t *dev, uint32_t frequency)
140 {
141         int r;
142         r = rtlsdr_set_center_freq(dev, frequency);
143         if (r < 0) {
144                 fprintf(stderr, "WARNING: Failed to set center freq.\n");
145         } else {
146                 fprintf(stderr, "Tuned to %u Hz.\n", frequency);
147         }
148         return r;
149 }
150
151 int verbose_set_sample_rate(rtlsdr_dev_t *dev, uint32_t samp_rate)
152 {
153         int r;
154         r = rtlsdr_set_sample_rate(dev, samp_rate);
155         if (r < 0) {
156                 fprintf(stderr, "WARNING: Failed to set sample rate.\n");
157         } else {
158                 fprintf(stderr, "Sampling at %u S/s.\n", samp_rate);
159         }
160         return r;
161 }
162
163 int verbose_direct_sampling(rtlsdr_dev_t *dev, int on)
164 {
165         int r;
166         r = rtlsdr_set_direct_sampling(dev, on);
167         if (r != 0) {
168                 fprintf(stderr, "WARNING: Failed to set direct sampling mode.\n");
169                 return r;
170         }
171         if (on == 0) {
172                 fprintf(stderr, "Direct sampling mode disabled.\n");}
173         if (on == 1) {
174                 fprintf(stderr, "Enabled direct sampling mode, input 1/I.\n");}
175         if (on == 2) {
176                 fprintf(stderr, "Enabled direct sampling mode, input 2/Q.\n");}
177         return r;
178 }
179
180 int verbose_offset_tuning(rtlsdr_dev_t *dev)
181 {
182         int r;
183         r = rtlsdr_set_offset_tuning(dev, 1);
184         if (r != 0) {
185                 fprintf(stderr, "WARNING: Failed to set offset tuning.\n");
186         } else {
187                 fprintf(stderr, "Offset tuning mode enabled.\n");
188         }
189         return r;
190 }
191
192 int verbose_auto_gain(rtlsdr_dev_t *dev)
193 {
194         int r;
195         r = rtlsdr_set_tuner_gain_mode(dev, 0);
196         if (r != 0) {
197                 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
198         } else {
199                 fprintf(stderr, "Tuner gain set to automatic.\n");
200         }
201         return r;
202 }
203
204 int verbose_gain_set(rtlsdr_dev_t *dev, int gain)
205 {
206         int r;
207         r = rtlsdr_set_tuner_gain_mode(dev, 1);
208         if (r < 0) {
209                 fprintf(stderr, "WARNING: Failed to enable manual gain.\n");
210                 return r;
211         }
212         r = rtlsdr_set_tuner_gain(dev, gain);
213         if (r != 0) {
214                 fprintf(stderr, "WARNING: Failed to set tuner gain.\n");
215         } else {
216                 fprintf(stderr, "Tuner gain set to %0.2f dB.\n", gain/10.0);
217         }
218         return r;
219 }
220
221 int verbose_ppm_set(rtlsdr_dev_t *dev, int ppm_error)
222 {
223         int r;
224         if (ppm_error == 0) {
225                 return 0;}
226         r = rtlsdr_set_freq_correction(dev, ppm_error);
227         if (r < 0) {
228                 fprintf(stderr, "WARNING: Failed to set ppm error.\n");
229         } else {
230                 fprintf(stderr, "Tuner error set to %i ppm.\n", ppm_error);
231         }
232         return r;
233 }
234
235 int verbose_reset_buffer(rtlsdr_dev_t *dev)
236 {
237         int r;
238         r = rtlsdr_reset_buffer(dev);
239         if (r < 0) {
240                 fprintf(stderr, "WARNING: Failed to reset buffers.\n");}
241         return r;
242 }
243
244 int verbose_device_search(char *s)
245 {
246         int i, device_count, device, offset;
247         char *s2;
248         char vendor[256], product[256], serial[256];
249         device_count = rtlsdr_get_device_count();
250         if (!device_count) {
251                 fprintf(stderr, "No supported devices found.\n");
252                 return -1;
253         }
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);
258         }
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));
265                 return device;
266         }
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) {
271                         continue;}
272                 device = i;
273                 fprintf(stderr, "Using device %d: %s\n",
274                         device, rtlsdr_get_device_name((uint32_t)device));
275                 return device;
276         }
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) {
281                         continue;}
282                 device = i;
283                 fprintf(stderr, "Using device %d: %s\n",
284                         device, rtlsdr_get_device_name((uint32_t)device));
285                 return device;
286         }
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);
291                 if (offset < 0) {
292                         continue;}
293                 if (strncmp(s, serial+offset, strlen(s)) != 0) {
294                         continue;}
295                 device = i;
296                 fprintf(stderr, "Using device %d: %s\n",
297                         device, rtlsdr_get_device_name((uint32_t)device));
298                 return device;
299         }
300         fprintf(stderr, "No matching devices found.\n");
301         return -1;
302 }
303
304 // vim: tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab