ifkit.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 // InterfacekitTest.cpp : Defines the entry point for the console application.
00019 //
00020 
00021 
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #include <libphidgets/phidget21.h>
00026 
00027 void display_generic_properties(CPhidgetHandle phid)
00028 {
00029         int sernum, version;
00030         const char *deviceptr;
00031         CPhidget_getDeviceType(phid, &deviceptr);
00032         CPhidget_getSerialNumber(phid, &sernum);
00033         CPhidget_getDeviceVersion(phid, &version);
00034 
00035         printf("%s\n", deviceptr);
00036         printf("Version: %8d SerialNumber: %10d\n", version, sernum);
00037         return;
00038 }
00039 
00040 
00041 int IFK_DetachHandler(CPhidgetHandle IFK, void *userptr)
00042 {
00043         printf("Detach handler ran!\n");
00044         return 0;
00045 }
00046 
00047 int IFK_ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
00048 {
00049         printf("Error handler ran!\n");
00050         return 0;
00051 }
00052 
00053 int IFK_OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
00054 {
00055         printf("Output %d is %d\n", Index, Value);
00056         return 0;
00057 }
00058 
00059 int IFK_InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
00060 {
00061         printf("Input %d is %d\n", Index, Value);
00062         return 0;
00063 }
00064 
00065 int IFK_SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
00066 {
00067         printf("Sensor %d is %d\n", Index, Value);
00068         return 0;
00069 }
00070 
00071 
00072 int IFK_AttachHandler(CPhidgetHandle IFK, void *userptr)
00073 {
00074         CPhidgetInterfaceKit_setSensorChangeTrigger((CPhidgetInterfaceKitHandle)IFK, 0, 0);
00075         printf("Attach handler ran!\n");
00076         return 0;
00077 }
00078 
00079 int test_interfacekit()
00080 {
00081         int numInputs, numOutputs, numSensors;
00082         int err;
00083         CPhidgetInterfaceKitHandle IFK = 0;
00084 
00085         //CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
00086 
00087         CPhidgetInterfaceKit_create(&IFK);
00088 
00089         CPhidgetInterfaceKit_set_OnInputChange_Handler(IFK, IFK_InputChangeHandler, NULL);
00090         CPhidgetInterfaceKit_set_OnOutputChange_Handler(IFK, IFK_OutputChangeHandler, NULL);
00091         CPhidgetInterfaceKit_set_OnSensorChange_Handler(IFK, IFK_SensorChangeHandler, NULL);
00092         CPhidget_set_OnAttach_Handler((CPhidgetHandle)IFK, IFK_AttachHandler, NULL);
00093         CPhidget_set_OnDetach_Handler((CPhidgetHandle)IFK, IFK_DetachHandler, NULL);
00094         CPhidget_set_OnError_Handler((CPhidgetHandle)IFK, IFK_ErrorHandler, NULL);
00095 
00096         CPhidget_open((CPhidgetHandle)IFK, -1);
00097 
00098         //wait 5 seconds for attachment
00099         if((err = CPhidget_waitForAttachment((CPhidgetHandle)IFK, 5000)) != EPHIDGET_OK )
00100         {
00101                 const char *errStr;
00102                 CPhidget_getErrorDescription(err, &errStr);
00103                 printf("Error waiting for attachment: (%d): %s\n",err,errStr);
00104                 goto exit;
00105         }
00106 
00107         display_generic_properties((CPhidgetHandle)IFK);
00108         CPhidgetInterfaceKit_getOutputCount((CPhidgetInterfaceKitHandle)IFK, &numOutputs);
00109         CPhidgetInterfaceKit_getInputCount((CPhidgetInterfaceKitHandle)IFK, &numInputs);
00110         CPhidgetInterfaceKit_getSensorCount((CPhidgetInterfaceKitHandle)IFK, &numSensors);
00111 
00112         CPhidgetInterfaceKit_setOutputState((CPhidgetInterfaceKitHandle)IFK, 0, 1);
00113 
00114         printf("Sensors:%d Inputs:%d Outputs:%d\n", numSensors, numInputs, numOutputs);
00115 
00116         while(1)
00117         {
00118                 CPhidgetInterfaceKit_setOutputState(IFK, 7, 1);
00119                 CPhidgetInterfaceKit_setOutputState(IFK, 7, 0);
00120         }
00121 
00122         CPhidgetInterfaceKit_setOutputState(IFK, 0, 1);
00123         sleep(1);
00124         CPhidgetInterfaceKit_setOutputState(IFK, 0, 0);
00125         sleep(1);
00126         CPhidgetInterfaceKit_setOutputState(IFK, 0, 1);
00127         sleep(1);
00128         CPhidgetInterfaceKit_setOutputState(IFK, 0, 0);
00129 
00130         sleep(5);
00131 
00132 exit:
00133         CPhidget_close((CPhidgetHandle)IFK);
00134         CPhidget_delete((CPhidgetHandle)IFK);
00135 
00136         return 0;
00137 }
00138 
00139 int main(int argc, char* argv[])
00140 {
00141         test_interfacekit();
00142         return 0;
00143 }
00144 


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 21:02:14