vrpn  07.33
Virtual Reality Peripheral Network
vrpn_Dial.h
Go to the documentation of this file.
1 // vrpn_Dial.h
2 // This implements a Dial class. A dial is an object that spins,
3 // possibly without bound. It returns the fraction of a revolution that
4 // it has turned as its message type.
5 
6 #ifndef VRPN_DIAL_H
7 #define VRPN_DIAL_H
8 
9 const int vrpn_DIAL_MAX = 128;
10 
11 #include <stddef.h> // for NULL
12 
13 #include "vrpn_BaseClass.h" // for vrpn_Callback_List, etc
14 #include "vrpn_Configure.h" // for VRPN_API, VRPN_CALLBACK
15 #include "vrpn_Shared.h" // for timeval
16 #include "vrpn_Types.h" // for vrpn_float64, vrpn_int32
17 
19 struct vrpn_HANDLERPARAM;
20 
22 public:
23  vrpn_Dial(const char *name, vrpn_Connection *c = NULL);
24 
25 protected:
26  vrpn_float64 dials[vrpn_DIAL_MAX];
27  vrpn_int32 num_dials;
28  struct timeval timestamp;
29  vrpn_int32 change_m_id; // change message id
30 
31  virtual int register_types(void);
32  virtual vrpn_int32 encode_to(char *buf, vrpn_int32 buflen, vrpn_int32 dial,
33  vrpn_float64 delta);
34  virtual void report_changes(void); // send report iff changed
35  virtual void report(void); // send report
36 };
37 
38 //----------------------------------------------------------
39 // Example server for an array of dials
40 // This will generate an array of dials that all spin at the same
41 // rate (revolutions/second), and which send reports at a different rate
42 // (updates/second). A real server would send reports whenever it saw
43 // dials changing, and would not have the spin_rate or update_rate parameters.
44 // This server can be used for testing to make sure a client is
45 // working correctly, and to ensure that a connection to a remote server
46 // is working (by running the example server with the name of the device that
47 // the real server would use).
48 
50 public:
51  vrpn_Dial_Example_Server(const char *name, vrpn_Connection *c,
52  vrpn_int32 numdials = 1,
53  vrpn_float64 spin_rate = 1.0,
54  vrpn_float64 update_rate = 10.0);
55  virtual void mainloop();
56 
57 protected:
58  vrpn_float64 _spin_rate; // The rate at which to spin (revolutions/sec)
59  vrpn_float64 _update_rate; // The rate at which to update (reports/sec)
60  // The dials[] array within the parent is used for the values
61  // The num_dials within the parent is used
62  // The timestamp field within the parent structure is used for timing
63  // The report_changes() or report() functions within the parent are used
64 };
65 
66 //----------------------------------------------------------
67 //************** Users deal with the following *************
68 
69 // User routine to handle a change in dial values. This is called when
70 // the dial callback is called (when a message from its counterpart
71 // across the connetion arrives).
72 
73 typedef struct _vrpn_DIALCB {
74  struct timeval msg_time; // Timestamp when change happened
75  vrpn_int32 dial; // which dial changed
76  vrpn_float64 change; // Fraction of a revolution it changed
77 } vrpn_DIALCB;
78 
79 typedef void(VRPN_CALLBACK *vrpn_DIALCHANGEHANDLER)(void *userdata,
80  const vrpn_DIALCB info);
81 
82 // Open a dial device that is on the other end of a connection
83 // and handle updates from it. This is the type of device
84 // that user code will deal with.
85 
87 public:
88  // The name of the device to connect to.
89  // Optional argument to be used when the Remote MUST listen on
90  // a connection that is already open.
91  vrpn_Dial_Remote(const char *name, vrpn_Connection *c = NULL);
93 
94  // This routine calls the mainloop of the connection it's on
95  virtual void mainloop();
96 
97  // (un)Register a callback handler to handle dial updates
98  virtual int register_change_handler(void *userdata,
99  vrpn_DIALCHANGEHANDLER handler)
100  {
101  return d_callback_list.register_handler(userdata, handler);
102  };
103  virtual int unregister_change_handler(void *userdata,
104  vrpn_DIALCHANGEHANDLER handler)
105  {
106  return d_callback_list.unregister_handler(userdata, handler);
107  }
108 
109 protected:
111 
112  static int VRPN_CALLBACK
113  handle_change_message(void *userdata, vrpn_HANDLERPARAM p);
114 };
115 
116 #endif
vrpn_BaseClass.h
vrpn_DIALCB::change
vrpn_float64 change
Definition: vrpn_Dial.h:76
vrpn_Types.h
vrpn_DIALCB::dial
vrpn_int32 dial
Definition: vrpn_Dial.h:75
vrpn_Dial_Remote
Definition: vrpn_Dial.h:86
vrpn_DIALCB
Definition: vrpn_Dial.h:73
vrpn_Callback_List< vrpn_DIALCB >
vrpn_Dial_Example_Server::_update_rate
vrpn_float64 _update_rate
Definition: vrpn_Dial.h:59
vrpn_Dial_Remote::unregister_change_handler
virtual int unregister_change_handler(void *userdata, vrpn_DIALCHANGEHANDLER handler)
Definition: vrpn_Dial.h:103
vrpn_Dial_Remote::register_change_handler
virtual int register_change_handler(void *userdata, vrpn_DIALCHANGEHANDLER handler)
Definition: vrpn_Dial.h:98
vrpn_Dial
Definition: vrpn_Dial.h:21
vrpn_HANDLERPARAM
This structure is what is passed to a vrpn_Connection message callback.
Definition: vrpn_Connection.h:44
vrpn_Shared.h
vrpn_Dial_Example_Server
Definition: vrpn_Dial.h:49
vrpn_BaseClass::mainloop
virtual void mainloop()=0
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition: vrpn_Connection.h:510
vrpn_BaseClass::register_types
virtual int register_types(void)=0
Register the types of messages this device sends/receives. Return 0 on success, -1 on fail.
VRPN_CALLBACK
#define VRPN_CALLBACK
Definition: vrpn_Configure.h:647
vrpn_DIALCHANGEHANDLER
void(VRPN_CALLBACK * vrpn_DIALCHANGEHANDLER)(void *userdata, const vrpn_DIALCB info)
Definition: vrpn_Dial.h:79
vrpn_Dial_Example_Server::_spin_rate
vrpn_float64 _spin_rate
Definition: vrpn_Dial.h:58
vrpn_Dial::change_m_id
vrpn_int32 change_m_id
Definition: vrpn_Dial.h:29
vrpn_Configure.h
vrpn_Dial::num_dials
vrpn_int32 num_dials
Definition: vrpn_Dial.h:27
VRPN_API
#define VRPN_API
Definition: vrpn_Configure.h:646
vrpn_BaseClass
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
Definition: vrpn_BaseClass.h:313
vrpn_DIAL_MAX
const int vrpn_DIAL_MAX
Definition: vrpn_Dial.h:9
vrpn_Dial_Remote::d_callback_list
vrpn_Callback_List< vrpn_DIALCB > d_callback_list
Definition: vrpn_Dial.h:110