From d5d9da3114bf069c05a8dcb7fca32ccd70405512 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 28 Jan 2010 19:54:55 -0800 Subject: Added gain handler class to manage wildcard gain settings. Gets overall gains and sets overall gains when used. Wild card gain will be a gain with an empty string name. --- include/usrp_uhd/Makefile.am | 1 + include/usrp_uhd/gain_handler.hpp | 88 +++++++++++++++++++++++++++++++++++++++ include/usrp_uhd/props.hpp | 12 ++++++ include/usrp_uhd/utils.hpp | 47 ++++++++++++++++++--- 4 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 include/usrp_uhd/gain_handler.hpp (limited to 'include/usrp_uhd') diff --git a/include/usrp_uhd/Makefile.am b/include/usrp_uhd/Makefile.am index fdb881498..0a19bfe56 100644 --- a/include/usrp_uhd/Makefile.am +++ b/include/usrp_uhd/Makefile.am @@ -10,6 +10,7 @@ this_includedir = $(includedir)/usrp_uhd this_include_HEADERS = \ device.hpp \ device_addr.hpp \ + gain_handler.hpp \ props.hpp \ utils.hpp \ wax.hpp diff --git a/include/usrp_uhd/gain_handler.hpp b/include/usrp_uhd/gain_handler.hpp new file mode 100644 index 000000000..c2f3d0884 --- /dev/null +++ b/include/usrp_uhd/gain_handler.hpp @@ -0,0 +1,88 @@ +// +// Copyright 2010 Ettus Research LLC +// + +#include +#include +#include +#include +#include + +#ifndef INCLUDED_USRP_UHD_GAIN_HANDLER_HPP +#define INCLUDED_USRP_UHD_GAIN_HANDLER_HPP + +namespace usrp_uhd{ + +class gain_handler{ +public: + typedef boost::shared_ptr sptr; + + template gain_handler( + wax::obj::ptr wax_obj_ptr, const T &gain_prop, + const T &gain_min_prop, const T &gain_max_prop, + const T &gain_step_prop, const T &gain_names_prop + ){ + _wax_obj_ptr = wax_obj_ptr; + _gain_prop = gain_prop; + _gain_min_prop = gain_min_prop; + _gain_max_prop = gain_max_prop; + _gain_step_prop = gain_step_prop; + _gain_names_prop = gain_names_prop; + _is_equal = boost::bind(&gain_handler::is_equal, _1, _2); + } + + ~gain_handler(void); + + /*! + * Intercept gets for overall gain, min, max, step. + * Ensures that the gain name is valid. + * \return true for handled, false to pass on + */ + bool intercept_get(const wax::type &key, wax::type &val); + + /*! + * Intercept sets for overall gain. + * Ensures that the gain name is valid. + * Ensures that the new gain is within range. + * \return true for handled, false to pass on + */ + bool intercept_set(const wax::type &key, const wax::type &val); + +private: + + wax::obj::ptr _wax_obj_ptr; + wax::type _gain_prop; + wax::type _gain_min_prop; + wax::type _gain_max_prop; + wax::type _gain_step_prop; + wax::type _gain_names_prop; + + /*! + * Verify that the key is valid: + * If its a named prop for gain, ensure that name is valid. + * If the name if not valid, throw a std::invalid_argument. + * The name can only be valid if its in the list of gain names. + */ + void _check_key(const wax::type &key); + + /* + * Private interface to test if two wax types are equal: + * The constructor will bind an instance of this for a specific type. + * This bound equals functions allows the intercept methods to be non-templated. + */ + template static bool is_equal(const wax::type &a, const wax::type &b){ + try{ + return wax::cast(a) == wax::cast(b); + } + catch(const wax::bad_cast &){ + return false; + } + } + boost::function _is_equal; + +}; + +} //namespace usrp_uhd + +#endif /* INCLUDED_USRP_UHD_GAIN_HANDLER_HPP */ + diff --git a/include/usrp_uhd/props.hpp b/include/usrp_uhd/props.hpp index 426554a53..b74493961 100644 --- a/include/usrp_uhd/props.hpp +++ b/include/usrp_uhd/props.hpp @@ -61,6 +61,18 @@ namespace usrp_uhd{ typedef std::vector prop_names_t; typedef boost::tuple named_prop_t; + /*! + * Utility function to separate a named property into its components. + * \param key a reference to the prop object + * \param name a reference to the name object + */ + inline named_prop_t extract_named_prop(const wax::type &key, const std::string &name = ""){ + if (key.type() == typeid(named_prop_t)){ + return wax::cast(key); + } + return named_prop_t(key, name); + } + /*! * Possible device properties: * In general, a device will have a single mboard. diff --git a/include/usrp_uhd/utils.hpp b/include/usrp_uhd/utils.hpp index a45473ab4..c1b2bad5c 100644 --- a/include/usrp_uhd/utils.hpp +++ b/include/usrp_uhd/utils.hpp @@ -3,15 +3,19 @@ // #include -#include +#include +#include +#include +#include #include +#include #ifndef INCLUDED_USRP_UHD_UTILS_HPP #define INCLUDED_USRP_UHD_UTILS_HPP namespace usrp_uhd{ -template +template //TODO template this better std::vector get_map_keys(const std::map &m){ std::vector v; std::pair p; @@ -21,12 +25,43 @@ std::vector get_map_keys(const std::map &m){ return v; } -//TODO implement a set and get gains that takes a wx obj ptr, and gain properties +} //namespace usrp_uhd + +/*! + * Useful templated functions and classes that I like to pretend are part of stl + */ +namespace std{ -//TODO check name in vector of names + class assert_error : public std::logic_error{ + public: + explicit assert_error(const string& what_arg) : logic_error(what_arg){ + /* NOP */ + } + }; -//TODO optionally extract a name from the named_prop_t + #define ASSERT_THROW(_x) if (not (_x)) { \ + throw std::assert_error("Assertion Failed: " + std::string(#_x)); \ + } -} //namespace usrp_uhd + template + T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){ + T tmp = init; + for ( ; first != last; ++first ){ + tmp = fcn(tmp, *first); + } + return tmp; + } + + template + bool has(InputIterator first, InputIterator last, const T &elem){ + return last != std::find(first, last, elem); + } + + template + T sum(const T &a, const T &b){ + return a + b; + } + +}//namespace std #endif /* INCLUDED_USRP_UHD_UTILS_HPP */ -- cgit v1.2.3