Merged r10463:10658 from jblum/gui_guts into trunk. Trunk passes distcheck.
[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                 base_value=None, #ignore (old wrapper)
44                 minval=0,
45                 maxval=1,
46                 factor=1,
47                 decimal_places=3,
48                 ref_level=0,
49                 sample_rate=1,
50                 number_rate=number_window.DEFAULT_NUMBER_RATE,
51                 average=False,
52                 avg_alpha=None,
53                 label='Number Plot',
54                 size=number_window.DEFAULT_WIN_SIZE,
55                 peak_hold=False,
56                 show_gauge=True,
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                 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                 self.controller.subscribe(AVERAGE_KEY, update_avg)
94                 self.controller.subscribe(AVG_ALPHA_KEY, update_avg)
95                 self.controller[AVERAGE_KEY] = average
96                 self.controller[AVG_ALPHA_KEY] = avg_alpha
97                 #start input watcher
98                 common.input_watcher(msgq, self.controller, MSG_KEY)
99                 #create window
100                 self.win = number_window.number_window(
101                         parent=parent,
102                         controller=self.controller,
103                         size=size,
104                         title=label,
105                         units=unit,
106                         real=self._real,
107                         minval=minval,
108                         maxval=maxval,
109                         decimal_places=decimal_places,
110                         show_gauge=show_gauge,
111                         average_key=AVERAGE_KEY,
112                         avg_alpha_key=AVG_ALPHA_KEY,
113                         peak_hold=peak_hold,
114                         msg_key=MSG_KEY,
115                         sample_rate_key=SAMPLE_RATE_KEY,
116                 )
117                 common.register_access_methods(self, self.controller)
118                 #backwards compadibility
119                 self.set_show_gauge = self.win.show_gauges
120
121 class number_sink_f(_number_sink_base):
122         _item_size = gr.sizeof_float
123         _real = True
124
125 class number_sink_c(_number_sink_base):
126         _item_size = gr.sizeof_gr_complex
127         _real = False
128
129 # ----------------------------------------------------------------
130 # Standalone test app
131 # ----------------------------------------------------------------
132
133 import wx
134 from gnuradio.wxgui import stdgui2
135
136 class test_app_flow_graph (stdgui2.std_top_block):
137     def __init__(self, frame, panel, vbox, argv):
138         stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
139
140         # build our flow graph
141         input_rate = 20.48e3
142
143         # Generate a real and complex sinusoids
144         src1 = gr.sig_source_f (input_rate, gr.GR_SIN_WAVE, 2.21e3, 1)
145         src2 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 2.21e3, 1)
146
147         # We add these throttle blocks so that this demo doesn't
148         # suck down all the CPU available.  Normally you wouldn't use these.
149         thr1 = gr.throttle(gr.sizeof_float, input_rate)
150         thr2 = gr.throttle(gr.sizeof_gr_complex, input_rate)
151
152         sink1 = number_sink_f (panel, unit='V',label="Real Data", avg_alpha=0.001,
153                             sample_rate=input_rate, minval=-1, maxval=1,
154                             ref_level=0, decimal_places=3)
155         vbox.Add (sink1.win, 1, wx.EXPAND)
156         sink2 = number_sink_c (panel, unit='V',label="Complex Data", avg_alpha=0.001,
157                             sample_rate=input_rate, minval=-1, maxval=1,
158                             ref_level=0, decimal_places=3)
159         vbox.Add (sink2.win, 1, wx.EXPAND)
160
161         self.connect (src1, thr1, sink1)
162         self.connect (src2, thr2, sink2)
163
164 def main ():
165     app = stdgui2.stdapp (test_app_flow_graph, "Number Sink Test App")
166     app.MainLoop ()
167
168 if __name__ == '__main__':
169     main ()
170