Files
OneWireAPI/Identifer.cs
2015-01-16 18:06:47 -05:00

81 lines
2.0 KiB
C#

using System.Text;
namespace OneWireAPI
{
public class Identifier
{
private readonly short[] _rawId; // The raw ID array
private readonly string _friendlyName; // Friendly display name
private readonly int _familyCode; // Family code
public Identifier()
{
// Create a blank ID
_rawId = new short[8];
}
public Identifier(byte[] deviceId)
{
// Create a blank ID
_rawId = new short[8];
// Copy the byte array to the short array
for (var i = 0; i < deviceId.Length; i++)
_rawId[i] = deviceId[i];
// Get the friendly name
_friendlyName = ConvertToString(_rawId);
// Get the family code
_familyCode = _rawId[0];
}
public Identifier(short[] deviceId)
{
// Store the ID supplied
_rawId = deviceId;
// Get the friendly name
_friendlyName = ConvertToString(_rawId);
// Get the family code
_familyCode = _rawId[0];
}
private static string ConvertToString(short[] rawId)
{
var friendlyId = new StringBuilder();
// Loop backwards over the ID array
for (var iIndex = rawId.Length - 1; iIndex >= 0; iIndex--)
{
// Convert the short value into a hex string and append it to the ID string
friendlyId.AppendFormat("{0:X2}", rawId[iIndex]);
}
// Return the ID string
return friendlyId.ToString();
}
public short[] RawId
{
get { return _rawId; }
}
public string Name
{
get { return _friendlyName; }
}
public int Family
{
get { return _familyCode; }
}
public override string ToString()
{
return _friendlyName;
}
}
}