Micro template library A library for building device drivers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

93 lines
2.8 KiB

  1. /*!
  2. * \file /utl/com/_1wire_id.h
  3. * \brief An 1-wire Rom ID type
  4. *
  5. * Copyright (C) 2018 Christos Choutouridis
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #ifndef __utl_com_1wire_id_h__
  22. #define __utl_com_1wire_id_h__
  23. #include <utl/core/impl.h>
  24. #include <utl/container/id.h>
  25. namespace utl {
  26. /*!
  27. * 1-wire Rom-ID type
  28. */
  29. class _1wire_id_t : public id_t<uint8_t, 8> {
  30. /*!
  31. * \name Constructors
  32. */
  33. //!@{
  34. public:
  35. _1wire_id_t () noexcept
  36. : id_t<uint8_t, 8> {0, 0, 0, 0, 0, 0, 0, 0} { }
  37. _1wire_id_t (const _1wire_id_t& id) noexcept {
  38. std::copy(id.begin(), id.end(), this->begin());
  39. }
  40. //!@}
  41. /*!
  42. * \name User functionality provided by the interface
  43. */
  44. //!@{
  45. //! Return nullDev reference
  46. static constexpr const _1wire_id_t& nullDev () { return nullDev_; }
  47. //!@{
  48. //! Return the RomID family code (The LSB)
  49. uint8_t& family() noexcept { return front(); }
  50. const uint8_t& family() const noexcept { return front(); }
  51. //!@}
  52. //!@{
  53. //! Access the CRC8 byte (The MSB).
  54. uint8_t& crc8() noexcept { return back(); }
  55. const uint8_t& crc8() const noexcept { return back(); }
  56. //!@}
  57. /*!
  58. * \brief
  59. * Full compare two dev_ids
  60. * \return The comparison result
  61. * \arg 0 dev_ids are equal
  62. * \arg -1 dev_id lhs is smaller than dev_id rhs
  63. * \arg 1 dev_id rhs is smaller than dev_id lhs
  64. */
  65. static int compare (const _1wire_id_t& lhs, const _1wire_id_t& rhs) noexcept {
  66. auto p = std::mismatch (lhs.rbegin(), lhs.rend(), rhs.rbegin(), rhs.rend());
  67. if (p.first == lhs.rend()) return 0;
  68. else if (*p.first < *p.second) return -1;
  69. else return 1;
  70. }
  71. //!@{
  72. //!@{
  73. //! Self referenced special null Rom_ID object as static
  74. private:
  75. static const _1wire_id_t nullDev_;
  76. //!@}
  77. };
  78. // Init static member
  79. const _1wire_id_t _1wire_id_t::nullDev_ { };
  80. } // namespace tbx
  81. #endif /* __utl_com_1wire_id_h__ */