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

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

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

	//initialize triggers, set data arrays to unknown
	for (i = 0; i<FREQCOUNTER_MAXINPUTS; i++)
	{
		phid->timeout[i] = PUNI_INT;
		phid->frequency[i] = PUNI_INT;
		phid->filterEcho[i] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN;
		phid->enabledEcho[i] = PUNI_BOOL;

		phid->totalCount[i] = 0;
		phid->totalTime[i] = 0;

		phid->countsGood[i] = PFALSE;
	}

	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(FrequencyCounter)
	int i;

	TESTPTR(phid);

	//initialize triggers, set data arrays to unknown
	for (i = 0; i<phid->phid.attr.frequencycounter.numFreqInputs; i++)
	{
		phid->timeout[i] = 1000000; //1 second (in microseconds) - detect down to 1Hz

		phid->frequency[i] = PUNK_INT;
		phid->filterEcho[i] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN;
		phid->enabledEcho[i] = PUNK_BOOL;
	}
	phid->lastPacketCount = PUNK_INT;

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

	//fill in enabledState and filterType, set other things to 0
	for (i = 0; i<phid->phid.attr.frequencycounter.numFreqInputs; i++)
	{
		phid->filter[i] = phid->filterEcho[i];
		phid->enabled[i] = phid->enabledEcho[i];
		
		phid->totalTicksSinceLastCount[i] = PUNK_INT;

		phid->totalCount[i] = 0;
		phid->totalTime[i] = 0;
	}
	phid->lastPacketCount = PUNK_INT;

	return EPHIDGET_OK;
}

//dataInput - parses device packets
CPHIDGETDATA(FrequencyCounter)
	int i, packetCount;
	int ticks, ticksAtLastCount[FREQCOUNTER_MAXINPUTS], counts[FREQCOUNTER_MAXINPUTS];
	char error_buffer[127];

	//Setup max/min values
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_FREQUENCYCOUNTER_2INPUT:
			if(phid->phid.deviceVersion < 200) 
			{
				ticks = buffer[0] + (buffer[1] << 8);

				counts[0] = buffer[2] + (buffer[3] << 8) + (buffer[4] << 16);
				ticksAtLastCount[0] = buffer[5] + (buffer[6] << 8);

				counts[1] = buffer[7] + (buffer[8] << 8) + (buffer[9] << 16);
				ticksAtLastCount[1] = buffer[10] + (buffer[11] << 8);

				//Filter type echo
				if(buffer[12] & FREQCOUNTER_FLAG_CH0_LOGIC)
					phid->filterEcho[0] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL;
				else
					phid->filterEcho[0] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING;
				if(buffer[12] & FREQCOUNTER_FLAG_CH1_LOGIC)
					phid->filterEcho[1] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL;
				else
					phid->filterEcho[1] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING;
				
				//Enabled state echo
				if(buffer[12] & FREQCOUNTER_FLAG_CH0_ENABLE)
					phid->enabledEcho[0] = PTRUE;
				else
					phid->enabledEcho[0] = PFALSE;
				if(buffer[12] & FREQCOUNTER_FLAG_CH1_ENABLE)
					phid->enabledEcho[1] = PTRUE;
				else
					phid->enabledEcho[1] = PFALSE;

				packetCount = (buffer[12] & 0xF0) >> 4;
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}

	if((phid->lastPacketCount != PUNK_INT) && ((phid->lastPacketCount+1)&0x0F) != packetCount)
	{
		FIRE_ERROR_NOQUEUE(EEPHIDGET_PACKETLOST, "One or more data packets were lost");
	}
	phid->lastPacketCount = packetCount;

	for(i=0;i<phid->phid.attr.frequencycounter.numFreqInputs; i++)
	{
		if(phid->enabledEcho[i] == PTRUE && phid->enabled[i] == PTRUE)
		{
			CThread_mutex_lock(&phid->resetlock);
			phid->totalTime[i] += (ticks * FREQCOUNTER_MICROSECONDS_PER_TICK);
			phid->totalCount[i] += counts[i];
			CThread_mutex_unlock(&phid->resetlock);

			if (counts[i] == 0)
			{
				//Do not accumulate if timed out
				if (phid->totalTicksSinceLastCount[i] != PUNK_INT)
					phid->totalTicksSinceLastCount[i] += ticks;

				//only accumulate counts up to timeOut
				if ((phid->totalTicksSinceLastCount[i] * FREQCOUNTER_MICROSECONDS_PER_TICK) > phid->timeout[i])
				{
					phid->frequency[i] = 0;

					//Fire one event with 0 counts to indicate that the Timeout has elapsed and frequency is now 0
					FIRE(Count, i, (phid->totalTicksSinceLastCount[i] * FREQCOUNTER_MICROSECONDS_PER_TICK), 0);
					phid->totalTicksSinceLastCount[i] = PUNK_INT;
				}
			}
			else
			{
				//1st count(s) since a timeout (or 1st read packet since opening)
				//don't try to calculate frequency because we don't to the 'ticks at first count'
				if (phid->totalTicksSinceLastCount[i] == PUNK_INT)
				{
					phid->totalTicksSinceLastCount[i] = ticks - ticksAtLastCount[i];
				}
				else
				{
					int countTimeSpan = (phid->totalTicksSinceLastCount[i] + ticksAtLastCount[i]) * FREQCOUNTER_MICROSECONDS_PER_TICK; //in microseconds
					phid->frequency[i] = (double)((double)counts[i] / ((double)countTimeSpan / 1000000.0));

					FIRE(Count, i, countTimeSpan, counts[i]);

					phid->totalTicksSinceLastCount[i] = ticks - ticksAtLastCount[i];
				}
			}
		}
		else
			phid->frequency[i] = PUNK_DBL;
	}

	return EPHIDGET_OK;
}

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

