Ejemplo n.º 1
0
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;
}
/**
 * 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;
}
Ejemplo n.º 3
0
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;
}