switch source package format to 3.0 quilt
[debian/gnuradio] / gr-msdd6000 / src / python-examples / msdd_rcv.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005,2006,2007 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3, or (at your option)
10 # any later version.
11
12 # GNU Radio is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 from gnuradio import gr, gru, eng_notation
24 from gnuradio import msdd
25 from gnuradio import blks2
26 from gnuradio.eng_option import eng_option
27 from gnuradio.wxgui import slider, powermate
28 from gnuradio.wxgui import stdgui2, form
29 from gnuradio.wxgui import fftsink2
30 from optparse import OptionParser
31 #from usrpm import usrp_dbid
32 import sys
33 import math
34 import wx
35
36 class wfm_rx_block (stdgui2.std_top_block):
37     def __init__(self,frame,panel,vbox,argv):
38         stdgui2.std_top_block.__init__ (self,frame,panel,vbox,argv)
39
40         parser=OptionParser(option_class=eng_option)        
41 #        parser.add_option("-R", "--rx-subdev-spec", type="subdev", default=None,
42 #                          help="select MSDD Rx side A or B (default=A)")
43         parser.add_option("-f", "--freq", type="eng_float", default=100.1e6,
44                           help="set frequency to FREQ", metavar="FREQ")
45         parser.add_option("-g", "--gain", type="eng_float", default=40,
46                           help="set gain in dB (default is midpoint)")
47 #        parser.add_option("-V", "--volume", type="eng_float", default=None,
48 #                          help="set volume (default is midpoint)")
49 #        parser.add_option("-O", "--audio-output", type="string", default="",
50 #                          help="pcm device name.  E.g., hw:0,0 or surround51 or /dev/dsp")
51
52         (options, args) = parser.parse_args()
53         if len(args) != 0:
54             parser.print_help()
55             sys.exit(1)
56         
57         self.frame = frame
58         self.panel = panel
59         
60         self.vol = 0
61         self.gain_range = (10, 70, .5)
62         self.state = "FREQ"
63         self.freq = 0
64         msdd_decim = 2
65
66         # build graph
67         self.fft_size = 8192
68         self.sample_rate = 200
69         self.u = msdd.source_c(0, 1, "10.45.4.44", 10000)
70         self.u.set_decim_rate(4)
71         self.u.set_desired_packet_size(0, 1460*100)        
72
73
74         #self.u.set_decim_rate(msdd_decim)
75 #        usrp_rate = adc_rate / msdd_decim           # 320 kS/s
76 #        chanfilt_decim = 1
77 #        demod_rate = usrp_rate / chanfilt_decim
78 #        audio_decimation = 10
79 #        audio_rate = demod_rate / audio_decimation  # 32 kHz
80 #
81 #        if options.rx_subdev_spec is None:
82 #            options.rx_subdev_spec = pick_subdevice(self.u)
83 #
84 #        self.u.set_mux(usrp.determine_rx_mux_value(self.u, options.rx_subdev_spec))
85 #        self.subdev = usrp.selected_subdev(self.u, options.rx_subdev_spec)
86 #        print "Using RX d'board %s" % (self.subdev.side_and_name(),)
87 #
88 #
89 #        chan_filt_coeffs = optfir.low_pass (1,           # gain
90 #                                            usrp_rate,   # sampling rate
91 #                                            80e3,        # passband cutoff
92 #                                            115e3,       # stopband cutoff
93 #                                            0.1,         # passband ripple
94 #                                            60)          # stopband attenuation
95 #        #print len(chan_filt_coeffs)
96 #        chan_filt = gr.fir_filter_ccf (chanfilt_decim, chan_filt_coeffs)
97 #
98 #        self.guts = blks2.wfm_rcv (demod_rate, audio_decimation)
99 #
100 #        self.volume_control = gr.multiply_const_ff(self.vol)
101 #
102 #        # sound card as final sink
103 #        audio_sink = audio.sink (int (audio_rate),
104 #                                 options.audio_output,
105 #                                 False)  # ok_to_block
106         
107         # now wire it all together
108         #self.connect (self.u, chan_filt, self.guts, self.volume_control, audio_sink)
109
110         self._build_gui(vbox)
111
112         if options.gain is None:
113             # if no gain was specified, use the mid-point in dB
114             #g = self.subdev.gain_range()
115             g = self.gain_range
116             options.gain = float(g[0]+g[1])/2
117 #
118 #        if options.volume is None:
119 #            g = self.volume_range()
120 #            options.volume = float(g[0]+g[1])/2
121 #            
122 #        if abs(options.freq) < 1e6:
123 #            options.freq *= 1e6
124
125         # set initial values
126 #
127         self.set_gain(options.gain)
128 #        self.set_vol(options.volume)
129         if not(self.set_freq(options.freq)):
130             self._set_status_msg("Failed to set initial frequency")
131         print "Frequency: ", self.u.rx_freq(0)
132
133
134     def _set_status_msg(self, msg, which=0):
135         self.frame.GetStatusBar().SetStatusText(msg, which)
136
137
138     def _build_gui(self, vbox):
139
140         def _form_set_freq(kv):
141             return self.set_freq(kv['freq'])
142
143         self.src_fft = None
144         if 1:
145             self.src_fft = fftsink2.fft_sink_c(self.panel, title="Data from MSDD",
146                                                fft_size=512, sample_rate=512)
147 #            self.s2f1 = gr.short_to_float()
148 #            self.scope = scopesink2.scope_sink_f(self.panel, sample_rate=self.sample_rate*self.fft_size)
149             
150             self.connect (self.u, self.src_fft)
151             #self.connect (self.s2f1, self.scope)
152             vbox.Add (self.src_fft.win, 4, wx.EXPAND)
153 #
154 #        if 1:
155 #            post_filt_fft = fftsink2.fft_sink_f(self.panel, title="Post Demod", 
156 #                                                fft_size=1024, sample_rate=usrp_rate,
157 #                                                y_per_div=10, ref_level=0)
158 #            self.connect (self.guts.fm_demod, post_filt_fft)
159 #            vbox.Add (post_filt_fft.win, 4, wx.EXPAND)
160 #
161 #        if 0:
162 #            post_deemph_fft = fftsink2.fft_sink_f(self.panel, title="Post Deemph",
163 #                                                  fft_size=512, sample_rate=audio_rate,
164 #                                                  y_per_div=10, ref_level=-20)
165 #            self.connect (self.guts.deemph, post_deemph_fft)
166 #            vbox.Add (post_deemph_fft.win, 4, wx.EXPAND)
167
168         
169         # control area form at bottom
170         self.myform = myform = form.form()
171
172         hbox = wx.BoxSizer(wx.HORIZONTAL)
173         hbox.Add((5,0), 0)
174         myform['freq'] = form.float_field(
175             parent=self.panel, sizer=hbox, label="Freq", weight=1,
176             callback=myform.check_input_and_call(_form_set_freq, self._set_status_msg))
177
178         hbox.Add((5,0), 0)
179         myform['freq_slider'] = \
180             form.quantized_slider_field(parent=self.panel, sizer=hbox, weight=3,
181                                         range=(30e6, 6e9, 1e6),
182                                         callback=self.set_freq)
183         hbox.Add((5,0), 0)
184         vbox.Add(hbox, 0, wx.EXPAND)
185
186         hbox = wx.BoxSizer(wx.HORIZONTAL)
187         hbox.Add((5,0), 0)
188 #
189 #        myform['volume'] = \
190 #            form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Volume",
191 #                                        weight=3, range=self.volume_range(),
192 #                                        callback=self.set_vol)
193 #        hbox.Add((5,0), 1)
194
195         myform['gain'] = \
196             form.quantized_slider_field(parent=self.panel, sizer=hbox, label="Gain",
197                                         weight=3, range=self.gain_range,
198                                         callback=self.set_gain)
199         hbox.Add((5,0), 0)
200         vbox.Add(hbox, 0, wx.EXPAND)
201 #
202 #        try:
203 #            self.knob = powermate.powermate(self.frame)
204 #            self.rot = 0
205 #            powermate.EVT_POWERMATE_ROTATE (self.frame, self.on_rotate)
206 #            powermate.EVT_POWERMATE_BUTTON (self.frame, self.on_button)
207 #        except:
208 #            print "FYI: No Powermate or Contour Knob found"
209
210
211     def on_rotate (self, event):
212         self.rot += event.delta
213         if (self.state == "FREQ"):
214             if self.rot >= 3:
215                 self.set_freq(self.freq + .1e6)
216                 self.rot -= 3
217             elif self.rot <=-3:
218                 self.set_freq(self.freq - .1e6)
219                 self.rot += 3
220         else:
221             step = self.volume_range()[2]
222             if self.rot >= 3:
223                 self.set_vol(self.vol + step)
224                 self.rot -= 3
225             elif self.rot <=-3:
226                 self.set_vol(self.vol - step)
227                 self.rot += 3
228             
229     def on_button (self, event):
230         if event.value == 0:        # button up
231             return
232         self.rot = 0
233         if self.state == "FREQ":
234             self.state = "VOL"
235         else:
236             self.state = "FREQ"
237         self.update_status_bar ()
238 #        
239 #
240 #    def set_vol (self, vol):
241 #        g = self.volume_range()
242 #        self.vol = max(g[0], min(g[1], vol))
243 #        self.volume_control.set_k(10**(self.vol/10))
244 #        self.myform['volume'].set_value(self.vol)
245 #        self.update_status_bar ()
246                                         
247     def set_freq(self, target_freq):
248         """
249         Set the center frequency we're interested in.
250
251         @param target_freq: frequency in Hz
252         @rypte: bool
253
254         Tuning is a two step process.  First we ask the front-end to
255         tune as close to the desired frequency as it can.  Then we use
256         the result of that operation and our target_frequency to
257         determine the value for the digital down converter.
258         """
259         r = self.u.set_rx_freq(0, target_freq);
260         
261         if r:
262             self.freq = target_freq
263             self.myform['freq'].set_value(target_freq)         # update displayed value
264             self.myform['freq_slider'].set_value(target_freq)  # update displayed value
265             self.update_status_bar()
266             self._set_status_msg("OK", 0)
267             return True
268
269         self._set_status_msg("Failed", 0)
270         return False
271
272     def set_gain(self, gain):
273         self.myform['gain'].set_value(gain)     # update displayed value
274         self.u.set_pga(0,gain)
275         
276     def update_status_bar (self):
277         msg = "Volume:%r  Setting:%s" % (self.vol, self.state)
278         self._set_status_msg(msg, 1)
279         #self.src_fft.set_baseband_freq(self.freq)
280 #
281 #    def volume_range(self):
282 #        return (-20.0, 0.0, 0.5)
283         
284
285 if __name__ == '__main__':
286     app = stdgui2.stdapp (wfm_rx_block, "MSDD FFT RX")
287     app.MainLoop ()