Micro template library A library for building device drivers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

108 lignes
3.6 KiB

  1. /*!
  2. * \file dev/gpio_dev.h
  3. * \brief
  4. * A STM32 gpio wrapper class
  5. *
  6. * \copyright Copyright (C) 2021 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. */
  30. #ifndef utl_dev_gpio_dev_h__
  31. #define utl_dev_gpio_dev_h__
  32. #include <utl/core/impl.h>
  33. namespace utl {
  34. /*!
  35. * CRTP class for gpio digital input-output.
  36. *
  37. * The derived class requirements are:
  38. * - bool read_impl ()
  39. * - void write_impl(bool)
  40. *
  41. * \tparam Impl_t The type of derived class
  42. */
  43. template <typename Impl_t>
  44. class DigitalInOut {
  45. public:
  46. _CRTP_IMPL(Impl_t);
  47. /*!
  48. * \name Object lifetime
  49. */
  50. //! @{
  51. protected:
  52. DigitalInOut() noexcept = default;
  53. ~DigitalInOut() = default;
  54. DigitalInOut(const DigitalInOut&) = delete; //!< No copy constructions
  55. DigitalInOut operator=(const DigitalInOut&) = delete; //!< No copies
  56. //! @}
  57. //! \name Public interface
  58. //! @{
  59. public:
  60. //! Reads the state of the pin. This is true for both input and output pins.
  61. //! \return The state of the pin
  62. bool read () noexcept { return impl().read_impl (); }
  63. //! Write a new state to pin. If the pin is set for output, otherwise this state will remain
  64. //! to pin registers and reflect to the pin state if we select output mode
  65. void write (bool state) noexcept { impl().write_impl(state); }
  66. //! Implicit conversion to bool for reading operations
  67. operator bool () noexcept {
  68. return read();
  69. }
  70. //! Stream from bool for write operations
  71. DigitalInOut& operator<< (bool state) noexcept {
  72. write(state);
  73. return *this;
  74. }
  75. //! Right stream to bool for read operations
  76. DigitalInOut& operator>> (bool& state) noexcept {
  77. state = read();
  78. return *this;
  79. }
  80. //! @}
  81. };
  82. /*!
  83. * This definition enables the "data << pin" syntax for read operation
  84. *
  85. * \tparam Impl_t The derived class type of the DigitalInOut
  86. *
  87. * \param lhs Left hand site operand
  88. * \param rhs Right hand site operand
  89. * \return The read value
  90. */
  91. template<typename Impl_t>
  92. bool operator<<(bool& lhs, DigitalInOut<Impl_t>& rhs) noexcept {
  93. lhs = rhs.read();
  94. return lhs;
  95. }
  96. }
  97. #endif //#ifndef utl_dev_gpio_dev_h__