Imported Upstream version 3.2.2
[debian/gnuradio] / gr-wxgui / src / python / numbersink2.py
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):
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                 #connect
85                 self.connect(self, sd, mult, add, avg, sink)
86                 #controller
87                 self.controller = pubsub()
88                 self.controller.subscribe(SAMPLE_RATE_KEY, sd.set_sample_rate)
89                 self.controller.publish(SAMPLE_RATE_KEY, sd.sample_rate)
90                 self.controller[AVERAGE_KEY] = average
91                 self.controller[AVG_ALPHA_KEY] = avg_alpha
92                 def update_avg(*args):
93                         if self.controller[AVERAGE_KEY]: avg.set_taps(self.controller[AVG_ALPHA_KEY])
94                         else: avg.set_taps(1.0)
95                 update_avg()
96                 self.controller.subscribe(AVERAGE_KEY, update_avg)
97                 self.controller.subscribe(AVG_ALPHA_KEY, update_avg)
98                 #start input watcher
99                 common.input_watcher(msgq, self.controller, MSG_KEY)
100                 #create window
101                 self.win = number_window.number_window(
102                         parent=parent,
103                         controller=self.controller,
104                         size=size,
105                         title=label,
106                         units=unit,
107                         real=self._real,
108                         minval=minval,
109                         maxval=maxval,
110                         decimal_places=decimal_places,
111                         show_gauge=show_gauge,
112                         average_key=AVERAGE_KEY,
113                         avg_alpha_key=AVG_ALPHA_KEY,
114                         peak_hold=peak_hold,
115                         msg_key=MSG_KEY,
116                         sample_rate_key=SAMPLE_RATE_KEY,
117                 )
118                 common.register_access_methods(self, self.controller)
119                 #backwards compadibility
120                 self.set_show_gauge = self.win.show_gauges
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