set_device_name.cpp
Go to the documentation of this file.
00001 // - Manager simple -
00002 // This is a simple example showing how to setup a phidget manager and display a list of the currently connected
00003 // Phidgets devices to the PC.
00004 //
00005 // Copyright 2008 Phidgets Inc.  All rights reserved.
00006 // This work is licensed under the Creative Commons Attribution 2.5 Canada License. 
00007 // view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
00008 
00009 #include <ros/ros.h>
00010 
00011 #include <stdio.h>
00012 #include <cstdio>
00013 #include <libphidgets/phidget21.h>
00014 
00015 int display_devices(CPhidgetManagerHandle MAN);
00016 
00017 int AttachHandler(CPhidgetHandle phid, void *userPtr)
00018 {
00019         int serialNo;
00020         const char *name;
00021         CPhidget_DeviceID id;
00022         CPhidget_DeviceClass cls;
00023 
00024         CPhidget_getDeviceName (phid, &name);
00025         CPhidget_getSerialNumber(phid, &serialNo);
00026         CPhidget_getDeviceClass(phid, &cls);
00027         CPhidget_getDeviceID(phid, &id);
00028 
00029         printf("%s %10d attached! (%d, %d) \n", name, serialNo, cls, id);
00030 
00031         display_devices((CPhidgetManagerHandle)userPtr);
00032 
00033         return 0;
00034 }
00035 
00036 int DetachHandler(CPhidgetHandle phid, void *userPtr)
00037 {
00038         int serialNo;
00039         const char *name;
00040 
00041         CPhidget_getDeviceName (phid, &name);
00042         CPhidget_getSerialNumber(phid, &serialNo);
00043         printf("%s %10d detached!\n", name, serialNo);
00044 
00045         return 0;
00046 }
00047 
00048 int ErrorHandler(CPhidgetManagerHandle MAN, void *usrptr, int Code, const char *Description)
00049 {
00050         printf("Error handled. %d - %s\n", Code, Description);
00051         return 0;
00052 }
00053 
00054 //Display the properties of the attached phidget(s) to the screen.  We will be displaying the name, serial number and version of the attached device(s).
00055 int display_devices(CPhidgetManagerHandle MAN)
00056 {
00057         int serialNo, version, numDevices, i;
00058         const char* ptr;
00059         const char* label;
00060         CPhidgetHandle *devices;
00061 
00062         CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
00063 
00064         printf("|-   # -|-        Label       -|-              Type              -|- Serial No. -|-  Version -|\n");
00065         printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
00066 
00067 
00068         for(i = 0; i < numDevices; i++)
00069         {
00070                 CPhidget_getDeviceType(devices[i], &ptr);
00071                 CPhidget_getDeviceLabel(devices[i], &label);
00072                 CPhidget_getSerialNumber(devices[i], &serialNo);
00073                 CPhidget_getDeviceVersion(devices[i], &version);
00074 
00075                 printf("|- %3d -|- %18s -|- %30s -|- %10d -|- %8d -|\n", i, label, ptr, serialNo, version);
00076                 printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
00077         }
00078 
00079         CPhidgetManager_freeAttachedDevicesArray(devices);
00080 
00081         printf("\nPress r to rename\n");
00082         printf("Press q to exit\n");
00083 
00084         return 0;
00085 }
00086 
00087 void set_label(CPhidgetManagerHandle MAN, int index)
00088 {
00089         int numDevices;
00090         char choise;
00091         const char* label_old;
00092         std::string label_new;
00093         CPhidgetHandle *devices;
00094 
00095         printf("index: %d\n", index);
00096 
00097         CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
00098         CPhidget_getDeviceLabel(devices[index], &label_old);
00099 
00100         printf("\nenter new label: ");
00101         getline(std::cin, label_new);
00102 
00103         printf("\n old label: %s \n”", label_old);
00104         printf("new label: %s \n", label_new.c_str());
00105         printf("is this correct? [Y/n]: ");
00106         choise = getchar();
00107         getchar();
00108         switch(choise)
00109         {
00110                 case '\n':
00111                 case 'Y':
00112                 case 'y':
00113                         if(CPhidget_setDeviceLabel(devices[index], label_new.c_str()) == EPHIDGET_OK)
00114                                 printf("\nnew label is: %s \n", label_new.c_str());
00115                         else
00116                                 printf("\nerror setting label!\n");
00117                         break;
00118                 case 'n':
00119                         printf("\nlabel is still: %s \n", label_old);
00120                         break;
00121                 default:
00122                         break;
00123         };
00124 
00125 }
00126 
00127 int set_device_label()
00128 {
00129         int err;
00130         //Declare an Manager handle
00131         CPhidgetManagerHandle man = 0;
00132 
00133         CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
00134 
00135         //create the Manager object
00136         CPhidgetManager_create(&man);
00137 
00138         //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
00139         //CPhidgetManager_set_OnAttach_Handler(man, AttachHandler, man);
00140         //CPhidgetManager_set_OnDetach_Handler(man, DetachHandler, man);
00141         //CPhidgetManager_set_OnError_Handler(man, ErrorHandler, NULL);
00142 
00143         //open the Manager for device connections
00144         CPhidgetManager_open(man);
00145 
00146         sleep(10);
00147 
00148         if ((err = CPhidget_waitForAttachment((CPhidgetHandle) man, 10000))
00149                         != EPHIDGET_OK)
00150         {
00151                 const char *errStr;
00152                 CPhidget_getErrorDescription(err, &errStr);
00153                 printf("Error waiting for attachment: (%d): %s", err, errStr);
00154         }
00155 
00156         char choise;
00157         display_devices(man);
00158 
00159         //end simulation
00160         choise = getchar();
00161         getchar();
00162         switch (choise)
00163         {
00164                 case 'r':
00165                         printf("Press index number of device you would like to rename\n");
00166                         choise = getchar();
00167                         getchar();
00168                         set_label(man, atoi(&choise));
00169                         break;
00170                 case 'q':
00171                         break;
00172                 default:
00173                         printf("Error\n");
00174         };
00175         //since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
00176         printf("Closing...\n");
00177         CPhidgetManager_close(man);
00178         CPhidgetManager_delete(man);
00179 
00180         //all done, exit
00181         return 0;
00182 }
00183 
00184 int main(int argc, char* argv[])
00185 {
00186         set_device_label();
00187         return 0;
00188 }
00189 


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Thu Aug 27 2015 12:46:06