aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetbridge.c
blob: fe3211d3836eb36c0a2e1a87622a234e7d5158eb (plain)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <string.h>
#include <assert.h>
#include "stdafx.h"
#include "cphidgetbridge.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"

// === Internal Functions === //

//clearVars - sets all device variables to unknown state
CPHIDGETCLEARVARS(Bridge)
	int i = 0;

	for (i = 0; i<BRIDGE_MAXINPUTS; i++)
	{
		phid->bridgeMin[i] = PUNI_DBL;
		phid->bridgeMax[i] = PUNI_DBL;
		phid->enabledEcho[i] = PUNI_BOOL;
		phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_UNKNOWN;
		phid->bridgeValue[i] = PUNI_DBL;

		phid->enabled[i] = PUNK_BOOL;
		phid->gain[i] = PHIDGET_BRIDGE_GAIN_UNKNOWN;
	}
	phid->dataRateEcho = PUNI_INT;
	phid->dataRateMin = PUNI_INT;
	phid->dataRateMax = PUNI_INT;

	return EPHIDGET_OK;
}

//initAfterOpen - sets up the initial state of an object, reading in packets from the device if needed
//				  used during attach initialization - on every attach
CPHIDGETINIT(Bridge)
	int i;
	TESTPTR(phid);

	//Make sure no old writes are still pending
	phid->outputPacketLen = 0;

	//Setup max/min values
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_BRIDGE_4INPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{
				phid->dataRateMin = 1000;
				phid->dataRateMax = 8;
			}
			else
				return EPHIDGET_BADVERSION;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
	{
		phid->bridgeMin[i] = PUNK_DBL;
		phid->bridgeMax[i] = PUNK_DBL;
		phid->bridgeValue[i] = PUNK_DBL;
		phid->enabledEcho[i] = PUNK_BOOL;
		phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_UNKNOWN;
		phid->outOfRange[i] = PFALSE;
		phid->lastOutOfRange[i] = PFALSE;
	}
	phid->dataRateEcho = PUNK_INT;

	//read in initial state - read until it returns 0
	while(CPhidget_read((CPhidgetHandle)phid)) SLEEP(8);

	//recover what we can, set others to unknown
	for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
	{
		phid->enabled[i] = phid->enabledEcho[i];
		phid->gain[i] = phid->gainEcho[i];
	}
	phid->dataRate = phid->dataRateEcho;

	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(Bridge)
	int i = 0;
	int bridgeRawVal;
	double bridgeVal;
	int dataRateRaw;
	double gain;
	int roundingFactor = 0;
	unsigned char err = PFALSE;
	char error_buffer[200];
	double bridgeval[BRIDGE_MAXINPUTS];

	if (length < 0) return EPHIDGET_INVALIDARG;
	TESTPTR(phid);
	TESTPTR(buffer);

	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_BRIDGE_4INPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{
				//Output Data Rate (Hz) = dataRateRaw * 8
				dataRateRaw = ((buffer[4] & 0x03) << 8) + buffer[5];
				//in ms
				phid->dataRateEcho = dataRateRaw * 8;

				for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
				{
					//gain
					switch(buffer[(i/2)+2] >> (i%2?0:4) & 0x0F)
					{
						case 0x00:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_1;
							gain = 1;
							roundingFactor = 5;
							break;
						case 0x03:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_8;
							gain = 8;
							roundingFactor = 6;
							break;
						case 0x04:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_16;
							gain = 16;
							roundingFactor = 7;
							break;
						case 0x05:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_32;
							gain = 32;
							roundingFactor = 7;
							break;
						case 0x06:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_64;
							gain = 64;
							roundingFactor = 7;
							break;
						case 0x07:
							phid->gainEcho[i] = PHIDGET_BRIDGE_GAIN_128;
							gain = 128;
							roundingFactor = 8;
							break;
					}

					if(gain == 1)
					{
						phid->bridgeMax[i] = 1000;
						phid->bridgeMin[i] = -1000;
					}
					else
					{
						//with PGA, allow +-99.5%, because if doesn't clamp properly
						phid->bridgeMax[i] = 995/gain;
						phid->bridgeMin[i] = -995/gain;
					}

					phid->enabledEcho[i] = (buffer[0] & (0x01 << i)) ? PTRUE : PFALSE;
					if(phid->enabledEcho[i] && (buffer[0] & (0x10 << i))) //enabled and have data
					{
						bridgeRawVal = (buffer[i*3 + 6] << 16) + (buffer[i*3 + 7] << 8) + buffer[i*3 + 8];
						bridgeVal = round_double(((bridgeRawVal - 0x800000) / (double)0x800000) * 1000.0 / (double)gain, roundingFactor);

						err = (buffer[1] & (0x01 << i)) ? PTRUE : PFALSE;

						if(err || bridgeVal > phid->bridgeMax[i] || bridgeVal < phid->bridgeMin[i])
						{
							bridgeval[i] = PUNK_DBL;
							
							if(!phid->lastOutOfRange[i])
							{
								if(bridgeVal > phid->bridgeMax[i])
								{
									FIRE_ERROR(EEPHIDGET_OUTOFRANGE, "Overrange condition detected on input %d, try lowering the gain.",i);
								}
								else if(bridgeVal < phid->bridgeMin[i])
								{
									FIRE_ERROR(EEPHIDGET_OUTOFRANGE, "Underrange condition detected on input %d, try lowering the gain.",i);
								}
							}
							phid->lastOutOfRange[i] = PTRUE;
						}
						else
						{
							if(phid->lastOutOfRange[i])
							{
								FIRE_ERROR(EEPHIDGET_OUTOFRANGE, "Input %d out of range state ended.", i);
							}
							phid->lastOutOfRange[i] = PFALSE;

							bridgeval[i] = bridgeVal;
						}
					}
					else if(!phid->enabledEcho[i])
					{
						bridgeval[i] = PUNK_DBL;
					}
					else
						bridgeval[i] = PUNI_DBL;
				}
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
	{
		if (bridgeval[i] != PUNI_DBL)
		{
			phid->bridgeValue[i] = bridgeval[i];
		}
	}

	//Send out events
	for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
	{
		if (bridgeval[i] != PUNI_DBL &&phid->bridgeValue[i] != PUNK_DBL)
		{
			FIRE(BridgeData, i, phid->bridgeValue[i]);
		}
	}

	return EPHIDGET_OK;
}

