cphidgetaccelerometer.c
Go to the documentation of this file.
00001 #include "stdafx.h"
00002 #include "cphidgetaccelerometer.h"
00003 #include "cusb.h"
00004 #include "math.h"
00005 #include "csocket.h"
00006 #include "cthread.h"
00007 
00008 // === Internal Functions === //
00009 
00010 //clearVars - sets all device variables to unknown state
00011 CPHIDGETCLEARVARS(Accelerometer)
00012         int i = 0;
00013 
00014         phid->accelerationMax = PUNI_DBL;
00015         phid->accelerationMin = PUNI_DBL;
00016 
00017         for (i = 0; i<ACCEL_MAXAXES; i++)
00018         {
00019                 phid->axis[i] = PUNI_DBL;
00020                 phid->axisLastTrigger[i] = PUNK_DBL;
00021                 phid->axisChangeTrigger[i] = PUNI_DBL;
00022         }
00023         return EPHIDGET_OK;
00024 }
00025 
00026 //initAfterOpen - sets up the initial state of an object, reading in packets from the device if needed
00027 //                                used during attach initialization - on every attach
00028 CPHIDGETINIT(Accelerometer)
00029         int i = 0;
00030 
00031         TESTPTR(phid);
00032 
00033         //Setup max/min values
00034         switch(phid->phid.deviceIDSpec)
00035         {
00036                 case PHIDID_ACCELEROMETER_2AXIS:
00037                         if (phid->phid.deviceVersion < 200)
00038                         {
00039                                 phid->accelerationMax = 2.1;
00040                                 phid->accelerationMin = -2.1;
00041                         }
00042                         else if ((phid->phid.deviceVersion  >= 200) && (phid->phid.deviceVersion  < 300))
00043                         {
00044                                 phid->accelerationMax = 10.1;
00045                                 phid->accelerationMin = -10.1;
00046                         }
00047                         else if ((phid->phid.deviceVersion  >= 300) && (phid->phid.deviceVersion  < 400))
00048                         {
00049                                 phid->accelerationMax = 5.1;
00050                                 phid->accelerationMin = -5.1;
00051                         }
00052                         else
00053                                 return EPHIDGET_BADVERSION;
00054                         break;
00055                 case PHIDID_ACCELEROMETER_3AXIS:
00056                         if ((phid->phid.deviceVersion  >= 400) && (phid->phid.deviceVersion  < 500))
00057                         {
00058                                 phid->accelerationMax = 3.1;
00059                                 phid->accelerationMin = -3.1;
00060                         }
00061                         else
00062                                 return EPHIDGET_BADVERSION;
00063                         break;
00064                 default:
00065                         return EPHIDGET_UNEXPECTED;
00066         }
00067 
00068         //initialize triggers, set data arrays to unknown
00069         for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
00070         {
00071                 phid->axis[i] = PUNK_DBL;
00072                 phid->axisLastTrigger[i] = PUNK_DBL;
00073                 phid->axisChangeTrigger[i] = 0.001;
00074         }
00075 
00076         //issue one read
00077         CPhidget_read((CPhidgetHandle)phid);
00078 
00079         return EPHIDGET_OK;
00080 }
00081 
00082 //dataInput - parses device packets
00083 CPHIDGETDATA(Accelerometer)
00084         int i = 0;
00085         double axis[ACCEL_MAXAXES];
00086 
00087         if (length<0) return EPHIDGET_INVALIDARG;
00088         TESTPTR(phid);
00089         TESTPTR(buffer);
00090         
00091         ZEROMEM(axis, sizeof(axis));
00092 
00093         //Parse device packets - store data locally
00094         switch(phidG->deviceIDSpec)
00095         {
00096                 case PHIDID_ACCELEROMETER_2AXIS:
00097                         if (phidG->deviceVersion < 200)
00098                         {
00099                                 int data = 0;
00100                                 data = (signed short)((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
00101                                 axis[0] = round_double((((double)data-16384) / 2000), 4);
00102                                 data = (signed short)((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
00103                                 axis[1] = round_double((((double)data-16384) / 2000), 4);
00104                         }
00105                         else if ((phidG->deviceVersion  >= 200) && (phidG->deviceVersion  < 300))
00106                         {
00107                                 int data = 0;
00108                                 data = (signed short)((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
00109                                 axis[0] = round_double((((double)data-16384) / 650), 4);
00110                                 data = (signed short)((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
00111                                 axis[1] = round_double((((double)data-16384) / 650), 4);
00112                         }
00113                         else if ((phidG->deviceVersion  >= 300) && (phidG->deviceVersion  < 400))
00114                         {
00115                                 int data = 0;
00116                                 data = ((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
00117                                 axis[0] = round_double((((double)(data-32768)) / 4000), 5);
00118                                 data = ((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
00119                                 axis[1] = round_double((((double)(data-32768)) / 4000), 5);
00120                         }
00121                         else
00122                                 return EPHIDGET_UNEXPECTED;
00123                         break;
00124                 case PHIDID_ACCELEROMETER_3AXIS:
00125                         if ((phidG->deviceVersion  >= 400) && (phidG->deviceVersion  < 500))
00126                         {
00127                                 int data = 0;
00128                                 data = ((unsigned short)buffer[0]+((unsigned short)buffer[1]<<8));
00129                                 axis[0] = round_double((((double)(data-32768)) / 6553.6), 5);
00130                                 data = ((unsigned short)buffer[2]+((unsigned short)buffer[3]<<8));
00131                                 axis[1] = round_double((((double)(data-32768)) / 6553.6), 5);
00132                                 data = ((unsigned short)buffer[4]+((unsigned short)buffer[5]<<8));
00133                                 axis[2] = round_double((((double)(data-32768)) / 6553.6), 5);
00134                         }
00135                         else
00136                                 return EPHIDGET_UNEXPECTED;
00137                         break;
00138                 default:
00139                         return EPHIDGET_UNEXPECTED;
00140         }
00141 
00142         //Make sure values are within defined range, and store to structure
00143         for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
00144         {
00145                 if(axis[i] > phid->accelerationMax) axis[i] = phid->accelerationMax;
00146                 if(axis[i] < phid->accelerationMin) axis[i] = phid->accelerationMin;
00147                 phid->axis[i] = axis[i];
00148         }
00149         
00150         //send out any events that exceed or match the trigger
00151         for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
00152         {
00153                 if (fabs(phid->axis[i] - phid->axisLastTrigger[i]) >= phid->axisChangeTrigger[i]
00154                         || phid->axisLastTrigger[i] == PUNK_DBL)
00155                 {
00156                         FIRE(AccelerationChange, i, phid->axis[i]);
00157                         phid->axisLastTrigger[i] = phid->axis[i];
00158                 }
00159         }
00160 
00161         return EPHIDGET_OK;
00162 }
00163 
00164 //eventsAfterOpen - sends out an event for all valid data, used during attach initialization
00165 CPHIDGETINITEVENTS(Accelerometer)
00166 
00167         for (i = 0; i<phid->phid.attr.accelerometer.numAxis; i++)
00168         {
00169                 if(phid->axis[i] != PUNK_DBL)
00170                 {
00171                         FIRE(AccelerationChange, i, phid->axis[i]);
00172                         phid->axisLastTrigger[i] = phid->axis[i];
00173                 }
00174         }
00175 
00176         return EPHIDGET_OK;
00177 }
00178 
00179 //getPacket - not used for accelerometer
00180 CGETPACKET(Accelerometer)
00181         return EPHIDGET_UNEXPECTED;
00182 }
00183 
00184 // === Exported Functions === //
00185 
00186 //create and initialize a device structure
00187 CCREATE(Accelerometer, PHIDCLASS_ACCELEROMETER)
00188 
00189 //event setup functions
00190 CFHANDLE(Accelerometer, AccelerationChange, int, double)
00191 
00192 CGET(Accelerometer,AxisCount,int)
00193         TESTPTRS(phid,pVal) 
00194         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00195         TESTATTACHED
00196 
00197         MASGN(phid.attr.accelerometer.numAxis)
00198 }
00199 
00200 CGETINDEX(Accelerometer,Acceleration,double)
00201         TESTPTRS(phid,pVal)     
00202         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00203         TESTATTACHED
00204         TESTINDEX(phid.attr.accelerometer.numAxis)
00205         TESTMASGN(axis[Index], PUNK_DBL)
00206 
00207         MASGN(axis[Index])
00208 }
00209 
00210 CGETINDEX(Accelerometer,AccelerationMax,double)
00211         TESTPTRS(phid,pVal)     
00212         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00213         TESTATTACHED
00214         TESTINDEX(phid.attr.accelerometer.numAxis)
00215         TESTMASGN(accelerationMax, PUNK_DBL)
00216 
00217         MASGN(accelerationMax)
00218 }
00219 
00220 CGETINDEX(Accelerometer,AccelerationMin,double)
00221         TESTPTRS(phid,pVal)     
00222         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00223         TESTATTACHED
00224         TESTINDEX(phid.attr.accelerometer.numAxis)
00225         TESTMASGN(accelerationMin, PUNK_DBL)
00226 
00227         MASGN(accelerationMin)
00228 }
00229 
00230 CGETINDEX(Accelerometer,AccelerationChangeTrigger,double)
00231         TESTPTRS(phid,pVal) 
00232         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00233         TESTATTACHED
00234         TESTINDEX(phid.attr.accelerometer.numAxis)
00235         TESTMASGN(axisChangeTrigger[Index], PUNK_DBL)
00236 
00237         MASGN(axisChangeTrigger[Index])
00238 }
00239 CSETINDEX(Accelerometer,AccelerationChangeTrigger,double)
00240         TESTPTR(phid) 
00241         TESTDEVICETYPE(PHIDCLASS_ACCELEROMETER)
00242         TESTATTACHED
00243         TESTINDEX(phid.attr.accelerometer.numAxis)
00244         TESTRANGE(0, phid->accelerationMax - phid->accelerationMin)
00245 
00246         if(CPhidget_statusFlagIsSet(phid->phid.status, PHIDGET_REMOTE_FLAG))
00247                 ADDNETWORKKEYINDEXED(Trigger, "%lE", axisChangeTrigger);
00248         else
00249                 phid->axisChangeTrigger[Index] = newVal;
00250 
00251         return EPHIDGET_OK;
00252 }
00253 
00254 // === Deprecated Functions === //
00255 
00256 CGET(Accelerometer,NumAxis,int)
00257         return CPhidgetAccelerometer_getAxisCount(phid, pVal);
00258 }


pedal_monitor
Author(s): Pedro Mendes
autogenerated on Fri Jun 6 2014 18:37:20