From 3a066ff25866999b611a9f7914c3da570616bc43 Mon Sep 17 00:00:00 2001 From: Dimitri Stolnikov Date: Sat, 5 May 2012 21:37:13 +0200 Subject: [PATCH] implement virtually arbitrary gain values internally, this will pick a certain gain value out of the valid gains using the "smallest error" strategy. --- lib/rtl/rtl_source_c.cc | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/rtl/rtl_source_c.cc b/lib/rtl/rtl_source_c.cc index c594abc..732f8e7 100644 --- a/lib/rtl/rtl_source_c.cc +++ b/lib/rtl/rtl_source_c.cc @@ -435,10 +435,31 @@ bool rtl_source_c::get_gain_mode( size_t chan ) return _auto_gain; } +double pick_closest_gain(osmosdr::gain_range_t &gains, double required) +{ + double result = required; + double distance = 100; + + BOOST_FOREACH(osmosdr::range_t gain, gains) + { + double diff = fabs(gain.start() - required); + + if (diff < distance) { + distance = diff; + result = gain.start(); + } + } + + return result; +} + double rtl_source_c::set_gain( double gain, size_t chan ) { + osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( chan ); + double picked_gain = pick_closest_gain( gains, gain ); + if (_dev) - rtlsdr_set_tuner_gain( _dev, int(gain * 10.0) ); + rtlsdr_set_tuner_gain( _dev, int(picked_gain * 10.0) ); return get_gain( chan ); }