static int CCONV AttachHandler (CPhidgetHandle device, void *userptr) { int serialNumber; const char *name; LocalErrorCatcher( CPhidget_getDeviceName(device, &name)); LocalErrorCatcher( CPhidget_getSerialNumber(device, &serialNumber)); PhidgetsDeviceManager* obj = (PhidgetsDeviceManager*)userptr; QString devname(name); // If the device name contains motor control in its name then emit a motor control card signal if(devname.contains("Motor Control")) { obj->pushToMotorControlCardSerials(serialNumber); // emit obj->motorCard(serialNumber); } else if(devname.contains("InterfaceKit")) { obj->setInterfaceKitSerial(serialNumber); } printf("Hello Device %s, Serial Number: %d\n", name, serialNumber); return 0; }
int CCONV DetachHandler (CPhidgetHandle device, void *userptr) { int serialNumber; const char *name; LocalErrorCatcher( CPhidget_getDeviceName(device, &name)); LocalErrorCatcher( CPhidget_getSerialNumber(device, &serialNumber)); printf("Goodbye Device %s, Serial Number: %d\n", name, serialNumber); return 0; }
int main(int argc, char* argv[]) { int result, numOutputs = 3, state = 0; const char *err, *ptr; CPhidgetInterfaceKitHandle phid = 0; LocalErrorCatcher(CPhidgetInterfaceKit_create(&phid)); LocalErrorCatcher(CPhidget_set_OnAttach_Handler((CPhidgetHandle) phid, AttachHandler, NULL)); LocalErrorCatcher(CPhidget_set_OnDetach_Handler((CPhidgetHandle) phid, DetachHandler, NULL)); LocalErrorCatcher(CPhidget_set_OnError_Handler((CPhidgetHandle) phid, ErrorHandler, NULL)); // printf("Phidget Simple Playground (plug and unplug devices)\n"); // printf("Opening...\n"); LocalErrorCatcher(CPhidget_open((CPhidgetHandle) phid, -1)); LocalErrorCatcher(CPhidget_waitForAttachment((CPhidgetHandle) phid, 100000)); CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr); CPhidgetInterfaceKit_getOutputCount(phid, &numOutputs); // printf("%s\n", ptr); printf("Turning power on\n"); CPhidgetInterfaceKit_setOutputState(phid, 0, 1); printf("done\n"); LocalErrorCatcher(CPhidget_close((CPhidgetHandle) phid)); LocalErrorCatcher(CPhidget_delete((CPhidgetHandle) phid)); return 0; }
int main() { Compass testCompass; InterfaceKit ifKit; LCD lcd; Motor motor; Spatial spatial; CPhidgetManagerHandle device = 0; LocalErrorCatcher( CPhidgetManager_create(&device)); LocalErrorCatcher( CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle) device, AttachHandler, NULL)); LocalErrorCatcher( CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle ) device, DetachHandler, NULL)); LocalErrorCatcher( CPhidgetManager_set_OnError_Handler((CPhidgetManagerHandle) device, LibraryErrorHandler, NULL)); printf("Starting Phidget Playground...\n"); // Most opening and closing would be via a cast to // (CPhidgetHandle), however, this manager has its // own handle struct to cast to. LocalErrorCatcher( CPhidgetManager_open((CPhidgetManagerHandle) device)); std::stringstream ss; lcd.clear(); for(int i=0;i<1000;i++){ testCompass.refresh(); ss << std::fixed << std::setprecision(1) << "Heading: " << testCompass.getHeading(); lcd.setText(ss.str(), 0); ss.str(std::string()); ss << std::fixed << std::setprecision(2) << spatial.getAcceleration(AXIS_X) << " " << spatial.getAcceleration(AXIS_Y) << " " << spatial.getAcceleration(AXIS_Z); lcd.setText(ss.str(), 1); ss.str(std::string()); usleep(100000); } printf("Press Enter to end...\n"); getchar(); LocalErrorCatcher( CPhidgetManager_close((CPhidgetManagerHandle) device)); LocalErrorCatcher( CPhidgetManager_delete((CPhidgetManagerHandle) device)); return 0; }
int PhidgetsDeviceManager::initDevices() { int res = LocalErrorCatcher(CPhidgetManager_create(&manager)); if(res != 0) return res; res = LocalErrorCatcher( CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle) manager, AttachHandler, this)); if(res != 0) return res; res = LocalErrorCatcher( CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle ) manager, DetachHandler, this)); if(res != 0) return res; res = LocalErrorCatcher( CPhidgetManager_set_OnError_Handler((CPhidgetManagerHandle) manager, LibraryErrorHandler, this)); if(res != 0) return res; // Most opening and closing would be via a cast to // (CPhidgetHandle), however, this manager has its // own handle struct to cast to. res= LocalErrorCatcher( CPhidgetManager_open((CPhidgetManagerHandle) manager)); return res; }
// This error handler can handle any CPhidget function that returns an int int LocalErrorCatcher (int errorCode) { const char *errorDescription; // If the error code is 0, everything is okay if (errorCode != 0) { // Otherwise, you can print specific messages or perform actions by error value. switch (errorCode) { default: printf("Error: An error occurred with code %d.\n", errorCode); LocalErrorCatcher( CPhidget_getErrorDescription (errorCode, &errorDescription)); printf("The description for this error is: %s\n", errorDescription); break; } } return 0; }