From 4c255bf3ff3c8883e5392b94b00acb4c10cf7c68 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 29 May 2019 13:23:48 +0000 Subject: [PATCH] - added and modified git-svn-id: http://moon:8086/svn/software/trunk/projects/GnuRadio@505 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- gr-jay/grc/CMakeLists.txt | 3 +- gr-jay/lib/squelch_impl.cc | 45 ++++++++++------- gr-jay/lib/squelch_impl.h | 1 + gr-jay/python/CMakeLists.txt | 4 +- gr-jay/python/__init__.py | 1 + gr-jay/python/message_filter.py | 81 ++++++++++++++++++++++++++++++ gr-jay/python/peak_manager.py | 19 +++---- gr-jay/python/qa_message_filter.py | 41 +++++++++++++++ 8 files changed, 163 insertions(+), 32 deletions(-) create mode 100644 gr-jay/python/message_filter.py create mode 100755 gr-jay/python/qa_message_filter.py diff --git a/gr-jay/grc/CMakeLists.txt b/gr-jay/grc/CMakeLists.txt index c8768b2..f700fec 100644 --- a/gr-jay/grc/CMakeLists.txt +++ b/gr-jay/grc/CMakeLists.txt @@ -27,5 +27,6 @@ install(FILES jay_peak_detect.xml jay_message_debug.xml jay_peak_manager.xml - jay_squelch.xml DESTINATION share/gnuradio/grc/blocks + jay_squelch.xml + jay_message_filter.xml DESTINATION share/gnuradio/grc/blocks ) diff --git a/gr-jay/lib/squelch_impl.cc b/gr-jay/lib/squelch_impl.cc index c1a1b28..e59b103 100644 --- a/gr-jay/lib/squelch_impl.cc +++ b/gr-jay/lib/squelch_impl.cc @@ -64,12 +64,6 @@ namespace gr { pmt::pmt_t key = pmt::car(pair); pmt::pmt_t val = pmt::cdr(pair); - if (pmt::symbol_to_string(key) == std::string("gain")) - { - float gain_dB = pmt::to_float(val) - 50.f; - m_gain_target = std::pow(10.f, gain_dB/10); - fprintf(stderr, "Set gain to %0.2f\n", m_gain_target); - } if (pmt::is_integer(val)) { fprintf(stderr, "[%s=%d]", pmt::symbol_to_string(key).c_str(), (int)pmt::to_long(val)); @@ -84,25 +78,38 @@ namespace gr { } fprintf(stderr, "\n"); } + + void squelch_impl::check_pair(pmt::pmt_t pair) + { + pmt::pmt_t key = pmt::car(pair); + pmt::pmt_t val = pmt::cdr(pair); + // Check pair for data + if (pmt::symbol_to_string(key) == std::string("gain")) + { + float gain_dB = pmt::to_float(val) - 40.f; + m_gain_target = std::pow(10.f, gain_dB/10); + } + } + void squelch_impl::on_message(pmt::pmt_t msg) { - if (pmt::is_pair(msg)) - { - print_pair(msg); - } - else if (pmt::is_dict(msg)) + if (pmt::is_dict(msg)) { pmt::pmt_t items = pmt::dict_items(msg); - for (int i=0; i < pmt::length(items); i++) - { - pmt::pmt_t pair = pmt::nth(i, items); - print_pair(pair); + try + { + size_t len_items = pmt::length(items); + for (int i=0; i < len_items; i++) + { + pmt::pmt_t pair = pmt::nth(i, items); + check_pair(pair); + } + } + catch (const std::exception& e) + { + check_pair(msg); } - } - else - { - fprintf(stderr, "%s\n", pmt::symbol_to_string(msg).c_str()); } } diff --git a/gr-jay/lib/squelch_impl.h b/gr-jay/lib/squelch_impl.h index a1cb5ef..aae431a 100644 --- a/gr-jay/lib/squelch_impl.h +++ b/gr-jay/lib/squelch_impl.h @@ -39,6 +39,7 @@ namespace gr { ~squelch_impl(); void print_pair(pmt::pmt_t pair); void on_message(pmt::pmt_t msg); + void check_pair(pmt::pmt_t pair); // Where all the action really happens int work(int noutput_items, diff --git a/gr-jay/python/CMakeLists.txt b/gr-jay/python/CMakeLists.txt index ec920ff..174eba3 100644 --- a/gr-jay/python/CMakeLists.txt +++ b/gr-jay/python/CMakeLists.txt @@ -32,7 +32,8 @@ endif() GR_PYTHON_INSTALL( FILES __init__.py - peak_manager.py DESTINATION ${GR_PYTHON_DIR}/jay + peak_manager.py + message_filter.py DESTINATION ${GR_PYTHON_DIR}/jay ) ######################################################################## @@ -52,3 +53,4 @@ GR_ADD_TEST(qa_peak_detect ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_p GR_ADD_TEST(qa_message_debug ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_message_debug.py) GR_ADD_TEST(qa_peak_manager ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_peak_manager.py) GR_ADD_TEST(qa_squelch ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_squelch.py) +GR_ADD_TEST(qa_message_filter ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/qa_message_filter.py) diff --git a/gr-jay/python/__init__.py b/gr-jay/python/__init__.py index 006e6d0..4020f7a 100644 --- a/gr-jay/python/__init__.py +++ b/gr-jay/python/__init__.py @@ -32,4 +32,5 @@ except ImportError: # import any pure python here from peak_manager import peak_manager +from message_filter import message_filter # diff --git a/gr-jay/python/message_filter.py b/gr-jay/python/message_filter.py new file mode 100644 index 0000000..888d2ee --- /dev/null +++ b/gr-jay/python/message_filter.py @@ -0,0 +1,81 @@ +#!/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 message_filter(gr.basic_block): + """ + docstring for block message_filter + """ + def __init__(self, filter='freq'): + gr.basic_block.__init__(self, + name="message_filter", + in_sig=None, + out_sig=None) + + self.filter = filter + + # Register in / out message ports + self.port_in = pmt.intern("in") + self.port_out = pmt.intern("out") + + self.message_port_register_in(self.port_in) + self.message_port_register_out(self.port_out) + + # Register in message handler + self.set_msg_handler(self.port_in, self.msg_handler) + + def delegate_pair(self, pair): + key = pmt.car(pair) + key_str = pmt.symbol_to_string(key) + + if self.filter in key_str: + self.message_port_pub(self.port_out, pair) + + def msg_handler(self, msg): + # Üble Verrenkungen, um fest zu stellen of dict oder pair + if pmt.is_dict(msg): + items = pmt.dict_items(msg) + try: + # Dict, wenn len_items > 1 + len_items = pmt.length(items) + + # Iterate through dict to get single pairs + for n in range(0, len_items): + pair = pmt.nth(n, items) + self.delegate_pair(pair) + except: + # Wrong Type exception -> pair + self.delegate_pair(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]) diff --git a/gr-jay/python/peak_manager.py b/gr-jay/python/peak_manager.py index 9f758e4..d07f33a 100644 --- a/gr-jay/python/peak_manager.py +++ b/gr-jay/python/peak_manager.py @@ -94,15 +94,12 @@ class peak_manager(gr.basic_block): if pmt.is_integer(val): value = pmt.to_long(val) peakMsg[key_str] = value -# print("{}={}".format(key_str, value)) elif pmt.is_real(val): value = pmt.to_float(val) peakMsg[key_str] = value -# print("{}={}".format(key_str, value)) elif pmt.is_bool(val): value = pmt.to_bool(val) peakMsg[key_str] = value -# print("{}={}".format(key_str, value)) id = peakMsg['id'] freq_MHz = peakMsg['freq_Hz']*1e-6 @@ -114,20 +111,20 @@ class peak_manager(gr.basic_block): if peakEntry is not None: portname = peakEntry['port_name'] print("Update " + peak_key + "(" + portname + ")") - pair = pmt.cons(pmt.intern("freq"), pmt.from_float(peakMsg['freq_Hz'])) - self.message_port_pub(pmt.intern(portname), pair) - pair = pmt.cons(pmt.intern("gain"), pmt.from_float(-peakMsg['height'])) - self.message_port_pub(pmt.intern(portname), pair) + 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 + ")") - pair = pmt.cons(pmt.intern("freq"), pmt.from_float(peakMsg['freq_Hz'])) - self.message_port_pub(pmt.intern(portname), pair) - pair = pmt.cons(pmt.intern("gain"), pmt.from_float(-peakMsg['height'])) - self.message_port_pub(pmt.intern(portname), pair) + 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)) diff --git a/gr-jay/python/qa_message_filter.py b/gr-jay/python/qa_message_filter.py new file mode 100755 index 0000000..c97370a --- /dev/null +++ b/gr-jay/python/qa_message_filter.py @@ -0,0 +1,41 @@ +#!/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. +# + +from gnuradio import gr, gr_unittest +from gnuradio import blocks +from message_filter import message_filter + +class qa_message_filter (gr_unittest.TestCase): + + def setUp (self): + self.tb = gr.top_block () + + def tearDown (self): + self.tb = None + + def test_001_t (self): + # set up fg + self.tb.run () + # check data + + +if __name__ == '__main__': + gr_unittest.run(qa_message_filter, "qa_message_filter.xml")