summaryrefslogtreecommitdiff
path: root/gr-wxgui/src/python/common.py
blob: f31e1a7085aac9983889668c2c8a72496815f464 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#
# Copyright 2008 Free Software Foundation, Inc.
#
# This file is part of GNU Radio
#
# GNU Radio is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Radio is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Radio; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
#

import threading
import numpy
import math
import wx

class prop_setter(object):
	def _register_set_prop(self, controller, control_key, *args):
		def set_method(value): controller[control_key] = value
		if args: set_method(args[0])
		setattr(self, 'set_%s'%control_key, set_method)

##################################################
# Custom Data Event
##################################################
EVT_DATA = wx.PyEventBinder(wx.NewEventType())
class DataEvent(wx.PyEvent):
	def __init__(self, data):
		wx.PyEvent.__init__(self, wx.NewId(), EVT_DATA.typeId)
		self.data = data

##################################################
# Input Watcher Thread
##################################################
class input_watcher(threading.Thread):
	"""
	Input watcher thread runs forever.
	Read messages from the message queue.
	Forward messages to the message handler.
	"""
	def __init__ (self, msgq, handle_msg):
		threading.Thread.__init__(self)
		self.setDaemon(1)
		self.msgq = msgq
		self._handle_msg = handle_msg
		self.keep_running = True
		self.start()

	def run(self):
		while self.keep_running: self._handle_msg(self.msgq.delete_head().to_string())

##################################################
# WX Shared Classes
##################################################
class LabelText(wx.StaticText):
	"""
	Label text to give the wx plots a uniform look.
	Get the default label text and set the font bold.
	"""
	def __init__(self, parent, label):
		wx.StaticText.__init__(self, parent, -1, label)
		font = self.GetFont()
		font.SetWeight(wx.FONTWEIGHT_BOLD)
		self.SetFont(font)

class IncrDecrButtons(wx.BoxSizer):
	"""
	A horizontal box sizer with a increment and a decrement button.
	"""
	def __init__(self, parent, on_incr, on_decr):
		"""
		@param parent the parent window
		@param on_incr the event handler for increment
		@param on_decr the event handler for decrement
		"""
		wx.BoxSizer.__init__(self, wx.HORIZONTAL)
		self._incr_button = wx.Button(parent, -1, '+', style=wx.BU_EXACTFIT)
		self._incr_button.Bind(wx.EVT_BUTTON, on_incr)
		self.Add(self._incr_button, 0, wx.ALIGN_CENTER_VERTICAL)
		self._decr_button = wx.Button(parent, -1, ' - ', style=wx.BU_EXACTFIT)
		self._decr_button.Bind(wx.EVT_BUTTON, on_decr)
		self.Add(self._decr_button, 0, wx.ALIGN_CENTER_VERTICAL)

	def Disable(self, disable=True): self.Enable(not disable)
	def Enable(self, enable=True):
		if enable:
			self._incr_button.Enable()
			self._decr_button.Enable()
		else:
			self._incr_button.Disable()
			self._decr_button.Disable()

class ToggleButtonController(wx.Button):
	def __init__(self, parent, controller, control_key, true_label, false_label):
		self._controller = controller
		self._control_key = control_key
		wx.Button.__init__(self, parent, -1, '', style=wx.BU_EXACTFIT)
		self.Bind(wx.EVT_BUTTON, self._evt_button)
		controller.subscribe(control_key, lambda x: self.SetLabel(x and true_label or false_label))

	def _evt_button(self, e):
		self._controller[self._control_key] = not self._controller[self._control_key]

class CheckBoxController(wx.CheckBox):
	def __init__(self, parent, label, controller, control_key):
		self._controller = controller
		self._control_key = control_key
		wx.CheckBox.__init__(self, parent, style=wx.CHK_2STATE, label=label)
		self.Bind(wx.EVT_CHECKBOX, self._evt_checkbox)
		controller.subscribe(control_key, lambda x: self.SetValue(bool(x)))

	def _evt_checkbox(self, e):
		self._controller[self._control_key] = bool(e.IsChecked())

class LogSliderController(wx.BoxSizer):
	"""
	Log slider controller with display label and slider.
	Gives logarithmic scaling to slider operation.
	"""
	def __init__(self, parent, label, min_exp, max_exp, slider_steps, controller, control_key, formatter=lambda x: ': %.6f'%x):
		wx.BoxSizer.__init__(self, wx.VERTICAL)
		self._label = wx.StaticText(parent, -1, label + formatter(1/3.0))
		self.Add(self._label, 0, wx.EXPAND)
		self._slider = wx.Slider(parent, -1, 0, 0, slider_steps, style=wx.SL_HORIZONTAL)
		self.Add(self._slider, 0, wx.EXPAND)
		def _on_slider_event(event):
			controller[control_key] = \
			10**(float(max_exp-min_exp)*self._slider.GetValue()/slider_steps + min_exp)
		self._slider.Bind(wx.EVT_SLIDER, _on_slider_event)
		def _on_controller_set(value):
			self._label.SetLabel(label + formatter(value))
			slider_value = slider_steps*(math.log10(value)-min_exp)/(max_exp-min_exp)
			slider_value = min(max(0, slider_value), slider_steps)
			if abs(slider_value - self._slider.GetValue()) > 1:
				self._slider.SetValue(slider_value)
		controller.subscribe(control_key, _on_controller_set)

	def Disable(self, disable=True): self.Enable(not disable)
	def Enable(self, enable=True):
		if enable:
			self._slider.Enable()
			self._label.Enable()
		else:
			self._slider.Disable()
			self._label.Disable()

