22 static const unsigned char CMD_BASIC = static_cast<unsigned char>(0xC0);
23 static const unsigned char CMD_HOMEREF = static_cast<unsigned char>(0xC1);
24 static const unsigned char CMD_HOMEPOS = static_cast<unsigned char>(0xC2);
25 static const unsigned char CMD_SETHOME = static_cast<unsigned char>(0xC3);
26 static const unsigned char CMD_BAUDSET = static_cast<unsigned char>(0xC4);
27 static const unsigned char CMD_ENDSESS = static_cast<unsigned char>(0xC5);
28 static const unsigned char CMD_GETMAXS = static_cast<unsigned char>(0xC6);
29 static const unsigned char CMD_SETPARM = static_cast<unsigned char>(0xC7);
30 static const unsigned char CMD_GETNAME = static_cast<unsigned char>(0xC8);
31 static const unsigned char CMD_GETPRID = static_cast<unsigned char>(0xC9);
32 static const unsigned char CMD_GETMODL = static_cast<unsigned char>(0xCA);
33 static const unsigned char CMD_GETSERN = static_cast<unsigned char>(0xCB);
34 static const unsigned char CMD_GETCOMM = static_cast<unsigned char>(0xCC);
35 static const unsigned char CMD_GETPERF = static_cast<unsigned char>(0xCD);
36 static const unsigned char CMD_GETVERS = static_cast<unsigned char>(0xCE);
37 static const unsigned char CMD_GETMOTN = static_cast<unsigned char>(0xCF);
38 static const unsigned char CMD_SETHREF = static_cast<unsigned char>(0xD0);
39 static const unsigned char CMD_FACREST = static_cast<unsigned char>(0xD1);
40 static const unsigned char CMD_INSMARK = static_cast<unsigned char>(0xD2);
48 #define STATUS_RESETTING (-1) // Resetting the box 49 #define STATUS_SYNCING (0) // Looking for the first character of report 50 #define STATUS_READING (1) // Looking for the rest of the report 57 #define MAX_TIME_INTERVAL (2000000) // max time between reports (usec) 59 static void pause (
double delay) {
62 unsigned long interval = (long) floor(1000000.0 * delay);
64 struct timeval start, now;
83 const int numchannels,
84 const int numencoders):
88 _numbuttons(numbuttons),
89 _numchannels(numchannels),
90 _numencoders(numencoders)
163 if (syncBaudrate (10.0)) {
164 printf(
"vrpn_ImmersionBox found\n");
169 if (0 == sendIboxCommand((
char) CMD_GETNAME, (
char *) &iname, .1)) {
170 fprintf(stderr,
"problems with ibox command CMD_GETNAME\n");
173 if (0 == sendIboxCommand((
char) CMD_GETPRID, (
char *) &
id, .1)) {
174 fprintf(stderr,
"problems with ibox command CMD_GETPRID\n");
177 if (0 == sendIboxCommand((
char) CMD_GETMODL, (
char *) &model, .1)){
178 fprintf(stderr,
"problems with ibox command CMD_GETMODL\n");
181 if (0 == sendIboxCommand((
char) CMD_GETSERN, (
char *) &serial, .1)){
182 fprintf(stderr,
"problems with ibox command CMD_GETSERN\n");
185 if (0 == sendIboxCommand((
char) CMD_GETCOMM, (
char *) &comment, .1)){
186 fprintf(stderr,
"problems with ibox command CMD_GETCOMM\n");
189 if (0 == sendIboxCommand((
char) CMD_GETPERF, (
char *) &parmf, .1)){
190 fprintf(stderr,
"problems with ibox command CMD_GETPERF\n");
193 if (0 == sendIboxCommand((
char) CMD_GETVERS, (
char *) &vers, .1)){
194 fprintf(stderr,
"problems with ibox command CMD_GETVERS\n");
199 printf(
"%s\n%s\n%s\n%s\n%s\n%s\n", iname,
id, serial, comment, parmf, vers);
208 unsigned char command[26] =
"";
211 command[2] = (
unsigned char) 10;
212 command[3] = commandByte;
214 for (
int i = 4; i < 25;i++)
223 fprintf(stderr,
"vrpnImmersionBox::reset: could not write command\n");
232 if (result <= 0 || command[0] != 0xcf) {
233 fprintf(stderr,
"vrpnImmersionBox::reset: no command echo\n");
240 printf(
"ImmersionBox reset complete.\n");
267 unsigned int buttonBits = 0;
269 struct timeval timeout;
271 timeout.tv_usec = 100000;
278 if (
buffer[0] != dataPacketHeader) {
296 (
unsigned char *) responseString,
300 if (result < dataRecordLength) {
306 buttonBits = responseString[0];
315 cerr <<
"left button pressed " << endl;
383 struct timeval current_time;
386 fprintf(stderr,
"Tracker failed to read... current_time=%ld:%ld, timestamp=%ld:%ld\n",
387 current_time.tv_sec, static_cast<long>(current_time.tv_usec),
396 fprintf(stderr,
"vrpn_ImmersionBox: Unknown mode (internal error)\n");
405 int vrpn_ImmersionBox::sendIboxCommand (
char cmd,
char * returnString,
double delay)
407 struct timeval timeout;
409 timeout.tv_usec = 15000;
420 if (NULL == returnString)
433 if (!(responseString[0] & 0x80))
437 if ((responseString[0] & 0x7f) != (cmd & 0x7f))
441 strcpy (returnString, &responseString[1]);
449 int vrpn_ImmersionBox::syncBaudrate (
double seconds) {
451 struct timeval miniDelay;
452 miniDelay.tv_sec = 0;
453 miniDelay.tv_usec = 50000;
455 unsigned long maxDelay = 1000000L * (long) seconds;
456 struct timeval start_time;
460 unsigned char responseString[8];
461 unsigned char * matchString = (
unsigned char *)
S_INITIALIZE ;
471 struct timeval current_time;
475 fprintf(stderr,
"vrpn_ImmersionBox::syncBaudrate timeout expired: %lf secs \n", seconds);
481 fprintf(stderr,
"vrpn_ImmersionBox::syncBaudrate could not write to serial port\n");
494 for (index = 0; index < 4; index++) {
496 if (responseString[index] != matchString[index])
509 matchString = (
unsigned char *)
"IBOX";
517 for (index = 0; index < 4; index++) {
519 if (responseString[index] != matchString[index])
#define MAX_TIME_INTERVAL
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
int vrpn_write_characters(int comm, const unsigned char *buffer, size_t bytes)
Write the buffer to the serial port.
virtual void report_changes(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report only if something has changed (for servers) Optionally, tell what time to stamp the val...
unsigned char buffer[1024]
virtual void report_changes(void)
int vrpn_flush_input_buffer(int comm)
Throw out any characters within the input buffer.
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_float64 channel[vrpn_CHANNEL_MAX]
Generic connection class not specific to the transport mechanism.
virtual int get_report(void)
virtual void report(vrpn_uint32 class_of_service=vrpn_CONNECTION_LOW_LATENCY, const struct timeval time=vrpn_ANALOG_NOW)
Send a report whether something has changed or not (for servers) Optionally, tell what time to stamp ...
All types of client/server/peer objects in VRPN should be derived from the vrpn_BaseClass type descri...
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
virtual void report(void)
virtual void clear_values(void)
virtual void mainloop(void)
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
int send_text_message(const char *msg, struct timeval timestamp, vrpn_TEXT_SEVERITY type=vrpn_TEXT_NORMAL, vrpn_uint32 level=0)
Sends a NULL-terminated text message from the device d_sender_id.
vrpn_ImmersionBox(const char *name, vrpn_Connection *c, const char *port, int baud, const int numbuttons, const int numchannels, const int numencoders)
#define vrpn_gettimeofday
unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
vrpn_float64 last[vrpn_CHANNEL_MAX]
vrpn_float64 dials[vrpn_DIAL_MAX]