aboutsummaryrefslogtreecommitdiffstats
path: root/python/wx_gain_panel.py
blob: e76d65379e07089dcc65263751030d51ed4f4d54 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

from gnuradio.gr.pubsub import pubsub
from gnuradio.wxgui import forms


GAIN_KEY = lambda x: 'gain:'+x


class wx_gain_panel(forms.static_box_sizer, pubsub):

	def __init__(self, parent, cobj, chan=0):
		# Super init
		pubsub.__init__(self)
		forms.static_box_sizer.__init__(self,
			parent	= parent,
			label	= "Gain Settings",
			orient	= wx.VERTICAL,
			bold	= True,
		)

		# Save params
		self.parent = parent
		self.cobj = cobj
		self.chan = 0

		# Init gui
		self._init_gui()

	def _init_gui(self):
		# Top spacer
		self.AddSpacer(3);

		# Space each gain
		for g_name in self.cobj.get_gain_names(self.chan):
			# Get range
			g_range = self.cobj.get_gain_range(g_name, self.chan)

			# Skip invalid/non-configurable ones
			if g_range.stop() <= g_range.start():
				continue

			# Current value
			self[GAIN_KEY(g_name)] = self.cobj.get_gain(g_name, self.chan)

			# Subscribe
			self.subscribe(
				GAIN_KEY(g_name),
				lambda gain,name=g_name,self=self: self.set_named_gain(name, gain)
			)

			# Create UI
			g_hbox = wx.BoxSizer(wx.HORIZONTAL)
			self.Add(g_hbox, 0, wx.EXPAND)
			self.AddSpacer(3)

			g_hbox.AddSpacer(3)
			forms.text_box(
				parent		= self.parent,
				sizer		= g_hbox,
				proportion	= 0,
				converter	= forms.float_converter(),
				ps			= self,
				key			= GAIN_KEY(g_name),
				label		= g_name + " Gain (dB)",
			)
			g_hbox.AddSpacer(3)
			forms.slider(
				parent		= self.parent,
				sizer		= g_hbox,
				proportion	= 1,
				ps			= self,
				key			= GAIN_KEY(g_name),
				minimum		= g_range.start(),
				maximum		= g_range.stop(),
				step_size	= g_range.step() or (g_range.stop() - g_range.start()) / 10.0,
			)
			g_hbox.AddSpacer(3)

	def set_named_gain(self, name, gain):
		if gain is None:
			g_range = self.cobj.get_gain_range(name, self.chan)
			self[GAIN_KEY(name)] = (g_range.start() + g_range.stop()) / 2.0
		else:
			cur_gain = self.cobj.get_gain(name, self.chan)
			if cur_gain != gain:
				print "%s %f %f" % (name, cur_gain, gain)
				self[GAIN_KEY(name)] = self.cobj.set_gain(gain, name, self.chan)