00001 #ifndef FCAM_PHIDGETDEVICE_CPP
00002 #define FCAM_PHIDGETDEVICE_CPP
00003
00004 #include "PhidgetDevice.h"
00005
00006 #include <stdio.h>
00007 namespace FCam {
00008 namespace F2 {
00009 std::vector<PhidgetDevice *> PhidgetDevice::inputChangeHandlers[8];
00010 std::vector<PhidgetDevice *> PhidgetDevice::outputChangeHandlers[8];
00011 std::vector<PhidgetDevice *> PhidgetDevice::sensorChangeHandlers[8];
00012 bool PhidgetDevice::phidgetsAvailable;
00013 CPhidgetInterfaceKitHandle PhidgetDevice::ifKit;
00014
00015
00016 PhidgetDevice::PhidgetDevice() {
00017 if (phidgetsAvailable == false) {
00018 printf("Phidgets aren't ready yet, lets set it up.\n");
00019
00020 CPhidgetInterfaceKit_create(&ifKit);
00021
00022 CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifKit, phidgetAttachHandler, this);
00023 CPhidget_set_OnDetach_Handler((CPhidgetHandle)ifKit, phidgetDetachHandler, this);
00024 CPhidget_set_OnError_Handler((CPhidgetHandle)ifKit, phidgetErrorHandler, this);
00025
00026 CPhidgetInterfaceKit_set_OnInputChange_Handler (ifKit, phidgetInputChangeHandler, this);
00027 CPhidgetInterfaceKit_set_OnSensorChange_Handler (ifKit, phidgetSensorChangeHandler, this);
00028 CPhidgetInterfaceKit_set_OnOutputChange_Handler (ifKit, phidgetOutputChangeHandler, this);
00029
00030 CPhidget_open((CPhidgetHandle)ifKit, -1);
00031 int result;
00032 const char * errMsg;
00033 if((result = CPhidget_waitForAttachment((CPhidgetHandle)ifKit, 1000))) {
00034 CPhidget_getErrorDescription(result, &errMsg);
00035 printf("Problem waiting for attachment: %s\n", errMsg);
00036 printf("Continuing without Phidgets support\n");
00037 } else {
00038 phidgetsAvailable = true;
00039 CPhidgetInterfaceKit_setOutputState(ifKit, 7, true);
00040 }
00041 } else {
00042 printf("Another phidget device already set up phidgets\n");
00043 }
00044 }
00045
00046 void PhidgetDevice::registerInputChangeHandler(int index, PhidgetDevice * device) {
00047 inputChangeHandlers[index].push_back(device);
00048 }
00049
00050 void PhidgetDevice::registerOutputChangeHandler(int index, PhidgetDevice * device) {
00051 outputChangeHandlers[index].push_back(device);
00052 }
00053
00054 void PhidgetDevice::registerSensorChangeHandler(int index, PhidgetDevice * device) {
00055 sensorChangeHandlers[index].push_back(device);
00056 }
00057
00058 int PhidgetDevice::phidgetAttachHandler(CPhidgetHandle IFK, void *userptr) {
00059
00060 return 0;
00061 }
00062
00063 int PhidgetDevice::phidgetDetachHandler(CPhidgetHandle IFK, void *userptr) {
00064
00065 return 0;
00066 }
00067
00068 int PhidgetDevice::phidgetErrorHandler(CPhidgetHandle IFK, void *userptr, int errCode, const char *errMsg) {
00069 printf("Phidgets Error %d: %s\n", errCode, errMsg);
00070 return 0;
00071 }
00072
00073 int PhidgetDevice::phidgetInputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int index, int state) {
00074 int result = 0;
00075
00076 if (inputChangeHandlers[index].size() > 0){
00077 PhidgetDevice * device;
00078 for (unsigned int i = 0; i < inputChangeHandlers[index].size(); ++i){
00079 device = inputChangeHandlers[index][i];
00080 result = device->handleInputChange(IFK, index, state);
00081
00082 }
00083 } else {
00084 printf("No handler found: Input %d changed to value %d.\n", index, state);
00085 }
00086 return result;
00087 }
00088
00089 int PhidgetDevice::phidgetOutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int index, int state) {
00090 int result = 0;
00091
00092 if (outputChangeHandlers[index].size() > 0){
00093 PhidgetDevice * device;
00094 for (unsigned int i = 0; i < outputChangeHandlers[index].size(); ++i){
00095 device = outputChangeHandlers[index][i];
00096 result = device->handleOutputChange(IFK, index, state);
00097 }
00098 } else {
00099 printf("No handler found: Output %d changed to value %d.\n", index, state);
00100 }
00101 return result;
00102
00103
00104
00105 }
00106
00107 int PhidgetDevice::phidgetSensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int index, int value) {
00108 int result = 0;
00109
00110 if (sensorChangeHandlers[index].size() > 0){
00111 PhidgetDevice * device;
00112 for (unsigned int i = 0; i < sensorChangeHandlers[index].size(); ++i){
00113 device = sensorChangeHandlers[index][i];
00114 result = device->handleSensorChange(IFK, index, value);
00115 }
00116 } else {
00117 printf("No handler found: Sensor %d changed to value %d.\n", index, value);
00118 }
00119 return result;
00120 }
00121
00122 int PhidgetDevice::handleInputChange(CPhidgetInterfaceKitHandle IFK, int index, int state) {
00123 printf("Default Handler: Input %d changed to state %d.\n", index, state);
00124 return 0;
00125 }
00126 int PhidgetDevice::handleOutputChange(CPhidgetInterfaceKitHandle IFK, int index, int state) {
00127 printf("Default Handler: Output %d changed to state %d.\n", index, state);
00128 return 0;
00129 }
00130 int PhidgetDevice::handleSensorChange(CPhidgetInterfaceKitHandle IFK, int index, int value) {
00131 printf("Default Handler: Sensor %d changed to value %d.\n", index, value);
00132 return 0;
00133 }
00134 }
00135 }
00136
00137
00138
00139
00140
00141 #endif