class DropDownController(wx.BoxSizer):
	"""
	Drop down controller with label and chooser.
	Srop down selection from a set of choices.
	"""
	def __init__(self, parent, label, choices, controller, control_key, size=(-1, -1)):
		"""
		@param parent the parent window
		@param label the label for the drop down
		@param choices a list of tuples -> (label, value)
		@param controller the prop val controller
		@param control_key the prop key for this control
		"""
		wx.BoxSizer.__init__(self, wx.HORIZONTAL)
		self._label = wx.StaticText(parent, -1, ' %s '%label)
		self.Add(self._label, 1, wx.ALIGN_CENTER_VERTICAL)
		self._chooser = wx.Choice(parent, -1, choices=[c[0] for c in choices], size=size)
		def _on_chooser_event(event):
			controller[control_key] = choices[self._chooser.GetSelection()][1]
		self._chooser.Bind(wx.EVT_CHOICE, _on_chooser_event)
		self.Add(self._chooser, 0, wx.ALIGN_CENTER_VERTICAL)
		def _on_controller_set(value):
			#only set the chooser if the value is a possible choice
			for i, choice in enumerate(choices):
				if value == choice[1]: self._chooser.SetSelection(i)
		controller.subscribe(control_key, _on_controller_set)

	def Disable(self, disable=True): self.Enable(not disable)
	def Enable(self, enable=True):
		if enable:
			self._chooser.Enable()
			self._label.Enable()
		else:
			self._chooser.Disable()
			self._label.Disable()

##################################################
# Shared Functions
##################################################
def get_exp(num):
	"""
	Get the exponent of the number in base 10.
	@param num the floating point number
	@return the exponent as an integer
	"""
	if num == 0: return 0
	return int(math.floor(math.log10(abs(num))))

def get_clean_num(num):
	"""
	Get the closest clean number match to num with bases 1, 2, 5.
	@param num the number
	@return the closest number
	"""
	if num == 0: return 0
	if num > 0: sign = 1
	else: sign = -1
	exp = get_exp(num)
	nums = numpy.array((1, 2, 5, 10))*(10**exp)
	return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]

def get_clean_incr(num):
	"""
	Get the next higher clean number with bases 1, 2, 5.
	@param num the number
	@return the next higher number
	"""
	num = get_clean_num(num)
	exp = get_exp(num)
	coeff = int(round(num/10**exp))
	return {
		-5: -2,
		-2: -1,
		-1: -.5,
		1: 2,
		2: 5,
		5: 10,
	}[coeff]*(10**exp)

def get_clean_decr(num):
	"""
	Get the next lower clean number with bases 1, 2, 5.
	@param num the number
	@return the next lower number
	"""
	num = get_clean_num(num)
	exp = get_exp(num)
	coeff = int(round(num/10**exp))
	return {
		-5: -10,
		-2: -5,
		-1: -2,
		1: .5,
		2: 1,
		5: 2,
	}[coeff]*(10**exp)

def get_min_max(samples):
	"""
	Get the minimum and maximum bounds for an array of samples.
	@param samples the array of real values
	@return a tuple of min, max
	"""
	scale_factor = 3
	mean = numpy.average(samples)
	rms = scale_factor*((numpy.sum((samples-mean)**2)/len(samples))**.5)
	min = mean - rms
	max = mean + rms
	return min, max

def get_si_components(num):
	"""
	Get the SI units for the number.
	Extract the coeff and exponent of the number.
	The exponent will be a multiple of 3.
	@param num the floating point number
	@return the tuple coeff, exp, prefix
	"""
	exp = get_exp(num)
	exp -= exp%3
	exp = min(max(exp, -24), 24) #bounds on SI table below
	prefix = {
		24: 'Y', 21: 'Z',
		18: 'E', 15: 'P',
		12: 'T', 9: 'G',
		6: 'M', 3: 'K',
		0: '',
		-3: 'm', -6: 'u',
		-9: 'n', -12: 'p',
		-15: 'f', -18: 'a',
		-21: 'z', -24: 'y',
	}[exp]
	coeff = num/10**exp
	return coeff, exp, prefix

def label_format(num):
	"""
	Format a floating point number into a presentable string.
	If the number has an small enough exponent, use regular decimal.
	Otherwise, format the number with floating point notation.
	Exponents are normalized to multiples of 3.
	In the case where the exponent was found to be -3,
	it is best to display this as a regular decimal, with a 0 to the left.
	@param num the number to format
	@return a label string
	"""
	coeff, exp, prefix = get_si_components(num)
	if -3 <= exp < 3: return '%g'%num
	return '%se%d'%('%.3g'%coeff, exp)

if __name__ == '__main__':
	import random
	for i in range(-25, 25):
		num = random.random()*10**i
		print num, ':', get_si_components(num)