set_device_name.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 // - Manager simple -
19 // This is a simple example showing how to setup a phidget manager and display a list of the currently connected
20 // Phidgets devices to the PC.
21 
22 
23 #include <ros/ros.h>
24 
25 #include <stdio.h>
26 #include <cstdio>
27 #include <libphidgets/phidget21.h>
28 
29 int display_devices(CPhidgetManagerHandle MAN);
30 
31 int AttachHandler(CPhidgetHandle phid, void *userPtr)
32 {
33  int serialNo;
34  const char *name;
35  CPhidget_DeviceID id;
36  CPhidget_DeviceClass cls;
37 
38  CPhidget_getDeviceName (phid, &name);
39  CPhidget_getSerialNumber(phid, &serialNo);
40  CPhidget_getDeviceClass(phid, &cls);
41  CPhidget_getDeviceID(phid, &id);
42 
43  printf("%s %10d attached! (%d, %d) \n", name, serialNo, cls, id);
44 
45  display_devices((CPhidgetManagerHandle)userPtr);
46 
47  return 0;
48 }
49 
50 int DetachHandler(CPhidgetHandle phid, void *userPtr)
51 {
52  int serialNo;
53  const char *name;
54 
55  CPhidget_getDeviceName (phid, &name);
56  CPhidget_getSerialNumber(phid, &serialNo);
57  printf("%s %10d detached!\n", name, serialNo);
58 
59  return 0;
60 }
61 
62 int ErrorHandler(CPhidgetManagerHandle MAN, void *usrptr, int Code, const char *Description)
63 {
64  printf("Error handled. %d - %s\n", Code, Description);
65  return 0;
66 }
67 
68 //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).
69 int display_devices(CPhidgetManagerHandle MAN)
70 {
71  int serialNo, version, numDevices, i;
72  const char* ptr;
73  const char* label;
74  CPhidgetHandle *devices;
75 
76  CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
77 
78  printf("|- # -|- Label -|- Type -|- Serial No. -|- Version -|\n");
79  printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
80 
81 
82  for(i = 0; i < numDevices; i++)
83  {
84  CPhidget_getDeviceType(devices[i], &ptr);
85  CPhidget_getDeviceLabel(devices[i], &label);
86  CPhidget_getSerialNumber(devices[i], &serialNo);
87  CPhidget_getDeviceVersion(devices[i], &version);
88 
89  printf("|- %3d -|- %18s -|- %30s -|- %10d -|- %8d -|\n", i, label, ptr, serialNo, version);
90  printf("|-------|----------------------|----------------------------------|--------------|------------|\n");
91  }
92 
93  CPhidgetManager_freeAttachedDevicesArray(devices);
94 
95  printf("\nPress r to rename\n");
96  printf("Press q to exit\n");
97 
98  return 0;
99 }
100 
101 void set_label(CPhidgetManagerHandle MAN, int index)
102 {
103  int numDevices;
104  char choise;
105  const char* label_old;
106  std::string label_new;
107  CPhidgetHandle *devices;
108 
109  printf("index: %d\n", index);
110 
111  CPhidgetManager_getAttachedDevices (MAN, &devices, &numDevices);
112  CPhidget_getDeviceLabel(devices[index], &label_old);
113 
114  printf("\nenter new label: ");
115  getline(std::cin, label_new);
116 
117  printf("\n old label: %s \n”", label_old);
118  printf("new label: %s \n", label_new.c_str());
119  printf("is this correct? [Y/n]: ");
120  choise = getchar();
121  getchar();
122  switch(choise)
123  {
124  case '\n':
125  case 'Y':
126  case 'y':
127  if(CPhidget_setDeviceLabel(devices[index], label_new.c_str()) == EPHIDGET_OK)
128  printf("\nnew label is: %s \n", label_new.c_str());
129  else
130  printf("\nerror setting label!\n");
131  break;
132  case 'n':
133  printf("\nlabel is still: %s \n", label_old);
134  break;
135  default:
136  break;
137  };
138 
139 }
140 
142 {
143  int err;
144  //Declare an Manager handle
145  CPhidgetManagerHandle man = 0;
146 
147  CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
148 
149  //create the Manager object
150  CPhidgetManager_create(&man);
151 
152  //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.
153  //CPhidgetManager_set_OnAttach_Handler(man, AttachHandler, man);
154  //CPhidgetManager_set_OnDetach_Handler(man, DetachHandler, man);
155  //CPhidgetManager_set_OnError_Handler(man, ErrorHandler, NULL);
156 
157  //open the Manager for device connections
158  CPhidgetManager_open(man);
159 
160  sleep(10);
161 
162  if ((err = CPhidget_waitForAttachment((CPhidgetHandle) man, 10000))
163  != EPHIDGET_OK)
164  {
165  const char *errStr;
166  CPhidget_getErrorDescription(err, &errStr);
167  printf("Error waiting for attachment: (%d): %s", err, errStr);
168  }
169 
170  char choise;
171  display_devices(man);
172 
173  //end simulation
174  choise = getchar();
175  getchar();
176  switch (choise)
177  {
178  case 'r':
179  printf("Press index number of device you would like to rename\n");
180  choise = getchar();
181  getchar();
182  set_label(man, atoi(&choise));
183  break;
184  case 'q':
185  break;
186  default:
187  printf("Error\n");
188  };
189  //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
190  printf("Closing...\n");
191  CPhidgetManager_close(man);
192  CPhidgetManager_delete(man);
193 
194  //all done, exit
195  return 0;
196 }
197 
198 int main(int argc, char* argv[])
199 {
201  return 0;
202 }
203 
int AttachHandler(CPhidgetHandle phid, void *userPtr)
int ErrorHandler(CPhidgetManagerHandle MAN, void *usrptr, int Code, const char *Description)
int i
Definition: tablet_leer.c:27
int main(int argc, char *argv[])
int display_devices(CPhidgetManagerHandle MAN)
int DetachHandler(CPhidgetHandle phid, void *userPtr)
void set_label(CPhidgetManagerHandle MAN, int index)
int set_device_label()


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Wed Apr 7 2021 02:11:43