git-svn-id: http://moon:8086/svn/software/trunk/projects/GnuRadio@505 b431acfa-c32f-4a4a-93f1-934dc6c82436
152 lines
4.6 KiB
Python
152 lines
4.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
#
|
|
# Copyright 2019 Jay Arrowfield.
|
|
#
|
|
# This is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3, or (at your option)
|
|
# any later version.
|
|
#
|
|
# This software is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this software; see the file COPYING. If not, write to
|
|
# the Free Software Foundation, Inc., 51 Franklin Street,
|
|
# Boston, MA 02110-1301, USA.
|
|
#
|
|
|
|
import numpy
|
|
import pmt
|
|
from gnuradio import gr
|
|
|
|
class peak_manager(gr.basic_block):
|
|
"""
|
|
docstring for block peak_manager
|
|
"""
|
|
def __init__(self, numPeaksMax):
|
|
gr.basic_block.__init__(self,
|
|
name="peak_manager",
|
|
in_sig=None,
|
|
out_sig=None)
|
|
|
|
self.numPeaksMax = numPeaksMax
|
|
self.peak_dict = {}
|
|
self.ports_avail = []
|
|
self.ports_used = []
|
|
|
|
print ("numPeaksMax : ", numPeaksMax)
|
|
|
|
# Register in / out message ports
|
|
self.message_port_register_in(pmt.intern("control"))
|
|
|
|
if self.numPeaksMax < 2:
|
|
out_port_name = "status"
|
|
self.ports_avail.append(out_port_name)
|
|
self.message_port_register_out(pmt.intern(out_port_name))
|
|
else:
|
|
for n in range(0, self.numPeaksMax):
|
|
out_port_name = "status" + str(n)
|
|
self.ports_avail.append(out_port_name)
|
|
self.message_port_register_out(pmt.intern(out_port_name))
|
|
|
|
# Register in message handler
|
|
self.set_msg_handler(pmt.intern("control"), self.msg_handler)
|
|
|
|
|
|
def peak_add(self, key, peak):
|
|
if len(self.ports_avail) > 0:
|
|
port_name = self.ports_avail.pop()
|
|
peak['port_name'] = port_name
|
|
self.peak_dict[key] = peak
|
|
return peak
|
|
return None
|
|
|
|
def peak_del(self, key):
|
|
if key in self.peak_dict:
|
|
port_name = self.peak_dict[key]['port_name']
|
|
self.ports_avail.append(port_name)
|
|
self.peak_dict.pop(key, None)
|
|
return True
|
|
return False
|
|
|
|
def peak_get(self, key):
|
|
result = None
|
|
if key in self.peak_dict:
|
|
result = self.peak_dict[key]
|
|
return result
|
|
|
|
def msg_handler(self, msg):
|
|
if pmt.is_dict(msg):
|
|
items = pmt.dict_items(msg)
|
|
|
|
peakMsg = {}
|
|
for n in range(0, pmt.length(items)):
|
|
pair = pmt.nth(n, items)
|
|
key = pmt.car(pair)
|
|
val = pmt.cdr(pair)
|
|
|
|
key_str = pmt.symbol_to_string(key)
|
|
|
|
if pmt.is_integer(val):
|
|
value = pmt.to_long(val)
|
|
peakMsg[key_str] = value
|
|
elif pmt.is_real(val):
|
|
value = pmt.to_float(val)
|
|
peakMsg[key_str] = value
|
|
elif pmt.is_bool(val):
|
|
value = pmt.to_bool(val)
|
|
peakMsg[key_str] = value
|
|
|
|
id = peakMsg['id']
|
|
freq_MHz = peakMsg['freq_Hz']*1e-6
|
|
|
|
peak_key = 'peak' + str(id)
|
|
peakEntry = self.peak_get(peak_key)
|
|
|
|
if peakMsg['state'] == 1:
|
|
if peakEntry is not None:
|
|
portname = peakEntry['port_name']
|
|
print("Update " + peak_key + "(" + portname + ")")
|
|
msg_dict = pmt.make_dict()
|
|
msg_dict = pmt.dict_add(msg_dict, pmt.intern("freq"), pmt.from_double(peakMsg['freq_Hz']))
|
|
msg_dict = pmt.dict_add(msg_dict, pmt.intern("gain"), pmt.from_double(-peakMsg['height']))
|
|
self.message_port_pub(pmt.intern(portname), msg_dict)
|
|
else:
|
|
print ("New peak #{} at {:0.2f} MHz".format(id, freq_MHz))
|
|
peakEntry = self.peak_add(peak_key, peakMsg)
|
|
if peakEntry is not None:
|
|
portname = peakEntry['port_name']
|
|
print("Added " + peak_key + "(" + portname + ")")
|
|
msg_dict = pmt.make_dict()
|
|
msg_dict = pmt.dict_add(msg_dict, pmt.intern("freq"), pmt.from_double(peakMsg['freq_Hz']))
|
|
msg_dict = pmt.dict_add(msg_dict, pmt.intern("gain"), pmt.from_double(-peakMsg['height']))
|
|
self.message_port_pub(pmt.intern(portname), msg_dict)
|
|
else:
|
|
if peakEntry is not None:
|
|
print ("Lost peak #{} at {:0.2f} MHz".format(id, freq_MHz))
|
|
portname = peakEntry['port_name']
|
|
pair = pmt.cons(pmt.intern("gain"), pmt.from_float(-120.0))
|
|
self.message_port_pub(pmt.intern(portname), pair)
|
|
if self.peak_del(peak_key):
|
|
print("Deleted " + peak_key + "(" + portname + ")")
|
|
|
|
|
|
else:
|
|
print(pmt.symbol_to_string(msg))
|
|
|
|
def forecast(self, noutput_items, ninput_items_required):
|
|
#setup size of input_items[i] for work call
|
|
for i in range(len(ninput_items_required)):
|
|
ninput_items_required[i] = noutput_items
|
|
|
|
def general_work(self, input_items, output_items):
|
|
output_items[0][:] = input_items[0]
|
|
consume(0, len(input_items[0]))
|
|
#self.consume_each(len(input_items[0]))
|
|
return len(output_items[0])
|
|
|