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
|
//
// Copyright 2010 Ettus Research LLC
//
#include <usrp_uhd/gain_handler.hpp>
#include <usrp_uhd/utils.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <vector>
using namespace usrp_uhd;
/***********************************************************************
* Helper functions and macros
**********************************************************************/
#define GET_PROP_NAMES() \
wax::cast<prop_names_t>((*_wax_obj_ptr)[_gain_names_prop])
/*!
* Helper function to simplify getting a named gain (also min, max, step).
*/
static gain_t get_named_gain(wax::obj::ptr wax_obj_ptr, wax::type prop, std::string name){
return wax::cast<gain_t>((*wax_obj_ptr)[named_prop_t(prop, name)]);
}
/***********************************************************************
* Class methods of gain handler
**********************************************************************/
gain_handler::~gain_handler(void){
/* NOP */
}
void gain_handler::_check_key(const wax::type &key_){
wax::type key; std::string name;
tie(key, name) = extract_named_prop(key_);
try{
//only handle non wildcard names
ASSERT_THROW(name != "");
//only handle these gain props
ASSERT_THROW(
_is_equal(key, _gain_prop) or
_is_equal(key, _gain_min_prop) or
_is_equal(key, _gain_max_prop) or
_is_equal(key, _gain_step_prop)
);
//check that the name is allowed
prop_names_t prop_names = GET_PROP_NAMES();
ASSERT_THROW(not std::has(prop_names.begin(), prop_names.end(), name));
//if we get here, throw an exception
throw std::invalid_argument(str(
boost::format("Unknown gain name %s") % name
));
}
catch(const std::assert_error &){}
}
bool gain_handler::intercept_get(const wax::type &key, wax::type &val){
_check_key(key); //verify the key
// use a vector of tuples to map properties to a reducer function
// we cant use a map because the wax::type cant be sorted
typedef boost::function<gain_t(gain_t, gain_t)> reducer_t;
typedef boost::tuple<wax::type, reducer_t> tuple_t;
reducer_t reducer_sum = boost::bind(std::sum<gain_t>, _1, _2);
reducer_t reducer_max = boost::bind(std::max<gain_t>, _1, _2);
std::vector<tuple_t> prop_to_reducer = boost::assign::tuple_list_of
(_gain_prop, reducer_sum)(_gain_min_prop, reducer_sum)
(_gain_max_prop, reducer_sum)(_gain_step_prop, reducer_max);
/*!
* Handle getting overall gains when a name is not specified.
* For the gain props below, set the overall value and return true.
*/
BOOST_FOREACH(tuple_t p2r, prop_to_reducer){
if (_is_equal(key, p2r.get<0>())){
//form the gains vector from the props vector
prop_names_t prop_names = GET_PROP_NAMES();
std::vector<gain_t> gains(prop_names.size());
std::transform(
prop_names.begin(), prop_names.end(), gains.begin(),
boost::bind(get_named_gain, _wax_obj_ptr, p2r.get<0>(), _1)
);
//reduce across the gain vector
val = std::reduce<gain_t>(gains.begin(), gains.end(), p2r.get<1>());
return true;
}
}
return false;
}
bool gain_handler::intercept_set(const wax::type &key_, const wax::type &val){
_check_key(key_); //verify the key
wax::type key; std::string name;
tie(key, name) = extract_named_prop(key_);
/*!
* Verify that a named gain component is in range.
*/
try{
//only handle the gain props
ASSERT_THROW(_is_equal(key, _gain_prop));
//check that the gain is in range
gain_t gain = wax::cast<gain_t>(val);
gain_t gain_min = get_named_gain(_wax_obj_ptr, _gain_min_prop, name);
gain_t gain_max = get_named_gain(_wax_obj_ptr, _gain_max_prop, name);
ASSERT_THROW(gain > gain_max or gain < gain_min);
//if we get here, throw an exception
throw std::range_error(str(
boost::format("gain %s is out of range of (%f, %f)") % name % gain_min % gain_max
));
}
catch(const std::assert_error &){}
/*!
* Handle setting the overall gain.
*/
if (_is_equal(key, _gain_prop) and name == ""){
gain_t gain = wax::cast<gain_t>(val);
prop_names_t prop_names = GET_PROP_NAMES();
BOOST_FOREACH(std::string name, prop_names){
//get the min, max, step for this gain name
gain_t gain_min = get_named_gain(_wax_obj_ptr, _gain_min_prop, name);
gain_t gain_max = get_named_gain(_wax_obj_ptr, _gain_max_prop, name);
gain_t gain_step = get_named_gain(_wax_obj_ptr, _gain_step_prop, name);
//clip g to be within the allowed range
gain_t g = std::min(std::max(gain, gain_min), gain_max);
//set g to be a multiple of the step size
g -= fmod(g, gain_step);
//set g to be the new gain
(*_wax_obj_ptr)[named_prop_t(_gain_prop, name)] = g;
//subtract g out of the total gain left to apply
gain -= g;
}
return true;
}
return false;
}
|