I am trying to get the pulse width of a OOK modulation ( to receive and decode the temperature on a Weather station external sensor, 433MHz ).
I tried the Python code of the embedded block:
""" Embedded Python Blocks: ... """
import numpy as np from gnuradio import gr import pmt from binascii import hexlify
class blk(gr.sync_block): # other base classes are basic_block, decim_block, interp_block
def __init__(self, time_zero = 2500, time_one=5000, time_start= 10000): # only default arguments here
"""arguments to this function show up as parameters in GRC"""
gr.sync_block.__init__(
self,
name='Pulse Time to Bits', # will show up in GRC
in_sig=[np.float32],
out_sig=[np.float32]
)
self.time_zero = time_zero
self.time_one = time_one
self.time_start = time_start
def work(self, input_items, output_items):
sample_bef = 0
sample_bef_n = 0
#global sample_bef
#global sample_bef_n
#global sample_intvl
#global sample_intvl_bef
n_input_items = len(input_items[0])
for s in range( n_input_items):
if((input_items[0][s] - sample_bef) > 0.2):
idx_abs = s + self.nitems_written(0) # time in absolute index ( current sample_rate )
time_idx = (idx_abs - sample_bef_n) # *1e3 /( sampl_rate / 40)
sample_bef_n = idx_abs
if((time_idx < 2000) and (time_idx>10)):
idx_abs_str = str(time_idx / 100) # +" # "+ str(n_input_items)
key = pmt.intern("ms")
value = pmt.intern(idx_abs_str)
self.add_item_tag(0, # Write to output port 0
self.nitems_written(0) + s, # Index of the tag in absolute terms
key, # Key of the tag
value # Value of the tag
)
sample_bef = input_items[0][s]
output_items[0][:] = input_items[0]
return len(output_items[0])
My problem is how to "save" sample_bef ( value ) and sample_bef_n ( time in absolute ) in order that when the scheduler calls the block it can calculate the correct pulse width. As it is, it only consider the samples in the range( n_input_items). I tried global variable and variable blocks with no sucess.
Your suggestions will be appreciated
Joaquim Silva