Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
IanBenzMaxim
Date:
Thu Jul 07 13:56:44 2016 -0500
Revision:
103:6dcbb5166da1
Parent:
Slaves/RomId.h@86:2ce08ca58b9e
Child:
139:f0e0a7976846
Consolidate RomId functionality in top-level directory.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 17:b646b1e3970b 1 /******************************************************************//**
j3 17:b646b1e3970b 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
j3 17:b646b1e3970b 3 *
j3 17:b646b1e3970b 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 17:b646b1e3970b 5 * copy of this software and associated documentation files (the "Software"),
j3 17:b646b1e3970b 6 * to deal in the Software without restriction, including without limitation
j3 17:b646b1e3970b 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 17:b646b1e3970b 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 17:b646b1e3970b 9 * Software is furnished to do so, subject to the following conditions:
j3 17:b646b1e3970b 10 *
j3 17:b646b1e3970b 11 * The above copyright notice and this permission notice shall be included
j3 17:b646b1e3970b 12 * in all copies or substantial portions of the Software.
j3 17:b646b1e3970b 13 *
j3 17:b646b1e3970b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 17:b646b1e3970b 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 17:b646b1e3970b 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 17:b646b1e3970b 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 17:b646b1e3970b 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 17:b646b1e3970b 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 17:b646b1e3970b 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 17:b646b1e3970b 21 *
j3 17:b646b1e3970b 22 * Except as contained in this notice, the name of Maxim Integrated
j3 17:b646b1e3970b 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 17:b646b1e3970b 24 * Products, Inc. Branding Policy.
j3 17:b646b1e3970b 25 *
j3 17:b646b1e3970b 26 * The mere transfer of this software does not imply any licenses
j3 17:b646b1e3970b 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 17:b646b1e3970b 28 * trademarks, maskwork rights, or any other form of intellectual
j3 17:b646b1e3970b 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 17:b646b1e3970b 30 * ownership rights.
j3 17:b646b1e3970b 31 **********************************************************************/
j3 17:b646b1e3970b 32
IanBenzMaxim 73:2cecc1372acc 33 #ifndef OneWire_RomId
IanBenzMaxim 73:2cecc1372acc 34 #define OneWire_RomId
j3 17:b646b1e3970b 35
IanBenzMaxim 86:2ce08ca58b9e 36 #include "Utilities/array.h"
IanBenzMaxim 86:2ce08ca58b9e 37 #include "Utilities/crc.h"
j3 17:b646b1e3970b 38
IanBenzMaxim 73:2cecc1372acc 39 namespace OneWire
j3 17:b646b1e3970b 40 {
IanBenzMaxim 73:2cecc1372acc 41 /// Standard container for a 1-Wire ROM ID.
IanBenzMaxim 73:2cecc1372acc 42 class RomId
IanBenzMaxim 73:2cecc1372acc 43 {
IanBenzMaxim 73:2cecc1372acc 44 public:
IanBenzMaxim 73:2cecc1372acc 45 /// Length of the buffer in bytes.
IanBenzMaxim 73:2cecc1372acc 46 static const size_t byteLen = 8;
IanBenzMaxim 73:2cecc1372acc 47
IanBenzMaxim 73:2cecc1372acc 48 /// Built-in array representation.
IanBenzMaxim 73:2cecc1372acc 49 typedef array<uint8_t, byteLen>::Buffer ByteBuffer;
j3 17:b646b1e3970b 50
IanBenzMaxim 73:2cecc1372acc 51 private:
IanBenzMaxim 73:2cecc1372acc 52 static const size_t familyCodeIdx = 0;
IanBenzMaxim 73:2cecc1372acc 53 static const size_t crc8Idx = 7;
IanBenzMaxim 73:2cecc1372acc 54
IanBenzMaxim 73:2cecc1372acc 55 /// Default starting value is all bytes 0x00.
IanBenzMaxim 73:2cecc1372acc 56 static const uint8_t defaultByteVal = 0x00;
j3 17:b646b1e3970b 57
IanBenzMaxim 73:2cecc1372acc 58 array<uint8_t, byteLen> m_romId;
j3 17:b646b1e3970b 59
IanBenzMaxim 73:2cecc1372acc 60 public:
IanBenzMaxim 73:2cecc1372acc 61 RomId() { reset(); }
IanBenzMaxim 73:2cecc1372acc 62 RomId(const RomId & romId) : m_romId(romId.m_romId) { }
IanBenzMaxim 73:2cecc1372acc 63 RomId(const ByteBuffer & romIdBytes) : m_romId(romIdBytes) { }
IanBenzMaxim 62:43039aeca2ab 64
IanBenzMaxim 73:2cecc1372acc 65 const RomId & operator=(const RomId & rhs)
IanBenzMaxim 73:2cecc1372acc 66 {
IanBenzMaxim 73:2cecc1372acc 67 this->m_romId = rhs.m_romId;
IanBenzMaxim 73:2cecc1372acc 68 return rhs;
IanBenzMaxim 73:2cecc1372acc 69 }
IanBenzMaxim 73:2cecc1372acc 70
IanBenzMaxim 73:2cecc1372acc 71 bool operator==(const RomId & rhs) const
IanBenzMaxim 73:2cecc1372acc 72 {
IanBenzMaxim 73:2cecc1372acc 73 return (this->m_romId == rhs.m_romId);
IanBenzMaxim 73:2cecc1372acc 74 }
IanBenzMaxim 73:2cecc1372acc 75
IanBenzMaxim 73:2cecc1372acc 76 bool operator!=(const RomId & rhs) const
IanBenzMaxim 73:2cecc1372acc 77 {
IanBenzMaxim 73:2cecc1372acc 78 return !operator==(rhs);
IanBenzMaxim 73:2cecc1372acc 79 }
IanBenzMaxim 73:2cecc1372acc 80
IanBenzMaxim 73:2cecc1372acc 81 /// Conversion to array reference.
IanBenzMaxim 73:2cecc1372acc 82 operator ByteBuffer &()
IanBenzMaxim 73:2cecc1372acc 83 {
IanBenzMaxim 73:2cecc1372acc 84 return m_romId;
IanBenzMaxim 73:2cecc1372acc 85 }
IanBenzMaxim 73:2cecc1372acc 86
IanBenzMaxim 73:2cecc1372acc 87 /// Conversion to const array reference.
IanBenzMaxim 73:2cecc1372acc 88 operator const ByteBuffer &() const
IanBenzMaxim 73:2cecc1372acc 89 {
IanBenzMaxim 73:2cecc1372acc 90 return m_romId;
IanBenzMaxim 73:2cecc1372acc 91 }
j3 17:b646b1e3970b 92
IanBenzMaxim 73:2cecc1372acc 93 /// Reset to the default starting value.
IanBenzMaxim 73:2cecc1372acc 94 void reset()
IanBenzMaxim 73:2cecc1372acc 95 {
IanBenzMaxim 73:2cecc1372acc 96 std::memset(m_romId, defaultByteVal, byteLen);
IanBenzMaxim 73:2cecc1372acc 97 }
j3 17:b646b1e3970b 98
IanBenzMaxim 73:2cecc1372acc 99 /// Read the Family Code byte.
IanBenzMaxim 73:2cecc1372acc 100 uint8_t familyCode() const
IanBenzMaxim 73:2cecc1372acc 101 {
IanBenzMaxim 73:2cecc1372acc 102 return m_romId[familyCodeIdx];
IanBenzMaxim 73:2cecc1372acc 103 }
IanBenzMaxim 73:2cecc1372acc 104
IanBenzMaxim 73:2cecc1372acc 105 /// Set the family code byte.
IanBenzMaxim 73:2cecc1372acc 106 void setFamilyCode(uint8_t familyCode)
IanBenzMaxim 73:2cecc1372acc 107 {
IanBenzMaxim 73:2cecc1372acc 108 m_romId[familyCodeIdx] = familyCode;
IanBenzMaxim 73:2cecc1372acc 109 }
j3 17:b646b1e3970b 110
IanBenzMaxim 73:2cecc1372acc 111 /// Read the CRC8 byte.
IanBenzMaxim 73:2cecc1372acc 112 uint8_t crc8() const
IanBenzMaxim 73:2cecc1372acc 113 {
IanBenzMaxim 73:2cecc1372acc 114 return m_romId[crc8Idx];
IanBenzMaxim 73:2cecc1372acc 115 }
IanBenzMaxim 73:2cecc1372acc 116
IanBenzMaxim 73:2cecc1372acc 117 /// Set the CRC8 byte.
IanBenzMaxim 73:2cecc1372acc 118 void setCrc8(uint8_t crc8)
IanBenzMaxim 73:2cecc1372acc 119 {
IanBenzMaxim 73:2cecc1372acc 120 m_romId[crc8Idx] = crc8;
IanBenzMaxim 73:2cecc1372acc 121 }
IanBenzMaxim 73:2cecc1372acc 122
IanBenzMaxim 73:2cecc1372acc 123 /// Check if the CRC8 is valid for the ROM ID.
IanBenzMaxim 73:2cecc1372acc 124 /// @returns True if the CRC8 is valid.
IanBenzMaxim 73:2cecc1372acc 125 bool crc8Valid() const
IanBenzMaxim 73:2cecc1372acc 126 {
IanBenzMaxim 82:c11090a32471 127 return (crc::calculateCrc8(m_romId, (byteLen - 1), 0x00) == crc8());
IanBenzMaxim 73:2cecc1372acc 128 }
IanBenzMaxim 73:2cecc1372acc 129
IanBenzMaxim 73:2cecc1372acc 130 /// Calculate and set the CRC8 for the ROM ID.
IanBenzMaxim 73:2cecc1372acc 131 void setValidCrc8()
IanBenzMaxim 73:2cecc1372acc 132 {
IanBenzMaxim 82:c11090a32471 133 setCrc8(crc::calculateCrc8(m_romId, (byteLen - 1), 0x00));
IanBenzMaxim 73:2cecc1372acc 134 }
j3 17:b646b1e3970b 135
IanBenzMaxim 73:2cecc1372acc 136 /// Check if the ROM ID is valid (Family Code and CRC8 are both valid).
IanBenzMaxim 73:2cecc1372acc 137 /// @returns True if the ROM ID is valid.
IanBenzMaxim 73:2cecc1372acc 138 bool valid() const
IanBenzMaxim 73:2cecc1372acc 139 {
IanBenzMaxim 73:2cecc1372acc 140 return (crc8Valid() && (familyCode() != defaultByteVal));
IanBenzMaxim 73:2cecc1372acc 141 }
IanBenzMaxim 73:2cecc1372acc 142 };
IanBenzMaxim 73:2cecc1372acc 143 }
j3 17:b646b1e3970b 144
IanBenzMaxim 49:36954b62f503 145 #endif