Merged r10463:10658 from jblum/gui_guts into trunk. Trunk passes distcheck.
[debian/gnuradio] / gr-wxgui / src / python / const_window.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 plotter
26 import common
27 import wx
28 import numpy
29 import math
30 import pubsub
31 from constants import *
32 from gnuradio import gr #for gr.prefs
33
34 ##################################################
35 # Constants
36 ##################################################
37 SLIDER_STEPS = 200
38 ALPHA_MIN_EXP, ALPHA_MAX_EXP = -6, -0.301
39 GAIN_MU_MIN_EXP, GAIN_MU_MAX_EXP = -6, -0.301
40 DEFAULT_FRAME_RATE = gr.prefs().get_long('wxgui', 'const_rate', 5)
41 DEFAULT_WIN_SIZE = (500, 400)
42 DEFAULT_CONST_SIZE = gr.prefs().get_long('wxgui', 'const_size', 2048)
43 CONST_PLOT_COLOR_SPEC = (0, 0, 1)
44 MARKER_TYPES = (
45         ('Dot Small', 1.0),
46         ('Dot Medium', 2.0),
47         ('Dot Large', 3.0),
48         ('Line Link', None),
49 )
50 DEFAULT_MARKER_TYPE = 2.0
51
52 ##################################################
53 # Constellation window control panel
54 ##################################################
55 class control_panel(wx.Panel):
56         """
57         A control panel with wx widgits to control the plotter.
58         """
59         def __init__(self, parent):
60                 """
61                 Create a new control panel.
62                 @param parent the wx parent window
63                 """
64                 self.parent = parent
65                 wx.Panel.__init__(self, parent, style=wx.SUNKEN_BORDER)
66                 control_box = wx.BoxSizer(wx.VERTICAL)
67                 self.marker_index = 2
68                 #begin control box
69                 control_box.Add(common.LabelText(self, 'Options'), 0, wx.ALIGN_CENTER)
70                 #alpha
71                 control_box.AddStretchSpacer()
72                 alpha_slider = common.LogSliderController(
73                         self, 'Alpha',
74                         ALPHA_MIN_EXP, ALPHA_MAX_EXP, SLIDER_STEPS,
75                         parent, ALPHA_KEY,
76                 )
77                 control_box.Add(alpha_slider, 0, wx.EXPAND)
78                 #gain_mu
79                 control_box.AddStretchSpacer()
80                 gain_mu_slider = common.LogSliderController(
81                         self, 'Gain Mu',
82                         GAIN_MU_MIN_EXP, GAIN_MU_MAX_EXP, SLIDER_STEPS,
83                         parent, GAIN_MU_KEY,
84                 )
85                 control_box.Add(gain_mu_slider, 0, wx.EXPAND)
86                 #marker
87                 control_box.AddStretchSpacer()
88                 marker_chooser = common.DropDownController(self, MARKER_TYPES, parent, MARKER_KEY)
89                 control_box.Add(common.LabelBox(self, 'Marker', marker_chooser), 0, wx.EXPAND)
90                 #run/stop
91                 control_box.AddStretchSpacer()
92                 self.run_button = common.ToggleButtonController(self, parent, RUNNING_KEY, 'Stop', 'Run')
93                 control_box.Add(self.run_button, 0, wx.EXPAND)
94                 #set sizer
95                 self.SetSizerAndFit(control_box)
96
97 ##################################################
98 # Constellation window with plotter and control panel
99 ##################################################
100 class const_window(wx.Panel, pubsub.pubsub):
101         def __init__(
102                 self,
103                 parent,
104                 controller,
105                 size,
106                 title,
107                 msg_key,
108                 alpha_key,
109                 beta_key,
110                 gain_mu_key,
111                 gain_omega_key,
112                 omega_key,
113                 sample_rate_key,
114         ):
115                 pubsub.pubsub.__init__(self)
116                 #proxy the keys
117                 self.proxy(MSG_KEY, controller, msg_key)
118                 self.proxy(ALPHA_KEY, controller, alpha_key)
119                 self.proxy(BETA_KEY, controller, beta_key)
120                 self.proxy(GAIN_MU_KEY, controller, gain_mu_key)
121                 self.proxy(GAIN_OMEGA_KEY, controller, gain_omega_key)
122                 self.proxy(OMEGA_KEY, controller, omega_key)
123                 self.proxy(SAMPLE_RATE_KEY, controller, sample_rate_key)
124                 #init panel and plot
125                 wx.Panel.__init__(self, parent, style=wx.SIMPLE_BORDER)
126                 self.plotter = plotter.channel_plotter(self)
127                 self.plotter.SetSize(wx.Size(*size))
128                 self.plotter.set_title(title)
129                 self.plotter.set_x_label('Inphase')
130                 self.plotter.set_y_label('Quadrature')
131                 self.plotter.enable_point_label(True)
132                 self.plotter.enable_grid_lines(True)
133                 #setup the box with plot and controls
134                 self.control_panel = control_panel(self)
135                 main_box = wx.BoxSizer(wx.HORIZONTAL)
136                 main_box.Add(self.plotter, 1, wx.EXPAND)
137                 main_box.Add(self.control_panel, 0, wx.EXPAND)
138                 self.SetSizerAndFit(main_box)
139                 #alpha and gain mu 2nd orders
140                 def set_beta(alpha): self[BETA_KEY] = .25*alpha**2
141                 self.subscribe(ALPHA_KEY, set_beta)
142                 def set_gain_omega(gain_mu): self[GAIN_OMEGA_KEY] = .25*gain_mu**2
143                 self.subscribe(GAIN_MU_KEY, set_gain_omega)
144                 #initialize values
145                 self[ALPHA_KEY] = self[ALPHA_KEY]
146                 self[GAIN_MU_KEY] = self[GAIN_MU_KEY]
147                 self[RUNNING_KEY] = True
148                 self[X_DIVS_KEY] = 8
149                 self[Y_DIVS_KEY] = 8
150                 self[MARKER_KEY] = DEFAULT_MARKER_TYPE
151                 #register events
152                 self.subscribe(MSG_KEY, self.handle_msg)
153                 self.subscribe(X_DIVS_KEY, self.update_grid)
154                 self.subscribe(Y_DIVS_KEY, self.update_grid)
155                 #initial update
156                 self.update_grid()
157
158         def handle_msg(self, msg):
159                 """
160                 Plot the samples onto the complex grid.
161                 @param msg the array of complex samples
162                 """
163                 if not self[RUNNING_KEY]: return
164                 #convert to complex floating point numbers
165                 samples = numpy.fromstring(msg, numpy.complex64)
166                 real = numpy.real(samples)
167                 imag = numpy.imag(samples)
168                 #plot
169                 self.plotter.set_waveform(
170                         channel=0,
171                         samples=(real, imag),
172                         color_spec=CONST_PLOT_COLOR_SPEC,
173                         marker=self[MARKER_KEY],
174                 )
175                 #update the plotter
176                 self.plotter.update()
177
178         def update_grid(self):
179                 #update the x axis
180                 x_max = 2.0
181                 self.plotter.set_x_grid(-x_max, x_max, common.get_clean_num(2.0*x_max/self[X_DIVS_KEY]))
182                 #update the y axis
183                 y_max = 2.0
184                 self.plotter.set_y_grid(-y_max, y_max, common.get_clean_num(2.0*y_max/self[Y_DIVS_KEY]))
185                 #update plotter
186                 self.plotter.update()
187
188
189
190