2 # Copyright 2008, 2009 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 grid_plotter_base import grid_plotter_base
30 LEGEND_TEXT_FONT_SIZE = 8
31 LEGEND_BOX_PADDING = 3
32 MIN_PADDING = 35, 10, 0, 0 #top, right, bottom, left
33 #constants for the waveform storage
34 SAMPLES_KEY = 'samples'
35 COLOR_SPEC_KEY = 'color_spec'
36 MARKERY_KEY = 'marker'
37 TRIG_OFF_KEY = 'trig_off'
39 ##################################################
40 # Channel Plotter for X Y Waveforms
41 ##################################################
42 class channel_plotter(grid_plotter_base):
44 def __init__(self, parent):
46 Create a new channel plotter.
49 grid_plotter_base.__init__(self, parent, MIN_PADDING)
51 self._legend_cache = self.new_gl_cache(self._draw_legend, 50)
52 self.enable_legend(False)
54 self._waveform_cache = self.new_gl_cache(self._draw_waveforms, 50)
55 self._channels = dict()
57 self.register_init(self._init_channel_plotter)
59 def _init_channel_plotter(self):
61 Run gl initialization tasks.
63 GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
65 def enable_legend(self, enable=None):
67 Enable/disable the legend.
68 @param enable true to enable
69 @return the enable state when None
71 if enable is None: return self._enable_legend
73 self._enable_legend = enable
74 self._legend_cache.changed(True)
77 def _draw_waveforms(self):
79 Draw the waveforms for each channel.
80 Scale the waveform data to the grid using gl matrix operations.
82 #use scissor to prevent drawing outside grid
83 GL.glEnable(GL.GL_SCISSOR_TEST)
86 self.padding_bottom+1,
87 self.width-self.padding_left-self.padding_right-1,
88 self.height-self.padding_top-self.padding_bottom-1,
90 for channel in reversed(sorted(self._channels.keys())):
91 samples = self._channels[channel][SAMPLES_KEY]
92 num_samps = len(samples)
93 if not num_samps: continue
94 #use opengl to scale the waveform
96 GL.glTranslatef(self.padding_left, self.padding_top, 0)
98 (self.width-self.padding_left-self.padding_right),
99 (self.height-self.padding_top-self.padding_bottom),
102 GL.glTranslatef(0, 1, 0)
103 if isinstance(samples, tuple):
104 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
105 points = zip(*samples)
107 x_scale, x_trans = 1.0/(num_samps-1), -self._channels[channel][TRIG_OFF_KEY]
108 points = zip(numpy.arange(0, num_samps), samples)
109 GL.glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
110 GL.glTranslatef(x_trans, -self.y_min, 0)
111 #draw the points/lines
112 GL.glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
113 marker = self._channels[channel][MARKERY_KEY]
115 GL.glVertexPointerf(points)
116 GL.glDrawArrays(GL.GL_LINE_STRIP, 0, len(points))
117 elif isinstance(marker, (int, float)) and marker > 0:
118 GL.glPointSize(marker)
119 GL.glVertexPointerf(points)
120 GL.glDrawArrays(GL.GL_POINTS, 0, len(points))
122 GL.glDisable(GL.GL_SCISSOR_TEST)
124 def _populate_point_label(self, x_val, y_val):
126 Get the text the will populate the point label.
127 Give X and Y values for the current point.
128 Give values for the channel at the X coordinate.
129 @param x_val the current x value
130 @param y_val the current y value
131 @return a string with newlines
134 label_str = '%s: %s\n%s: %s'%(
135 self.x_label, common.eng_format(x_val, self.x_units),
136 self.y_label, common.eng_format(y_val, self.y_units),
138 for channel in sorted(self._channels.keys()):
139 samples = self._channels[channel][SAMPLES_KEY]
140 num_samps = len(samples)
141 if not num_samps: continue
142 if isinstance(samples, tuple): continue
143 #linear interpolation
144 x_index = (num_samps-1)*(x_val-self.x_min)/(self.x_max-self.x_min)
145 x_index_low = int(math.floor(x_index))
146 x_index_high = int(math.ceil(x_index))
147 scale = x_index - x_index_low + self._channels[channel][TRIG_OFF_KEY]
148 y_value = (samples[x_index_high] - samples[x_index_low])*scale + samples[x_index_low]
149 label_str += '\n%s: %s'%(channel, common.eng_format(y_value, self.y_units))
152 def _draw_legend(self):
154 Draw the legend in the upper right corner.
155 For each channel, draw a rectangle out of the channel color,
156 and overlay the channel text on top of the rectangle.
158 if not self.enable_legend(): return
159 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
160 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
161 samples = self._channels[channel][SAMPLES_KEY]
162 if not len(samples): continue
163 color_spec = self._channels[channel][COLOR_SPEC_KEY]
164 txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
165 w, h = txt.get_size()
167 GL.glColor3f(*color_spec)
169 x_off - w - LEGEND_BOX_PADDING,
170 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
171 w+2*LEGEND_BOX_PADDING,
172 h+2*LEGEND_BOX_PADDING,
174 txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
175 x_off -= w + 4*LEGEND_BOX_PADDING
177 def clear_waveform(self, channel):
179 Remove a waveform from the list of waveforms.
180 @param channel the channel key
183 if channel in self._channels.keys():
184 self._channels.pop(channel)
185 self._legend_cache.changed(True)
186 self._waveform_cache.changed(True)
189 def set_waveform(self, channel, samples=[], color_spec=(0, 0, 0), marker=None, trig_off=0):
191 Set the waveform for a given channel.
192 @param channel the channel key
193 @param samples the waveform samples
194 @param color_spec the 3-tuple for line color
195 @param marker None for line
196 @param trig_off fraction of sample for trigger offset
199 if channel not in self._channels.keys(): self._legend_cache.changed(True)
200 self._channels[channel] = {
201 SAMPLES_KEY: samples,
202 COLOR_SPEC_KEY: color_spec,
204 TRIG_OFF_KEY: trig_off,
206 self._waveform_cache.changed(True)
209 if __name__ == '__main__':
210 app = wx.PySimpleApp()
211 frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
212 vbox = wx.BoxSizer(wx.VERTICAL)
214 plotter = channel_plotter(frame)
215 plotter.set_x_grid(-1, 1, .2)
216 plotter.set_y_grid(-1, 1, .4)
217 vbox.Add(plotter, 1, wx.EXPAND)
219 plotter = channel_plotter(frame)
220 plotter.set_x_grid(-1, 1, .2)
221 plotter.set_y_grid(-1, 1, .4)
222 vbox.Add(plotter, 1, wx.EXPAND)
224 frame.SetSizerAndFit(vbox)
225 frame.SetSize(wx.Size(800, 600))