aboutsummaryrefslogtreecommitdiffstats
path: root/cphidgetanalog.c
blob: d0defaa3a03f5c3ebe4563902081eb1736e4028b (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
#include "stdafx.h"
#include "cphidgetanalog.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"

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

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

	for(i=0;i<ANALOG_MAXOUTPUTS;i++)
	{
		phid->changedVoltage[i] = PUNK_BOOL;
		phid->nextVoltage[i] = PUNK_DBL;

		phid->changedEnabled[i] = PUNK_BOOL;
		phid->nextEnabled[i] = PUNK_BOOL;

		phid->enabled[i] = PUNK_BOOL;
		phid->voltage[i] = PUNK_DBL;

		phid->voltageEcho[i] = PUNI_DBL;
		phid->enabledEcho[i] = PUNI_BOOL;
	}
	phid->voltageMax = PUNI_DBL;
	phid->voltageMin = PUNI_DBL;

	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(Analog)
	int i = 0;

	TESTPTR(phid);

	//set data arrays to unknown
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_ANALOG_4OUTPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{
				for(i=0;i<phid->phid.attr.analog.numAnalogOutputs;i++)
				{
					phid->changedVoltage[i] = PFALSE;
					phid->nextVoltage[i] = PUNK_DBL;

					phid->changedEnabled[i] = PFALSE;
					phid->nextEnabled[i] = PUNK_BOOL;

					phid->enabled[i] = PUNK_BOOL;
					phid->voltage[i] = PUNK_DBL;

					phid->voltageEcho[i] = PUNK_DBL;
					phid->enabledEcho[i] = PUNK_BOOL;

					phid->lastOvercurrent[i] = PFALSE;
				}
				phid->voltageMax = 10;
				phid->voltageMin = -10;

				phid->lastTsd = PFALSE;
			}
			else
				return EPHIDGET_BADVERSION;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}
	phid->controlPacketWaiting = PFALSE;
	
	//issue a read - fill in data
	CPhidget_read((CPhidgetHandle)phid);
	for(i=0;i<phid->phid.attr.analog.numAnalogOutputs;i++)
	{
		phid->voltage[i] = phid->voltageEcho[i];
		phid->enabled[i] = phid->enabledEcho[i];
	}

	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(Analog)
	int i = 0;
	char error_buffer[128];
	unsigned char overcurrent[ANALOG_MAXOUTPUTS] = {PFALSE};
	unsigned char tsd = PFALSE;
	int iVoltage;

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

	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_ANALOG_4OUTPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{
				// Bit-->	|   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |
				//Byte 0	| oc[3] | oc[2] | oc[1] | oc[0] | en[3] | en[2] | en[1] | en[0] |
				//Byte 1	|   0   |   0   |   0   |   0   |   0   |   0   |   0   |  TSD  |
				//Byte 2	|       voltage[0] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 3	|                   voltage[0] bits 11-4                        |
				//Byte 4	|       voltage[1] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 5	|                   voltage[1] bits 11-4                        |
				//Byte 6	|       voltage[2] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 7	|                   voltage[2] bits 11-4                        |
				//Byte 8	|       voltage[3] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 9	|                   voltage[3] bits 11-4                        |
				for (i = 0; i < phid->phid.attr.analog.numAnalogOutputs; i++)
				{
					iVoltage =  (((signed char)buffer[i*2 + 3]) << 4) + (buffer[i*2 + 2] >> 4);
					phid->voltageEcho[i] = round_double((iVoltage * 10 / 2047.0), 3);
					phid->enabledEcho[i] = (buffer[0] & (0x01 << i)) ? PTRUE : PFALSE;
					overcurrent[i] = (buffer[0] & (0x10 << i)) ? PTRUE : PFALSE;
				}
				tsd = buffer[1];
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}
	
	for (i = 0; i < phid->phid.attr.analog.numAnalogOutputs; i++)
	{
		if(overcurrent[i] && !phid->lastOvercurrent[i])
		{
			FIRE_ERROR(EEPHIDGET_OVERCURRENT, "Output %d is trying to draw > 20mA - possible short circuit.", i);
		}
		else if(!overcurrent[i] && phid->lastOvercurrent[i])
		{
			FIRE_ERROR(EEPHIDGET_OK, "Output %d overcurrent state ended.", i);
		}
		phid->lastOvercurrent[i] = overcurrent[i];
	}
	if(tsd && !phid->lastTsd)
	{
		FIRE_ERROR(EEPHIDGET_OVERTEMP, "Thermal shutdown detected. All outputs have been disabled.");
	}
	else if(!tsd && phid->lastTsd)
	{
		FIRE_ERROR(EEPHIDGET_OK, "Thermal shutdown state ended.");
	}
	phid->lastTsd = tsd;

	return EPHIDGET_OK;
}

