Creating Your First Block: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
This tutorial will guide you through creating your first block with the ''Embedded Python Block''. The previous tutorial is here: [[Streams_and_Vectors|Streams and Vectors]]
This tutorial will guide you through creating your first block with the ''Embedded Python Block''. The previous tutorial is here: [[Streams_and_Vectors|Streams and Vectors]]
The example block we are creating will either add or multiply the two blocks based on an input parameter.


== Opening Code Editor ==
== Opening Code Editor ==
Line 49: Line 51:
# Returns the number of samples produced
# 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 ==
== Changing Parameter Name ==


The pseudo code for the Python block is:
The first step will be to rename ''example_param'' to ''additionFlag'' to be more descriptive. From the editor menu select ''Find and Replace'':
 
<pre>if (additionFlag is True)
    then add the two inputs
else
    then multiply the two inputs</pre>
 
The first step will be to rename ''example_param'' to ''additionFlag''. From the editor menu select ''Find and Replace'':


[[File:SelectFindAndReplace.png|700px]]
[[File:SelectFindAndReplace.png|700px]]
Line 78: Line 72:


[[File:AdditionFlagDefaultParam.png|700px]]
[[File:AdditionFlagDefaultParam.png|700px]]
== Editing Work Function ==
Now that the variable name has been changed the ''work'' function needs to be re-written to provide the desired functionality.
The pseudo code for the Python block is:
<pre>if (additionFlag is True)
    then add the two inputs
else
    then multiply the two inputs</pre>

Revision as of 18:53, 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

The example block we are creating will either add or multiply the two blocks based on an input parameter.

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:

AddPythonBlockToWorkspace.png


Double-click the box to edit the properties. The Embedded Python Block has two properties,

  1. Code, a click-box which contains a link to the Python code for the block and
  2. Example_Param, an input parameter to the block.


Click on Open in Editor to edit the Python code:

EmbeddedPythonBlockProperties.png


You will be prompted with another choice for which editor to use to write the Python code. Click Use Default:

ClickUseDefault.png


An editor window will then display the Python code for the Embedded Python Block:

PythonCodeGedit.png


Components of a Python Block

There are three important sections in the Python block code:

  1. import statements in green
  2. __init__ function in orange
  3. work function in red

PythonBlockCodeFunctions.png


The import statement includes the NumPy and GNU Radio libraries.

The __init__ statement:

  1. Accepts the example_param parameter with a default argument of 1.0
  2. Declares the block to have a np.complex64 input and output, which is the GNU Radio Complex Float 32 data type
  3. Stores the self.example_param variable from the input parameter

The work function:

  1. Has the input input_items and output output_items parameters
  2. Applies a mathematical operation to input_items and stores the result in output_items
  3. Returns the number of samples produced


Changing Parameter Name

The first step will be to rename example_param to additionFlag to be more descriptive. From the editor menu select Find and Replace:

SelectFindAndReplace.png


Enter example_param under Find, and additionFlag under Replace with, and then click Replace All:

FindReplaceExamplePara.png


The parameter will be changed and the Python code will then look like this:

PythonBlockAdditionFlag.png


Now change the default value to be True:

AdditionFlagDefaultParam.png


Editing Work Function

Now that the variable name has been changed the work function needs to be re-written to provide the desired functionality.

The pseudo code for the Python block is:

if (additionFlag is True)
    then add the two inputs
else
    then multiply the two inputs