Implementation of 1-Wire with added Alarm Search Functionality
Dependents: Max32630_One_Wire_Interface
Diff: RomId.hpp
- Revision:
- 49:36954b62f503
- Parent:
- 33:a4c015046956
- Child:
- 62:43039aeca2ab
--- a/RomId.hpp Thu Apr 07 11:26:20 2016 -0500 +++ b/RomId.hpp Fri Apr 08 16:11:16 2016 -0500 @@ -38,41 +38,70 @@ #include "array.hpp" +/// Standard container for a 1-Wire ROM ID. class RomId { public: + /// Length of the buffer in bytes. static const std::size_t byteLen = 8; + + /// Built-in array representation. typedef array<std::uint8_t, byteLen>::Buffer ByteBuffer; + /// Perform a CRC8 calculation. + /// @param crc8 Beginning state of the CRC generator. + /// @param data Data to pass though the CRC generator. + /// @returns The calculated CRC8. static std::uint8_t calculateCRC8(std::uint8_t crc8, std::uint8_t data); + + /// Perform a CRC8 calculation with variable length data. + /// @param[in] data Data array to pass through the CRC generator. + /// @param data_len Length of the data array to process. + /// @param crc Beginning state of the CRC generator. + /// @returns The calculated CRC8. static std::uint8_t calculateCRC8(const std::uint8_t * data, std::size_t data_len, std::uint8_t crc = 0); private: static const std::size_t familyCodeIdx = 0; static const std::size_t crc8Idx = 7; + + /// Default starting value is all bytes 0x00. static const std::uint8_t defaultByteVal = 0x00; array<std::uint8_t, byteLen> m_romId; public: - const RomId & operator=(const RomId & rhs) { + const RomId & operator=(const RomId & rhs) + { this->m_romId = rhs.m_romId; return rhs; } - bool operator==(const RomId & rhs) const { + + bool operator==(const RomId & rhs) const + { return (this->m_romId == rhs.m_romId); } - bool operator!=(const RomId & rhs) const { + + bool operator!=(const RomId & rhs) const + { return !operator==(rhs); } - operator ByteBuffer &() { - return m_romId; // Conversion to array reference + + /// Conversion to array reference. + operator ByteBuffer &() + { + return m_romId; } - operator const ByteBuffer &() const { - return m_romId; // Conversion to const array reference + + /// Conversion to const array reference. + operator const ByteBuffer &() const + { + return m_romId; } - void reset() { + /// Reset to the default starting value. + void reset() + { std::memset(m_romId, defaultByteVal, byteLen); } @@ -80,29 +109,49 @@ RomId(const RomId & romId) : m_romId(romId.m_romId) { } RomId(const ByteBuffer & romIdBytes) : m_romId(romIdBytes) { } - std::uint8_t familyCode() const { + /// Read the Family Code byte. + std::uint8_t familyCode() const + { return m_romId[familyCodeIdx]; } - void setFamilyCode(std::uint8_t familyCode) { + + /// Set the family code byte. + void setFamilyCode(std::uint8_t familyCode) + { m_romId[familyCodeIdx] = familyCode; } - std::uint8_t crc8() const { + /// Read the CRC8 byte. + std::uint8_t crc8() const + { return m_romId[crc8Idx]; } - void setCrc8(std::uint8_t crc8) { + + /// Set the CRC8 byte. + void setCrc8(std::uint8_t crc8) + { m_romId[crc8Idx] = crc8; } - bool crc8Valid() const { + + /// Check if the CRC8 is valid for the ROM ID. + /// @returns True if the CRC8 is valid. + bool crc8Valid() const + { return (calculateCRC8(m_romId, (byteLen - 1), 0x00) == crc8()); } - void setValidCrc8() { + + /// Calculate and set the CRC8 for the ROM ID. + void setValidCrc8() + { setCrc8(calculateCRC8(m_romId, (byteLen - 1), 0x00)); } - bool valid() const { + /// Check if the ROM ID is valid (Family Code and CRC8 are both valid). + /// @returns True if the ROM ID is valid. + bool valid() const + { return (crc8Valid() && (familyCode() != defaultByteVal)); } }; -#endif \ No newline at end of file +#endif