diff --git a/platforms/common/include/px4_platform_common/bitmask.h b/platforms/common/include/px4_platform_common/bitmask.h new file mode 100644 index 0000000000..49e17d1ed0 --- /dev/null +++ b/platforms/common/include/px4_platform_common/bitmask.h @@ -0,0 +1,154 @@ +/**************************************************************************** + * + * Copyright (c) 2020 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file bitmask.h + * @brief Provides SFINAE for type safe templated bitmask operations + * @author Nuno Marques + * + */ + +#pragma once + +#ifdef __cplusplus + +#include + +template +struct enable_bitmask_operators { + static const bool enable = false; +}; + +namespace px4 +{ + +#define ENABLE_BIT_OPERATORS(E) \ + template<> \ + struct enable_bitmask_operators \ + { \ + static const bool enable = true; \ + }; + +template +typename std::enable_if::enable, E>::type +operator==(E lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + return static_cast( + static_cast(lhs) == + static_cast(rhs) + ); +} + +template +typename std::enable_if::enable, E>::type +operator~(E lhs) +{ + typedef typename std::underlying_type::type underlying; + return static_cast( + ~static_cast(lhs) + ); +} + +template +typename std::enable_if::enable, E>::type +operator|(E lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + return static_cast( + static_cast(lhs) | + static_cast(rhs) + ); +} + +template +typename std::enable_if::enable, E &>::type +operator|=(E &lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + lhs = static_cast( + static_cast(lhs) | + static_cast(rhs) + ); + return lhs; +} + +template +typename std::enable_if::enable, E>::type +operator&(E lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + return static_cast( + static_cast(lhs) & + static_cast(rhs) + ); +} + +template +typename std::enable_if::enable, E &>::type +operator&=(E &lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + lhs = static_cast( + static_cast(lhs) & + static_cast(rhs) + ); + return lhs; +} + +template +typename std::enable_if::enable, E>::type +operator^(E lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + return static_cast( + static_cast(lhs) ^ + static_cast(rhs) + ); +} + +template +typename std::enable_if::enable, E &>::type +operator^=(E &lhs, E rhs) +{ + typedef typename std::underlying_type::type underlying; + lhs = static_cast( + static_cast(lhs) ^ + static_cast(rhs) + ); + return lhs; +} + +} /* namespace px4 */ + +#endif /* __cplusplus */ diff --git a/src/modules/simulator/simulator.h b/src/modules/simulator/simulator.h index 53b94e51b3..79f1e45dde 100644 --- a/src/modules/simulator/simulator.h +++ b/src/modules/simulator/simulator.h @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -80,6 +81,34 @@ #include #include +//! Enumeration to use on the bitmask in HIL_SENSOR +enum class SensorSource { + ACCEL = 0b111, + GYRO = 0b111000, + MAG = 0b111000000, + BARO = 0b1101000000000, + DIFF_PRESS = 0b10000000000 +}; +ENABLE_BIT_OPERATORS(SensorSource) + +//! AND operation for the enumeration and unsigned types that returns the bitmask +template +static inline SensorSource operator &(A lhs, B rhs) +{ + // make it type safe + static_assert((std::is_same::value || std::is_same::value), + "first argument is not uint32_t or SensorSource enum type"); + static_assert((std::is_same::value || std::is_same::value), + "second argument is not uint32_t or SensorSource enum type"); + + typedef typename std::underlying_type::type underlying; + + return static_cast( + static_cast(lhs) & + static_cast(rhs) + ); +} + class Simulator : public ModuleParams { public: @@ -247,15 +276,6 @@ private: }; #endif - ///! Enumeration to use on the bitmask in HIL_SENSOR - enum SensorSource { - ACCEL = 0x0007, - GYRO = 0x0038, - MAG = 0x01C0, - BARO = 0x1A00, - DIFF_PRESS = 0x0400 - }; - DEFINE_PARAMETERS( (ParamFloat) _param_sim_bat_drain, ///< battery drain interval (ParamFloat) _battery_min_percentage, //< minimum battery percentage