/*! * \file drv/gpio.h * \brief * A STM32 gpio wrapper class * * \copyright Copyright (C) 2021 Christos Choutouridis * *
License
* The MIT License (MIT) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *
*/ #ifndef TBX_DRV_GPIO_H_ #define TBX_DRV_GPIO_H_ #include namespace tbx { /*! * CRTP class for gpio digital input-output. * * The derived class requirements are: * - bool read_impl () * - void write_impl(bool) * * \tparam Impl_t The type of derived class */ template class DigitalInOut { public: _CRTP_IMPL(Impl_t); /*! * \name Object lifetime */ //! @{ protected: DigitalInOut() noexcept = default; ~DigitalInOut() = default; DigitalInOut(const DigitalInOut&) = delete; //!< No copy constructions DigitalInOut operator=(const DigitalInOut&) = delete; //!< No copies //! @} //! \name Public interface //! @{ public: //! Reads the state of the pin. This is true for both input and output pins. //! \return The state of the pin bool read () noexcept { return impl().read_impl (); } //! Write a new state to pin. If the pin is set for output, otherwise this state will remain //! to pin registers and reflect to the pin state if we select output mode void write (bool state) noexcept { impl().write_impl(state); } //! Implicit conversion to bool for reading operations operator bool () noexcept { return read(); } //! Stream from bool for write operations DigitalInOut& operator<< (bool state) noexcept { write(state); return *this; } //! Right stream to bool for read operations DigitalInOut& operator>> (bool& state) noexcept { state = read(); return *this; } //! @} }; /*! * This definition enables the "data << pin" syntax for read operation * * \tparam Impl_t The derived class type of the DigitalInOut * * \param lhs Left hand site operand * \param rhs Right hand site operand * \return The read value */ template bool operator<<(bool& lhs, DigitalInOut& rhs) noexcept { lhs = rhs.read(); return lhs; } } #endif //#ifndef TBX_DRV_STM32GPIO_H_