phidget.cpp
Go to the documentation of this file.
1 #include "phidgets_api/phidget.h"
2 
3 #include <cstdio>
4 
5 namespace phidgets {
6 
8 {
9 }
10 
12 {
13  // close(); // segfaults, why?
14  CPhidget_delete(handle_);
15 }
16 
18 {
19  CPhidget_set_OnAttach_Handler(handle_, &Phidget::AttachHandler, this);
20  CPhidget_set_OnDetach_Handler(handle_, &Phidget::DetachHandler, this);
21  CPhidget_set_OnError_Handler(handle_, &Phidget::ErrorHandler, this);
22 }
23 
24 void Phidget::init(CPhidgetHandle handle)
25 {
26  handle_ = handle;
27 }
28 
29 int Phidget::openAndWaitForAttachment(int serial_number, int timeout)
30 {
31  int ret = CPhidget_open(handle_, serial_number);
32  if (ret != EPHIDGET_OK)
33  {
34  return ret;
35  }
36 
37  return CPhidget_waitForAttachment(handle_, timeout);
38 }
39 
41 {
42  return CPhidget_close(handle_);
43 }
44 
46 {
47  char a[1000];
48  const char *deviceptr = a;
49  CPhidget_getDeviceType(handle_, &deviceptr);
50  return std::string(deviceptr);
51 }
52 
54 {
55  char a[1000];
56  const char *deviceptr = a;
57  CPhidget_getDeviceName(handle_, &deviceptr);
58  return std::string(deviceptr);
59 }
60 
62 {
63  char a[1000];
64  const char *deviceptr = a;
65  CPhidget_getDeviceType(handle_, &deviceptr);
66  return std::string(deviceptr);
67 }
68 
70 {
71  char a[1000];
72  const char *deviceptr = a;
73  CPhidget_getLibraryVersion(&deviceptr);
74  return std::string(deviceptr);
75 }
76 
78 {
79  int sernum;
80  CPhidget_getSerialNumber(handle_, &sernum);
81  return sernum;
82 }
83 
85 {
86  int version;
87  CPhidget_getDeviceVersion(handle_, &version);
88  return version;
89 }
90 
91 std::string Phidget::getErrorDescription(int errorCode)
92 {
93  char a[1000];
94  const char *errorPtr = a;
95  CPhidget_getErrorDescription(errorCode, &errorPtr);
96  return std::string(errorPtr);
97 }
98 
100 {
101  printf("Phidget attached (serial# %d)\n", getDeviceSerialNumber());
102 }
103 
105 {
106  printf("Phidget detached (serial# %d)\n", getDeviceSerialNumber());
107 }
108 
109 void Phidget::errorHandler(int error)
110 {
111  printf("Phidget error [%d]: %s\n", error,
112  getErrorDescription(error).c_str());
113 }
114 
115 int Phidget::AttachHandler(CPhidgetHandle /* handle */, void *userptr)
116 {
117  ((Phidget *)userptr)->attachHandler();
118  return 0;
119 }
120 
121 int Phidget::DetachHandler(CPhidgetHandle /* handle */, void *userptr)
122 {
123  ((Phidget *)userptr)->detachHandler();
124  return 0;
125 }
126 
127 int Phidget::ErrorHandler(CPhidgetHandle /* handle */, void *userptr,
128  int ErrorCode, const char * /* unknown */)
129 {
130  ((Phidget *)userptr)->errorHandler(ErrorCode);
131  return 0;
132 }
133 
134 } // namespace phidgets
int openAndWaitForAttachment(int serial_number, int timeout)
Open a connection to a Phidget.
Definition: phidget.cpp:29
static int DetachHandler(CPhidgetHandle handle, void *userptr)
Definition: phidget.cpp:121
static int AttachHandler(CPhidgetHandle handle, void *userptr)
Definition: phidget.cpp:115
void registerHandlers()
Definition: phidget.cpp:17
int getDeviceSerialNumber()
Get the Phidget&#39;s serial number.
Definition: phidget.cpp:77
virtual void detachHandler()
Definition: phidget.cpp:104
static int ErrorHandler(CPhidgetHandle handle, void *userptr, int ErrorCode, const char *unknown)
Definition: phidget.cpp:127
static std::string getErrorDescription(int errorCode)
Lookup the string for a CPhidget Error Code.
Definition: phidget.cpp:91
int close()
Close the connection to the phidget.
Definition: phidget.cpp:40
CPhidgetHandle handle_
Definition: phidget.h:88
virtual void errorHandler(int error)
Definition: phidget.cpp:109
virtual ~Phidget()
Definition: phidget.cpp:11
virtual void attachHandler()
Definition: phidget.cpp:99
std::string getDeviceType()
Get the device type string.
Definition: phidget.cpp:45
std::string getDeviceLabel()
Get the device label string.
Definition: phidget.cpp:61
std::string getLibraryVersion()
Get the library version string.
Definition: phidget.cpp:69
int getDeviceVersion()
Get the Phidget&#39;s version.
Definition: phidget.cpp:84
void init(CPhidgetHandle handle)
Definition: phidget.cpp:24
std::string getDeviceName()
Get the device name string.
Definition: phidget.cpp:53


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Fri Apr 9 2021 02:56:02