void modesInitRTLSDR(void) { int j; int device_count; char vendor[256], product[256], serial[256]; device_count = rtlsdr_get_device_count(); if (!device_count) { fprintf(stderr, "[dvbt][e]No supported RTLSDR devices found.\n"); exit(1); } fprintf(stderr, "[dvbt][i]Found %d device(s):\n", device_count); for (j = 0; j < device_count; j++) { rtlsdr_get_device_usb_strings(j, vendor, product, serial); fprintf(stderr, "[dvbt][i]%d: %s, %s, SN: %s %s\n", j, vendor, product, serial, (j == Modes.dev_index) ? "(currently selected)" : ""); } if (rtlsdr_open(&Modes.dev, Modes.dev_index) < 0) { fprintf(stderr, "[dvbt][e]Error opening the RTLSDR device: %s\n", strerror(errno)); exit(1); } /* Set gain, frequency, sample rate, and reset the device. */ rtlsdr_set_tuner_gain_mode(Modes.dev, (Modes.gain == MODES_AUTO_GAIN) ? 0 : 1); if (Modes.gain != MODES_AUTO_GAIN) { if (Modes.gain == MODES_MAX_GAIN) { /* Find the maximum gain available. */ int numgains; int gains[100]; numgains = rtlsdr_get_tuner_gains(Modes.dev, gains); Modes.gain = gains[numgains-1]; fprintf(stderr, "[dvbt][i]Max available gain is: %.2f\n", Modes.gain/10.0); } rtlsdr_set_tuner_gain(Modes.dev, Modes.gain); fprintf(stderr, "[dvbt][i]Setting gain to: %.2f\n", Modes.gain/10.0); } else { fprintf(stderr, "[dvbt][i]Using automatic gain control.\n"); } if( Modes.ppm_error != 0) { fprintf(stderr, "[dvbt][i]Setting ppm error to: %d\n", Modes.ppm_error); } rtlsdr_set_freq_correction(Modes.dev, Modes.ppm_error); if (Modes.enable_agc) rtlsdr_set_agc_mode(Modes.dev, 1); rtlsdr_set_center_freq(Modes.dev, Modes.freq); rtlsdr_set_sample_rate(Modes.dev, MODES_DEFAULT_RATE); rtlsdr_reset_buffer(Modes.dev); fprintf(stderr, "[dvbt][i]Gain reported by device: %.2f\n", rtlsdr_get_tuner_gain(Modes.dev)/10.0); }
void dabStick::setExternalGain (int gain) { static int oldGain = 0; if (gain == oldGain) return; if ((gain < 0) || (gain >= gainsCount)) return; oldGain = gain; rtlsdr_set_tuner_gain (device, gains [gain]); (void)rtlsdr_get_tuner_gain (device); gainDisplay -> display (gain); }
void regain(rtlsdr_dev_t *d, int gain) { int g; g = rtlsdr_get_tuner_gain(dev); if (g == gain) { return;} if (gain == AUTO_GAIN) { /* switch to auto */ rtlsdr_set_tuner_gain_mode(dev, 0); return; } if (g == AUTO_GAIN) { /* switch to manual */ rtlsdr_set_tuner_gain_mode(dev, 1); } rtlsdr_set_tuner_gain(dev, gain); }
// // =============================== RTLSDR handling ========================== // int modesInitRTLSDR(void) { int j; int device_count, dev_index = 0; char vendor[256], product[256], serial[256]; if (Modes.dev_name) { if ( (dev_index = verbose_device_search(Modes.dev_name)) < 0 ) return -1; } device_count = rtlsdr_get_device_count(); if (!device_count) { fprintf(stderr, "No supported RTLSDR devices found.\n"); return -1; } fprintf(stderr, "Found %d device(s):\n", device_count); for (j = 0; j < device_count; j++) { rtlsdr_get_device_usb_strings(j, vendor, product, serial); fprintf(stderr, "%d: %s, %s, SN: %s %s\n", j, vendor, product, serial, (j == dev_index) ? "(currently selected)" : ""); } if (rtlsdr_open(&Modes.dev, dev_index) < 0) { fprintf(stderr, "Error opening the RTLSDR device: %s\n", strerror(errno)); return -1; } // Set gain, frequency, sample rate, and reset the device rtlsdr_set_tuner_gain_mode(Modes.dev, (Modes.gain == MODES_AUTO_GAIN) ? 0 : 1); if (Modes.gain != MODES_AUTO_GAIN) { int *gains; int numgains; numgains = rtlsdr_get_tuner_gains(Modes.dev, NULL); if (numgains <= 0) { fprintf(stderr, "Error getting tuner gains\n"); return -1; } gains = malloc(numgains * sizeof(int)); if (rtlsdr_get_tuner_gains(Modes.dev, gains) != numgains) { fprintf(stderr, "Error getting tuner gains\n"); free(gains); return -1; } if (Modes.gain == MODES_MAX_GAIN) { int highest = -1; int i; for (i = 0; i < numgains; ++i) { if (gains[i] > highest) highest = gains[i]; } Modes.gain = highest; fprintf(stderr, "Max available gain is: %.2f dB\n", Modes.gain/10.0); } else { int closest = -1; int i; for (i = 0; i < numgains; ++i) { if (closest == -1 || abs(gains[i] - Modes.gain) < abs(closest - Modes.gain)) closest = gains[i]; } if (closest != Modes.gain) { Modes.gain = closest; fprintf(stderr, "Closest available gain: %.2f dB\n", Modes.gain/10.0); } } free(gains); fprintf(stderr, "Setting gain to: %.2f dB\n", Modes.gain/10.0); if (rtlsdr_set_tuner_gain(Modes.dev, Modes.gain) < 0) { fprintf(stderr, "Error setting tuner gains\n"); return -1; } } else { fprintf(stderr, "Using automatic gain control.\n"); } rtlsdr_set_freq_correction(Modes.dev, Modes.ppm_error); if (Modes.enable_agc) rtlsdr_set_agc_mode(Modes.dev, 1); rtlsdr_set_center_freq(Modes.dev, Modes.freq); rtlsdr_set_sample_rate(Modes.dev, (unsigned)Modes.sample_rate); rtlsdr_reset_buffer(Modes.dev); fprintf(stderr, "Gain reported by device: %.2f dB\n", rtlsdr_get_tuner_gain(Modes.dev)/10.0); return 0; }
int main(int argc, char* argv[]) { int rv; async_started = 0; quit_please = 0; buffers_received = 0; signal(SIGINT, sighandler); printf("Opening output file...\n"); outf = fopen("rtlsdr_out.bin", "w"); if(outf == NULL) { printf("Error opening output file: %d.\nExiting.\n", errno); return 1; } rv = rtlsdr_get_device_count(); if(rv == 0) { printf("No RTL-SDR devices found, exiting.\n"); return 2; } printf("Found %d device(s).\n", rv); printf("Opening the first, '%s'...\n", rtlsdr_get_device_name(0)); rv = rtlsdr_open(&rtlsdr, 0); if(rv != 0) { printf("Error opening device: %d\nExiting.\n", rv); return 3; } printf("Setting frequency to 315MHz...\n"); rv = rtlsdr_set_center_freq(rtlsdr, 315000000); if(rv != 0) { printf("Error setting frequency: %d\nExiting.\n", rv); return 4; } printf("Frequency set to %uHz.\n", rtlsdr_get_center_freq(rtlsdr)); printf("Setting gain mode to automatic.\n"); rv = rtlsdr_set_tuner_gain_mode(rtlsdr, 0); if(rv != 0) { printf("Error setting gain mode: %d\nExiting.\n", rv); return 5; } printf("Gain currently set to %d.\n", rtlsdr_get_tuner_gain(rtlsdr)); printf("Setting sample rate to 240kHz...\n"); rv = rtlsdr_set_sample_rate(rtlsdr, 240000); if(rv != 0) { printf("Error setting sample rate: %d\nExiting.\n", rv); return 6; } printf("Sample rate set to %u.\n", rtlsdr_get_sample_rate(rtlsdr)); printf("Setting AGC on...\n"); rv = rtlsdr_set_agc_mode(rtlsdr, 1); if(rv != 0) { printf("Error setting AGC: %d\nExiting.\n", rv); return 7; } printf("Clearing buffer and streaming data...\n"); rv = rtlsdr_reset_buffer(rtlsdr); if(rv != 0) { printf("Error clearing buffer: %d\nExiting.\n", rv); return 8; } async_started = 1; rv = rtlsdr_read_async(rtlsdr, read_callback, NULL, 0, 0); if(rv != 0) { printf("Error setting up async streaming: %d\nExiting.\n", rv); return 9; } rtlsdr_cancel_async(rtlsdr); rtlsdr_close(rtlsdr); return 0; }