Skip to content

Instantly share code, notes, and snippets.

@standarddeviant
Created May 19, 2022 12:53
Show Gist options
  • Save standarddeviant/7ee0b25468d395ad5a8c5cc97d951142 to your computer and use it in GitHub Desktop.
Save standarddeviant/7ee0b25468d395ad5a8c5cc97d951142 to your computer and use it in GitHub Desktop.
"""
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
"""int32 counter (bytes out)"""
def __init__(self, incr=1): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
gr.sync_block.__init__(
self,
name='int32 counter (bytes out)', # will show up in GRC
in_sig=[],
out_sig=[np.byte]
)
# if an attribute with the same name as a parameter is found,
# a callback is registered (properties work, too).
self.incr = np.int32(incr)
self.count = np.int32(0)
self.leftover = bytearray()
def work(self, input_items, output_items):
"""example: multiply with constant"""
# we need to output `output_items` bytes
# we already have len(self.leftover)
# so we must add this number of integers
# np.ceil( (output_items - len(self.leftover))/4 )
nitems = len(output_items[0])
ints_to_add = np.ceil( (nitems - len(self.leftover))/4 )
# update working bytearray
tmpba = np.arange(self.count, self.count+(self.incr*ints_to_add), self.incr, dtype='int32').tobytes()
self.leftover += tmpba
# assign output
output_items[0][:] = self.leftover[0:nitems]
# update book-keeping variables, count and leftover
self.count += (self.incr * ints_to_add)
del(self.leftover[0:nitems])
# return number of items
return len(output_items[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment