Commit 797a2f026063bb674830c1a308f882e0c2166e2e

  • avatar
  • Jarkko Vilhunen <jarkko.vilhunen @j…u.fi> (Committer)
  • Mon Mar 21 00:53:35 EET 2011
  • avatar
  • Jarkko Vilhunen <jarkko.vilhunen @j…u.fi> (Author)
  • Mon Mar 21 00:53:35 EET 2011
Add InterfaceKit example as a basis to work on
Makefile
(4 / 0)
  
1CFLAGS=-g
2LDFLAGS=-lphidget21
3
4main:
main.c
(196 / 0)
  
1// - InterfaceKit simple -
2// This simple example simply creates an InterfaceKit handle, hooks the event handlers and opens it. It then waits
3// for an InterfaceKit to be attached and waits for events to be fired. We progress through three steps, 1. Normal settings,
4// 2. Setting analog sensor sensitivity to 100, 3. Toggling Ratiometric, waiting for user input to proceed to next step to allow
5// data to be read.
6//
7// Copyright 2008 Phidgets Inc. All rights reserved.
8// This work is licensed under the Creative Commons Attribution 2.5 Canada License.
9// view a copy of this license, visit http://creativecommons.org/licenses/by/2.5/ca/
10
11#include <stdio.h>
12#include <phidget21.h>
13
14int AttachHandler(CPhidgetHandle IFK, void *userptr)
15{
16 int serialNo;
17 const char *name;
18
19 CPhidget_getDeviceName(IFK, &name);
20 CPhidget_getSerialNumber(IFK, &serialNo);
21
22 printf("%s %10d attached!\n", name, serialNo);
23
24 return 0;
25}
26
27int DetachHandler(CPhidgetHandle IFK, void *userptr)
28{
29 int serialNo;
30 const char *name;
31
32 CPhidget_getDeviceName (IFK, &name);
33 CPhidget_getSerialNumber(IFK, &serialNo);
34
35 printf("%s %10d detached!\n", name, serialNo);
36
37 return 0;
38}
39
40int ErrorHandler(CPhidgetHandle IFK, void *userptr, int ErrorCode, const char *unknown)
41{
42 printf("Error handled. %d - %s", ErrorCode, unknown);
43 return 0;
44}
45
46//callback that will run if an input changes.
47//Index - Index of the input that generated the event, State - boolean (0 or 1) representing the input state (on or off)
48int InputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
49{
50 printf("Digital Input: %d > State: %d\n", Index, State);
51 return 0;
52}
53
54//callback that will run if an output changes.
55//Index - Index of the output that generated the event, State - boolean (0 or 1) representing the output state (on or off)
56int OutputChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int State)
57{
58 printf("Digital Output: %d > State: %d\n", Index, State);
59 return 0;
60}
61
62//callback that will run if the sensor value changes by more than the OnSensorChange trigger.
63//Index - Index of the sensor that generated the event, Value - the sensor read value
64int SensorChangeHandler(CPhidgetInterfaceKitHandle IFK, void *usrptr, int Index, int Value)
65{
66 printf("Sensor: %d > Value: %d\n", Index, Value);
67 return 0;
68}
69
70//Display the properties of the attached phidget to the screen. We will be displaying the name, serial number and version of the attached device.
71//Will also display the number of inputs, outputs, and analog inputs on the interface kit as well as the state of the ratiometric flag
72//and the current analog sensor sensitivity.
73int display_properties(CPhidgetInterfaceKitHandle phid)
74{
75 int serialNo, version, numInputs, numOutputs, numSensors, triggerVal, ratiometric, i;
76 const char* ptr;
77
78 CPhidget_getDeviceType((CPhidgetHandle)phid, &ptr);
79 CPhidget_getSerialNumber((CPhidgetHandle)phid, &serialNo);
80 CPhidget_getDeviceVersion((CPhidgetHandle)phid, &version);
81
82 CPhidgetInterfaceKit_getInputCount(phid, &numInputs);
83 CPhidgetInterfaceKit_getOutputCount(phid, &numOutputs);
84 CPhidgetInterfaceKit_getSensorCount(phid, &numSensors);
85 CPhidgetInterfaceKit_getRatiometric(phid, &ratiometric);
86
87 printf("%s\n", ptr);
88 printf("Serial Number: %10d\nVersion: %8d\n", serialNo, version);
89 printf("# Digital Inputs: %d\n# Digital Outputs: %d\n", numInputs, numOutputs);
90 printf("# Sensors: %d\n", numSensors);
91 printf("Ratiometric: %d\n", ratiometric);
92
93 for(i = 0; i < numSensors; i++)
94 {
95 CPhidgetInterfaceKit_getSensorChangeTrigger (phid, i, &triggerVal);
96
97 printf("Sensor#: %d > Sensitivity Trigger: %d\n", i, triggerVal);
98 }
99
100 return 0;
101}
102
103
104int interfacekit_simple()
105{
106 int result, numSensors, i;
107 const char *err;
108
109 //Declare an InterfaceKit handle
110 CPhidgetInterfaceKitHandle ifKit = 0;
111
112 //create the InterfaceKit object
113 CPhidgetInterfaceKit_create(&ifKit);
114
115 //Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
116 CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifKit, AttachHandler, NULL);
117 CPhidget_set_OnDetach_Handler((CPhidgetHandle)ifKit, DetachHandler, NULL);
118 CPhidget_set_OnError_Handler((CPhidgetHandle)ifKit, ErrorHandler, NULL);
119
120 //Registers a callback that will run if an input changes.
121 //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
122 CPhidgetInterfaceKit_set_OnInputChange_Handler (ifKit, InputChangeHandler, NULL);
123
124 //Registers a callback that will run if the sensor value changes by more than the OnSensorChange trig-ger.
125 //Requires the handle for the IntefaceKit, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
126 CPhidgetInterfaceKit_set_OnSensorChange_Handler (ifKit, SensorChangeHandler, NULL);
127
128 //Registers a callback that will run if an output changes.
129 //Requires the handle for the Phidget, the function that will be called, and an arbitrary pointer that will be supplied to the callback function (may be NULL).
130 CPhidgetInterfaceKit_set_OnOutputChange_Handler (ifKit, OutputChangeHandler, NULL);
131
132 //open the interfacekit for device connections
133 CPhidget_open((CPhidgetHandle)ifKit, -1);
134
135 //get the program to wait for an interface kit device to be attached
136 printf("Waiting for interface kit to be attached....");
137 if((result = CPhidget_waitForAttachment((CPhidgetHandle)ifKit, 10000)))
138 {
139 CPhidget_getErrorDescription(result, &err);
140 printf("Problem waiting for attachment: %s\n", err);
141 return 0;
142 }
143
144 //Display the properties of the attached interface kit device
145 display_properties(ifKit);
146
147 //read interface kit event data
148 printf("Reading.....\n");
149
150 //keep displaying interface kit data until user input is read
151 printf("Press any key to go to next step\n");
152 getchar();
153
154 printf("Modifying sensor sensitivity triggers....\n");
155
156 //get the number of sensors available
157 CPhidgetInterfaceKit_getSensorCount(ifKit, &numSensors);
158
159 //Change the sensitivity trigger of the sensors
160 for(i = 0; i < numSensors; i++)
161 {
162 CPhidgetInterfaceKit_setSensorChangeTrigger(ifKit, i, 100); //we'll just use 10 for fun
163 }
164
165 //read interface kit event data
166 printf("Reading.....\n");
167
168 //keep displaying interface kit data until user input is read
169 printf("Press any key to go to next step\n");
170 getchar();
171
172 printf("Toggling Ratiometric....\n");
173
174 CPhidgetInterfaceKit_setRatiometric(ifKit, 0);
175
176 //read interface kit event data
177 printf("Reading.....\n");
178
179 //keep displaying interface kit data until user input is read
180 printf("Press any key to end\n");
181 getchar();
182
183 //since user input has been read, this is a signal to terminate the program so we will close the phidget and delete the object we created
184 printf("Closing...\n");
185 CPhidget_close((CPhidgetHandle)ifKit);
186 CPhidget_delete((CPhidgetHandle)ifKit);
187
188 //all done, exit
189 return 0;
190}
191
192int main(int argc, char* argv[])
193{
194 interfacekit_simple();
195 return 0;
196}