//----------------------------------------------------------------------------- void Close() { if (DeviceID >= 0) { // Shut off the data stream freespace_message msg; memset(&msg, 0, sizeof(msg)); if (Device.hVer >= 2) { msg.messageType = FREESPACE_MESSAGE_DATAMODECONTROLV2REQUEST; msg.dataModeControlV2Request.packetSelect = 0; // No output msg.dataModeControlV2Request.modeAndStatus = 1 << 1; // operating mode == sleep } else { msg.messageType = FREESPACE_MESSAGE_DATAMODEREQUEST; msg.dataModeRequest.enableUserPosition = 0; msg.dataModeRequest.inhibitPowerManager = 0; } int err = freespace_sendMessage(DeviceID, &msg); // Detach from the tracker freespace_closeDevice(DeviceID); DeviceID = -1; } freespace_exit(); }
/** * main * This example uses the asynchronous API to * - detect removal or additon of devices * - get the product ID information from added devices * Devices may or may not be connected when this example is launched * Devices may be add/removed (pugged/unplugged) while this example is running. */ int main(int argc, char* argv[]) { int numIds; int deviceIds[FREESPACE_MAXIMUM_DEVICE_COUNT]; int rc; // Flag to indicate that the application should quit // Set by the control signal handler int quit = 0; printVersionInfo(argv[0]); addControlHandler(&quit); // Initialize the freespace library rc = freespace_init(); if (rc != FREESPACE_SUCCESS) { printf("Initialization error. rc=%d\n", rc); return 1; } // Set the callback to catch the initial devices. printf("Detecting the Freespace devices already connected...\n"); freespace_setDeviceHotplugCallback(hotplugCallback, NULL); freespace_perform(); printf("Waiting for Freespace devices to be inserted...\n"); printf("Type Ctrl-C to exit\n"); while (!quit) { // Easy event loop - just poll freespace_perform periodically // rather than waiting on select or WaitForMultipleObjects Sleep(100); // Callbacks are called from within the perform call. freespace_perform(); } printf("Exiting\n"); printf("Cleaning up all devices...\n"); rc = freespace_getDeviceList(deviceIds, FREESPACE_MAXIMUM_DEVICE_COUNT, &numIds); if (rc == FREESPACE_SUCCESS) { int i; for (i = 0; i < numIds; i++) { freespace_closeDevice(deviceIds[i]); } } else { printf("Error getting device list.\n"); } freespace_exit(); return 0; }
/** * main * This example uses the synchronous API to * - find a device * - open the device found * - configure the device to output motion * - read motion messages sent by the device * This example assume the device is already connected. */ int main(int argc, char* argv[]) { struct freespace_message message; FreespaceDeviceId device; int numIds; // The number of device ID found int rc; // Return code //struct MultiAxisSensor pointer; //struct MultiAxisSensor angVel; // Flag to indicate that the application should quit // Set by the control signal handler int quit = 0; printVersionInfo(argv[0]); addControlHandler(&quit); // Initialize the freespace library rc = freespace_init(); if (rc != FREESPACE_SUCCESS) { printf("Initialization error. rc=%d\n", rc); return 1; } printf("Scanning for Freespace devices...\n"); // Get the ID of the first device in the list of availble devices rc = freespace_getDeviceList(&device, 1, &numIds); if (numIds == 0) { printf("Didn't find any devices.\n"); return 1; } printf("Found a device. Trying to open it...\n"); // Prepare to communicate with the device found above rc = freespace_openDevice(device); if (rc != FREESPACE_SUCCESS) { printf("Error opening device: %d\n", rc); return 1; } // Display the device information. printDeviceInfo(device); // Make sure any old messages are cleared out of the system rc = freespace_flush(device); if (rc != FREESPACE_SUCCESS) { printf("Error flushing device: %d\n", rc); return 1; } // Cleanup when done and configure the device to output mouse packets printf("Sending message to enable mouse data.\n"); memset(&message, 0, sizeof(message)); // Init message fields to 0 message.messageType = FREESPACE_MESSAGE_DATAMODECONTROLV2REQUEST; message.dataModeControlV2Request.packetSelect = 1; // Mouse packet message.dataModeControlV2Request.mode = 0; // Set full motion rc = freespace_sendMessage(device, &message); if (rc != FREESPACE_SUCCESS) { printf("Could not send message: %d.\n", rc); } // Close communications with the device printf("Cleaning up...\n"); freespace_closeDevice(device); // Cleanup the library freespace_exit(); return 0; }
/** * main * This example uses the synchronous API to * - find a device * - open the device found * - send a message * - look for a response * This example assumes that the device is already connected. */ int main(int argc, char* argv[]) { FreespaceDeviceId device; // Keep track of the device you are talking to struct freespace_message send; // A place to create messages to send to the device struct freespace_message receive; // A place to put a message received from the device int numIds; // Keep track of how many devices are available int rc; // Return Code int retryCount = 0; // How many times tried so far to get a response // Flag to indicate that the application should quit // Set by the control signal handler int quit = 0; printVersionInfo(argv[0]); addControlHandler(&quit); // Initialize the freespace library rc = freespace_init(); if (rc != FREESPACE_SUCCESS) { printf("Initialization error. rc=%d\n", rc); return 1; } printf("Scanning for Freespace devices...\n"); // Get the ID of the first device in the list of availble devices rc = freespace_getDeviceList(&device, 1, &numIds); if (numIds == 0) { printf("Didn't find any devices.\n"); return 1; } printf("Found a device. Trying to open it...\n"); // Prepare to communicate with the device found above rc = freespace_openDevice(device); if (rc != FREESPACE_SUCCESS) { printf("Error opening device: %d\n", rc); return 1; } // Display the device information. printDeviceInfo(device); // Make sure any old messages are cleared out of the system rc = freespace_flush(device); if (rc != FREESPACE_SUCCESS) { printf("Error flushing device: %d\n", rc); return 1; } printf("Requesting battery level messages.\n"); memset(&send, 0, sizeof(send)); // Start with a clean message struct // Populate the message fields. Two options are shown below. Uncomment one desired // and comment out the one not desired. //send.messageType = FREESPACE_MESSAGE_BATTERYLEVELREQUEST; // To send a battery level request send.messageType = FREESPACE_MESSAGE_PRODUCTIDREQUEST; // To send a product ID request while (!quit) { if (retryCount < RETRY_COUNT_LIMIT) { retryCount++; // Send the message constructed above. rc = freespace_sendMessage(device, &send); if (rc != FREESPACE_SUCCESS) { printf("Could not send message: %d.\n", rc); } // Read the response message. rc = freespace_readMessage(device, &receive, 100); if (rc == FREESPACE_SUCCESS) { // Print the received message freespace_printMessage(stdout, &receive); retryCount = 0; } else if (rc == FREESPACE_ERROR_TIMEOUT) { printf("<timeout> Try moving the Freespace device to wake it up.\n"); } else if (rc == FREESPACE_ERROR_INTERRUPTED) { printf("<interrupted>\n"); } else { printf("Error reading: %d. Quitting...\n", rc); break; } } else { printf("Did not receive response after %d trials\n", RETRY_COUNT_LIMIT); quit = 1; } SLEEP; } printf("Cleaning up...\n"); freespace_closeDevice(device); freespace_exit(); return 0; }