00001
00002
00003
00004
00005
00006
00007
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
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
00131 CPhidgetManagerHandle man = 0;
00132
00133 CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
00134
00135
00136 CPhidgetManager_create(&man);
00137
00138
00139
00140
00141
00142
00143
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
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
00176 printf("Closing...\n");
00177 CPhidgetManager_close(man);
00178 CPhidgetManager_delete(man);
00179
00180
00181 return 0;
00182 }
00183
00184 int main(int argc, char* argv[])
00185 {
00186 set_device_label();
00187 return 0;
00188 }
00189