// C_application.c : A demonstration of the C programing languge and use with Phidgets. //Uses a Phidget interface kit with a slider or rotation sensor attached to port 0 and a Phidget servo. #include //Used to intercept the Ctrl-C Command used to exit the program #include //Provides access to the _getch() function #include //Provides access to the cout function #include //Provides access to the sleep() function #include "phidget21.h" //ensure you have this .h file in the same directory as this file //Provides access to all Phidget functions and handles using namespace std; //Sets the default namespace which removes the need for std:: in front of the cout function CPhidgetServoHandle servo; //Defines the global Phidget servo handle static int exitnow = 0; //Global variable to determine when the program should exit. int __stdcall attach_handler(CPhidgetHandle phid, void *userPtr) { //Function called whenever a Phidget is attached const char *namePtr; CPhidget_getDeviceName(phid, &namePtr); //Creates a pointer that points to the name of the Phidget cout << namePtr <<" Attached\n"; //Writes to the command line to show that a Phidget has been attached return 0; } int __stdcall detach_handler(CPhidgetHandle phid, void *userPtr) { //Function called whenever a Phidget is detached cout << (char *)userPtr << " Detached\n"; //Displays the name of the Phidget as determined by the programer in the main function return 0; } int __stdcall ifk_sensor_change_handler (CPhidgetInterfaceKitHandle phid, void *userPtr, int SIndex, int SValue) { //Function called whenever the Phidget21 library detects a sensor change if (SIndex == 0) //Determines if the sensor experiencing the change is at index 0. { int sensor_value = 0; sensor_value = SValue; CPhidgetInterfaceKit_getSensorValue (phid, 0, &sensor_value); //Both lines do the exact same thing and only one need be used. They are responsible for determining the value of the sensor at index 0. sensor_value = (int)(sensor_value * .18); //Converts the sensors value from its input range of 0 - 999 to a range of 0-180 which the Phidget servo can use to deterimine a position CPhidgetServo_setMotorPosition (servo, 0, sensor_value); //Sets the position of the servo } return 0; } void sighandler (int n) { //Function to handle termination signals printf ("Got a signal. Exiting.\n"); exitnow = 1; //Sets the exitnow variable to signal program termination } int main(char argv[]) { //Main function called when the program loads char ifk_name[4] = "IFK"; char servo_name[6] = "Servo"; //Defines names for the Phidgets char exitcode = 0; signal (SIGTERM, sighandler); //Assigns the sighandler() function to handle SIGTERM which is a generic termination command signal (SIGINT, sighandler); //Assigns the sighandler() function to handle SIGINT which is the Ctrl-C key command CPhidgetInterfaceKitHandle ifk; CPhidgetInterfaceKit_create(&ifk); CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifk, attach_handler, NULL); CPhidget_set_OnDetach_Handler((CPhidgetHandle)ifk, detach_handler, &ifk_name); CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifk, ifk_sensor_change_handler, NULL); CPhidget_open((CPhidgetHandle)ifk, -1); //All of the Phidget initilization instructions for the interface kit. CPhidget_waitForAttachment((CPhidgetHandle)ifk, 10000); //Have to wait to ensure that phidget has been attached before attempting to do anything with it or to recieve events CPhidgetServo_create(&servo); CPhidget_set_OnAttach_Handler((CPhidgetHandle)servo, attach_handler, NULL); CPhidget_set_OnDetach_Handler((CPhidgetHandle)servo, detach_handler, &servo_name); CPhidget_open((CPhidgetHandle)servo, -1); //All of the Phidget initilization instructions for the servo. CPhidget_waitForAttachment((CPhidgetHandle)servo, 10000); //Have to wait to ensure that phidget has been attached before attempting to do anything with it or to recieve events while(exitcode!= 'q') exitcode = _getch(); //The first method to exit the program. This method can only be used in windows and exits on q being pressed //while(exitcode!= 'q') // scanf("%c", &exitcode); //The second method to exit the program. Exits on q[enter] //while(!exitnow) Sleep(500); //The third method to exit the program. Exits on CTRL+C using the signal handlers. CPhidget_close((CPhidgetHandle)ifk); CPhidget_close((CPhidgetHandle)servo); CPhidget_delete((CPhidgetHandle)ifk); CPhidget_delete((CPhidgetHandle)servo); //Closes and deletes Phidget handles. Handles always need to be deleted when they are no longer needed or fall out of scope to prevent memory leaks. return 0; }