2 # Copyright 2008, 2009, 2010 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)
50 self.set_use_persistence(False)
52 self._legend_cache = self.new_gl_cache(self._draw_legend, 50)
53 self.enable_legend(False)
55 self._waveform_cache = self.new_gl_cache(self._draw_waveforms, 50)
56 self._channels = dict()
58 self.register_init(self._init_channel_plotter)
60 def _init_channel_plotter(self):
62 Run gl initialization tasks.
64 GL.glEnableClientState(GL.GL_VERTEX_ARRAY)
66 def enable_legend(self, enable=None):
68 Enable/disable the legend.
69 @param enable true to enable
70 @return the enable state when None
72 if enable is None: return self._enable_legend
74 self._enable_legend = enable
75 self._legend_cache.changed(True)
78 def _draw_waveforms(self):
80 Draw the waveforms for each channel.
81 Scale the waveform data to the grid using gl matrix operations.
83 #use scissor to prevent drawing outside grid
84 GL.glEnable(GL.GL_SCISSOR_TEST)
87 self.padding_bottom+1,
88 self.width-self.padding_left-self.padding_right-1,
89 self.height-self.padding_top-self.padding_bottom-1,
91 for channel in reversed(sorted(self._channels.keys())):
92 samples = self._channels[channel][SAMPLES_KEY]
93 num_samps = len(samples)
94 if not num_samps: continue
95 #use opengl to scale the waveform
97 GL.glTranslatef(self.padding_left, self.padding_top, 0)
99 (self.width-self.padding_left-self.padding_right),
100 (self.height-self.padding_top-self.padding_bottom),
103 GL.glTranslatef(0, 1, 0)
104 if isinstance(samples, tuple):
105 x_scale, x_trans = 1.0/(self.x_max-self.x_min), -self.x_min
106 points = zip(*samples)
108 x_scale, x_trans = 1.0/(num_samps-1), -self._channels[channel][TRIG_OFF_KEY]
109 points = zip(numpy.arange(0, num_samps), samples)
110 GL.glScalef(x_scale, -1.0/(self.y_max-self.y_min), 1)
111 GL.glTranslatef(x_trans, -self.y_min, 0)
112 #draw the points/lines
113 GL.glColor3f(*self._channels[channel][COLOR_SPEC_KEY])
114 marker = self._channels[channel][MARKERY_KEY]
116 GL.glVertexPointerf(points)
117 GL.glDrawArrays(GL.GL_LINE_STRIP, 0, len(points))
118 elif isinstance(marker, (int, float)) and marker > 0:
119 GL.glPointSize(marker)
120 GL.glVertexPointerf(points)
121 GL.glDrawArrays(GL.GL_POINTS, 0, len(points))
123 GL.glDisable(GL.GL_SCISSOR_TEST)
125 def _populate_point_label(self, x_val, y_val):
127 Get the text the will populate the point label.
128 Give X and Y values for the current point.
129 Give values for the channel at the X coordinate.
130 @param x_val the current x value
131 @param y_val the current y value
132 @return a string with newlines
135 label_str = '%s: %s\n%s: %s'%(
136 self.x_label, common.eng_format(x_val, self.x_units),
137 self.y_label, common.eng_format(y_val, self.y_units),
139 for channel in sorted(self._channels.keys()):
140 samples = self._channels[channel][SAMPLES_KEY]
141 num_samps = len(samples)
142 if not num_samps: continue
143 if isinstance(samples, tuple): continue
144 #linear interpolation
145 x_index = (num_samps-1)*(x_val-self.x_min)/(self.x_max-self.x_min)
146 x_index_low = int(math.floor(x_index))
147 x_index_high = int(math.ceil(x_index))
148 scale = x_index - x_index_low + self._channels[channel][TRIG_OFF_KEY]
149 y_value = (samples[x_index_high] - samples[x_index_low])*scale + samples[x_index_low]
150 label_str += '\n%s: %s'%(channel, common.eng_format(y_value, self.y_units))
153 def _draw_legend(self):
155 Draw the legend in the upper right corner.
156 For each channel, draw a rectangle out of the channel color,
157 and overlay the channel text on top of the rectangle.
159 if not self.enable_legend(): return
160 x_off = self.width - self.padding_right - LEGEND_BOX_PADDING
161 for i, channel in enumerate(reversed(sorted(self._channels.keys()))):
162 samples = self._channels[channel][SAMPLES_KEY]
163 if not len(samples): continue
164 color_spec = self._channels[channel][COLOR_SPEC_KEY]
165 txt = gltext.Text(channel, font_size=LEGEND_TEXT_FONT_SIZE)
166 w, h = txt.get_size()
168 GL.glColor3f(*color_spec)
170 x_off - w - LEGEND_BOX_PADDING,
171 self.padding_top/2 - h/2 - LEGEND_BOX_PADDING,
172 w+2*LEGEND_BOX_PADDING,
173 h+2*LEGEND_BOX_PADDING,
175 txt.draw_text(wx.Point(x_off - w, self.padding_top/2 - h/2))
176 x_off -= w + 4*LEGEND_BOX_PADDING
178 def clear_waveform(self, channel):
180 Remove a waveform from the list of waveforms.
181 @param channel the channel key
184 if channel in self._channels.keys():
185 self._channels.pop(channel)
186 self._legend_cache.changed(True)
187 self._waveform_cache.changed(True)
190 def set_waveform(self, channel, samples=[], color_spec=(0, 0, 0), marker=None, trig_off=0):
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
197 @param trig_off fraction of sample for trigger offset
200 if channel not in self._channels.keys(): self._legend_cache.changed(True)
201 self._channels[channel] = {
202 SAMPLES_KEY: samples,
203 COLOR_SPEC_KEY: color_spec,
205 TRIG_OFF_KEY: trig_off,
207 self._waveform_cache.changed(True)
210 if __name__ == '__main__':
211 app = wx.PySimpleApp()
212 frame = wx.Frame(None, -1, 'Demo', wx.DefaultPosition)
213 vbox = wx.BoxSizer(wx.VERTICAL)
215 plotter = channel_plotter(frame)
216 plotter.set_x_grid(-1, 1, .2)
217 plotter.set_y_grid(-1, 1, .4)
218 vbox.Add(plotter, 1, wx.EXPAND)
220 plotter = channel_plotter(frame)
221 plotter.set_x_grid(-1, 1, .2)
222 plotter.set_y_grid(-1, 1, .4)
223 vbox.Add(plotter, 1, wx.EXPAND)
225 frame.SetSizerAndFit(vbox)
226 frame.SetSize(wx.Size(800, 600))