Thanks!  That worked.  Here's a full working block:

"""
Embedded Python Blocks:

Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__  will
be the parameters. All of them are required to have default values!
"""

import numpy as np
from gnuradio import gr

```
class blk(gr.sync_block):  # other base classes are basic_block,
decim_block, interp_block
    """Embedded Python Block example - a simple multiply const"""

    def __init__(self, example_param=1.0):  # only default arguments here
        """arguments to this function show up as parameters in GRC"""
        gr.sync_block.__init__(
            self,
            name='Embedded Python Block',   # will show up in GRC
            in_sig=[np.complex64],
            out_sig=[np.complex64]
        )
        # if an attribute with the same name as a parameter is found,
        # a callback is registered (properties work, too).
        self.example_param = example_param

    def work(self, input_items, output_items):
        """example: multiply with constant"""
        output_items[0][:] = input_items[0] * self.example_param
        return len(output_items[0])

    @property
    def example_param(self):
        return self._example_param

    @example_param.setter
    def example_param(self, value):
       print('here')
       self._example_param = value
```


On Tue, Jun 25, 2024 at 8:33 AM Philipp Niedermayer <p.niederma...@gsi.de>
wrote:

> As a workaround, use python's property construct to intercept changes on
> class attribute level:
>
> class blk(...):
>     def __init__(self, example_param=1.0):
>         ...
>
>         # [...] (properties work, too)
>         self.example_param = example_param
>
>     @property
>     def example_param(self):
>         return self._example_param
>
>     @example_param.setter
>     def example_param(self, value):
>        print('here')
>        self._example_param = value
>
>
> ------------------------------
> *From:* Jameson Collins [mailto:jameson.coll...@gmail.com
> <jameson.coll...@gmail.com>]
> *Date:* Thursday, June 20, 2024 at 14:10 UTC+2
> *Subject:* Can Embedded Python Blocks have callbacks?
>
> This was my concern, and it does appear to behave that way.
>
> On Wed, Jun 19, 2024 at 4:34 PM Daniel Estévez <dan...@destevez.net>
> wrote:
>
>> On 19/06/2024 14:51, Jameson Collins wrote:
>> > I'm trying to use a callback to set a variable in an embedded python
>> > block.  Using the tutorial
>> > (https://wiki.gnuradio.org/index.php/Embedded_Python_Block
>> > <https://wiki.gnuradio.org/index.php/Embedded_Python_Block>) as an
>> > example I added the function below.  I've found that this callback
>> never
>> > gets called when I update this value from a GUI.  Should it be?
>> >
>> > |def set_example_param(self, example_param): print("here")|
>>
>> Hi Jameson,
>>
>> I think Embedded Python blocks can only have (automatically generated)
>> callbacks for __init__() arguments that are assigned as
>>
>> self.foo = foo
>>
>> in the body of __init__() (the example template that you get when you
>> create a new Embedded Python block shows how this works).
>>
>> If you need more complex callbacks, I think you need to create a regular
>> Python block in an OOT module.
>>
>> Best,
>> Daniel.
>>
>>
>>

Reply via email to