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