gr-wxgui: adds stripchart trigger mode to graphics sinks
[debian/gnuradio] / gr-wxgui / src / python / scopesink_gl.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 scope_window
26 import common
27 from gnuradio import gr
28 from pubsub import pubsub
29 from constants import *
30
31 class ac_couple_block(gr.hier_block2):
32         """
33         AC couple the incoming stream by subtracting out the low pass signal.
34         Mute the low pass filter to disable ac coupling.
35         """
36
37         def __init__(self, controller, ac_couple_key, sample_rate_key):
38                 gr.hier_block2.__init__(
39                         self,
40                         "ac_couple",
41                         gr.io_signature(1, 1, gr.sizeof_float),
42                         gr.io_signature(1, 1, gr.sizeof_float),
43                 )
44                 #blocks
45                 lpf = gr.single_pole_iir_filter_ff(0.0)
46                 sub = gr.sub_ff()
47                 mute = gr.mute_ff()
48                 #connect
49                 self.connect(self, sub, self)
50                 self.connect(self, lpf, mute, (sub, 1))
51                 #subscribe
52                 controller.subscribe(ac_couple_key, lambda x: mute.set_mute(not x))
53                 controller.subscribe(sample_rate_key, lambda x: lpf.set_taps(0.05))
54                 #initialize
55                 controller[ac_couple_key] = controller[ac_couple_key]
56                 controller[sample_rate_key] = controller[sample_rate_key]
57
58 ##################################################
59 # Scope sink block (wrapper for old wxgui)
60 ##################################################
61 class _scope_sink_base(gr.hier_block2, common.wxgui_hb):
62         """
63         A scope block with a gui window.
64         """
65
66         def __init__(
67                 self,
68                 parent,
69                 title='',
70                 sample_rate=1,
71                 size=scope_window.DEFAULT_WIN_SIZE,
72                 v_scale=0,
73                 t_scale=0,
74                 v_offset=0,
75                 xy_mode=False,
76                 ac_couple=False,
77                 num_inputs=1,
78                 trig_mode=scope_window.DEFAULT_TRIG_MODE,
79                 frame_rate=scope_window.DEFAULT_FRAME_RATE,
80                 **kwargs #do not end with a comma
81         ):
82                 if not t_scale: t_scale = 10.0/sample_rate
83                 #init
84                 gr.hier_block2.__init__(
85                         self,
86                         "scope_sink",
87                         gr.io_signature(num_inputs, num_inputs, self._item_size),
88                         gr.io_signature(0, 0, 0),
89                 )
90                 #scope
91                 msgq = gr.msg_queue(2)
92                 scope = gr.oscope_sink_f(sample_rate, msgq)
93                 #controller
94                 self.controller = pubsub()
95                 self.controller.subscribe(SAMPLE_RATE_KEY, scope.set_sample_rate)
96                 self.controller.publish(SAMPLE_RATE_KEY, scope.sample_rate)
97                 self.controller.subscribe(DECIMATION_KEY, scope.set_decimation_count)
98                 self.controller.publish(DECIMATION_KEY, scope.get_decimation_count)
99                 self.controller.subscribe(TRIGGER_LEVEL_KEY, scope.set_trigger_level)
100                 self.controller.publish(TRIGGER_LEVEL_KEY, scope.get_trigger_level)
101                 self.controller.subscribe(TRIGGER_MODE_KEY, scope.set_trigger_mode)
102                 self.controller.publish(TRIGGER_MODE_KEY, scope.get_trigger_mode)
103                 self.controller.subscribe(TRIGGER_SLOPE_KEY, scope.set_trigger_slope)
104                 self.controller.publish(TRIGGER_SLOPE_KEY, scope.get_trigger_slope)
105                 self.controller.subscribe(TRIGGER_CHANNEL_KEY, scope.set_trigger_channel)
106                 self.controller.publish(TRIGGER_CHANNEL_KEY, scope.get_trigger_channel)
107                 actual_num_inputs = self._real and num_inputs or num_inputs*2
108                 #init ac couple
109                 for i in range(actual_num_inputs):
110                         self.controller[common.index_key(AC_COUPLE_KEY, i)] = ac_couple
111                 #start input watcher
112                 common.input_watcher(msgq, self.controller, MSG_KEY)
113                 #create window
114                 self.win = scope_window.scope_window(
115                         parent=parent,
116                         controller=self.controller,
117                         size=size,
118                         title=title,
119                         frame_rate=frame_rate,
120                         num_inputs=actual_num_inputs,
121                         sample_rate_key=SAMPLE_RATE_KEY,
122                         t_scale=t_scale,
123                         v_scale=v_scale,
124                         v_offset=v_offset,
125                         xy_mode=xy_mode,
126                         trig_mode=trig_mode,
127                         ac_couple_key=AC_COUPLE_KEY,
128                         trigger_level_key=TRIGGER_LEVEL_KEY,
129                         trigger_mode_key=TRIGGER_MODE_KEY,
130                         trigger_slope_key=TRIGGER_SLOPE_KEY,
131                         trigger_channel_key=TRIGGER_CHANNEL_KEY,
132                         decimation_key=DECIMATION_KEY,
133                         msg_key=MSG_KEY,
134                 )
135                 common.register_access_methods(self, self.win)
136                 #connect
137                 if self._real:
138                         for i in range(num_inputs):
139                                 self.wxgui_connect(
140                                         (self, i),
141                                         ac_couple_block(self.controller, common.index_key(AC_COUPLE_KEY, i), SAMPLE_RATE_KEY),
142                                         (scope, i),
143                                 )
144                 else:
145                         for i in range(num_inputs):
146                                 c2f = gr.complex_to_float() 
147                                 self.wxgui_connect((self, i), c2f)
148                                 for j in range(2):
149                                         self.connect(
150                                                 (c2f, j), 
151                                                 ac_couple_block(self.controller, common.index_key(AC_COUPLE_KEY, 2*i+j), SAMPLE_RATE_KEY),
152                                                 (scope, 2*i+j),
153                                         )
154
155 class scope_sink_f(_scope_sink_base):
156         _item_size = gr.sizeof_float
157         _real = True
158
159 class scope_sink_c(_scope_sink_base):
160         _item_size = gr.sizeof_gr_complex
161         _real = False
162
163 # ----------------------------------------------------------------
164 # Stand-alone test application
165 # ----------------------------------------------------------------
166
167 import wx
168 from gnuradio.wxgui import stdgui2
169
170 class test_top_block (stdgui2.std_top_block):
171     def __init__(self, frame, panel, vbox, argv):
172         stdgui2.std_top_block.__init__ (self, frame, panel, vbox, argv)
173
174         if len(argv) > 1:
175             frame_decim = int(argv[1]) 
176         else:
177             frame_decim = 1
178
179         if len(argv) > 2:
180             v_scale = float(argv[2])  # start up at this v_scale value
181         else:
182             v_scale = None  # start up in autorange mode, default
183
184         if len(argv) > 3:
185             t_scale = float(argv[3])  # start up at this t_scale value
186         else:
187             t_scale = .00003  # old behavior
188
189         print "frame decim %s  v_scale %s  t_scale %s" % (frame_decim,v_scale,t_scale)
190             
191         input_rate = 1e6
192
193         # Generate a complex sinusoid
194         self.src0 = gr.sig_source_c (input_rate, gr.GR_SIN_WAVE, 25.1e3, 1e3)
195
196         # We add this throttle block so that this demo doesn't suck down
197         # all the CPU available.  You normally wouldn't use it...
198         self.thr = gr.throttle(gr.sizeof_gr_complex, input_rate)
199
200         scope = scope_sink_c (panel,"Secret Data",sample_rate=input_rate,
201                               v_scale=v_scale, t_scale=t_scale)
202         vbox.Add (scope.win, 1, wx.EXPAND)
203
204         # Ultimately this will be
205         # self.connect("src0 throttle scope")
206         self.connect(self.src0, self.thr, scope) 
207
208 def main ():
209     app = stdgui2.stdapp (test_top_block, "O'Scope Test App")
210     app.MainLoop ()
211
212 if __name__ == '__main__':
213     main ()