-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifer.cs
More file actions
68 lines (53 loc) · 1.68 KB
/
Identifer.cs
File metadata and controls
68 lines (53 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Collections.Generic;
using System.Text;
namespace OneWireAPI
{
public class Identifier
{
public short[] RawId { get; private set; }
public string Name { get; private set; }
public int Family { get; private set; }
public Identifier()
{
// Create a blank ID
RawId = new short[8];
}
public Identifier(IList<byte> deviceId)
{
// Create a blank ID
RawId = new short[8];
// Copy the byte array to the short array
for (var index = 0; index < deviceId.Count; index++)
RawId[index] = deviceId[index];
// Get the friendly name
Name = ConvertToString(RawId);
// Get the family code
Family = RawId[0];
}
public Identifier(short[] deviceId)
{
// Store the ID supplied
RawId = deviceId;
// Get the friendly name
Name = ConvertToString(RawId);
// Get the family code
Family = RawId[0];
}
private static string ConvertToString(IList<short> rawId)
{
var friendlyId = new StringBuilder();
// Loop backwards over the ID array
for (var index = rawId.Count - 1; index >= 0; index--)
{
// Convert the short value into a hex string and append it to the ID string
friendlyId.AppendFormat("{0:X2}", rawId[index]);
}
// Return the ID string
return friendlyId.ToString();
}
public override string ToString()
{
return Name;
}
}
}