Skip to content

Commit

Permalink
Try to make tuner more robust in case of error when reading async.
Browse files Browse the repository at this point in the history
Version 2.3.5
  • Loading branch information
Wolfrax committed May 30, 2018
1 parent 54b491e commit 477260f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion radar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pkg_resources import get_distribution, DistributionNotFound
import os.path

VERSION = "2.3.4"
VERSION = "2.3.5"

try:
_dist = get_distribution('spots')
Expand Down
15 changes: 14 additions & 1 deletion radar/tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def __init__(self, sr=2.0e6, cf=1090e6, gain='max', filename=None):

self.logger.info("Tuner initializing")

self.sdr_async_ts = 0.0
if filename is None:
self.sdr = rtlsdr.RtlSdr()
self.sdr.DEFAULT_ASYNC_BUF_NUMBER = self.MODES_ASYNC_BUF_NUMBER

self.sdr.sample_rate = sr
self.sdr.center_freq = cf
self.sdr.gain = max(self.sdr.get_gains()) if gain == 'max' else gain
self.original_gain = gain
self.sdr.set_agc_mode(0)
self.logger.info("Tuner initialised to gain {}".format(self.sdr.gain))

Expand Down Expand Up @@ -121,10 +123,21 @@ def run(self):

try:
# self.sdr.read_samples_async(self._sdr_cb, num_samples=self.MODES_DATA_LEN)
self.sdr_async_ts = time.time()
self.sdr.read_bytes_async(self._sdr_cb, num_bytes=self.MODES_DATA_LEN)
except IOError as msg:
self.logger.error("Tuner caught rtlsdr error reading async {}".format(msg))
self.die()

if time.time() - self.sdr_async_ts >= 1.0:
self.sdr = rtlsdr.RtlSdr()
self.sdr.gain = max(self.sdr.get_gains()) if self.original_gain == 'max' else self.original_gain
self.sdr.set_agc_mode(0)
self.logger.info("Tuner re-initialised to gain {}".format(self.sdr.gain))
self.run()
else:
# It was less than 1 sec than we tried to create a new instance of sdr, give it up
self.logger.info("Tuner no luck to re-initialised...time to die")
self.die()
else:
self._sdr_cb(self.sig, None)

Expand Down

0 comments on commit 477260f

Please sign in to comment.