-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adapter.cs
150 lines (123 loc) · 4.75 KB
/
Adapter.cs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace OneWireAPI
{
public class Adapter
{
private static Session _session;
private static Identifier _lastId;
public static void Initialize(Session session)
{
// Store the session we are dealing with
_session = session;
}
public static void Select(Identifier id)
{
// Set the ID of the device we want to talk to
var result = TMEX.TMRom(_session.SessionHandle, _session.StateBuffer, id.RawId);
// Check the result
if (result != 1)
{
// Throw a ROM exception
throw new OneWireException(OneWireException.ExceptionFunction.Select, id, result);
}
// Copy the ID as the last selected ID
_lastId = id;
// Access the device
Access();
}
public static void Access()
{
// Attempt to access the device
var result = TMEX.TMAccess(_session.SessionHandle, _session.StateBuffer);
// Check to see if we could access the device
if (result != 1)
{
// Throw an access exception
throw new OneWireException(OneWireException.ExceptionFunction.Access, _lastId, result);
}
}
public static short SendBlock(byte[] data, short byteCount)
{
// Send the block and return the result
var result = TMEX.TMBlockStream(_session.SessionHandle, data, byteCount);
// Check to see if the bytes sent matches the value returned
if (result != byteCount)
{
// Throw an access exception
throw new OneWireException(OneWireException.ExceptionFunction.SendBlock, _lastId, result);
}
// Return the result
return result;
}
public static short SendBlock(byte[] data, short byteCount, bool reset)
{
// Send the block and return the result
var result = reset ? TMEX.TMBlockStream(_session.SessionHandle, data, byteCount) : TMEX.TMBlockIO(_session.SessionHandle, data, byteCount);
// Check to see if the bytes sent matches the value returned
if (result != byteCount)
{
// Throw an access exception
throw new OneWireException(OneWireException.ExceptionFunction.SendBlock, _lastId, result);
}
// Return the result
return result;
}
public static short ReadBit()
{
// Send the byte and get back what was sent
var result = TMEX.TMTouchBit(_session.SessionHandle, 0xFF);
// Return the result
return result;
}
public static short SendBit(short output)
{
// Send the byte and get back what was sent
var result = TMEX.TMTouchBit(_session.SessionHandle, output);
// Check that the value was sent correctly
if (result != output)
{
// Throw an exception
throw new OneWireException(OneWireException.ExceptionFunction.SendBit, _lastId);
}
// Return the result
return result;
}
public static short ReadByte()
{
// Send the byte and get back what was sent
var result = TMEX.TMTouchByte(_session.SessionHandle, 0xFF);
// Return the result
return result;
}
public static short Reset()
{
// Reset all devices
return TMEX.TMTouchReset(_session.SessionHandle);
}
public static short SendByte(short output)
{
// Send the byte and get back what was sent
var result = TMEX.TMTouchByte(_session.SessionHandle, output);
// Check that the value was sent correctly
if (result != output)
{
// Throw an exception
throw new OneWireException(OneWireException.ExceptionFunction.SendByte, _lastId);
}
// Return the result
return result;
}
public static short SetLevel(TMEX.LevelOperation nOperation, TMEX.LevelMode nLevelMode, TMEX.LevelPrime nPrimed)
{
// Set the level
var result = TMEX.TMOneWireLevel(_session.SessionHandle, TMEX.LevelOperation.Write, TMEX.LevelMode.Normal, TMEX.LevelPrime.Immediate);
// Check the result
if (result < 0)
{
// Throw an exception
throw new OneWireException(OneWireException.ExceptionFunction.SetLevel, result);
}
// Return the result
return result;
}
}
}