0

i new on stackoverflow, so i tried to make Thread from MainWindow, but it does not work when callback function i put in run(self):. below is part of code

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QThread, pyqtSignal, QTimer,QRectF,QEvent
# For graphs
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg

from libhackrf import *
from matplotlib import mlab as mlab
class SDRThread(QThread):
    def __init__(self, sample_rate = 2.4e6,center_freq = 100.0e6,freq_correction = 60, gain = 33.8, chunks = 1024, dev_index=0):
        QThread.__init__(self)
        # configure device
        try:
            self.sdr = HackRF(self.device_index)

        except:
            print("No Hardware Detected")
            self.isRunning = False
            
        else:
            self.sample_rate = sample_rate
            self.sdr.set_sample_rate(self.sample_rate)  # Hz
            self.sdr.set_freq(int(center_freq + (sample_rate/2)))
            self.sdr.set_lna_gain(24) #3
            self.sdr.set_vga_gain(18)

            self.CHUNK = chunks
            self.fft = np.random.rand(self.CHUNK) * 1e-6
            #self.sdr.start_rx(self.sdr_async_callback)   ## IT WORKED BUT NOT THREADING my app is freezing
            self.isRunning = True

    
    def __del__(self):
        pass
    def stop_thread(self):
        self.isRunning = False
        self.sdr.stop_rx()
        self.sdr.close()
        

    def sdr_tune(self,cf):
        self.sdr.set_freq(int(cf))

    def sdr_gain(self,gain=33.8):
        pass
        #self.sdr.gain = gain


    def run(self):
           print("OK1") # it worked
        self.sdr.start_rx(self.sdr_async_callback) # does not work

    def sdr_async_callback(self, hackrf_transfer):
        print("OK2")  # IT Does not TYPE if  self.sdr.start_rx(self.sdr_async_callback) finction i put in run(self)
        array_type = (c_byte * self.CHUNK*2)
        values = cast(hackrf_transfer.contents.buffer, POINTER(array_type)).contents
        # iq data here
        iq = bytes2iq(bytearray(values))
        power, _ = mlab.psd(iq, NFFT=self.CHUNK, Fs=self.sample_rate, scale_by_freq=False)
        #print(iq[514])
        self.fft = np.sqrt(power)
        while True:
            print("adwd")
            time.sleep(1)
        return 0

i try self.sdr.start_rx(self.sdr_async_callback) function to define in init phase, the callback function (sdr_async_callback) worked but it not threading my main app is freez. how can i solve this problem? Can someone halp me

Dav Mar
  • 1
  • 1

0 Answers0