//eventsAfterOpen - sends out an event for all valid data, used during attach initialization - not used
CPHIDGETINITEVENTS(Analog)
	phid = 0;
	return EPHIDGET_OK;
}

//getPacket - used by write thread to get the next packet to send to device
CGETPACKET(Analog)
	int i = 0;
	int iVoltage;

	CPhidgetAnalogHandle phid = (CPhidgetAnalogHandle)phidG;

	TESTPTRS(phid, buf)
	TESTPTR(lenp)

	if (*lenp < phid->phid.outputReportByteLength)
		return EPHIDGET_INVALIDARG;

	CThread_mutex_lock(&phid->phid.outputLock);
	
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_ANALOG_4OUTPUT:
			if ((phid->phid.deviceVersion >= 100) && (phid->phid.deviceVersion < 200))
			{	
				// Bit-->	|   7   |   6   |   5   |   4   |   3   |   2   |   1   |   0   |
				//Byte 0	|       voltage[0] bits 3-0     | en[3] | en[2] | en[1] | en[0] |
				//Byte 1	|                   voltage[0] bits 11-4                        |
				//Byte 2	|       voltage[1] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 3	|                   voltage[1] bits 11-4                        |
				//Byte 4	|       voltage[2] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 5	|                   voltage[2] bits 11-4                        |
				//Byte 6	|       voltage[3] bits 3-0     |   0   |   0   |   0   |   0   |
				//Byte 7	|                   voltage[3] bits 11-4                        |
				for (i = 0; i < phid->phid.attr.analog.numAnalogOutputs; i++)
				{
					if (phid->changedVoltage[i]) {
						phid->voltage[i] = phid->nextVoltage[i];
						phid->changedVoltage[i] = PFALSE;
						phid->nextVoltage[i] = PUNK_DBL;
					}
					if (phid->changedEnabled[i]) {
						phid->enabled[i] = phid->nextEnabled[i];
						phid->changedEnabled[i] = PFALSE;
						phid->nextEnabled[i] = PUNK_BOOL;
					}
					//fill in buffer
					iVoltage = round(phid->voltage[i] * 2047 / 10.0);
					buf[i*2] = iVoltage << 4;
					buf[i*2 + 1] = iVoltage >> 4;
					if(phid->enabled[i])
						buf[0] |= 1<<i;
				}
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	*lenp = phid->phid.outputReportByteLength;

	CThread_mutex_unlock(&phid->phid.outputLock);

	return EPHIDGET_OK;
}

//sends a packet to the device asynchronously, blocking if the 1-packet queue is full
//	-every channel has its own 1 state mini-queue
static int CCONV CPhidgetAnalog_sendPacket_setVoltage(CPhidgetAnalogHandle phid, unsigned int index, double voltage)
{
	int waitReturn;
	CThread_mutex_lock(&phid->phid.writelock);
again:
	if (!CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_ATTACHED_FLAG))
	{
		CThread_mutex_unlock(&phid->phid.writelock);
		return EPHIDGET_NOTATTACHED;
	}
	CThread_mutex_lock(&phid->phid.outputLock);
	//if we have already requested a change on this channel
	if (phid->changedVoltage[index]) {
		//and it was different then this time
		if (phid->nextVoltage[index] != voltage) {
			CThread_mutex_unlock(&phid->phid.outputLock);
			//then wait for it to get written
			waitReturn = CThread_wait_on_event(&phid->phid.writtenEvent, 2500);
			switch(waitReturn)
			{
			case WAIT_OBJECT_0:
				break;
			case WAIT_ABANDONED:
				CThread_mutex_unlock(&phid->phid.writelock);
				return EPHIDGET_UNEXPECTED;
			case WAIT_TIMEOUT:
				CThread_mutex_unlock(&phid->phid.writelock);
				return EPHIDGET_TIMEOUT;
			}
			//and try again
			goto again;
		} else {
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_mutex_unlock(&phid->phid.writelock);
			return EPHIDGET_OK;
		}
	//otherwise
	} else {
		//if it's different then current, queue it up
		if (phid->voltage[index] != voltage) {
			phid->changedVoltage[index] = PTRUE;
			phid->nextVoltage[index] = voltage;
			CThread_reset_event(&phid->phid.writtenEvent);
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_set_event(&phid->phid.writeAvailableEvent);
		}
		//if it's the same, just return
		else
		{
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_mutex_unlock(&phid->phid.writelock);
			return EPHIDGET_OK;
		}
	}
	CThread_mutex_unlock(&phid->phid.writelock);
	return EPHIDGET_OK;
}

