2 # Copyright 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.
22 from gnuradio import eng_notation
25 class abstract_converter(object):
26 def external_to_internal(self, v):
28 Convert from user specified value to value acceptable to underlying primitive.
29 The underlying primitive usually expects strings.
31 raise NotImplementedError
32 def internal_to_external(self, s):
34 Convert from underlying primitive value to user specified value.
35 The underlying primitive usually expects strings.
37 raise NotImplementedError
39 return "Any string is acceptable"
41 class identity_converter(abstract_converter):
42 def external_to_internal(self,v):
44 def internal_to_external(self, s):
47 ########################################################################
48 # Commonly used converters
49 ########################################################################
50 class chooser_converter(abstract_converter):
52 Convert between a set of possible choices and an index.
53 Used in the chooser base and all sub-classes.
55 def __init__(self, choices):
56 #choices must be a list because tuple does not have .index() in python2.5
57 self._choices = list(choices)
58 def external_to_internal(self, choice):
59 return self._choices.index(choice)
60 def internal_to_external(self, index):
61 return self._choices[index]
63 return 'Enter a possible value in choices: "%s"'%str(self._choices)
65 class bool_converter(abstract_converter):
67 The internal representation is boolean.
68 The external representation is specified.
69 Used in the check box form.
71 def __init__(self, true, false):
74 def external_to_internal(self, v):
75 if v == self._true: return True
76 if v == self._false: return False
77 raise Exception, 'Value "%s" is not a possible option.'%v
78 def internal_to_external(self, v):
79 if v: return self._true
80 else: return self._false
82 return "Value must be in (%s, %s)."%(self._true, self._false)
84 class eval_converter(abstract_converter):
86 A catchall converter when int and float are not enough.
87 Evaluate the internal representation with python's eval().
88 Possible uses, set a complex number, constellation points.
91 def __init__(self, formatter=lambda x: '%s'%(x)):
92 self._formatter = formatter
93 def external_to_internal(self, v):
94 return self._formatter(v)
95 def internal_to_external(self, s):
98 return "Value must be evaluatable by python's eval."
100 class str_converter(abstract_converter):
101 def __init__(self, formatter=lambda x: '%s'%(x)):
102 self._formatter = formatter
103 def external_to_internal(self, v):
104 return self._formatter(v)
105 def internal_to_external(self, s):
108 class int_converter(abstract_converter):
109 def __init__(self, formatter=lambda x: '%d'%round(x)):
110 self._formatter = formatter
111 def external_to_internal(self, v):
112 return self._formatter(v)
113 def internal_to_external(self, s):
116 return "Enter an integer. Leading 0x indicates hex"
118 class float_converter(abstract_converter):
119 def __init__(self, formatter=eng_notation.num_to_str):
120 self._formatter = formatter
121 def external_to_internal(self, v):
122 return self._formatter(v)
123 def internal_to_external(self, s):
124 return eng_notation.str_to_num(s)
126 return "Enter a float with optional scale suffix. E.g., 100.1M"
128 class slider_converter(abstract_converter):
130 Scale values to and from the slider.
132 def __init__(self, minimum, maximum, num_steps, cast):
133 assert minimum < maximum
135 self._offset = minimum
136 self._scaler = float(maximum - minimum)/num_steps
138 def external_to_internal(self, v):
139 return (v - self._offset)/self._scaler
140 def internal_to_external(self, v):
141 return self._cast(v*self._scaler + self._offset)
143 return "Value should be within slider range"
145 class log_slider_converter(slider_converter):
146 def __init__(self, min_exp, max_exp, num_steps, base):
147 assert min_exp < max_exp
150 slider_converter.__init__(self, minimum=min_exp, maximum=max_exp, num_steps=num_steps, cast=float)
151 def external_to_internal(self, v):
152 return slider_converter.external_to_internal(self, math.log(v, self._base))
153 def internal_to_external(self, v):
154 return self._base**slider_converter.internal_to_external(self, v)