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
|
#include "stdafx.h"
#include "cphidgetweightsensor.h"
#include "cusb.h"
#include "csocket.h"
#include "cthread.h"
#include <math.h>
// === Internal Functions === //
//clearVars - sets all device variables to unknown state
CPHIDGETCLEARVARS(WeightSensor)
phid->Weight = PUNI_DBL;
phid->lastweight = PUNK_DBL;
phid->WeightChangeTrigger = 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(WeightSensor)
TESTPTR(phid);
//initialize triggers, set data arrays to unknown
phid->Weight = PUNK_DBL;
phid->lastweight = PUNK_DBL;
phid->WeightChangeTrigger = 0.1;
//issue one read
CPhidget_read((CPhidgetHandle)phid);
return EPHIDGET_OK;
}
//dataInput - parses device packets
CPHIDGETDATA(WeightSensor)
double Weight = 0;
if (length<0) return EPHIDGET_INVALIDARG;
TESTPTR(phid);
TESTPTR(buffer);
//Parse device packets - store data locally
switch(phid->phid.deviceIDSpec)
{
case PHIDID_WEIGHTSENSOR:
Weight = (((double)((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8))) / 100.0 ) - 40.0;
Weight = round_double(Weight, 2);
break;
default:
return EPHIDGET_UNEXPECTED;
}
//store to structure
phid->Weight = Weight;
//send out any events that exceed or match the trigger
if (fabs(phid->Weight - phid->lastweight) >= phid->WeightChangeTrigger || phid->lastweight == PUNK_DBL)
{
FIRE(WeightChange, phid->Weight);
phid->lastweight = phid->Weight;
}
return EPHIDGET_OK;
}
//eventsAfterOpen - sends out an event for all valid data, used during attach initialization
CPHIDGETINITEVENTS(WeightSensor)
if (phid->Weight != PUNK_DBL)
{
FIRE(WeightChange, phid->Weight);
phid->lastweight = phid->Weight;
}
return EPHIDGET_OK;
}
//getPacket - not used for WeightSensor
CGETPACKET(WeightSensor)
return EPHIDGET_UNEXPECTED;
}
// === Exported Functions === //
//create and initialize a device structure
CCREATE(WeightSensor, PHIDCLASS_WEIGHTSENSOR)
//event setup functions
CFHANDLE(WeightSensor, WeightChange, double)
CGET(WeightSensor,Weight,double)
TESTPTRS(phid,pVal)
TESTDEVICETYPE(PHIDCLASS_WEIGHTSENSOR)
TESTATTACHED
TESTMASGN(Weight, PUNK_DBL)
MASGN(Weight)
}
CGET(WeightSensor,WeightChangeTrigger,double)
TESTPTRS(phid,pVal)
TESTDEVICETYPE(PHIDCLASS_WEIGHTSENSOR)
TESTATTACHED
TESTMASGN(WeightChangeTrigger, PUNK_DBL)
MASGN(WeightChangeTrigger)
}
CSET(WeightSensor,WeightChangeTrigger,double)
TESTPTR(phid)
TESTDEVICETYPE(PHIDCLASS_WEIGHTSENSOR)
TESTATTACHED
TESTRANGE(0, 300)
if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
ADDNETWORKKEY(Trigger, "%lE", WeightChangeTrigger);
else
phid->WeightChangeTrigger = newVal;
return EPHIDGET_OK;
}
|