Skip to main content

Embedding GRC

Jia-YinAbout 1 mincomm

Now we need to embed the classes generated by GNU Radio. Copy the fm_recv.py file generated in the previous unit to the working directory. Then modify the main.py from the previous section, and place the following lines of code at the bottom of the init function.

self.grc = fm_recv()
self.layout().replaceWidget(self.wGRC, self.grc)
self.layout().removeWidget(self.wGRC)
self.wGRC.deleteLater()
self.wGRC = None
self.grc.start()
self.grc.show()

These lines of code mainly replace the original wGRC component with the component generated by GRC, and then delete the wGRC component.

Run the code to see if the following result is generated:

Next, adjust the frequency to see if it can run properly and hear the radio programs.

Next, add the trigger events for the following buttons in __init__:

self.bICRT.clicked.connect(lambda: self.setFreq(100.7))
self.bFengCheng.clicked.connect(lambda: self.setFreq(98.7))
self.bGuoJi.clicked.connect(lambda: self.setFreq(101.1))

Then add a method to the class:

def setFreq(self, freq):
    self.grc.set_f(freq*1e6)

Run it again, try clicking on the buttons of various radio stations, and it should work properly, allowing you to listen to broadcasts from different stations.

Since we now control the radio station using buttons, the frequency scroll bar is no longer needed, so we plan to remove the original frequency scroll bar in GRC.

Modify the original GRC file of fm_recv again to remove the GUI Range f and regenerate the Python file.

Next, we want to directly control the frequency setting of the GRC signal source, modify the setFreq method in main.py as follows:

def setFreq(self, freq):
    self.grc.analog_sig_source_x_0.set_frequency(100e6-freq*1e6)

Run the program again to see if the frequency scroll bar has been removed.

Exercise 3

  1. Complete the above instructions and verify that the execution results are correct.
  2. (Optional) We can also set up the volume adjustment function ourselves and remove the volume adjustment function in GRC. Volume settings can be designed using a slider, radio button, or dropdown menu. You can also keep the GRC scroll bar, but modify its style. Please research possible methods to modify the volume setting function yourself.