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