OneWire Library for Arduino  version: 1.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
RomId.h
Go to the documentation of this file.
1 /******************************************************************/
33 #ifndef OneWire_RomId
34 #define OneWire_RomId
35 
36 #include <string.h>
37 #include "Utilities/array.h"
38 #include "Utilities/crc.h"
39 
40 namespace OneWire
41 {
43  class RomId
44  {
45  public:
47  static const size_t byteLen = 8;
48 
51 
52  private:
53  static const size_t familyCodeIdx = 0;
54  static const size_t crc8Idx = 7;
55 
57  static const uint8_t defaultByteVal = 0x00;
58 
60 
61  public:
62  RomId() { reset(); }
63  RomId(const RomId & romId) : m_romId(romId.m_romId) { }
64  RomId(const ByteBuffer & romIdBytes) : m_romId(romIdBytes) { }
65 
66  const RomId & operator=(const RomId & rhs)
67  {
68  this->m_romId = rhs.m_romId;
69  return rhs;
70  }
71 
72  bool operator==(const RomId & rhs) const
73  {
74  return (this->m_romId == rhs.m_romId);
75  }
76 
77  bool operator!=(const RomId & rhs) const
78  {
79  return !operator==(rhs);
80  }
81 
83  operator ByteBuffer &()
84  {
85  return m_romId;
86  }
87 
89  operator const ByteBuffer &() const
90  {
91  return m_romId;
92  }
93 
95  void reset()
96  {
97  memset(m_romId, defaultByteVal, byteLen);
98  }
99 
101  uint8_t familyCode() const
102  {
103  return m_romId[familyCodeIdx];
104  }
105 
107  void setFamilyCode(uint8_t familyCode)
108  {
109  m_romId[familyCodeIdx] = familyCode;
110  }
111 
113  uint8_t crc8() const
114  {
115  return m_romId[crc8Idx];
116  }
117 
119  void setCrc8(uint8_t crc8)
120  {
121  m_romId[crc8Idx] = crc8;
122  }
123 
126  bool crc8Valid() const
127  {
128  return (crc::calculateCrc8(m_romId, (byteLen - 1), 0x00) == crc8());
129  }
130 
133  {
134  setCrc8(crc::calculateCrc8(m_romId, (byteLen - 1), 0x00));
135  }
136 
139  bool valid() const
140  {
141  return (crc8Valid() && (familyCode() != defaultByteVal));
142  }
143  };
144 }
145 
146 #endif
uint8_t calculateCrc8(uint8_t crc8, uint8_t data)
Definition: crc.cpp:39
Standard container for a 1-Wire ROM ID.
Definition: RomId.h:43
bool operator!=(const RomId &rhs) const
Definition: RomId.h:77
void setFamilyCode(uint8_t familyCode)
Set the family code byte.
Definition: RomId.h:107
RomId(const ByteBuffer &romIdBytes)
Definition: RomId.h:64
bool valid() const
Definition: RomId.h:139
bool crc8Valid() const
Definition: RomId.h:126
RomId()
Definition: RomId.h:62
const RomId & operator=(const RomId &rhs)
Definition: RomId.h:66
void setCrc8(uint8_t crc8)
Set the CRC8 byte.
Definition: RomId.h:119
bool operator==(const RomId &rhs) const
Definition: RomId.h:72
Definition: DS2484.h:41
array< uint8_t, byteLen >::Buffer ByteBuffer
Built-in array representation.
Definition: RomId.h:50
uint8_t familyCode() const
Read the Family Code byte.
Definition: RomId.h:101
RomId(const RomId &romId)
Definition: RomId.h:63
uint8_t crc8() const
Read the CRC8 byte.
Definition: RomId.h:113
void setValidCrc8()
Calculate and set the CRC8 for the ROM ID.
Definition: RomId.h:132
Generic array class similar to std::array.
Definition: array.h:43
static const size_t byteLen
Length of the buffer in bytes.
Definition: RomId.h:47
void reset()
Reset to the default starting value.
Definition: RomId.h:95