From 4cb1448c34429e444ee30c923699427337d398bc Mon Sep 17 00:00:00 2001 From: drewby08 Date: Thu, 22 Mar 2018 13:03:42 +0000 Subject: [PATCH 1/4] adding fm demodulation via python subprocess --- controller.py | 39 +++++++++++++++++++++++++++++++++++++-- freqshow.py | 12 ++++++------ model.py | 12 ++++++++++-- views.py | 4 +++- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/controller.py b/controller.py index d94ead2..0a6cc97 100644 --- a/controller.py +++ b/controller.py @@ -25,12 +25,18 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from views import * - +import subprocess +import time 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 +48,36 @@ def __init__(self, model): self._current_view = None self.change_to_instant() - def change_view(self, view): + def demodulate(self, *args): + if not self.demodulating: + if self.rtl_fm_process is not None: + self.rtl_fm_process.terminate() + + if self.aplay_process is not None: + self.aplay_process.terminate() + + 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: + if self.rtl_fm_process is not None: + self.rtl_fm_process.terminate() + + if self.aplay_process is not None: + self.aplay_process.terminate() + self.demodulating = False + #reopen sdr for system + self.model.open_sdr() + self.model.set_center_freq(self.prev_center_freq) + + def change_view(self, view): + 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..95190a0 100644 --- a/freqshow.py +++ b/freqshow.py @@ -70,13 +70,13 @@ if __name__ == '__main__': # Initialize pygame and SDL to use the PiTFT display and touchscreen. - os.putenv('SDL_VIDEODRIVER', 'fbcon') - os.putenv('SDL_FBDEV' , '/dev/fb1') - os.putenv('SDL_MOUSEDRV' , 'TSLIB') - os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen') + #os.putenv('SDL_VIDEODRIVER', 'fbcon') + #os.putenv('SDL_FBDEV' , '/dev/fb1') + #os.putenv('SDL_MOUSEDRV' , 'TSLIB') + #os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen') pygame.display.init() pygame.font.init() - pygame.mouse.set_visible(False) + pygame.mouse.set_visible(True) # Get size of screen and create main rendering surface. size = (pygame.display.Info().current_w, pygame.display.Info().current_h) screen = pygame.display.set_mode(size, pygame.FULLSCREEN) @@ -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 From 4df636b1ad05c29fb552de7af8f1cda0b6f06a28 Mon Sep 17 00:00:00 2001 From: drewby08 Date: Thu, 22 Mar 2018 13:09:59 +0000 Subject: [PATCH 2/4] refactoring --- controller.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/controller.py b/controller.py index 0a6cc97..8e22b4b 100644 --- a/controller.py +++ b/controller.py @@ -49,32 +49,28 @@ def __init__(self, model): self.change_to_instant() def demodulate(self, *args): - if not self.demodulating: - if self.rtl_fm_process is not None: - self.rtl_fm_process.terminate() + #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 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: - if self.rtl_fm_process is not None: - self.rtl_fm_process.terminate() - - if self.aplay_process is not None: - self.aplay_process.terminate() - self.demodulating = False #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() From 405eab39ec87bb7905a494b9427416464a9a75c2 Mon Sep 17 00:00:00 2001 From: drewby08 Date: Thu, 22 Mar 2018 13:16:26 +0000 Subject: [PATCH 3/4] not using time here --- controller.py | 1 - 1 file changed, 1 deletion(-) diff --git a/controller.py b/controller.py index 8e22b4b..07a6520 100644 --- a/controller.py +++ b/controller.py @@ -26,7 +26,6 @@ # SOFTWARE. from views import * import subprocess -import time class FreqShowController(object): """Class which controls the views shown in the application and mediates From 05197486655519cce329120d525a24715114146c Mon Sep 17 00:00:00 2001 From: drew Date: Sat, 29 Jun 2019 13:49:45 -0400 Subject: [PATCH 4/4] uncommenting SDL environment variables --- freqshow.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/freqshow.py b/freqshow.py index 95190a0..4956ba7 100644 --- a/freqshow.py +++ b/freqshow.py @@ -70,13 +70,13 @@ if __name__ == '__main__': # Initialize pygame and SDL to use the PiTFT display and touchscreen. - #os.putenv('SDL_VIDEODRIVER', 'fbcon') - #os.putenv('SDL_FBDEV' , '/dev/fb1') - #os.putenv('SDL_MOUSEDRV' , 'TSLIB') - #os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen') + os.putenv('SDL_VIDEODRIVER', 'fbcon') + os.putenv('SDL_FBDEV' , '/dev/fb1') + os.putenv('SDL_MOUSEDRV' , 'TSLIB') + os.putenv('SDL_MOUSEDEV' , '/dev/input/touchscreen') pygame.display.init() pygame.font.init() - pygame.mouse.set_visible(True) + pygame.mouse.set_visible(False) # Get size of screen and create main rendering surface. size = (pygame.display.Info().current_w, pygame.display.Info().current_h) screen = pygame.display.set_mode(size, pygame.FULLSCREEN)