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


phidgets_api
Author(s): Tully Foote, Ivan Dryanovski
autogenerated on Tue May 7 2019 03:19:24