Merge branch 'maint'
[debian/gnuradio] / gr-wxgui / src / python / common.py
1 #
2 # Copyright 2008, 2009 Free Software Foundation, Inc.
3 #
4 # This file is part of GNU Radio
5 #
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)
9 # any later version.
10 #
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.
15 #
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.
20 #
21
22 ##################################################
23 # conditional disconnections of wx flow graph
24 ##################################################
25 import wx
26 from gnuradio import gr
27
28 RUN_ALWAYS = gr.prefs().get_bool ('wxgui', 'run_always', False)
29
30 class wxgui_hb(object):
31         """
32         The wxgui hier block helper/wrapper class:
33         A hier block should inherit from this class to make use of the wxgui connect method.
34         To use, call wxgui_connect in place of regular connect; self.win must be defined.
35         The implementation will conditionally enable the copy block after the source (self).
36         This condition depends on weather or not the window is visible with the parent notebooks.
37         This condition will be re-checked on every ui update event.
38         """
39
40         def wxgui_connect(self, *points):
41                 """
42                 Use wxgui connect when the first point is the self source of the hb.
43                 The win property of this object should be set to the wx window.
44                 When this method tries to connect self to the next point,
45                 it will conditionally make this connection based on the visibility state.
46                 All other points will be connected normally.
47                 """
48                 try:
49                         assert points[0] == self or points[0][0] == self
50                         copy = gr.copy(self._hb.input_signature().sizeof_stream_item(0))
51                         handler = self._handler_factory(copy.set_enabled)
52                         if RUN_ALWAYS == False:
53                                 handler(False) #initially disable the copy block
54                         else:
55                                 handler(True) #initially enable the copy block
56                         self._bind_to_visible_event(win=self.win, handler=handler)
57                         points = list(points)
58                         points.insert(1, copy) #insert the copy block into the chain
59                 except (AssertionError, IndexError): pass
60                 self.connect(*points) #actually connect the blocks
61
62         @staticmethod
63         def _handler_factory(handler):
64                 """
65                 Create a function that will cache the visibility flag,
66                 and only call the handler when that flag changes.
67                 @param handler the function to call on a change
68                 @return a function of 1 argument
69                 """
70                 cache = [None]
71                 def callback(visible):
72                         if cache[0] == visible: return
73                         cache[0] = visible
74                         #print visible, handler
75                         if RUN_ALWAYS == False:
76                                 handler(visible)
77                         else:
78                                 handler(True)
79                 return callback
80
81         @staticmethod
82         def _bind_to_visible_event(win, handler):
83                 """
84                 Bind a handler to a window when its visibility changes.
85                 Specifically, call the handler when the window visibility changes.
86                 This condition is checked on every update ui event.
87                 @param win the wx window
88                 @param handler a function of 1 param
89                 """
90                 #is the window visible in the hierarchy
91                 def is_wx_window_visible(my_win):
92                         while True:
93                                 parent = my_win.GetParent()
94                                 if not parent: return True #reached the top of the hierarchy
95                                 #if we are hidden, then finish, otherwise keep traversing up
96                                 if isinstance(parent, wx.Notebook) and parent.GetCurrentPage() != my_win: return False
97                                 my_win = parent
98                 #call the handler, the arg is shown or not
99                 def handler_factory(my_win, my_handler):
100                         def callback(evt):
101                                 my_handler(is_wx_window_visible(my_win))
102                                 evt.Skip() #skip so all bound handlers are called
103                         return callback
104                 handler = handler_factory(win, handler)
105                 #bind the handler to all the parent notebooks
106                 win.Bind(wx.EVT_UPDATE_UI, handler)
107
108 ##################################################
109 # Helpful Functions
110 ##################################################
111
112 #A macro to apply an index to a key
113 index_key = lambda key, i: "%s_%d"%(key, i+1)
114
115 def _register_access_method(destination, controller, key):
116         """
117         Helper function for register access methods.
118         This helper creates distinct set and get methods for each key
119         and adds them to the destination object.
120         """
121         def set(value): controller[key] = value
122         setattr(destination, 'set_'+key, set)
123         def get(): return controller[key]
124         setattr(destination, 'get_'+key, get) 
125
126 def register_access_methods(destination, controller):
127         """
128         Register setter and getter functions in the destination object for all keys in the controller.
129         @param destination the object to get new setter and getter methods
130         @param controller the pubsub controller
131         """
132         for key in controller.keys(): _register_access_method(destination, controller, key)
133
134 ##################################################
135 # Input Watcher Thread
136 ##################################################
137 from gnuradio import gru
138
139 class input_watcher(gru.msgq_runner):
140         """
141         Input watcher thread runs forever.
142         Read messages from the message queue.
143         Forward messages to the message handler.
144         """
145         def __init__ (self, msgq, controller, msg_key, arg1_key='', arg2_key=''):
146                 self._controller = controller
147                 self._msg_key = msg_key
148                 self._arg1_key = arg1_key
149                 self._arg2_key = arg2_key
150                 gru.msgq_runner.__init__(self, msgq, self.handle_msg)
151
152         def handle_msg(self, msg):
153                 if self._arg1_key: self._controller[self._arg1_key] = msg.arg1()
154                 if self._arg2_key: self._controller[self._arg2_key] = msg.arg2()
155                 self._controller[self._msg_key] = msg.to_string()
156
157
158 ##################################################
159 # Shared Functions
160 ##################################################
161 import numpy
162 import math
163
164 def get_exp(num):
165         """
166         Get the exponent of the number in base 10.
167         @param num the floating point number
168         @return the exponent as an integer
169         """
170         if num == 0: return 0
171         return int(math.floor(math.log10(abs(num))))
172
173 def get_clean_num(num):
174         """
175         Get the closest clean number match to num with bases 1, 2, 5.
176         @param num the number
177         @return the closest number
178         """
179         if num == 0: return 0
180         sign = num > 0 and 1 or -1
181         exp = get_exp(num)
182         nums = numpy.array((1, 2, 5, 10))*(10**exp)
183         return sign*nums[numpy.argmin(numpy.abs(nums - abs(num)))]
184
185 def get_clean_incr(num):
186         """
187         Get the next higher clean number with bases 1, 2, 5.
188         @param num the number
189         @return the next higher number
190         """
191         num = get_clean_num(num)
192         exp = get_exp(num)
193         coeff = int(round(num/10**exp))
194         return {
195                 -5: -2,
196                 -2: -1,
197                 -1: -.5,
198                 1: 2,
199                 2: 5,
200                 5: 10,
201         }[coeff]*(10**exp)
202
203 def get_clean_decr(num):
204         """
205         Get the next lower clean number with bases 1, 2, 5.
206         @param num the number
207         @return the next lower number
208         """
209         num = get_clean_num(num)
210         exp = get_exp(num)
211         coeff = int(round(num/10**exp))
212         return {
213                 -5: -10,
214                 -2: -5,
215                 -1: -2,
216                 1: .5,
217                 2: 1,
218                 5: 2,
219         }[coeff]*(10**exp)
220
221 def get_min_max(samples):
222         """
223         Get the minimum and maximum bounds for an array of samples.
224         @param samples the array of real values
225         @return a tuple of min, max
226         """
227         factor = 2.0
228         mean = numpy.average(samples)
229         std = numpy.std(samples)
230         fft = numpy.abs(numpy.fft.fft(samples - mean))
231         envelope = 2*numpy.max(fft)/len(samples)
232         ampl = max(std, envelope) or 0.1
233         return mean - factor*ampl, mean + factor*ampl
234
235 def get_min_max_fft(fft_samps):
236         """
237         Get the minimum and maximum bounds for an array of fft samples.
238         @param samples the array of real values
239         @return a tuple of min, max
240         """
241         #get the peak level (max of the samples)
242         peak_level = numpy.max(fft_samps)
243         #separate noise samples
244         noise_samps = numpy.sort(fft_samps)[:len(fft_samps)/2]
245         #get the noise floor
246         noise_floor = numpy.average(noise_samps)
247         #get the noise deviation
248         noise_dev = numpy.std(noise_samps)
249         #determine the maximum and minimum levels
250         max_level = peak_level
251         min_level = noise_floor - abs(2*noise_dev)
252         return min_level, max_level