//eventsAfterOpen - sends out an event for all valid data, used during attach initialization
CPHIDGETINITEVENTS(Bridge)

	for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
	{
		if (phid->bridgeValue[i] != PUNK_DBL)
		{
			FIRE(BridgeData, i, phid->bridgeValue[i]);
		}
	}

	return EPHIDGET_OK;
}

//getPacket - used by write thread to get the next packet to send to device
CGETPACKET_BUF(Bridge)

//sendpacket - sends a packet to the device asynchronously, blocking if the 1-packet queue is full
CSENDPACKET_BUF(Bridge)

//makePacket - constructs a packet using current device state
CMAKEPACKET(Bridge)
	TESTPTRS(phid, buffer);

	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_BRIDGE_4INPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{
				int i;
				int dataRateRaw;
				int gainRaw;

				//ms->raw
				dataRateRaw = phid->dataRate / 8;

				buffer[0] = 0;
				buffer[1] = 0;
				buffer[2] = 0;
				for (i = 0; i<phid->phid.attr.bridge.numBridgeInputs; i++)
				{
					if(phid->enabled[i])
						buffer[0] |= (0x01<<i);

					switch(phid->gain[i])
					{
						case PHIDGET_BRIDGE_GAIN_1:
						case PHIDGET_BRIDGE_GAIN_UNKNOWN:
							gainRaw = 0x00;
							break;
						case PHIDGET_BRIDGE_GAIN_8:
							gainRaw = 0x03;
							break;
						case PHIDGET_BRIDGE_GAIN_16:
							gainRaw = 0x04;
							break;
						case PHIDGET_BRIDGE_GAIN_32:
							gainRaw = 0x05;
							break;
						case PHIDGET_BRIDGE_GAIN_64:
							gainRaw = 0x06;
							break;
						case PHIDGET_BRIDGE_GAIN_128:
							gainRaw = 0x07;
							break;
					}
					buffer[(i/2)+1] |= gainRaw << (i%2?0:4);
				}
				buffer[3] = dataRateRaw & 0xFF;
				buffer[4] = (dataRateRaw >> 8) & 0x03;

			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	return EPHIDGET_OK;
}

