diff --git a/controller.py b/controller.py index d94ead2..07a6520 100644 --- a/controller.py +++ b/controller.py @@ -25,12 +25,17 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from views import * - +import subprocess class FreqShowController(object): """Class which controls the views shown in the application and mediates changing between views. """ + rtl_fm_process = None + aplay_process = None + demodulating = False + prev_center_freq = 90.3 + def __init__(self, model): """Initialize controller with specified FreqShow model.""" self.model = model @@ -42,7 +47,32 @@ def __init__(self, model): self._current_view = None self.change_to_instant() - def change_view(self, view): + def demodulate(self, *args): + #kill the previous sub process + if self.rtl_fm_process is not None: + self.rtl_fm_process.terminate() + + if self.aplay_process is not None: + self.aplay_process.terminate() + + if not self.demodulating: + self.model.close_sdr() + self.prev_center_freq = self.model.get_center_freq() + freq = self.prev_center_freq * 1000000.0 + self.rtl_fm_process = subprocess.Popen(["rtl_fm", "-M", "fm", "-s", "200000", "-r", "48000", "-f", str(freq) ], stdout=subprocess.PIPE) + self.aplay_process = subprocess.Popen(["aplay", "-r", "48000", "-f", "S16_LE"], stdin=self.rtl_fm_process.stdout) + self.demodulating = True + else: + #reopen sdr for system + self.model.open_sdr() + self.model.set_center_freq(self.prev_center_freq) + self.demodulating = False + + def change_view(self, view): + #if currently demodulating stop + if self.demodulating: + self.demodulate() + """Change to specified view.""" self._prev_view = self._current_view self._current_view = view diff --git a/freqshow.py b/freqshow.py index 1b563a6..4956ba7 100644 --- a/freqshow.py +++ b/freqshow.py @@ -101,4 +101,4 @@ fscontroller.current().click(pygame.mouse.get_pos()) # Update and render the current view. fscontroller.current().render(screen) - pygame.display.update() \ No newline at end of file + pygame.display.update() diff --git a/model.py b/model.py index 3dded55..aa24813 100644 --- a/model.py +++ b/model.py @@ -26,7 +26,7 @@ from rtlsdr import * import freqshow - +import time class FreqShowModel(object): def __init__(self, width, height): @@ -47,6 +47,14 @@ def __init__(self, width, height): self.set_sample_rate(2.4) self.set_gain('AUTO') + def close_sdr(self): + self.sdr.close() + + def open_sdr(self): + self.sdr.close() + time.sleep(1) + self.sdr.open() + def _clear_intensity(self): if self.min_auto_scale: self.min_intensity = None @@ -59,7 +67,7 @@ def get_center_freq(self): return self.sdr.get_center_freq()/1000000.0 def set_center_freq(self, freq_mhz): - """Set tuner center frequency to provided megahertz value.""" + """Set tuner center frequency to provided megahertz value.""" try: self.sdr.set_center_freq(freq_mhz*1000000.0) self._clear_intensity() diff --git a/views.py b/views.py index 83b3230..5d84123 100644 --- a/views.py +++ b/views.py @@ -265,7 +265,8 @@ def __init__(self, model, controller): self.buttons.add(0, 2, gain_text, colspan=4, click=self.gain_click) self.buttons.add(0, 3, min_text, colspan=2, click=self.min_click) self.buttons.add(2, 3, max_text, colspan=2, click=self.max_click) - self.buttons.add(0, 4, 'BACK', click=self.controller.change_to_main) + self.buttons.add(0, 4, 'Demodulation', colspan=2, click=self.controller.demodulate) + self.buttons.add(2, 4, 'BACK', colspan=2, click=self.controller.change_to_main) def render(self, screen): # Clear view and render buttons. @@ -339,6 +340,7 @@ def __init__(self, model, controller): self.buttons.add(3, 0, 'QUIT', click=self.quit_click, bg_color=freqshow.CANCEL_BG) self.overlay_enabled = True + model.open_sdr() def render_spectrogram(self, screen): """Subclass should implement spectorgram rendering in the provided