ifkit.c
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 // InterfacekitTest.cpp : Defines the entry point for the console application.
19 //
20 
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <libphidgets/phidget21.h>
26 
27 void display_generic_properties(CPhidgetHandle phid)
28 {
29  int sernum, version;
30  const char *deviceptr;
31  CPhidget_getDeviceType(phid, &deviceptr);
32  CPhidget_getSerialNumber(phid, &sernum);
33  CPhidget_getDeviceVersion(phid, &version);
34 
35  printf("%s\n", deviceptr);
36  printf("Version: %8d SerialNumber: %10d\n", version, sernum);
37  return;
38 }
39 
40 
41 int IFK_DetachHandler(CPhidgetHandle IFK, void *userptr)
42 {
43  printf("Detach handler ran!\n");
44  return 0;
45 }
46 
47 int IFK_ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
48 {
49  printf("Error handler ran!\n");
50  return 0;
51 }
52 
53 int IFK_OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
54 {
55  printf("Output %d is %d\n", Index, Value);
56  return 0;
57 }
58 
59 int IFK_InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
60 {
61  printf("Input %d is %d\n", Index, Value);
62  return 0;
63 }
64 
65 int IFK_SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
66 {
67  printf("Sensor %d is %d\n", Index, Value);
68  return 0;
69 }
70 
71 
72 int IFK_AttachHandler(CPhidgetHandle IFK, void *userptr)
73 {
74  CPhidgetInterfaceKit_setSensorChangeTrigger((CPhidgetInterfaceKitHandle)IFK, 0, 0);
75  printf("Attach handler ran!\n");
76  return 0;
77 }
78 
80 {
81  int numInputs, numOutputs, numSensors;
82  int err;
83  CPhidgetInterfaceKitHandle IFK = 0;
84 
85  //CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
86 
87  CPhidgetInterfaceKit_create(&IFK);
88 
89  CPhidgetInterfaceKit_set_OnInputChange_Handler(IFK, IFK_InputChangeHandler, NULL);
90  CPhidgetInterfaceKit_set_OnOutputChange_Handler(IFK, IFK_OutputChangeHandler, NULL);
91  CPhidgetInterfaceKit_set_OnSensorChange_Handler(IFK, IFK_SensorChangeHandler, NULL);
92  CPhidget_set_OnAttach_Handler((CPhidgetHandle)IFK, IFK_AttachHandler, NULL);
93  CPhidget_set_OnDetach_Handler((CPhidgetHandle)IFK, IFK_DetachHandler, NULL);
94  CPhidget_set_OnError_Handler((CPhidgetHandle)IFK, IFK_ErrorHandler, NULL);
95 
96  CPhidget_open((CPhidgetHandle)IFK, -1);
97 
98  //wait 5 seconds for attachment
99  if((err = CPhidget_waitForAttachment((CPhidgetHandle)IFK, 5000)) != EPHIDGET_OK )
100  {
101  const char *errStr;
102  CPhidget_getErrorDescription(err, &errStr);
103  printf("Error waiting for attachment: (%d): %s\n",err,errStr);
104  goto exit;
105  }
106 
107  display_generic_properties((CPhidgetHandle)IFK);
108  CPhidgetInterfaceKit_getOutputCount((CPhidgetInterfaceKitHandle)IFK, &numOutputs);
109  CPhidgetInterfaceKit_getInputCount((CPhidgetInterfaceKitHandle)IFK, &numInputs);
110  CPhidgetInterfaceKit_getSensorCount((CPhidgetInterfaceKitHandle)IFK, &numSensors);
111 
112  CPhidgetInterfaceKit_setOutputState((CPhidgetInterfaceKitHandle)IFK, 0, 1);
113 
114  printf("Sensors:%d Inputs:%d Outputs:%d\n", numSensors, numInputs, numOutputs);
115 
116  while(1)
117  {
118  CPhidgetInterfaceKit_setOutputState(IFK, 7, 1);
119  CPhidgetInterfaceKit_setOutputState(IFK, 7, 0);
120  }
121 
122  CPhidgetInterfaceKit_setOutputState(IFK, 0, 1);
123  sleep(1);
124  CPhidgetInterfaceKit_setOutputState(IFK, 0, 0);
125  sleep(1);
126  CPhidgetInterfaceKit_setOutputState(IFK, 0, 1);
127  sleep(1);
128  CPhidgetInterfaceKit_setOutputState(IFK, 0, 0);
129 
130  sleep(5);
131 
132 exit:
133  CPhidget_close((CPhidgetHandle)IFK);
134  CPhidget_delete((CPhidgetHandle)IFK);
135 
136  return 0;
137 }
138 
139 int main(int argc, char* argv[])
140 {
142  return 0;
143 }
144 
int IFK_AttachHandler(CPhidgetHandle IFK, void *userptr)
Definition: ifkit.c:72
int main(int argc, char *argv[])
Definition: ifkit.c:139
int IFK_InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
Definition: ifkit.c:59
int IFK_OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
Definition: ifkit.c:53
int test_interfacekit()
Definition: ifkit.c:79
void display_generic_properties(CPhidgetHandle phid)
Definition: ifkit.c:27
int IFK_DetachHandler(CPhidgetHandle IFK, void *userptr)
Definition: ifkit.c:41
int IFK_ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
Definition: ifkit.c:47
int IFK_SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *userptr, int Index, int Value)
Definition: ifkit.c:65


cob_phidgets
Author(s): Florian Weisshardt
autogenerated on Wed Apr 7 2021 02:11:43