This repository has been archived by the owner on Sep 3, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorld.c
113 lines (85 loc) · 3.35 KB
/
HelloWorld.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Phidget Hello World Program for all devices
* (c) Phidgets 2012
*/
#include <stdlib.h>
#include <stdio.h>
#include <phidget21.h>
// -------------------- Event Functions ---------------------------------------
int CCONV AttachHandler (CPhidgetHandle device, void *userptr) {
int serialNumber;
const char *name;
LocalErrorCatcher(
CPhidget_getDeviceName(device, &name));
LocalErrorCatcher(
CPhidget_getSerialNumber(device, &serialNumber));
printf("Hello Device %s, Serial Number: %d\n", name, serialNumber);
return EXIT_SUCCESS;
}
int CCONV DetachHandler (CPhidgetHandle device, void *userptr) {
int serialNumber;
const char *name;
LocalErrorCatcher(
CPhidget_getDeviceName(device, &name));
LocalErrorCatcher(
CPhidget_getSerialNumber(device, &serialNumber));
printf("Goodbye Device %s, Serial Number: %d\n", name, serialNumber);
return EXIT_SUCCESS;
}
// When using an error handler with the manager, it takes a
// CPhidgetManagerHandle, when using an individual object, the
// object serves as its own handle.
int CCONV LibraryErrorHandler (CPhidgetManagerHandle device, void *usrptr,
int errorCode, const char *errorDescription) {
printf("Error Event: %d - %s\n", errorCode, errorDescription);
return EXIT_SUCCESS;
}
// This error handler can handle any CPhidget function that returns an int
int LocalErrorCatcher (int errorCode) {
const char *errorDescription;
// If the error code is 0, everything is okay
if (errorCode != 0) {
// Otherwise, you can print specific messages or perform actions by error value.
switch (errorCode) {
default:
printf("Error: An error occurred with code %d.\n", errorCode);
LocalErrorCatcher(
CPhidget_getErrorDescription (errorCode, &errorDescription));
printf("The description for this error is: %s\n", errorDescription);
break;
}
}
return EXIT_SUCCESS;
}
// -------------------- Main Code ---------------------------------------------
int main(int argc, char* argv[]) {
int result;
const char *err;
CPhidgetManagerHandle device = 0;
LocalErrorCatcher(
CPhidgetManager_create(&device));
LocalErrorCatcher(
CPhidgetManager_set_OnAttach_Handler((CPhidgetManagerHandle) device,
AttachHandler, NULL));
LocalErrorCatcher(
CPhidgetManager_set_OnDetach_Handler((CPhidgetManagerHandle ) device,
DetachHandler, NULL));
LocalErrorCatcher(
CPhidgetManager_set_OnError_Handler((CPhidgetManagerHandle) device,
LibraryErrorHandler, NULL));
printf("Opening...\n");
// Most opening and closing would be via a cast to
// (CPhidgetHandle), however, this manager has its
// own handle struct to cast to.
LocalErrorCatcher(
CPhidgetManager_open((CPhidgetManagerHandle) device));
printf("Phidget Simple Playground (plug and unplug devices)\n");
printf("Press Enter to end anytime...\n");
getchar();
printf("Closing...\n");
LocalErrorCatcher(
CPhidgetManager_close((CPhidgetManagerHandle) device));
LocalErrorCatcher(
CPhidgetManager_delete((CPhidgetManagerHandle) device));
return EXIT_SUCCESS;
}