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

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

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

	phid->accelerationMax = PUNI_DBL;
	phid->accelerationMin = PUNI_DBL;

	for (i = 0; i<ACCEL_MAXAXES; i++)
	{
		phid->axis[i] = PUNI_DBL;
		phid->axisLastTrigger[i] = PUNK_DBL;
		phid->axisChangeTrigger[i] = 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(Accelerometer)
	int i = 0;

	TESTPTR(phid);

	//Setup max/min values
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_ACCELEROMETER_2AXIS:
			if (phid->phid.deviceVersion < 200)
			{
				phid->accelerationMax = 2.1;
				phid->accelerationMin = -2.1;
			}
			else if ((phid->phid.deviceVersion  >= 200) && (phid->phid.deviceVersion  < 300))
			{
				phid->accelerationMax = 10.1;
				phid->accelerationMin = -10.1;
			}
			else if ((phid->phid.deviceVersion  >= 300) && (phid->phid.deviceVersion  < 400))
			{
				phid->accelerationMax = 5.1;
				phid->accelerationMin = -5.1;
			}
			else
				return EPHIDGET_BADVERSION;
			break;
		case PHIDID_ACCELEROMETER_3AXIS:
			if ((phid->phid.deviceVersion  >= 400) && (phid->phid.deviceVersion  < 500))
			{
				phid->accelerationMax = 3.1;
				phid->accelerationMin = -3.1;
			}
			else
				return EPHIDGET_BADVERSION;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	//initialize triggers, set data arrays to unknown
	for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
	{
		phid->axis[i] = PUNK_DBL;
		phid->axisLastTrigger[i] = PUNK_DBL;
		phid->axisChangeTrigger[i] = 0.001;
	}

	//issue one read
	CPhidget_read((CPhidgetHandle)phid);

	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(Accelerometer)
	int i = 0;
	double axis[ACCEL_MAXAXES];

	if (length<0) return EPHIDGET_INVALIDARG;
	TESTPTR(phid);
	TESTPTR(buffer);
	
	ZEROMEM(axis, sizeof(axis));

	//Parse device packets - store data locally
	switch(phidG->deviceIDSpec)
	{
		case PHIDID_ACCELEROMETER_2AXIS:
			if (phidG->deviceVersion < 200)
			{
				int data = 0;
				data = (signed short)((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
				axis[0] = round_double((((double)data-16384) / 2000), 4);
				data = (signed short)((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
				axis[1] = round_double((((double)data-16384) / 2000), 4);
			}
			else if ((phidG->deviceVersion  >= 200) && (phidG->deviceVersion  < 300))
			{
				int data = 0;
				data = (signed short)((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
				axis[0] = round_double((((double)data-16384) / 650), 4);
				data = (signed short)((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
				axis[1] = round_double((((double)data-16384) / 650), 4);
			}
			else if ((phidG->deviceVersion  >= 300) && (phidG->deviceVersion  < 400))
			{
				int data = 0;
				data = ((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
				axis[0] = round_double((((double)(data-32768)) / 4000), 5);
				data = ((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
				axis[1] = round_double((((double)(data-32768)) / 4000), 5);
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		case PHIDID_ACCELEROMETER_3AXIS:
			if ((phidG->deviceVersion  >= 400) && (phidG->deviceVersion  < 500))
			{
				int data = 0;
				data = ((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
				axis[0] = round_double((((double)(data-32768)) / 6553.6), 5);
				data = ((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
				axis[1] = round_double((((double)(data-32768)) / 6553.6), 5);
				data = ((unsigned short)buffer[4]+((unsigned short)buffer[5]<<8));
				axis[2] = round_double((((double)(data-32768)) / 6553.6), 5);
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	//Make sure values are within defined range, and store to structure
	for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
	{
		if(axis[i] > phid->accelerationMax) axis[i] = phid->accelerationMax;
		if(axis[i] < phid->accelerationMin) axis[i] = phid->accelerationMin;
		phid->axis[i] = axis[i];
	}
	
	//send out any events that exceed or match the trigger
	for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
	{
		if (fabs(phid->axis[i] - phid->axisLastTrigger[i]) >= phid->axisChangeTrigger[i]
			|| phid->axisLastTrigger[i] == PUNK_DBL)
		{
			FIRE(AccelerationChange, i, phid->axis[i]);
			phid->axisLastTrigger[i] = phid->axis[i];
		}
	}

	return EPHIDGET_OK;
}

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

	for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
	{
		if(phid->axis[i] != PUNK_DBL)
		{
			FIRE(AccelerationChange, i, phid->axis[i]);
			phid->axisLastTrigger[i] = phid->axis[i];
		}
	}

	return EPHIDGET_OK;
}

//getPacket - not used for accelerometer
CGETPACKET(Accelerometer)
	return EPHIDGET_UNEXPECTED;
}

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

//create and initialize a device structure
CCREATE(Accelerometer, PHIDCLASS_ACCELEROMETER)

//event setup functions
CFHANDLE(Accelerometer, AccelerationChange, int, double)

CGET(Accelerometer,AxisCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED

	MASGN(phid.attr.accelerometer.numAxis)
}

CGETINDEX(Accelerometer,Acceleration,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED
	TESTINDEX(phid.attr.accelerometer.numAxis)
	TESTMASGN(axis[Index], PUNK_DBL)

	MASGN(axis[Index])
}

CGETINDEX(Accelerometer,AccelerationMax,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED
	TESTINDEX(phid.attr.accelerometer.numAxis)
	TESTMASGN(accelerationMax, PUNK_DBL)

	MASGN(accelerationMax)
}

CGETINDEX(Accelerometer,AccelerationMin,double)
	TESTPTRS(phid,pVal) 	
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED
	TESTINDEX(phid.attr.accelerometer.numAxis)
	TESTMASGN(accelerationMin, PUNK_DBL)

	MASGN(accelerationMin)
}

CGETINDEX(Accelerometer,AccelerationChangeTrigger,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED
	TESTINDEX(phid.attr.accelerometer.numAxis)
	TESTMASGN(axisChangeTrigger[Index], PUNK_DBL)

	MASGN(axisChangeTrigger[Index])
}
CSETINDEX(Accelerometer,AccelerationChangeTrigger,double)
	TESTPTR(phid) 
	TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
	TESTATTACHED
	TESTINDEX(phid.attr.accelerometer.numAxis)
	TESTRANGE(0, phid->accelerationMax - phid->accelerationMin)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Trigger, "%lE", axisChangeTrigger);
	else
		phid->axisChangeTrigger[Index] = newVal;

	return EPHIDGET_OK;
}

// === Deprecated Functions === //

CGET(Accelerometer,NumAxis,int)
	return CPhidgetAccelerometer_getAxisCount(phid, pVal);
}