Imported Upstream version 3.0
[debian/gnuradio] / gr-usrp / src / tx_debug_gui.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2005 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 2, 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 import sys
24 import wx
25 from gnuradio.wxgui import form
26
27 class tx_debug_gui(wx.Frame):
28     def __init__(self, tx_subdev, title="Tx Debug"):
29         wx.Frame.__init__(self, None, -1, title)
30
31         self.subdev = tx_subdev
32         self.subdev._u.set_verbose(True)
33         
34         self.CreateStatusBar (1)
35
36         self.panel = wx.Panel(self, -1)
37         self.vbox = wx.BoxSizer(wx.VERTICAL)
38         self.panel.SetSizer(self.vbox)
39         self.panel.SetAutoLayout(True)
40
41         self._create_form()
42
43         self.vbox.Fit(self.panel)
44
45         self.frame_vbox = wx.BoxSizer(wx.VERTICAL)
46         self.frame_vbox.Add(self.panel, 1, wx.EXPAND)
47         self.SetSizer(self.frame_vbox)
48         self.SetAutoLayout(True)
49         self.frame_vbox.Fit(self)
50         
51     # ----------------------------------------------------------------
52
53     def _write_9862(self, regno, v):
54         return self.subdev._u._write_9862(self.subdev._which, regno, v)
55
56     def _set_dac_offset(self, i_or_q, offset, offset_pin):
57         return self.subdev._u.set_dac_offset(self.subdev._which * 2 + i_or_q, offset, offset_pin)
58
59     def _set_dac_fine_gain(self, i_or_q, gain, coarse):
60         return self._write_9862(14 + i_or_q, (coarse & 0xC0) | (gain & 0x3f))
61
62     def _create_form(self):
63         self._create_dac_offset()
64         self._create_dac_fine_gain()
65         self._create_pga()
66         
67     # ----------------------------------------------------------------
68
69     def _create_dac_offset(self):
70
71         sbs = wx.StaticBoxSizer(wx.StaticBox(self.panel), wx.VERTICAL)
72
73         hbox = wx.BoxSizer(wx.HORIZONTAL)
74         hbox.Add(wx.StaticText(self.panel, -1, "DAC Offset"), 5, 0)
75         sbs.Add(hbox, 0, 1)
76
77
78         self._create_dac_offset_helper(sbs, 0)
79         self._create_dac_offset_helper(sbs, 1)
80
81         self.vbox.Add(sbs, 0, wx.EXPAND)
82
83     def _create_dac_offset_helper(self, vbox, i_or_q):
84
85         def doit(kv):
86             drive_positive = kv['drive_positive']
87             dac_offset = kv['dac_offset']
88             print "drive_positive =", drive_positive
89             print "dac_offset[%d] = %4d" % (i_or_q, dac_offset)
90             
91             # FIXME signed magnitude??
92             # dac_offset = signed_mag10(dac_offset)
93             return self._set_dac_offset(i_or_q, dac_offset, int(drive_positive))
94         
95         def signed_mag10(x):
96             # not clear from doc if this is really 2's comp or 10-bit signed magnitude
97             # we'll guess it's 10-bit signed mag
98             if x < 0:
99                 return (1 << 9) | min(511, max(0, abs(x)))
100             else:
101                 return (0 << 9) | min(511, max(0, abs(x)))
102
103         myform = form.form()
104         hbox = wx.BoxSizer(wx.HORIZONTAL)
105         vbox.Add(hbox, 0, wx.EXPAND)
106         myform['drive_positive'] = form.checkbox_field(parent=self.panel, sizer=hbox,
107                                                        callback=myform.check_input_and_call(doit),
108                                                        weight=0,
109                                                        label="drive +ve")
110         myform['dac_offset'] = form.slider_field(parent=self.panel, sizer=hbox,
111                                                  callback=myform.check_input_and_call(doit),
112                                                  min=-512, max=511, value=0,
113                                                  weight=5)
114         
115     # ----------------------------------------------------------------
116
117     def _create_dac_fine_gain(self):
118         sbs = wx.StaticBoxSizer(wx.StaticBox(self.panel), wx.VERTICAL)
119
120         hbox = wx.BoxSizer(wx.HORIZONTAL)
121         hbox.Add(wx.StaticText(self.panel, -1, "DAC Gain"), 5, 0)
122         sbs.Add(hbox, 0, 1)
123
124         self._create_dac_gain_helper(sbs, 0)
125         self._create_dac_gain_helper(sbs, 1)
126
127         self.vbox.Add(sbs, 0, wx.EXPAND)
128
129     def _create_dac_gain_helper(self, vbox, i_or_q):
130
131         d = { "1/1"  : 0xC0,
132               "1/2"  : 0x40,
133               "1/11" : 0x00 }
134
135         def doit(kv):
136             dac_gain = kv['dac_gain']
137             coarse_s = kv['coarse']
138             print "dac_gain[%d] = %4d" % (i_or_q, dac_gain)
139             print "coarse = ", coarse_s
140             return self._set_dac_fine_gain(i_or_q, dac_gain, d[coarse_s])
141         
142         myform = form.form()
143         hbox = wx.BoxSizer(wx.HORIZONTAL)
144         vbox.Add(hbox, 0, wx.EXPAND)
145         myform['coarse'] = form.radiobox_field(parent=self.panel, sizer=hbox,
146                                                callback=myform.check_input_and_call(doit),
147                                                choices=['1/11', '1/2', '1/1'],
148                                                weight=1, value='1/1')
149         myform['dac_gain'] = form.slider_field(parent=self.panel, sizer=hbox,
150                                                callback=myform.check_input_and_call(doit),
151                                                min=-32, max=31, value=0,
152                                                weight=4)
153
154
155     # ----------------------------------------------------------------
156
157     def _create_pga(self):
158         sbs = wx.StaticBoxSizer(wx.StaticBox(self.panel), wx.VERTICAL)
159
160         form.quantized_slider_field(parent=self.panel, sizer=sbs, label="PGA",
161                                     weight=3, range=self.subdev.gain_range(),
162                                     callback=self.subdev.set_gain)
163
164         self.vbox.Add(sbs, 0, wx.EXPAND)
165
166
167     # ----------------------------------------------------------------
168
169     
170     def _set_status_msg(self, msg):
171         self.GetStatusBar().SetStatusText(msg, 0)
172
173         
174 if False and __name__ == '__main__':
175
176     class demo_app (wx.App):
177         def __init__ (self):
178             wx.App.__init__(self)
179
180         def OnInit (self):
181             frame = tx_debug_gui(None, "Debug TX")
182             frame.Show(True)
183             self.SetTopWindow (frame)
184             return True
185
186     app = demo_app()
187     app.MainLoop()