int FreeSpaceTracker::connect() { OutputDebugString("Free Tracker Connect\n"); // Initialize the freespace library int err = freespace_init(); if (err) return err; // Look for a single tracker int num_devices; err = freespace_getDeviceList(&DeviceID, 1, &num_devices); if (err) return err; if (num_devices == 0) return FREESPACE_ERROR_NO_DEVICE; // Attach to the tracker err = freespace_openDevice(DeviceID); if (err) { DeviceID = -1; return err; } err = freespace_getDeviceInfo(DeviceID, &Device); // Flush the message stream err = freespace_flush(DeviceID); if (err) return err; // Turn on the orientation message stream freespace_message msg; memset(&msg, 0, sizeof(msg)); if (Device.hVer >= 2) { // version 2 protocol msg.messageType = FREESPACE_MESSAGE_DATAMODECONTROLV2REQUEST; msg.dataModeControlV2Request.packetSelect = 3; // User Frame (orientation) msg.dataModeControlV2Request.modeAndStatus = 0; // operating mode == full motion } else { // version 1 protocol msg.messageType = FREESPACE_MESSAGE_DATAMODEREQUEST; msg.dataModeRequest.enableUserPosition = 1; msg.dataModeRequest.inhibitPowerManager = 1; } err = freespace_sendMessage(DeviceID, &msg); if (err) return err; // Now the tracker is ready to read return 0; }
/** * Callback that * - displays the devices that have been inserted into and removed from the system. * - requests the firmware version of any devices that have been inserted into the system. * Implements freespace_hotplugCallback * @param event The type of event. * @param id The affected device. * @param cookie Not used for this application. */ void hotplugCallback(enum freespace_hotplugEvent event, FreespaceDeviceId id, void* cookie) { int rc; struct freespace_message message; switch (event) { case FREESPACE_HOTPLUG_INSERTION: // Get and print USB HID information about the device. printf("Device Inserted: %d\n", id); rc = printDeviceInfo(id); if (rc != FREESPACE_SUCCESS) { printf("Could not display device info: %d\n", rc); return; } // Open the device to be able to communicate with it rc = freespace_openDevice(id); if (rc != FREESPACE_SUCCESS) { printf("Error opening device.\n"); return; } // Set the handler that handles messages received from the device. freespace_setReceiveMessageCallback(id, receiveMessageCallback, NULL); // Create and send a product ID request message to the device memset(&message, 0, sizeof(message)); // Start with a clean message struct message.messageType = FREESPACE_MESSAGE_PRODUCTIDREQUEST; // Allow message.dest to have the default value of 0. This will cause the message // to go to the remote or the module rc = freespace_sendMessageAsync(id, &message, 100, NULL, 0); if (rc != FREESPACE_SUCCESS) { printf("Error sending productID request\n"); return; } // To communicate with a dongle set the dest field to 1 message.dest = 1; rc = freespace_sendMessageAsync(id, &message, 100, NULL, 0); if (rc != FREESPACE_SUCCESS) { printf("Error sending productID request\n"); return; } break; case FREESPACE_HOTPLUG_REMOVAL: printf("Device Removed: %d\n", id); break; default: printf("Unrecognized freespace_hotplugEvent.\n"); break; } }
vrpn_Freespace* vrpn_Freespace::create(const char *name, vrpn_Connection *conn, int device_index, bool send_body_frames, bool send_user_frames) { // initialize libfreespace vrpn_Freespace::freespaceInit(); const unsigned MAX_DEVICES = 100; FreespaceDeviceId devices[MAX_DEVICES]; int numIds = 0; int rc; rc = freespace_getDeviceList(devices, MAX_DEVICES, &numIds); if ( (rc != FREESPACE_SUCCESS) || (numIds < (device_index+1)) ) { fprintf(stderr, "vrpn_Freespace::create: Didn't find enough devices: %d.\n", numIds); return NULL; } FreespaceDeviceId freespaceId = devices[device_index]; rc = freespace_openDevice(freespaceId); if (rc != FREESPACE_SUCCESS) { fprintf(stderr, "vrpn_Freespace::create: Could not open device %d.\n", device_index); return NULL; } struct FreespaceDeviceInfo deviceInfo; rc = freespace_getDeviceInfo(freespaceId, &deviceInfo); if (rc != FREESPACE_SUCCESS) { return NULL; } rc = freespace_flush(freespaceId); if (rc != FREESPACE_SUCCESS) { fprintf(stderr, "vrpn_Freespace::create: Error flushing device: %d\n", rc); return NULL; } vrpn_Freespace* dev = new vrpn_Freespace(freespaceId, &deviceInfo, name, conn); dev->deviceSetConfiguration(send_body_frames, send_user_frames); printf("Added freespace device: %s : \n", name); return dev; }
/** * 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; }