// === Exported Functions === //

//create and initialize a device structure
CCREATE(Bridge, PHIDCLASS_BRIDGE)

//event setup functions
CFHANDLE(Bridge, BridgeData, int, double)

CGET(Bridge,InputCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED

	MASGN(phid.attr.bridge.numBridgeInputs)
}

CGETINDEX(Bridge,BridgeValue,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTINDEX(phid.attr.bridge.numBridgeInputs)
	TESTMASGN(bridgeValue[Index], PUNK_DBL)

	MASGN(bridgeValue[Index])
}

CGETINDEX(Bridge,BridgeMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTINDEX(phid.attr.bridge.numBridgeInputs)
	TESTMASGN(bridgeMax[Index], PUNK_DBL)

	MASGN(bridgeMax[Index])
}

CGETINDEX(Bridge,BridgeMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTINDEX(phid.attr.bridge.numBridgeInputs)
	TESTMASGN(bridgeMin[Index], PUNK_DBL)

	MASGN(bridgeMin[Index])
}

CGETINDEX(Bridge,Enabled,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTINDEX(phid.attr.bridge.numBridgeInputs)
	TESTMASGN(enabledEcho[Index], PUNK_BOOL)

	MASGN(enabledEcho[Index])
}
CSETINDEX(Bridge,Enabled,int)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTRANGE(PFALSE, PTRUE)
	TESTINDEX(phid.attr.bridge.numBridgeInputs)

	if(newVal == PFALSE) phid->bridgeValue[Index] = PUNK_DBL;

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Enabled, "%d", enabled);
	else
		SENDPACKET(Bridge, enabled[Index]);

	return EPHIDGET_OK;
}

CGETINDEX(Bridge,Gain,CPhidgetBridge_Gain)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTINDEX(phid.attr.bridge.numBridgeInputs)
	TESTMASGN(gainEcho[Index], PHIDGET_BRIDGE_GAIN_UNKNOWN)

	MASGN(gainEcho[Index])
}
CSETINDEX(Bridge,Gain,CPhidgetBridge_Gain)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTRANGE(PHIDGET_BRIDGE_GAIN_1, PHIDGET_BRIDGE_GAIN_128)
	TESTINDEX(phid.attr.bridge.numBridgeInputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Gain, "%d", gain);
	else
		SENDPACKET(Bridge, gain[Index]);

	return EPHIDGET_OK;
}

CSET(Bridge,DataRate,int)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTRANGE(phid->dataRateMax, phid->dataRateMin)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEY(DataRate, "%d", dataRate);
	else
		SENDPACKET(Bridge, dataRate);

	return EPHIDGET_OK;
}
CGET(Bridge,DataRate,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTMASGN(dataRateEcho, PUNK_INT)

	MASGN(dataRateEcho)
}

CGET(Bridge,DataRateMax,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTMASGN(dataRateMax, PUNK_INT)

	MASGN(dataRateMax)
}

CGET(Bridge,DataRateMin,int)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_BRIDGE)
	TESTATTACHED
	TESTMASGN(dataRateMin, PUNK_INT)

	MASGN(dataRateMin)
}