Browse Source

WIP: add gpio device to utl

master
parent
commit
45f4155b6d
1 changed files with 107 additions and 0 deletions
  1. +107
    -0
      include/utl/dev/gpio_dev.h

+ 107
- 0
include/utl/dev/gpio_dev.h View File

@@ -0,0 +1,107 @@
/*!
* \file dev/gpio_dev.h
* \brief
* A STM32 gpio wrapper class
*
* \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
*
* <dl class=\"section copyright\"><dt>License</dt><dd>
* 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.
* </dd></dl>
*/

#ifndef utl_dev_gpio_dev_h__
#define utl_dev_gpio_dev_h__

#include <utl/core/impl.h>

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 <typename Impl_t>
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<typename Impl_t>
bool operator<<(bool& lhs, DigitalInOut<Impl_t>& rhs) noexcept {
lhs = rhs.read();
return lhs;
}
}

#endif //#ifndef utl_dev_gpio_dev_h__

Loading…
Cancel
Save