//sends a packet to the device asynchronously, blocking if the 1-packet queue is full
//	-every channel has its own 1 state mini-queue
static int CCONV CPhidgetAnalog_sendPacket_setEnabled(CPhidgetAnalogHandle phid, unsigned int index, int enabled)
{
	int waitReturn;
	CThread_mutex_lock(&phid->phid.writelock);
again:
	if (!CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_ATTACHED_FLAG))
	{
		CThread_mutex_unlock(&phid->phid.writelock);
		return EPHIDGET_NOTATTACHED;
	}
	CThread_mutex_lock(&phid->phid.outputLock);
	//if we have already requested a change on this channel
	if (phid->changedEnabled[index]) {
		//and it was different then this time
		if (phid->nextEnabled[index] != enabled) {
			CThread_mutex_unlock(&phid->phid.outputLock);
			//then wait for it to get written
			waitReturn = CThread_wait_on_event(&phid->phid.writtenEvent, 2500);
			switch(waitReturn)
			{
			case WAIT_OBJECT_0:
				break;
			case WAIT_ABANDONED:
				CThread_mutex_unlock(&phid->phid.writelock);
				return EPHIDGET_UNEXPECTED;
			case WAIT_TIMEOUT:
				CThread_mutex_unlock(&phid->phid.writelock);
				return EPHIDGET_TIMEOUT;
			}
			//and try again
			goto again;
		} else {
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_mutex_unlock(&phid->phid.writelock);
			return EPHIDGET_OK;
		}
	//otherwise
	} else {
		//if it's different then current, queue it up
		if (phid->enabled[index] != enabled) {
			phid->changedEnabled[index] = PTRUE;
			phid->nextEnabled[index] = enabled;
			CThread_reset_event(&phid->phid.writtenEvent);
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_set_event(&phid->phid.writeAvailableEvent);
		}
		//if it's the same, just return
		else
		{
			CThread_mutex_unlock(&phid->phid.outputLock);
			CThread_mutex_unlock(&phid->phid.writelock);
			return EPHIDGET_OK;
		}
	}
	CThread_mutex_unlock(&phid->phid.writelock);
	return EPHIDGET_OK;
}

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

//create and initialize a device structure
CCREATE(Analog, PHIDCLASS_ANALOG)

CGET(Analog,OutputCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED

	MASGN(phid.attr.analog.numAnalogOutputs)
}

CGETINDEX(Analog,Voltage,double)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTINDEX(phid.attr.analog.numAnalogOutputs)
	TESTMASGN(voltageEcho[Index], PUNK_DBL)

	MASGN(voltageEcho[Index])
}

CSETINDEX(Analog,Voltage,double)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTRANGE(phid->voltageMin, phid->voltageMax)
	TESTINDEX(phid.attr.analog.numAnalogOutputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Voltage, "%lf", voltage);
	else
		return CPhidgetAnalog_sendPacket_setVoltage(phid, Index, newVal);

	return EPHIDGET_OK;
}

CGETINDEX(Analog,VoltageMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTINDEX(phid.attr.analog.numAnalogOutputs)
	TESTMASGN(voltageMax, PUNK_DBL)

	MASGN(voltageMax)
}

CGETINDEX(Analog,VoltageMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTINDEX(phid.attr.analog.numAnalogOutputs)
	TESTMASGN(voltageMin, PUNK_DBL)

	MASGN(voltageMin)
}

CGETINDEX(Analog,Enabled,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTINDEX(phid.attr.analog.numAnalogOutputs)
	TESTMASGN(enabledEcho[Index], PUNK_BOOL)

	MASGN(enabledEcho[Index])
}
CSETINDEX(Analog,Enabled,int)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_ANALOG)
	TESTATTACHED
	TESTRANGE(PFALSE, PTRUE)
	TESTINDEX(phid.attr.analog.numAnalogOutputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Enabled, "%d", enabled);
	else
		return CPhidgetAnalog_sendPacket_setEnabled(phid, Index, newVal);

	return EPHIDGET_OK;
}