set_device_name.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017  
00018 // - Manager simple -
00019 // This is a simple example showing how to setup a phidget manager and display a list of the currently connected
00020 // Phidgets devices to the PC.
00021 
00022 
00023 #include <ros/ros.h>
00024 
00025 #include <stdio.h>
00026 #include <cstdio>
00027 #include <libphidgets/phidget21.h>
00028 
00029 int display_devices(CPhidgetManagerHandle MAN);
00030 
00031 int AttachHandler(CPhidgetHandle phid, void *userPtr)
00032 {
00033         int serialNo;
00034         const char *name;
00035         CPhidget_DeviceID id;
00036         CPhidget_DeviceClass cls;
00037 
00038         CPhidget_getDeviceName (phid, &name);
00039         CPhidget_getSerialNumber(phid, &serialNo);
00040         CPhidget_getDeviceClass(phid, &cls);
00041         CPhidget_getDeviceID(phid, &id);
00042 
00043         printf("%s %10d attached! (%d, %d) \n", name, serialNo, cls, id);
00044 
00045         display_devices((CPhidgetManagerHandle)userPtr);
00046 
00047         return 0;
00048 }
00049 
00050 int DetachHandler(CPhidgetHandle phid, void *userPtr)
00051 {
00052         int serialNo;
00053         const char *name;
00054 
00055         CPhidget_getDeviceName (phid, &name);
00056         CPhidget_getSerialNumber(phid, &serialNo);
00057         printf("%s %10d detached!\n", name, serialNo);
00058 
00059         return 0;
00060 }
00061 
00062 int ErrorHandler(CPhidgetManagerHandle MAN, void *usrptr, int Code, const char *Description)
00063 {
00064         printf("Error handled. %d - %s\n", Code, Description);
00065         return 0;
00066 }
00067 
00068 //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).
00069 int display_devices(CPhidgetManagerHandle MAN)
00070 {
00071         int serialNo, version, numDevices, i;
00072         const char* ptr;
00073         const char* label;
00074         CPhidgetHandle *devices;
00075 
00076         CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
00077 
00078         printf("|-   # -|-        Label       -|-              Type              -|- Serial No. -|-  Version -|\n");
00079         printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
00080 
00081 
00082         for(i = 0; i < numDevices; i++)
00083         {
00084                 CPhidget_getDeviceType(devices[i], &ptr);
00085                 CPhidget_getDeviceLabel(devices[i], &label);
00086                 CPhidget_getSerialNumber(devices[i], &serialNo);
00087                 CPhidget_getDeviceVersion(devices[i], &version);
00088 
00089                 printf("|- %3d -|- %18s -|- %30s -|- %10d -|- %8d -|\n", i, label, ptr, serialNo, version);
00090                 printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
00091         }
00092 
00093         CPhidgetManager_freeAttachedDevicesArray(devices);
00094 
00095         printf("\nPress r to rename\n");
00096         printf("Press q to exit\n");
00097 
00098         return 0;
00099 }
00100 
00101 void set_label(CPhidgetManagerHandle MAN, int index)
00102 {
00103         int numDevices;
00104         char choise;
00105         const char* label_old;
00106         std::string label_new;
00107         CPhidgetHandle *devices;
00108 
00109         printf("index: %d\n", index);
00110 
00111         CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
00112         CPhidget_getDeviceLabel(devices[index], &label_old);
00113 
00114         printf("\nenter new label: ");
00115         getline(std::cin, label_new);
00116 
00117         printf("\n old label: %s \n”", label_old);
00118         printf("new label: %s \n", label_new.c_str());
00119         printf("is this correct? [Y/n]: ");
00120         choise = getchar();
00121         getchar();
00122         switch(choise)
00123         {
00124                 case '\n':
00125                 case 'Y':
00126                 case 'y':
00127                         if(CPhidget_setDeviceLabel(devices[index], label_new.c_str()) == EPHIDGET_OK)
00128                                 printf("\nnew label is: %s \n", label_new.c_str());
00129                         else
00130                                 printf("\nerror setting label!\n");
00131                         break;
00132                 case 'n':
00133                         printf("\nlabel is still: %s \n", label_old);
00134                         break;
00135                 default:
00136                         break;
00137         };
00138 
00139 }
00140 
00141 int set_device_label()
00142 {
00143         int err;
00144         //Declare an Manager handle
00145         CPhidgetManagerHandle man = 0;
00146 
00147         CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
00148 
00149         //create the Manager object
00150         CPhidgetManager_create(&man);
00151 
00152         //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.
00153         //CPhidgetManager_set_OnAttach_Handler(man, AttachHandler, man);
00154         //CPhidgetManager_set_OnDetach_Handler(man, DetachHandler, man);
00155         //CPhidgetManager_set_OnError_Handler(man, ErrorHandler, NULL);
00156 
00157         //open the Manager for device connections
00158         CPhidgetManager_open(man);
00159 
00160         sleep(10);
00161 
00162         if ((err = CPhidget_waitForAttachment((CPhidgetHandle) man, 10000))
00163                         != EPHIDGET_OK)
00164         {
00165                 const char *errStr;
00166                 CPhidget_getErrorDescription(err, &errStr);
00167                 printf("Error waiting for attachment: (%d): %s", err, errStr);
00168         }
00169 
00170         char choise;
00171         display_devices(man);
00172 
00173         //end simulation
00174         choise = getchar();
00175         getchar();
00176         switch (choise)
00177         {
00178                 case 'r':
00179                         printf("Press index number of device you would like to rename\n");
00180                         choise = getchar();
00181                         getchar();
00182                         set_label(man, atoi(&choise));
00183                         break;
00184                 case 'q':
00185                         break;
00186                 default:
00187                         printf("Error\n");
00188         };
00189         //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
00190         printf("Closing...\n");
00191         CPhidgetManager_close(man);
00192         CPhidgetManager_delete(man);
00193 
00194         //all done, exit
00195         return 0;
00196 }
00197 
00198 int main(int argc, char* argv[])
00199 {
00200         set_device_label();
00201         return 0;
00202 }
00203 


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 21:02:14