//Extra things to do during a free
//This is run before the other things that free does
int CPhidgetFrequencyCounter_free(CPhidgetHandle phidG)
{
	CPhidgetFrequencyCounterHandle phid = (CPhidgetFrequencyCounterHandle)phidG;
	CThread_mutex_destroy(&phid->resetlock);
	return EPHIDGET_OK;
}

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

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

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

	//Setup max/min values
	switch(phid->phid.deviceIDSpec)
	{
		case PHIDID_FREQUENCYCOUNTER_2INPUT:
			if(phid->phid.deviceVersion < 200) 
			{
				buffer[0] = 0;
				if(phid->enabled[0] == PTRUE)
					buffer[0] |= FREQCOUNTER_FLAG_CH0_ENABLE;
				else
					phid->enabled[0] = PFALSE;
				if(phid->filter[0] == PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL)
					buffer[0] |= FREQCOUNTER_FLAG_CH0_LOGIC;
				else
					phid->filter[0] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING;

				if(phid->enabled[1] == PTRUE)
					buffer[0] |= FREQCOUNTER_FLAG_CH1_ENABLE;
				else
					phid->enabled[1] = PFALSE;
				if(phid->filter[1] == PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL)
					buffer[0] |= FREQCOUNTER_FLAG_CH1_LOGIC;
				else
					phid->filter[1] = PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING;
			}
			else
				return EPHIDGET_UNEXPECTED;
			break;
		default:
			return EPHIDGET_UNEXPECTED;
	}
	return EPHIDGET_OK;
}

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

//create and initialize a device structure
CCREATE_EXTRA(FrequencyCounter, PHIDCLASS_FREQUENCYCOUNTER)
	CThread_mutex_init(&phid->resetlock);
	phid->phid.fptrFree = CPhidgetFrequencyCounter_free;
	return EPHIDGET_OK;
}

//event setup functions
CFHANDLE(FrequencyCounter, Count, int, int, int)

CGET(FrequencyCounter,FrequencyInputCount,int)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED

	MASGN(phid.attr.frequencycounter.numFreqInputs)
}

CGETINDEX(FrequencyCounter,Frequency,double)
	TESTPTRS(phid,pVal) 
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)
	TESTMASGN(frequency[Index], PUNK_DBL)

	MASGN(frequency[Index])
}

CGETINDEX(FrequencyCounter,TotalTime,__int64)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	MASGN(totalTime[Index])
}

CGETINDEX(FrequencyCounter,TotalCount,__int64)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	MASGN(totalCount[Index])
}

CGETINDEX(FrequencyCounter,TotalTime32,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	*pVal = (int)phid->totalTime[Index]; return EPHIDGET_OK;
}

CGETINDEX(FrequencyCounter,TotalCount32,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	*pVal = (int)phid->totalCount[Index]; return EPHIDGET_OK;
}

CGETINDEX(FrequencyCounter,Timeout,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	MASGN(timeout[Index])
}
CSETINDEX(FrequencyCounter,Timeout,int)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTRANGE(100000, 100000000) //0.1-100 seconds
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Timeout, "%d", timeout);
	else
		SENDPACKET(FrequencyCounter, timeout[Index]);

	return EPHIDGET_OK;
}

CGETINDEX(FrequencyCounter,Enabled,int)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)
	TESTMASGN(enabledEcho[Index], PUNK_BOOL)

	MASGN(enabledEcho[Index])
}
CSETINDEX(FrequencyCounter,Enabled,int)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTRANGE(PFALSE, PTRUE)
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

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

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

	return EPHIDGET_OK;
}

CGETINDEX(FrequencyCounter,Filter,CPhidgetFrequencyCounter_FilterType)
	TESTPTRS(phid,pVal)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)
	TESTMASGN(filterEcho[Index], PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_UNKNOWN)

	MASGN(filterEcho[Index])
}
CSETINDEX(FrequencyCounter,Filter,CPhidgetFrequencyCounter_FilterType)
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTRANGE(PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_ZERO_CROSSING, PHIDGET_FREQUENCYCOUNTER_FILTERTYPE_LOGIC_LEVEL)
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
		ADDNETWORKKEYINDEXED(Filter, "%d", filter);
	else
		SENDPACKET(FrequencyCounter, filter[Index]);

	return EPHIDGET_OK;
}

PHIDGET21_API int CCONV CPhidgetFrequencyCounter_reset(CPhidgetFrequencyCounterHandle phid, int Index)
{
	TESTPTR(phid)
	TESTDEVICETYPE(PHIDCLASS_FREQUENCYCOUNTER)
	TESTATTACHED
	TESTINDEX(phid.attr.frequencycounter.numFreqInputs)

	if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
	{
		int newVal = phid->flip[Index]^1;
		ADDNETWORKKEYINDEXED(Reset, "%d", flip);
	}
	else
	{
		CThread_mutex_lock(&phid->resetlock);
		phid->totalCount[Index] = 0;
		phid->totalTime[Index] = 0;
		CThread_mutex_unlock(&phid->resetlock);
	}

	return EPHIDGET_OK;
}