2 # Copyright 2008 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
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)
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.
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.
23 from plotter_base import grid_plotter_base
24 from OpenGL.GL import *
25 from gnuradio.wxgui import common
30 LEGEND_TEXT_FONT_SIZE = 8
31 LEGEND_BOX_PADDING = 3
32 PADDING = 35, 15, 40, 60 #top, right, bottom, left
33 #constants for the waveform storage
34 SAMPLES_KEY = 'samples'
35 COLOR_SPEC_KEY = 'color_spec'
36 MARKERY_KEY = 'marker'
38 ##################################################
39 # Channel Plotter for X Y Waveforms
40 ##################################################
41 class channel_plotter(grid_plotter_base):
43 def __init__(self, parent):
45 Create a new channel plotter.
48 grid_plotter_base.__init__(self, parent, PADDING)
49 self._channels = dict()
50 self.enable_legend(False)
54 Run gl initialization tasks.
56 glEnableClientState(GL_VERTEX_ARRAY)
57 self._grid_compiled_list_id = glGenLists(1)
59 def enable_legend(self, enable=None):
61 Enable/disable the legend.
62 @param enable true to enable
63 @return the enable state when None
65 if enable is None: return self._enable_legend
67 self._enable_legend = enable
73 Draw the grid and waveforms.
77 #store the grid drawing operations
79 glNewList(self._grid_compiled_list_id, GL_COMPILE)
85 glCallList(self._grid_compiled_list_id)
86 #use scissor to prevent drawing outside grid
87 glEnable(GL_SCISSOR_TEST)
90 self.padding_bottom+1,
91 self.width-self.padding_left-self.padding_right-1,
92 self.height-self.padding_top-self.padding_bottom-1,
95 self._draw_waveforms()
96 glDisable(GL_SCISSOR_TEST)
97 self._draw_point_label()
98 #swap buffer into display
102 def _draw_waveforms(self):
104 Draw the waveforms for each channel.
105 Scale the waveform data to the grid using gl matrix operations.
107 for channel in reversed(sorted(self._channels.keys())):
108 samples = self._channels[channel][SAMPLES_KEY]
109 num_samps = len(samples)
110 if not num_samps: continue
111 #use opengl to scale the waveform
113 glTranslatef(self.padding_left, self.padding_top, 0)
115 (self.width-self.padding_left-self.padding_right),
116 (self.height-self.padding_top-self.padding_bottom),
119 glTranslatef(0, 1, 0)
120 if isinstance(samples, tuple):
121 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
122 points = zip(*samples)
124 x_scale, x_trans = 1.0/(num_samps-1), 0
125 points = zip(numpy.arange(0, num_samps), samples)
126 glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
127 glTranslatef(x_trans, -self.y_min, 0)
128 #draw the points/lines
129 glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
130 marker = self._channels[channel][MARKERY_KEY]
131 if marker: glPointSize(marker)
132 glVertexPointerf(points)
133 glDrawArrays(marker is None and GL_LINE_STRIP or GL_POINTS, 0, len(points))
136 def _populate_point_label(self, x_val, y_val):
138 Get the text the will populate the point label.
139 Give X and Y values for the current point.
140 Give values for the channel at the X coordinate.
141 @param x_val the current x value
142 @param y_val the current y value
143 @return a string with newlines
146 label_str = '%s: %s %s\n%s: %s %s'%(
148 common.label_format(x_val),
149 self.x_units, self.y_label,
150 common.label_format(y_val),
153 for channel in sorted(self._channels.keys()):
154 samples = self._channels[channel][SAMPLES_KEY]
155 num_samps = len(samples)
156 if not num_samps: continue
157 if isinstance(samples, tuple): continue
158 #linear interpolation
159 x_index = (num_samps-1)*(x_val/self.x_scalar-self.x_min)/(self.x_max-self.x_min)
160 x_index_low = int(math.floor(x_index))
161 x_index_high = int(math.ceil(x_index))
162 y_value = (samples[x_index_high] - samples[x_index_low])*(x_index - x_index_low) + samples[x_index_low]
163 label_str += '\n%s: %s %s'%(channel, common.label_format(y_value), self.y_units)
166 def _draw_legend(self):
168 Draw the legend in the upper right corner.
169 For each channel, draw a rectangle out of the channel color,
170 and overlay the channel text on top of the rectangle.
172 if not self.enable_legend(): return
173 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
174 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
175 samples = self._channels[channel][SAMPLES_KEY]
176 if not len(samples): continue
177 color_spec = self._channels[channel][COLOR_SPEC_KEY]
178 txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
179 w, h = txt.get_size()
181 glColor3f(*color_spec)
183 x_off - w - LEGEND_BOX_PADDING,
184 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
185 w+2*LEGEND_BOX_PADDING,
186 h+2*LEGEND_BOX_PADDING,
188 txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
189 x_off -= w + 4*LEGEND_BOX_PADDING
191 def set_waveform(self, channel, samples, color_spec, marker=None):
193 Set the waveform for a given channel.
194 @param channel the channel key
195 @param samples the waveform samples
196 @param color_spec the 3-tuple for line color
197 @param marker None for line
200 if channel not in self._channels.keys(): self.changed(True)
201 self._channels[channel] = {
202 SAMPLES_KEY: samples,
203 COLOR_SPEC_KEY: color_spec,
208 if __name__ == '__main__':
209 app = wx.PySimpleApp()
210 frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
211 vbox = wx.BoxSizer(wx.VERTICAL)
213 plotter = channel_plotter(frame)
214 plotter.set_x_grid(-1, 1, .2)
215 plotter.set_y_grid(-1, 1, .4)
216 vbox.Add(plotter, 1, wx.EXPAND)
218 plotter = channel_plotter(frame)
219 plotter.set_x_grid(-1, 1, .2)
220 plotter.set_y_grid(-1, 1, .4)
221 vbox.Add(plotter, 1, wx.EXPAND)
223 frame.SetSizerAndFit(vbox)
224 frame.SetSize(wx.Size(800, 600))