Implementation of 1-Wire with added Alarm Search Functionality

Dependents:   Max32630_One_Wire_Interface

Committer:
j3
Date:
Fri Mar 18 20:21:05 2016 +0000
Revision:
17:b646b1e3970b
Updated library with error codes on return statements

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
j3 17:b646b1e3970b 33
j3 17:b646b1e3970b 34 #ifndef _ROMID_HPP
j3 17:b646b1e3970b 35 #define _ROMID_HPP
j3 17:b646b1e3970b 36
j3 17:b646b1e3970b 37 #include "mbed.h"
j3 17:b646b1e3970b 38 #include <cstdint>
j3 17:b646b1e3970b 39 #include <cstring>
j3 17:b646b1e3970b 40
j3 17:b646b1e3970b 41 class RomId
j3 17:b646b1e3970b 42 {
j3 17:b646b1e3970b 43 public:
j3 17:b646b1e3970b 44 static const std::size_t byteLen = 8;
j3 17:b646b1e3970b 45 typedef std::uint8_t byteBuffer[byteLen];
j3 17:b646b1e3970b 46
j3 17:b646b1e3970b 47 static std::uint8_t calculateCRC8(uint8_t crc8, uint8_t data);
j3 17:b646b1e3970b 48 static std::uint8_t calculateCRC8(const uint8_t * data, size_t data_len, uint8_t crc);
j3 17:b646b1e3970b 49
j3 17:b646b1e3970b 50 private:
j3 17:b646b1e3970b 51 static const std::size_t familyCodeIdx = 0;
j3 17:b646b1e3970b 52 static const std::size_t crc8Idx = 7;
j3 17:b646b1e3970b 53 static const std::uint8_t defaultByteVal = 0x00;
j3 17:b646b1e3970b 54
j3 17:b646b1e3970b 55 byteBuffer m_romId;
j3 17:b646b1e3970b 56
j3 17:b646b1e3970b 57 public:
j3 17:b646b1e3970b 58 const RomId & operator=(const RomId & rhs) {
j3 17:b646b1e3970b 59 std::memcpy(this->m_romId, rhs.m_romId, byteLen);
j3 17:b646b1e3970b 60 return rhs;
j3 17:b646b1e3970b 61 }
j3 17:b646b1e3970b 62 bool operator==(const RomId & rhs) const {
j3 17:b646b1e3970b 63 return (std::memcmp(this->m_romId, rhs.m_romId, byteLen) == 0);
j3 17:b646b1e3970b 64 }
j3 17:b646b1e3970b 65 bool operator!=(const RomId & rhs) const {
j3 17:b646b1e3970b 66 return !operator==(rhs);
j3 17:b646b1e3970b 67 }
j3 17:b646b1e3970b 68 operator byteBuffer &() {
j3 17:b646b1e3970b 69 return m_romId; // Conversion to array reference
j3 17:b646b1e3970b 70 }
j3 17:b646b1e3970b 71 operator const byteBuffer &() const {
j3 17:b646b1e3970b 72 return m_romId; // Conversion to const array reference
j3 17:b646b1e3970b 73 }
j3 17:b646b1e3970b 74
j3 17:b646b1e3970b 75 void reset() {
j3 17:b646b1e3970b 76 std::memset(m_romId, defaultByteVal, byteLen);
j3 17:b646b1e3970b 77 }
j3 17:b646b1e3970b 78
j3 17:b646b1e3970b 79 RomId() {
j3 17:b646b1e3970b 80 reset();
j3 17:b646b1e3970b 81 }
j3 17:b646b1e3970b 82 RomId(const RomId & romId) {
j3 17:b646b1e3970b 83 operator=(romId);
j3 17:b646b1e3970b 84 }
j3 17:b646b1e3970b 85 RomId(const byteBuffer & romIdBytes) {
j3 17:b646b1e3970b 86 std::memcpy(m_romId, romIdBytes, byteLen);
j3 17:b646b1e3970b 87 }
j3 17:b646b1e3970b 88
j3 17:b646b1e3970b 89 std::uint8_t familyCode() const {
j3 17:b646b1e3970b 90 return m_romId[familyCodeIdx];
j3 17:b646b1e3970b 91 }
j3 17:b646b1e3970b 92 void setFamilyCode(std::uint8_t familyCode) {
j3 17:b646b1e3970b 93 m_romId[familyCodeIdx] = familyCode;
j3 17:b646b1e3970b 94 }
j3 17:b646b1e3970b 95
j3 17:b646b1e3970b 96 std::uint8_t crc8() const {
j3 17:b646b1e3970b 97 return m_romId[crc8Idx];
j3 17:b646b1e3970b 98 }
j3 17:b646b1e3970b 99 void setCrc8(std::uint8_t crc8) {
j3 17:b646b1e3970b 100 m_romId[crc8Idx] = crc8;
j3 17:b646b1e3970b 101 }
j3 17:b646b1e3970b 102 bool crc8Valid() const {
j3 17:b646b1e3970b 103 return (calculateCRC8(m_romId, (byteLen - 1), 0x00) == crc8());
j3 17:b646b1e3970b 104 }
j3 17:b646b1e3970b 105 void setValidCrc8() {
j3 17:b646b1e3970b 106 setCrc8(calculateCRC8(m_romId, (byteLen - 1), 0x00));
j3 17:b646b1e3970b 107 }
j3 17:b646b1e3970b 108
j3 17:b646b1e3970b 109 bool valid() const {
j3 17:b646b1e3970b 110 return (crc8Valid() && (familyCode() != defaultByteVal));
j3 17:b646b1e3970b 111 }
j3 17:b646b1e3970b 112 };
j3 17:b646b1e3970b 113
j3 17:b646b1e3970b 114 #endif