-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWire.java
More file actions
371 lines (322 loc) · 12.3 KB
/
Wire.java
File metadata and controls
371 lines (322 loc) · 12.3 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package org.usfirst.FTC5866.library;
/**
* Created by Olavi Kamppari on 10/25/2015.
*
* Added to Github on 11/16/2015 (https://github.com/OliviliK/FTC_Library/blob/master/Wire.java)
*
* Revisions:
* 151107 by OAK:
* 1) in portIsReady fixed problem related to repeated read of same address
* 2) in executeCommands added missing line to stop idle polling
* 151116 by OAK:
* 1) added method requestCount(), to complement the existing responseCount()
* 2) added method write() with second parameter as object (for no value)
*/
import android.util.Log;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.I2cController;
import com.qualcomm.robotcore.hardware.I2cDevice;
import java.util.concurrent.locks.Lock;
public class Wire implements I2cController.I2cPortReadyCallback {
// --------------------------------- CONSTANTS -------------------------------------------------
// Cache buffer constant values
static final byte
READ_MODE = 0x00 - 128, // MSB = 1
WRITE_MODE = 0x00; // MSB = 0
// Cache buffer index values
static final int
CACHE_MODE = 0, // MSB = 1 when read mode is active
DEV_ADDR = 1, // Device address
REG_NUMBER = 2, // Register address
REG_COUNT = 3, // Register count
DATA_OFFSET = 4, // First byte of transferred data
LAST_INDEX = 29, // Last index available for data
ACTION_FLAG = 31, // 0 = idle, -1 = transfer is active
CACHE_SIZE = 32; // dCache fixed size
// --------------------------------- CLASS VARIABLES -------------------------------------------
private ArrayQueue<Element> downQueue; // Down stream buffer
private ArrayQueue<Element> upQueue; // Up stream buffer
private I2cDevice wireDev; // Generic I2C Device Object
private byte wireDevAddr; // Generic Device Address
private byte[] rCache; // Read Cache
private byte[] wCache; // Write Cache
private Lock rLock; // Lock for Read Cache
private Lock wLock; // Lock for Write Cache & request queue
private byte[] dCache; // Buffer for down stream details
private int dNext; // Next location for incoming bytes
private byte[] uCache; // Buffer for up stream response
private int uNext; // Next location for response bytes
private int uLimit; // Last location for response bytes
private long uMicros; // Time stamp, microseconds since start
private long startTime; // Start time in nanoseconds
private boolean isIdle; // Mechanism to control polling
// --------------------------------- CLASS INIT AND CLOSE ---------------------------------------
public Wire(HardwareMap hardwareMap, String deviceName, int devAddr) {
downQueue = new ArrayQueue<Element>();
upQueue = new ArrayQueue<Element>();
wireDev = hardwareMap.i2cDevice.get(deviceName);
wireDevAddr = (byte) devAddr;
rCache = wireDev.getI2cReadCache();
wCache = wireDev.getI2cWriteCache();
rLock = wireDev.getI2cReadCacheLock();
wLock = wireDev.getI2cWriteCacheLock();
dCache = new byte[CACHE_SIZE];
dNext = DATA_OFFSET;
uCache = new byte[CACHE_SIZE];
uMicros = 0L;
startTime = System.nanoTime();
uNext = DATA_OFFSET;
uLimit = uNext;
isIdle = true;
wireDev.registerForI2cPortReadyCallback(this);
}
public void close() {
wireDev.deregisterForPortReadyCallback();
downQueue.close();
upQueue.close();
wireDev.close();
}
//------------------------------------------------- Public Methods -------------------------
public void beginWrite(int regNumber) {
dCache[CACHE_MODE] = WRITE_MODE;
dCache[DEV_ADDR] = wireDevAddr;
dCache[REG_NUMBER] = (byte) regNumber;
dNext = DATA_OFFSET;
}
public void write(int value) {
if (dNext >= LAST_INDEX) return; // Max write size has been reached
dCache[dNext++] = (byte) value;
}
public void write(int regNumber, int value) {
beginWrite(regNumber);
write(value);
endWrite();
}
public void writeHL(int regNumber, int value) {
beginWrite(regNumber);
write((byte) (value >> 8));
write((byte) (value));
endWrite();
}
public void writeLH(int regNumber, int value) {
beginWrite(regNumber);
write((byte) (value));
write((byte) (value >> 8));
endWrite();
}
public void write(int regNumber, Object x) {
if (x == null) {
beginWrite(regNumber);
endWrite();
}
}
public void endWrite() {
dCache[REG_COUNT] = (byte) (dNext - DATA_OFFSET);
addRequest();
}
public void requestFrom(int regNumber, int regCount) {
dCache[CACHE_MODE] = READ_MODE;
dCache[DEV_ADDR] = wireDevAddr;
dCache[REG_NUMBER] = (byte) regNumber;
dCache[REG_COUNT] = (byte) regCount;
addRequest();
}
public int responseCount() {
int count = 0;
try {
rLock.lock();
count = upQueue.length();
} finally {
rLock.unlock();
}
return count;
}
public int requestCount() {
int count = 0;
try {
wLock.lock();
count = downQueue.length();
} finally {
wLock.unlock();
}
return count;
}
public boolean getResponse() {
boolean responseReceived = false;
uNext = DATA_OFFSET;
uLimit = uNext;
try {
rLock.lock(); // Explicit protection with rLock
if (!upQueue.isEmpty()) {
responseReceived = true;
uMicros = getFromQueue(uCache, upQueue);
uLimit = uNext + uCache[REG_COUNT];
}
} finally {
rLock.unlock();
}
return responseReceived;
}
public boolean isRead() {
return (uCache[CACHE_MODE] == READ_MODE);
}
public boolean isWrite() {
return (uCache[CACHE_MODE] == WRITE_MODE);
}
public int registerNumber() {
return uCache[REG_NUMBER] & 0xff;
}
public int deviceAddress() {
return uCache[DEV_ADDR] & 0xff;
}
public long micros() {
return uMicros;
}
public int available() {
return uLimit - uNext;
}
public int read() {
if (uNext >= uLimit) return 0;
return uCache[uNext++] & 0xff;
}
public int readHL() {
int high = read();
int low = read();
return 256 * high + low;
}
public int readLH() {
int low = read();
int high = read();
return 256 * high + low;
}
//------------------------------------------------- Main routine: Device CallBack -------------
public void portIsReady(int port) {
boolean isValidReply = false;
try {
rLock.lock();
if (
rCache[CACHE_MODE] == wCache[CACHE_MODE] &&
rCache[DEV_ADDR] == wCache[DEV_ADDR] &&
rCache[REG_NUMBER] == wCache[REG_NUMBER] &&
rCache[REG_COUNT] == wCache[REG_COUNT]) {
storeReceivedData(); // Store read/write data
rCache[REG_COUNT] = -1; // Mark the reply used
isValidReply = true;
}
} finally {
rLock.unlock();
}
if (isValidReply) {
executeCommands(); // Start next transmission
} else {
boolean isPollingRequired = false;
try {
wLock.lock(); // Protect the testing
isPollingRequired = (wCache[DEV_ADDR] == wireDevAddr);
} finally {
wLock.unlock();
}
if (isPollingRequired) {
wireDev.readI2cCacheFromController(); // Keep polling active
}
}
}
// --------------------------------- Commands to DIM -------------------------------------------
private void executeCommands() {
try {
wLock.lock();
if (downQueue.isEmpty()) {
isIdle = true;
wCache[DEV_ADDR] = -1; // No further polling is required
} else {
getFromQueue(wCache, downQueue); // Ignore timestamp
wCache[ACTION_FLAG] = -1;
}
} finally {
wLock.unlock();
}
if (!isIdle) {
wireDev.writeI2cCacheToController();
}
}
private void addRequest() {
boolean isStarting = false;
// logCache('d',"addRequest");
try {
wLock.lock();
if (isIdle) {
int length = DATA_OFFSET + dCache[REG_COUNT];
for (int i = 0; i < length; i++) wCache[i] = dCache[i];
wCache[ACTION_FLAG] = -1;
isIdle = false;
isStarting = true;
} else {
addToQueue(0L, dCache, downQueue);
}
} finally {
wLock.unlock();
}
if (isStarting) {
wireDev.writeI2cCacheToController();
}
}
// --------------------------------- PROCESSING OF RECEIVED DATA -------------------------------
private void storeReceivedData() {
// rCache has been locked
long uMicros = (System.nanoTime() - startTime) / 1000L;
addToQueue(uMicros, rCache, upQueue);
// logCache('r',"storeReceivedData");
}
//------------------------------------------------- Add and Remove from Queue ------------------
private void addToQueue(long timeStamp, byte[] cache, ArrayQueue<Element> queue) {
int length = DATA_OFFSET + cache[REG_COUNT];
Element element = new Element();
element.timeStamp = timeStamp;
element.cache = new byte[length];
for (int i = 0; i < length; i++) element.cache[i] = cache[i];
queue.add(element);
}
private long getFromQueue(byte[] cache, ArrayQueue<Element> queue) {
Element element = queue.remove();
if (element == null) return 0;
int length = element.cache.length;
long timeStamp = element.timeStamp;
for (int i = 0; i < length; i++) cache[i] = element.cache[i];
return timeStamp;
}
class Element {
public long timeStamp;
public byte[] cache;
}
private void logCache(char cacheLetter, String function) {
switch (cacheLetter){
case 'd': logCache(dCache,function,cacheLetter); break;
case 'w': logCache(wCache,function,cacheLetter); break;
case 'r': logCache(rCache,function,cacheLetter); break;
case 'u': logCache(uCache,function,cacheLetter); break;
}
}
private void logCache(byte[] cache, String function, char cacheLetter) {
int showCount = cache[3];
boolean isWrite = (cache[0] == WRITE_MODE);
switch (cacheLetter){
case 'd':
case 'w':
if (cache[0] != WRITE_MODE) showCount = 0;
break;
case 'r':
case 'u':
if (cache[0] == WRITE_MODE) showCount = 0;
break;
}
if (showCount > 6) showCount = 6;
String msg = String.format(
"%17s %c[%d][%d], %cCache: mod=0x%02X, dev=0x%02X, reg=0x%02X, cnt=%2d ",
function,isIdle?'T':'F',downQueue.length(),upQueue.length(),
cacheLetter,cache[0],cache[1],cache[2],cache[3]);
for (int i=0;i<showCount;i++) {
msg += String.format(" 0x%02X",cache[4+i]);
}
Log.i("Wire",msg);
}
}