root / gr-wxgui / src / python / numbersink2.py @ af5e21e3
History | View | Annotate | Download (5.2 kB)
| 1 | #
|
|---|---|
| 2 | # Copyright 2008 Free Software Foundation, Inc.
|
| 3 | #
|
| 4 | # This file is part of GNU Radio
|
| 5 | #
|
| 6 | # GNU Radio is free software; you can redistribute it and/or modify
|
| 7 | # it under the terms of the GNU General Public License as published by
|
| 8 | # the Free Software Foundation; either version 3, or (at your option)
|
| 9 | # any later version.
|
| 10 | #
|
| 11 | # GNU Radio is distributed in the hope that it will be useful,
|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 | # GNU General Public License for more details.
|
| 15 | #
|
| 16 | # You should have received a copy of the GNU General Public License
|
| 17 | # along with GNU Radio; see the file COPYING. If not, write to
|
| 18 | # the Free Software Foundation, Inc., 51 Franklin Street,
|
| 19 | # Boston, MA 02110-1301, USA.
|
| 20 | #
|
| 21 | |
| 22 | ##################################################
|
| 23 | # Imports
|
| 24 | ##################################################
|
| 25 | import number_window |
| 26 | import common |
| 27 | from gnuradio import gr, blks2 |
| 28 | from pubsub import pubsub |
| 29 | from constants import * |
| 30 | |
| 31 | ##################################################
|
| 32 | # Number sink block (wrapper for old wxgui)
|
| 33 | ##################################################
|
| 34 | class _number_sink_base(gr.hier_block2, common.wxgui_hb): |
| 35 | """
|
| 36 | An decimator block with a number window display
|
| 37 | """ |
| 38 | |
| 39 | def __init__( |
| 40 | self,
|
| 41 | parent, |
| 42 | unit='units',
|
| 43 | minval=0,
|
| 44 | maxval=1,
|
| 45 | factor=1,
|
| 46 | decimal_places=3,
|
| 47 | ref_level=0,
|
| 48 | sample_rate=1,
|
| 49 | number_rate=number_window.DEFAULT_NUMBER_RATE, |
| 50 | average=False,
|
| 51 | avg_alpha=None,
|
| 52 | label='Number Plot',
|
| 53 | size=number_window.DEFAULT_WIN_SIZE, |
| 54 | peak_hold=False,
|
| 55 | show_gauge=True,
|
| 56 | **kwargs #catchall for backwards compatibility
|
| 57 | ): |
| 58 | #ensure avg alpha
|
| 59 | if avg_alpha is None: avg_alpha = 2.0/number_rate |
| 60 | #init
|
| 61 | gr.hier_block2.__init__( |
| 62 | self,
|
| 63 | "number_sink",
|
| 64 | gr.io_signature(1, 1, self._item_size), |
| 65 | gr.io_signature(0, 0, 0), |
| 66 | ) |
| 67 | #blocks
|
| 68 | sd = blks2.stream_to_vector_decimator( |
| 69 | item_size=self._item_size,
|
| 70 | sample_rate=sample_rate, |
| 71 | vec_rate=number_rate, |
| 72 | vec_len=1,
|
| 73 | ) |
| 74 | if self._real: |
| 75 | mult = gr.multiply_const_ff(factor) |
| 76 | add = gr.add_const_ff(ref_level) |
| 77 | avg = gr.single_pole_iir_filter_ff(1.0)
|
| 78 | else:
|
| 79 | mult = gr.multiply_const_cc(factor) |
| 80 | add = gr.add_const_cc(ref_level) |
| 81 | avg = gr.single_pole_iir_filter_cc(1.0)
|
| 82 | msgq = gr.msg_queue(2)
|
| 83 | sink = gr.message_sink(self._item_size, msgq, True) |
| 84 | #controller
|
| 85 | self.controller = pubsub()
|
| 86 | self.controller.subscribe(SAMPLE_RATE_KEY, sd.set_sample_rate)
|
| 87 | self.controller.publish(SAMPLE_RATE_KEY, sd.sample_rate)
|
| 88 | self.controller[AVERAGE_KEY] = average
|
| 89 | self.controller[AVG_ALPHA_KEY] = avg_alpha
|
| 90 | def update_avg(*args): |
| 91 | if self.controller[AVERAGE_KEY]: avg.set_taps(self.controller[AVG_ALPHA_KEY]) |
| 92 | else: avg.set_taps(1.0) |
| 93 | update_avg() |
| 94 | self.controller.subscribe(AVERAGE_KEY, update_avg)
|
| 95 | self.controller.subscribe(AVG_ALPHA_KEY, update_avg)
|
| 96 | #start input watcher
|
| 97 | common.input_watcher(msgq, self.controller, MSG_KEY)
|
| 98 | #create window
|
| 99 | self.win = number_window.number_window(
|
| 100 | parent=parent, |
| 101 | controller=self.controller,
|
| 102 | size=size, |
| 103 | title=label, |
| 104 | units=unit, |
| 105 | real=self._real,
|
| 106 | minval=minval, |
| 107 | maxval=maxval, |
| 108 | decimal_places=decimal_places, |
| 109 | show_gauge=show_gauge, |
| 110 | average_key=AVERAGE_KEY, |
| 111 | avg_alpha_key=AVG_ALPHA_KEY, |
| 112 | peak_hold=peak_hold, |
| 113 | msg_key=MSG_KEY, |
| 114 | sample_rate_key=SAMPLE_RATE_KEY, |
| 115 | ) |
| 116 | common.register_access_methods(self, self.controller) |
| 117 | #backwards compadibility
|
| 118 | self.set_show_gauge = self.win.show_gauges |
| 119 | #connect
|
| 120 | self.wxgui_connect(self, sd, mult, add, avg, sink) |
| 121 | |
| 122 | class number_sink_f(_number_sink_base): |
| 123 | _item_size = gr.sizeof_float |
| 124 | _real = True
|
| 125 | |
| 126 | class number_sink_c(_number_sink_base): |
| 127 | _item_size = gr.sizeof_gr_complex |
| 128 | _real = False
|
| 129 | |
| 130 | # ----------------------------------------------------------------
|
| 131 | # Standalone test app
|
| 132 | # ----------------------------------------------------------------
|
| 133 | |
| 134 | import wx |
| 135 | from gnuradio.wxgui import stdgui2 |
| 136 | |
| 137 | class test_app_flow_graph (stdgui2.std_top_block): |
| 138 | def __init__(self, frame, panel, vbox, argv): |
| 139 | stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
|
| 140 | |
| 141 | # build our flow graph
|
| 142 | input_rate = 20.48e3
|
| 143 | |
| 144 | # Generate a real and complex sinusoids
|
| 145 | src1 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2.21e3, 1) |
| 146 | src2 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2.21e3, 1) |
| 147 | |
| 148 | # We add these throttle blocks so that this demo doesn't
|
| 149 | # suck down all the CPU available. Normally you wouldn't use these.
|
| 150 | thr1 = gr.throttle(gr.sizeof_float, input_rate) |
| 151 | thr2 = gr.throttle(gr.sizeof_gr_complex, input_rate) |
| 152 | |
| 153 | sink1 = number_sink_f (panel, unit='V',label="Real Data", avg_alpha=0.001, |
| 154 | sample_rate=input_rate, minval=-1, maxval=1, |
| 155 | ref_level=0, decimal_places=3) |
| 156 | vbox.Add (sink1.win, 1, wx.EXPAND)
|
| 157 | sink2 = number_sink_c (panel, unit='V',label="Complex Data", avg_alpha=0.001, |
| 158 | sample_rate=input_rate, minval=-1, maxval=1, |
| 159 | ref_level=0, decimal_places=3) |
| 160 | vbox.Add (sink2.win, 1, wx.EXPAND)
|
| 161 | |
| 162 | self.connect (src1, thr1, sink1)
|
| 163 | self.connect (src2, thr2, sink2)
|
| 164 | |
| 165 | def main (): |
| 166 | app = stdgui2.stdapp (test_app_flow_graph, "Number Sink Test App")
|
| 167 | app.MainLoop () |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | main () |
| 171 |