Kaynağa Gözat

DEV: a crtp helper for GPIO pin added

master
ebeveyn
işleme
1537c1e996
2 değiştirilmiş dosya ile 108 ekleme ve 0 silme
  1. +107
    -0
      include/drv/gpio.h
  2. +1
    -0
      include/tbx.h

+ 107
- 0
include/drv/gpio.h Dosyayı Görüntüle

@@ -0,0 +1,107 @@
/*!
* \file drv/gpio.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 TBX_DRV_GPIO_H_
#define TBX_DRV_GPIO_H_

#include <core/core.h>

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 <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 TBX_DRV_STM32GPIO_H_

+ 1
- 0
include/tbx.h Dosyayı Görüntüle

@@ -40,5 +40,6 @@
#include <utils/shared.h>
#include <drv/cli_device.h>
#include <drv/gpio.h>
#endif /* TBX_H_ */

Yükleniyor…
İptal
Kaydet