From 45f4155b6de85a607b1c0f9a590c196e9aef13cb Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Mon, 4 Oct 2021 21:18:01 +0300 Subject: [PATCH] WIP: add gpio device to utl --- include/utl/dev/gpio_dev.h | 107 +++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 include/utl/dev/gpio_dev.h diff --git a/include/utl/dev/gpio_dev.h b/include/utl/dev/gpio_dev.h new file mode 100644 index 0000000..0deb4f7 --- /dev/null +++ b/include/utl/dev/gpio_dev.h @@ -0,0 +1,107 @@ +/*! + * \file dev/gpio_dev.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 utl_dev_gpio_dev_h__ +#define utl_dev_gpio_dev_h__ + +#include + +namespace utl { + + /*! + * 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 utl_dev_gpio_dev_h__