diff --git a/lib/gr/file_sink.py b/lib/gr/file_sink.py index 41006b8e3d396bea57614229b13490daa57baa57..c5cfdcafb7d8c02577678578f780ab3b6bb1f39c 100644 --- a/lib/gr/file_sink.py +++ b/lib/gr/file_sink.py @@ -11,30 +11,21 @@ from gnuradio import gr, blocks from gnuradio.eng_option import eng_option from gnuradio.filter import firdes +from PyQt5.QtCore import QThread, pyqtSignal, QObject + from lib.disk import Disk -class FileSink(gr.top_block): +class FileSink(QThread,gr.top_block): """ FileSink class to create I/Q sample binary dump files. """ - def __init__(self, - dump_file, - freq, - samp_rate, - capture_time, - device, - parent=None - ): + def __init__(self): + QThread.__init__(self) gr.top_block.__init__(self, "file_sink_block") + self.samp_rate = None - self.file_path = str(dump_file) - self.samp_rate = samp_rate - self.freq = freq - self.capture_time = capture_time - self.capture_device = \ - osmosdr.source(args="numchan=" + str(1) + " " + "" ) - - self._build_blocks() + def __del__(self): + self.wait() def needed_disc_space(self): """ @@ -42,6 +33,15 @@ class FileSink(gr.top_block): """ pass + def run(self): + """ + Capture alias + """ + print("run samp_rate: %s" % self.samp_rate) + self._build_blocks() + print("Start capture") + self.capture() + def capture(self): """ Capture samples to dump file. @@ -62,7 +62,7 @@ class FileSink(gr.top_block): return self.capture_time def get_capture_device(self): - return self.get_capture_device + return self.device def _dump_file_writable(self): """ @@ -71,9 +71,12 @@ class FileSink(gr.top_block): Disk.path_writeble(self.file_path) def _build_blocks(self): + """ + Create gr blocks. + """ # blocks # configure capture device - self.device_source = self.capture_device + self.device_source = osmosdr.source(args="numchan=" + str(1) + " " + "" ) self.device_source.set_sample_rate(self.samp_rate) self.device_source.set_center_freq(self.freq, 0) self.device_source.set_freq_corr(0, 0) @@ -99,3 +102,20 @@ class FileSink(gr.top_block): # connect blocks self.connect((self.blocks_head_0, 0), (self.blocks_file_meta_sink_0, 0)) self.connect((self.device_source, 0), (self.blocks_head_0, 0)) + + def set_dump_file(self, dump_file): + self.dump_file = str(dump_file) + + def set_freq(self, freq): + self.freq = freq + + def set_samp_rate(self, samp_rate): + print("set samp_rate: " % samp_rate) + self.samp_rate = samp_rate + + def set_capture_time(self, capture_time): + self.capture_time = capture_time + + def set_device(self, device = None): + self.device = osmosdr.source(args="numchan=" + str(1) + " " + "" ) + diff --git a/lib/gui/gui.py b/lib/gui/gui.py index 344906e34bfa734fb4b4e3729ce2b6d529ad0183..ab8f46509a651897a51680cfbf8a39cbf5ade641 100644 --- a/lib/gui/gui.py +++ b/lib/gui/gui.py @@ -5,7 +5,6 @@ import os import logging import datetime import re -import threading from PyQt5 import QtCore, QtGui, QtWidgets, uic @@ -14,7 +13,6 @@ from lib.su import Su from lib.disk import Disk from lib.gr.file_sink import FileSink - from lib.gui.update_progress_bar import UpdateProgressBar class SuGui(QtWidgets.QMainWindow): @@ -40,6 +38,7 @@ class SuGui(QtWidgets.QMainWindow): self.button_stop.clicked.connect(self.stop_button) self.button_close.clicked.connect(self.close_button) self.button_hw_refresh.clicked.connect(self.refresh_hw_button) + self.comboBox_env.currentIndexChanged.connect(self.configure_env) # menubar self.actionQuit.triggered.connect(self.close_button) self.actionAbout.triggered.connect(self.about_button) @@ -63,6 +62,9 @@ class SuGui(QtWidgets.QMainWindow): self.rb_433: 433e6, self.rb_868: 868e6 } + self.comboBox_env.addItem("") + self.comboBox_env.addItem("EnOcean v1") + self.comboBox_env.addItem("HomeMatic") # add released actions to radio buttons for button in self.radio_buttons_samp_rate_and_time.keys(): button.released.connect(self._update_status_label) @@ -70,8 +72,9 @@ class SuGui(QtWidgets.QMainWindow): # labels self._update_status_label() self.label_file_overwrite.setText("change path") - # progress_bar - self.progressBar.setValue(0) + # create thread class for progressbar update + self.progress_bar_thread = UpdateProgressBar() + def start_button(self): """ @@ -79,23 +82,24 @@ class SuGui(QtWidgets.QMainWindow): """ if self._check_dependencies_before_start_capture(): options = self._extract_options() - file_sink = FileSink( - options['dump_file_path'], - options['freq'], - options['samp_rate'], - options['capture_time'], - options['device'] - ) + file_sink = FileSink() + file_sink.set_dump_file = options['dump_file_path'] + file_sink.set_freq = options['freq'] + file_sink.set_samp_rate = options['samp_rate'] + file_sink.set_capture_time = options['capture_time'] + file_sink.set_device = options['device'] + self.button_stop.setEnabled(True) self.button_start.setEnabled(False) # Thread stuff - self.request = UpdateProgressBar(options['capture_time']) - print(self.__class__) - self.connect(self.request, QtCore.SIGNAL("mysignal(QString)"), self.update_progress_bar, QtCore.Qt.QueuedConnection) - self.request.start() + # progress_bar + self.progress_bar_thread.set_measure_time(options['capture_time']) + self.progress_bar_thread.progress.connect(self.update_progress_bar, QtCore.Qt.QueuedConnection) + self.progress_bar_thread.start() + self._set_setEnabled(False) - #file_sink.capture() + #file_sink.start() def update_progress_bar(self, text): """ @@ -106,13 +110,21 @@ class SuGui(QtWidgets.QMainWindow): :param text: text to add to the process bar :type text: str """ - self.progress_bar.setValue(text) + self.progressBar.setValue(text) def stop_button(self): """ Stop button action in main window. """ - pass + # terminate running threads + self.progress_bar_thread.terminate() + self.update_progress_bar(0) + # enable buttons again + self._set_setEnabled(True) + # disable stop button again + self.button_stop.setEnabled(False) + self.button_start.setEnabled(True) + def close_button(self): """ @@ -157,6 +169,58 @@ class SuGui(QtWidgets.QMainWindow): else: self.label_disk_space.setStyleSheet('color: black') + def configure_env(self): + """ + Configure envirenment with selected home automatation system. + + Name | Index + | 0 + EnOcean | 1 + HomeMatic | 2 + """ + current_index = self.comboBox_env.currentIndex() + if current_index != 0: + self._set_setEnabled(False, freq_button_only = True) + + # https://stackoverflow.com/questions/11479816/what-is-the-python-equivalent-for-a-case-switch-statement + if current_index == 1: + self._configure_enocean() + elif current_index == 2: + self._configure_homematic() + else: + self._set_setEnabled(True) + + def _configure_enocean(self): + self.rb_868.setChecked(True) + self.rb_433.setChecked(False) + + + def _configure_homematic(self): + self.rb_868.setChecked(True) + self.rb_433.setChecked(False) + + def _set_setEnabled(self, status = True, freq_button_only = False): + """ + Enable or disable buttons the user can interact with. + + param: status: boolean + """ + self.rb_433.setEnabled(status) + self.rb_868.setEnabled(status) + if freq_button_only == False: + self.rb_1mhz.setEnabled(status) + self.rb_2mhz.setEnabled(status) + self.rb_3mhz.setEnabled(status) + self.rb_1m.setEnabled(status) + self.rb_5m.setEnabled(status) + self.rb_10m.setEnabled(status) + self.rb_1h.setEnabled(status) + self.rb_24h.setEnabled(status) + self.lineEdit_dump_file.setEnabled(status) + self.comboBox_hardware.setEnabled(status) + self.comboBox_env.setEnabled(status) + self.button_hw_refresh.setEnabled(status) + def _get_default_dump_file_path(self): """ @@ -196,17 +260,17 @@ class SuGui(QtWidgets.QMainWindow): It returns True or False. """ if len(self.lineEdit_dump_file.text()) == 0: - QtWidgets.QMessageBox.about(self, "Dumpfile path missing…", \ + QtWidgets.QMessageBox.about(self, "Dumpfile path missing…", \ "Please add dump file path.") return False elif len(Hardware.get_connected_supported_devices()) == 0: - QtWidgets.QMessageBox.about(self, "No supported RF hardware "\ + QtWidgets.QMessageBox.about(self, "No supported RF hardware " \ "connected…", \ "Please connect RF hardware.") self.refresh_hw_button() return False elif len(self.comboBox_hardware.currentText()) == 0: - QtWidgets.QMessageBox.about(self, "No supported RF hardware "\ + QtWidgets.QMessageBox.about(self, "No supported RF hardware " \ "selected…", \ "Select RF hardware.") self.refresh_hw_button() @@ -267,13 +331,19 @@ class SuGui(QtWidgets.QMainWindow): def _extract_options(self): """ Extract selected options from gui. + + Buttons """ freq_button = self._get_checked_freq_button() time_and_samp_rate_buttons = self._get_checked_time_and_samp_rate_radio_buttons() # radio buttons options = {} - options['capture_time'], options['samp_rate'] = \ - time_and_samp_rate_buttons.values() + # + for button, value in time_and_samp_rate_buttons.iteritems(): + if re.search(r"MHz", button.text()): + options['samp_rate'] = value + else: + options['capture_time'] = value options['freq'] = freq_button.values()[0] # line inputs options['dump_file_path'] = self.lineEdit_dump_file.text() diff --git a/lib/gui/su_mainwindow.ui b/lib/gui/su_mainwindow.ui index cda6d2c0608e16f4b50966327df93577906df701..bce5c17ce778b94e8e0a615816e4ebc45674ed1b 100644 --- a/lib/gui/su_mainwindow.ui +++ b/lib/gui/su_mainwindow.ui @@ -123,7 +123,7 @@ <string extracomment="600">10 min</string> </property> <property name="checked"> - <bool>true</bool> + <bool>false</bool> </property> </widget> <widget class="QRadioButton" name="rb_1h"> @@ -142,7 +142,7 @@ <bool>true</bool> </property> <property name="checked"> - <bool>false</bool> + <bool>true</bool> </property> <property name="autoExclusive"> <bool>true</bool> diff --git a/lib/gui/update_progress_bar.py b/lib/gui/update_progress_bar.py index 0354c9cb1e9607f1470288c6ffa0907de44c4fed..76352e930d64743b1342bb47a3a705212449cdc8 100644 --- a/lib/gui/update_progress_bar.py +++ b/lib/gui/update_progress_bar.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- import time -from PyQt5.QtCore import QThread, pyqtSignal +from PyQt5.QtCore import QThread, pyqtSignal, QObject class UpdateProgressBar(QThread): - update_pb = pyqtSignal('QString', name='updateProgressBar') - def __init__(self, measure_time): + progress = pyqtSignal(int) + + def __init__(self): """ Make a new thread instance. @@ -14,19 +15,41 @@ class UpdateProgressBar(QThread): :type measure_time: int """ QThread.__init__(self) - self.measure_time = measure_time - self.start_time = float(time.time()) def __del__(self): self.wait() def run(self): - while self.sleep(2): - #self.update_pb.emit(SIGNAL('update_progress_bar(QString)'), \ time_to_percent) - self.emit(QtCore.SIGNAL("mysignal(QString)"), time_to_percent) + self.set_start_time() + + while True: + running_time_in_percent = self.time_to_percent() + self.progress.emit(running_time_in_percent) + + if running_time_in_percent >= 100: + # kill thread if 100 % is reached + break + else: + time.sleep(1) def time_to_percent(self): - running_time = float(time.time()) - self.start_time + running_time = float(time.time())-self.start_time percent = (running_time/self.measure_time)*100 return int(percent) + + # Create setter for + # + def set_measure_time(self, t): + """ + Set time to measure. + + + """ + self.measure_time = t + + def set_start_time(self): + """ + Set start time of capture. + """ + self.start_time = float(time.time()) \ No newline at end of file