Creating Your First Block: Difference between revisions
Mattcarrick (talk | contribs) |
Mattcarrick (talk | contribs) |
||
Line 51: | Line 51: | ||
The example block we are creating will either add or multiply the two blocks based on the input parameter ''example_param''. In order to do this, the ''work'' function will need to be modified. | The example block we are creating will either add or multiply the two blocks based on the input parameter ''example_param''. In order to do this, the ''work'' function will need to be modified. | ||
== | == Changing Parameter Name == | ||
The pseudo code for the Python block is: | The pseudo code for the Python block is: | ||
Line 68: | Line 68: | ||
[[File:FindReplaceExamplePara.png|500px]] | [[File:FindReplaceExamplePara.png|500px]] | ||
The parameter will be changed and the Python code will then look like this: | |||
[[File:PythonBlockAdditionFlag.png|700px]] |
Revision as of 18:47, 10 January 2022
This tutorial will guide you through creating your first block with the Embedded Python Block. The previous tutorial is here: Streams and Vectors
Opening Code Editor
The Embedded Python Block is a tool to quickly prototype a block within a flowgraph. Search for the Python Block and add it to the workspace:
Double-click the box to edit the properties. The Embedded Python Block has two properties,
- Code, a click-box which contains a link to the Python code for the block and
- Example_Param, an input parameter to the block.
Click on Open in Editor to edit the Python code:
You will be prompted with another choice for which editor to use to write the Python code. Click Use Default:
An editor window will then display the Python code for the Embedded Python Block:
Components of a Python Block
There are three important sections in the Python block code:
- import statements in green
- __init__ function in orange
- work function in red
The import statement includes the NumPy and GNU Radio libraries.
The __init__ statement:
- Accepts the example_param parameter with a default argument of 1.0
- Declares the block to have a np.complex64 input and output, which is the GNU Radio Complex Float 32 data type
- Stores the self.example_param variable from the input parameter
The work function:
- Has the input input_items and output output_items parameters
- Applies a mathematical operation to input_items and stores the result in output_items
- Returns the number of samples produced
The example block we are creating will either add or multiply the two blocks based on the input parameter example_param. In order to do this, the work function will need to be modified.
Changing Parameter Name
The pseudo code for the Python block is:
if (additionFlag is True) then add the two inputs else then multiply the two inputs
The first step will be to rename example_param to additionFlag. From the editor menu select Find and Replace:
Enter example_param under Find, and additionFlag under Replace with, and then click Replace All:
The parameter will be changed and the Python code will then look like this: