Basic Phidget Example Program 2

From wikidb
Jump to: navigation, search
 #incluce <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include "phidget21.h"


 void display_generic_properties(CPhidgetHandle phid)
 {
   int sernum, version;
   const char *deviceptr;
   CPhidget_getDeviceType((CPhidgetHandle)phid, &deviceptr);
   CPhidget_getSerialNumber((CPhidgetHandle)phid, &sernum);
   CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
   printf("%s\n", deviceptr);
   printf("Version: %8d SerialNumber: %10d\n", version, sernum);
   return;
 }
 int AttachHandler(CPhidgetHandle phid, void *userptr)
 {
   printf("phid1 Attach: ");
   display_generic_properties(phid);
   //while(1) usleep(1000000);
   return 0;
 }
 int DetachHandler(CPhidgetHandle phid, void *userptr)
 {
   printf("phid1 Detach: ");
   display_generic_properties(phid);
   return 0;
 }
 int ErrorHandler(CPhidgetHandle phid, void *userptr, 
                  int ErrorCode, const char *unknown)
 {
   printf("Error handler ran!\n");
   return 0;
 }
 int main(int argc, char* argv[])
 {
   int numberInputs;
   int value;
   int i;
   int j;
   int result;
   const char *err;
   // create the servo handler
   CPhidgetInterfaceKitHandle Kit888 = 0;
   CPhidgetInterfaceKit_create(&Kit888);
   // attach handlers to the sensor handler
   //  CPhidget_set_OnAttach_Handler((CPhidgetHandle)Kit888, AttachHandler, NULL);
   //  CPhidget_set_OnDetach_Handler((CPhidgetHandle)Kit888, DetachHandler, NULL);
   //  CPhidget_set_OnError_Handler((CPhidgetHandle)Kit888, ErrorHandler, NULL);
   // Open the sensor handler and wait for 10 seconds, otherwise exit
   CPhidget_open((CPhidgetHandle)Kit888, -1);
   if((result = CPhidget_waitForAttachment((CPhidgetHandle)Kit888, 10000)))
   {
     CPhidget_getErrorDescription(result, &err);
     printf("Problem waiting for attachment: %s\n", err);
     return 0;
   }
   sleep(1);
   for (j=0; j<10; j++)
     {
       // get anolog inputs
       CPhidgetInterfaceKit_getNumSensors (Kit888, &numberInputs);
       //printf ("Number of sensors inputs: %d\n", numberInputs);
       for (i=0; i<numberInputs; i++)
         {
           result = CPhidgetInterfaceKit_getSensorValue (Kit888, i, &value);
           printf ("%d  ", value);
           //printf ("result: %d   Value %d: %d\n", result, i, value);
         }
       printf ("\n");
       sleep(1);
     }
   // Shut it down
   CPhidget_close((CPhidgetHandle)Kit888);
   CPhidget_delete((CPhidgetHandle)Kit888);
   return 0;
 }