VRPN 06.04 |
An application can open an analog device, like a joystick or set of sliders, which can send analog values back. After defining the analog device object, analog values in different channels can be read through callback handlers. Selections from the #include file//---------------------------------------------------------- //************** Users deal with the following ************* // User routine to handle a change . This is called when // the analog callback is called (when a message from its counterpart // across the connetion arrives). typedef struct { struct timeval msg_time; // Time of button press/release int num_channel; // how many channels double channel[vrpn_CHANNEL_MAX]; // channel diliever analog values } vrpn_ANALOGCB; typedef void (*vrpn_ANALOGCHANGEHANDLER)(void *userdata, const vrpn_ANALOGCB info); // Open a analog device that is on the other end of a connection // and handle updates from it. This is the type of analog device // that user code will deal with. class vrpn_Analog_Remote: public vrpn_Analog { public: // The name of the button device to connect to vrpn_Analog_Remote(char *name); // This routine calls the mainloop of the connection it's on virtual void mainloop(void); // (un)Register a callback handler to handle a button state change virtual int register_change_handler(void *userdata, vrpn_ANALOGCHANGEHANDLER handler); virtual int unregister_change_handler(void *userdata, vrpn_ANALOGCHANGEHANDLER handler); protected: typedef struct vrpn_RBCS { void *userdata; vrpn_ANALOGCHANGEHANDLER handler; struct vrpn_RBCS *next; } vrpn_ANALOGCHANGELIST; vrpn_ANALOGCHANGELIST *change_list; static int handle_change_message(void *userdata, vrpn_HANDLERPARAM p); }; Using vrpn_Analog_RemoteA vrpn_Analog_Remote object is used by passing the name of the analog device you want to connect to as a parameter to the constructor, and then using the analog device interface to read in analog values in different channels. Here is a simple example program that just prints the new values of each channels as the reports arrive: #include <stdlib.h> #include <stdio.h> #include <vrpn_Analog.h> /***************************************************************************** * Callback handler * *****************************************************************************/ void handle_analog(void *userdata, vrpn_ANALOGCB b) { for (int i=0; i< b.num_channel; i++) printf("Chan[%d] = %lf\n", i, b.channel[i]); } int main(int argc, char *argv[]) { int done = 0; vrpn_Analog_Remote *ana; /* initialize the analog */ ana = new vrpn_Analog_Remote("analog0@ioglab"); // Set up the analog callback handler ana->register_change_handler(NULL, handle_analog); /* * main interactive loop */ while ( ! done ) { // Let the analog device do its thing ana->mainloop(); } } /* main */ |