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 #use opengl to scale the waveform
112 glTranslatef(self.padding_left, self.padding_top, 0)
114 (self.width-self.padding_left-self.padding_right),
115 (self.height-self.padding_top-self.padding_bottom),
118 glTranslatef(0, 1, 0)
119 if isinstance(samples, tuple):
120 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
121 points = zip(*samples)
123 x_scale, x_trans = 1.0/(num_samps-1), 0
124 points = zip(numpy.arange(0, num_samps), samples)
125 glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
126 glTranslatef(x_trans, -self.y_min, 0)
127 #draw the points/lines
128 glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
129 marker = self._channels[channel][MARKERY_KEY]
130 if marker: glPointSize(marker)
131 glVertexPointer(2, GL_FLOAT, 0, points)
132 glDrawArrays(marker is None and GL_LINE_STRIP or GL_POINTS, 0, len(points))
135 def _populate_point_label(self, x_val, y_val):
137 Get the text the will populate the point label.
138 Give X and Y values for the current point.
139 Give values for the channel at the X coordinate.
140 @param x_val the current x value
141 @param y_val the current y value
142 @return a string with newlines
145 label_str = '%s: %s %s\n%s: %s %s'%(
147 common.label_format(x_val),
148 self.x_units, self.y_label,
149 common.label_format(y_val),
152 for channel in sorted(self._channels.keys()):
153 samples = self._channels[channel][SAMPLES_KEY]
154 num_samps = len(samples)
155 if not num_samps: continue
156 if isinstance(samples, tuple): continue
157 #linear interpolation
158 x_index = (num_samps-1)*(x_val/self.x_scalar-self.x_min)/(self.x_max-self.x_min)
159 x_index_low = int(math.floor(x_index))
160 x_index_high = int(math.ceil(x_index))
161 y_value = (samples[x_index_high] - samples[x_index_low])*(x_index - x_index_low) + samples[x_index_low]
162 label_str += '\n%s: %s %s'%(channel, common.label_format(y_value), self.y_units)
165 def _draw_legend(self):
167 Draw the legend in the upper right corner.
168 For each channel, draw a rectangle out of the channel color,
169 and overlay the channel text on top of the rectangle.
171 if not self.enable_legend(): return
172 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
173 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
174 samples = self._channels[channel][SAMPLES_KEY]
175 if not len(samples): continue
176 color_spec = self._channels[channel][COLOR_SPEC_KEY]
177 txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
178 w, h = txt.get_size()
180 glColor3f(*color_spec)
182 x_off - w - LEGEND_BOX_PADDING,
183 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
184 w+2*LEGEND_BOX_PADDING,
185 h+2*LEGEND_BOX_PADDING,
187 txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
188 x_off -= w + 4*LEGEND_BOX_PADDING
190 def set_waveform(self, channel, samples, color_spec, marker=None):
192 Set the waveform for a given channel.
193 @param channel the channel key
194 @param samples the waveform samples
195 @param color_spec the 3-tuple for line color
196 @param marker None for line
199 if channel not in self._channels.keys(): self.changed(True)
200 self._channels[channel] = {
201 SAMPLES_KEY: samples,
202 COLOR_SPEC_KEY: color_spec,
207 if __name__ == '__main__':
208 app = wx.PySimpleApp()
209 frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
210 vbox = wx.BoxSizer(wx.VERTICAL)
212 plotter = channel_plotter(frame)
213 plotter.set_x_grid(-1, 1, .2)
214 plotter.set_y_grid(-1, 1, .4)
215 vbox.Add(plotter, 1, wx.EXPAND)
217 plotter = channel_plotter(frame)
218 plotter.set_x_grid(-1, 1, .2)
219 plotter.set_y_grid(-1, 1, .4)
220 vbox.Add(plotter, 1, wx.EXPAND)
222 frame.SetSizerAndFit(vbox)
223 frame.SetSize(wx.Size(800, 600))