vrpn  07.33
Virtual Reality Peripheral Network
vrpn_Tracker_SpacePoint.C
Go to the documentation of this file.
1 /*
2  * vrpn_Tracker_SpacePoint.cpp
3  *
4  * Created on: Nov 22, 2010
5  * Author: janoc
6  */
7 
8 #include <stdio.h> // for fprintf, stderr
9 #include <string.h> // for memset
10 
11 #include "vrpn_Connection.h" // for vrpn_CONNECTION_LOW_LATENCY, etc
13 
14 const unsigned SPACEPOINT_VENDOR = 0x20ff;
15 const unsigned SPACEPOINT_PRODUCT = 0x0100;
16 
18 
19 #ifdef VRPN_USE_HID
21  vrpn_Tracker(name, trackercon), vrpn_Button(name, trackercon),
23 {
24  memset(d_quat, 0, 4 * sizeof(float));
25  d_quat[3] = 1.0;
26 
29 }
30 
31 void vrpn_Tracker_SpacePoint::on_data_received(size_t bytes, vrpn_uint8 *buffer)
32 {
33  /*
34  * Horrible kludge - as we do not have a way to select the correct endpoint, we have to use a hack
35  * to identify the correct device. In Windows the SpacePoint appears as 2 devices with same VID/PID, one for each endpoint
36  * The correct device sends a report 15 bytes long. If we get a report of a different length, we try to open
37  * the other device instead.
38  */
39 
40  // test from the app note
41  // the quaternion should be: 0.2474, -0.1697, -0.1713, 0.9384
42  /*
43  bytes = 15;
44  vrpn_uint8 test_dta[] =
45  { 0x2f, 0x85, 0x23, 0x8a, 0x5b, 0x90, 0xac, 0x9f, 0x49, 0x6a, 0x12, 0x6a, 0x1e, 0xf8, 0xd0 };
46  memcpy(buffer, test_dta, 15 * sizeof(vrpn_uint8));
47  */
48 
49  if (bytes == 15) {
50  vrpn_uint8 * bufptr = buffer + 6;
51 
52  for (int i = 0; i < 4; i++) {
53  d_quat[i] = (vrpn_unbuffer_from_little_endian<vrpn_uint16>(bufptr) - 32768) / 32768.0;
54  }
55 
56  buttons[0] = buffer[14] & 0x1;
57  buttons[1] = buffer[14] & 0x2;
58 
59  // Find out what time we received the new information, then send any
60  // changes to the client.
64 
65  // send tracker orientation
66  d_sensor = 0;
67  memset(pos, 0, sizeof(vrpn_float64) * 3); // no position
68 
69  char msgbuf[1000];
70  int len = vrpn_Tracker::encode_to(msgbuf);
72  fprintf(stderr, "SpacePoint tracker: can't write message: tossing\n");
73  }
74 
75  // send buttons
77 
78  } else {
79  // try the other iface
80  // as we are keeping the first one open,
81  // it will not enumerate and we get the next one. Horrible kludge :(
82  reconnect();
83  }
84 }
85 
87 {
88  if (connected()) {
89  // device update. This will call on_data_received() if we get something.
90  update();
91 
92  // server update
94  }
95 }
96 
97 #endif
vrpn_HidInterface::connected
virtual bool connected() const
Returns true iff the last device I/O succeeded.
Definition: vrpn_HumanInterface.C:23
vrpn_Connection::pack_message
virtual int pack_message(vrpn_uint32 len, struct timeval time, vrpn_int32 type, vrpn_int32 sender, const char *buffer, vrpn_uint32 class_of_service)
Pack a message that will be sent the next time mainloop() is called. Turn off the RELIABLE flag if yo...
Definition: vrpn_Connection.C:4632
vrpn_Tracker
Definition: vrpn_Tracker.h:49
vrpn_Button::report_changes
virtual void report_changes(void)
Definition: vrpn_Button.C:422
vrpn_HidInterface::reconnect
virtual bool reconnect()
Tries to reconnect to an acceptable device. Call this if you suspect a hotplug event has occurred.
Definition: vrpn_HumanInterface.C:55
vrpn_Tracker::encode_to
virtual int encode_to(char *buf)
Definition: vrpn_Tracker.C:533
vrpn_Tracker_SpacePoint::mainloop
virtual void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
Definition: vrpn_Tracker_SpacePoint.C:86
vrpn_Tracker::d_sensor
vrpn_int32 d_sensor
Definition: vrpn_Tracker.h:94
vrpn_HidInterface
Definition: vrpn_HumanInterface.h:68
vrpn_Tracker::d_quat
vrpn_float64 d_quat[4]
Definition: vrpn_Tracker.h:95
vrpn_Tracker::timestamp
struct timeval timestamp
Definition: vrpn_Tracker.h:100
vrpn_HidInterface::update
virtual void update()
Polls the device buffers and causes on_data_received callbacks if appropriate You NEED to call this f...
Definition: vrpn_HumanInterface.C:140
vrpn_Tracker_SpacePoint::_timestamp
struct timeval _timestamp
Definition: vrpn_Tracker_SpacePoint.h:35
vrpn_Button::num_buttons
vrpn_int32 num_buttons
Definition: vrpn_Button.h:47
vrpn_CONNECTION_LOW_LATENCY
const vrpn_uint32 vrpn_CONNECTION_LOW_LATENCY
Definition: vrpn_Connection.h:122
vrpn_Button::buttons
unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS]
Definition: vrpn_Button.h:44
vrpn_BaseClassUnique::d_connection
vrpn_Connection * d_connection
Connection that this object talks to.
Definition: vrpn_BaseClass.h:224
SPACEPOINT_VENDOR
const unsigned SPACEPOINT_VENDOR
Definition: vrpn_Tracker_SpacePoint.C:14
vrpn_HidProductAcceptor
Accepts any device with the given vendor and product IDs.
Definition: vrpn_HumanInterface.h:150
vrpn_BaseClassUnique::d_sender_id
vrpn_int32 d_sender_id
Sender ID registered with the connection.
Definition: vrpn_BaseClass.h:228
vrpn_Button::timestamp
struct timeval timestamp
Definition: vrpn_Button.h:48
vrpn_Tracker::position_m_id
vrpn_int32 position_m_id
Definition: vrpn_Tracker.h:80
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition: vrpn_Connection.h:510
vrpn_Connection.h
vrpn_gettimeofday
#define vrpn_gettimeofday
Definition: vrpn_Shared.h:89
SPACEPOINT_PRODUCT
const unsigned SPACEPOINT_PRODUCT
Definition: vrpn_Tracker_SpacePoint.C:15
vrpn_Button
This is the base class for both the client and server for a button device (a device with one or more ...
Definition: vrpn_Button.h:30
vrpn_Tracker_SpacePoint::on_data_received
virtual void on_data_received(size_t bytes, vrpn_uint8 *buffer)
Derived class reimplements this callback.
Definition: vrpn_Tracker_SpacePoint.C:31
vrpn_Tracker_SpacePoint.h
vrpn_Tracker_SpacePoint::vrpn_Tracker_SpacePoint
vrpn_Tracker_SpacePoint(const char *name, vrpn_Connection *trackercon)
Definition: vrpn_Tracker_SpacePoint.C:20
vrpn_Tracker::pos
vrpn_float64 pos[3]
Definition: vrpn_Tracker.h:95
VRPN_SUPPRESS_EMPTY_OBJECT_WARNING
#define VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
Definition: vrpn_Configure.h:495
vrpn_BaseClassUnique::server_mainloop
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
Definition: vrpn_BaseClass.C:603