# Implementing callbacks in custom blocks

**URL:** <https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079>\
**Category:** GNU Radio\
**Created:** [July 20, 2014, 8:00am UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079 "2014-07-20T08:00:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [July 20, 2014, 8:00am UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079/1 "2014-07-20T08:00:17Z")

</div>

Using gr\_modtool, I am trying to make a simple Gain block (called  
var\_gain)  
which multiplies the input with a constant. I have a private variable  
called  
d\_gain in the var\_gain\_impl class and a public function set\_gain(float  
gain)  
which sets the value of d\_gain to the argument. I modified the xml file  
by  
adding the line  
set\_gain($gain)  
after the line.

## During execution, gain value does not change even after changing it through a WX Slider. If I run it from terminal, I am getting the following error

Traceback (most recent call last):  
File  
“/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py”,  
line  
180, in \_handle  
def \_handle(self, event): self[INT\_KEY] = self.\_slider.GetValue()  
File “/usr/local/lib/python2.7/dist-packages/gnuradio/gr/pubsub.py”,  
line  
52, in **setitem**  
sub(val)  
File  
“/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py”,  
line  
138, in \_translate\_internal\_to\_external  
if self.\_callback: self.\_callback(self[EXT\_KEY])  
File “top\_block.py”, line 102, in set\_gg  
self.fsk\_var\_gain\_0.set\_gain(self.gg)  
AttributeError: ‘var\_gain\_sptr’ object has no attribute ‘set\_gain’

I tried looking at some in-built blocks to see how callback is  
implemented.  
Then I saw that the in-built blocks have pure virtual functions in the  
include/module\_name/var\_gain.h header file. I tried implementing that  
but  
the result is still the same. What am I missing here?

–  
View this message in context:  
[http://gnuradio.4.n7.nabble.com/Implementing-callbacks-in-custom-blocks-tp49462.html](http://gnuradio.4.n7.nabble.com/Implementing-callbacks-in-custom-blocks-tp49462.html)  
Sent from the GnuRadio mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![jsam45](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@jsam45](https://www.ruby-forum.com/u/jsam45)\
**Post date:** [July 20, 2014, 12:24pm UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079/2 "2014-07-20T12:24:42Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

hey,

I’m not entirely sure if it still holds true, but that might be an  
error related to SWIG. For some reason virtual header files don’t get  
swig’ed again unless explicitly triggered. Just do a ‘make clean’ and  
‘make’ again. That should rebuild your module from scratch and also  
swig your headers.

happy hacking  
Johannes

On 20.07.2014 07:58, jsam45 wrote:

> “/usr/local/lib/python2.7/dist-packages/gnuradio/wxgui/forms/forms.py”,  
> I tried looking at some in-built blocks to see how callback is

Sent from the GnuRadio mailing list archive at [Nabble.com](http://Nabble.com).

> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ Discuss-gnuradio  
> mailing list [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1  
Comment: Using GnuPG with Thunderbird - [http://www.enigmail.net/](http://www.enigmail.net/)

iQIcBAEBAgAGBQJTy5jKAAoJEO7fmkDsqywMl0cP/i4pl0q1e22rkQlNqikQ2fhX  
tltuulJ+EffNnjkUd/6mo5xOPLDW19gGgODxK5aYPuK7/jU2Dw+xoOiAo2oiORTW  
l9umtB4Vv3C3KRNpyOl1y36Gh+31T+kgu2UHcR6vwb239lGiwxuesKl8A2QvjqKo  
dzbGUn1ctmQ6a3zhOsQVSPS7fQ/Rq8f7oMIeAK94Mu0LuONO8sYPsDwTi7IuImqr  
q3ENR7RB1O/w1rRgIDL3Tp/zUHuhLROt1wIzvnFAFY4uQRijbm88hUaaH5gC/V8J  
QWiX9CVnUmrsUX1YHGDy/Yjd6X0JzqHIFFegiU8TpIxCrts7X8vVuYBKV3jyauis  
VA+K8PBZjOJ0afh4qfBQ58fEYUEB83uhHvuVM27u07rhJyW9xwakjF3eJEWejny1  
uUsGFFH6OuS8kO8pzq0LD6rSfat1BSzW+ylLkhcaR1I6lcyDaCSN676wFqZ7UpBa  
snmC32uYh4N9x+c7pFx0A5Kxl8HDOgqD//GoztN6gJZU2sa0c1mAACbEk6YA1YEg  
TcM/dNGCiMlZFKiXr0UswA6BOZGdcvgb0Y0aM54a8ly7x70dB5oxB2/6DWvO3NE2  
AmEBAaXHFtnDLGWcTpccbpLS5crFtsMZM56OkgMhtGuf0oLrHJ39BUo9YGo/8kPx  
dj7NSYqGWMIqjt1TMAOy  
=J7G6  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![jsam45](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@jsam45](https://www.ruby-forum.com/u/jsam45)\
**Post date:** [July 20, 2014, 1:13pm UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079/3 "2014-07-20T13:13:02Z")

</div>

In addition to what Johannes said:

Make sure that you have

```
virtual void set_gain(float gain) = 0;

```

in your var\_gain.h and

```
virtual void set_gain(float gain);

```

in your var\_gain\_impl.h, as well as

```
void
var_gain_impl::set_gain(float gain) {
    ...
}

```

in your var\_gain\_impl.cc.

The idea is that the user of your block only sees var\_gain and its  
public attributes, while you and your module work in var\_gain\_impl to  
make it easier for swig and readers to understand your “interface” for  
using that class and giving you the freedom to have whatever methods  
you’d need in the \_impl without asking yourself if potential users might  
be confused by these methods.

And another side note for future readers: I assume you implement this as  
an exercise, which is a very good idea!  
If someone just needs functionality of this kind, there’s  
multiply\_const\_??.

Greetings,  
Marcus

---

<div class="post-metadata">

**Author:** ![jsam45](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@jsam45](https://www.ruby-forum.com/u/jsam45)\
**Post date:** [July 20, 2014, 4:16pm UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079/4 "2014-07-20T16:16:35Z")

</div>

Remaking after ‘make clean’ solved the problem. Thanks a lot for the  
help!

Joseph Samuel

–  
View this message in context:  
[http://gnuradio.4.n7.nabble.com/Implementing-callbacks-in-custom-blocks-tp49462p49468.html](http://gnuradio.4.n7.nabble.com/Implementing-callbacks-in-custom-blocks-tp49462p49468.html)  
Sent from the GnuRadio mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![jsam45](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@jsam45](https://www.ruby-forum.com/u/jsam45)\
**Post date:** [July 21, 2014, 6:06pm UTC](https://www.ruby-forum.com/t/implementing-callbacks-in-custom-blocks/231079/5 "2014-07-21T18:06:58Z")

</div>

* * *

Discuss-gnuradio mailing list  
removed\_email\_address@domain.invalid  
[https://lists.gnu.org/mailman/listinfo/discuss-gnuradio](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)
