vrpn  07.33
Virtual Reality Peripheral Network
vrpn_Analog_Output.h
Go to the documentation of this file.
1 // vrpn_Analog_Output.h
2 // David Borland, September 2002
3 //
4 // These classes are for setting values for an analog output device. The
5 // vrpn_Analog was getting overloaded by trying to have functionality for both
6 // reading and writing in it. If wanting to read analog values from a device, a
7 // vrpn_Analog should be used, if wanting to write analog values to a device, a
8 // vrpn_Analog_Output should be used. This is similar to the Tracker/Poser
9 // dichotomy.
10 
11 #ifndef VRPN_ANALOG_OUTPUT_H
12 #define VRPN_ANALOG_OUTPUT_H
13 
14 #include <stddef.h> // for NULL
15 
16 #include "vrpn_Analog.h" // for vrpn_CHANNEL_MAX
17 #include "vrpn_BaseClass.h" // for vrpn_Callback_List, etc
18 #include "vrpn_Configure.h" // for VRPN_CALLBACK, VRPN_API
19 #include "vrpn_Connection.h" // for vrpn_CONNECTION_RELIABLE, etc
20 #include "vrpn_Shared.h" // for timeval
21 #include "vrpn_Types.h" // for vrpn_int32, vrpn_float64, etc
22 
23 // Similar to vrpn_Analog, but messages are different
24 // Members beginning with o_ are also found in vrpn_Analog, the o_ is
25 // so that you can derive a class from both without getting ambiguities
27 public:
28  vrpn_Analog_Output(const char* name, vrpn_Connection* c = NULL);
29 
30  // Print the status of the analog output device
31  void o_print(void);
32 
33  vrpn_int32 getNumChannels() const { return o_num_channel; }
34 
35 protected:
36  vrpn_float64 o_channel[vrpn_CHANNEL_MAX];
37  vrpn_int32 o_num_channel;
38  struct timeval o_timestamp;
39  vrpn_int32 request_m_id; //< Request to change message from client
40  vrpn_int32 request_channels_m_id; //< Request to change channels message
41  // from client
42  vrpn_int32 report_num_channels_m_id; //< Report of the number of active
43  // channels, from the server
44  vrpn_int32 got_connection_m_id; //< new-connection notification
45  int o_status;
46 
47  virtual int register_types(void);
48 };
49 
50 // A *Sample* Analog output server. Use this, or derive your own server
51 // from vrpn_Analog_Output with this as a guide. You can remove the
52 // user-level callback code (both the type before this class and the
53 // list and the handler register/deregister) if the server is controlling
54 // a device directly.
55 
57 public:
58  vrpn_Analog_Output_Server(const char* name, vrpn_Connection* c,
59  vrpn_int32 numChannels = vrpn_CHANNEL_MAX);
60  virtual ~vrpn_Analog_Output_Server(void);
61 
62  virtual void mainloop() { server_mainloop(); }
63 
67  vrpn_int32 setNumChannels(vrpn_int32 sizeRequested);
68 
70  const vrpn_float64* o_channels(void) const { return o_channel; };
71 
72 protected:
73  virtual bool report_num_channels(
74  vrpn_uint32 class_of_service = vrpn_CONNECTION_RELIABLE);
75  virtual vrpn_int32 encode_num_channels_to(char* buf, vrpn_int32 num);
76 
81  static int VRPN_CALLBACK
82  handle_request_message(void* userdata, vrpn_HANDLERPARAM p);
83 
88  static int VRPN_CALLBACK
89  handle_request_channels_message(void* userdata, vrpn_HANDLERPARAM p);
90 
93  static int VRPN_CALLBACK
94  handle_got_connection(void* userdata, vrpn_HANDLERPARAM p);
95 };
96 
97 // A more complicated analog server that provides a
98 // user routine to handle a change in analog values. This is called when
99 // the analog callback is called (when a message from its counterpart
100 // across the connection arrives). This callback is called whenever
101 // EITHER type of change message arrives (either a single-channel change
102 // or a multiple-channel change.
103 
104 typedef struct _vrpn_ANALOGOUTPUTCB {
105  struct timeval msg_time; // Timestamp of analog data
106  vrpn_int32 num_channel; // how many channels
107  const vrpn_float64* channel; // analog values (pointer to channels)
109 
111  void* userdata, const vrpn_ANALOGOUTPUTCB info);
112 
114  : public vrpn_Analog_Output_Server {
115 public:
117  const char* name, vrpn_Connection* c,
118  vrpn_int32 numChannels = vrpn_CHANNEL_MAX);
119  virtual ~vrpn_Analog_Output_Callback_Server(void);
120 
121  // (un)Register a callback handler to handle analog value change.
122  // These will be called whenever EITHER type of change message is
123  // received, either a single channel or multiple channels. This is
124  // useful for applications that "have a" server, rather than derive
125  // from the server.
126  virtual int register_change_handler(void* userdata,
128  {
129  return d_callback_list.register_handler(userdata, handler);
130  };
131  virtual int
134  {
135  return d_callback_list.unregister_handler(userdata, handler);
136  }
137 
138 protected:
143  static int VRPN_CALLBACK
144  handle_change_message(void* userdata, vrpn_HANDLERPARAM p);
145 
149 };
150 
151 // Open an analog output device that is on the other end of a connection
152 // and send updates to it. This is the type of analog output device
153 // that user code will deal with.
155 public:
156  // The name of the analog device to connect to
157  // Optional argument to be used when the Remote should listen on
158  // a connection that is already open.
159  vrpn_Analog_Output_Remote(const char* name, vrpn_Connection* c = NULL);
160  virtual ~vrpn_Analog_Output_Remote(void);
161 
162  // This routine calls the mainloop of the connection it's on
163  virtual void mainloop();
164 
165  // Request the analog to change its value to the one specified.
166  // Returns false on failure.
167  virtual bool request_change_channel_value(
168  unsigned int chan, vrpn_float64 val,
169  vrpn_uint32 class_of_service = vrpn_CONNECTION_RELIABLE);
170 
171  // Request the analog to change values all at once. If more values are
172  // given
173  // than we have channels, the extra values are discarded. If less values
174  // are
175  // given than we have channels, the extra channels are set to 0.
176  // Returns false on failure
177  virtual bool request_change_channels(
178  int num, vrpn_float64* vals,
179  vrpn_uint32 class_of_service = vrpn_CONNECTION_RELIABLE);
180 
181 protected:
182  // How we hear about the number of active channels
183  static int VRPN_CALLBACK
184  handle_report_num_channels(void* userdata, vrpn_HANDLERPARAM p);
185 
186  // Routines used to send requests from the client
187  virtual vrpn_int32 encode_change_to(char* buf, vrpn_int32 chan,
188  vrpn_float64 val);
189  virtual vrpn_int32 encode_change_channels_to(char* buf, vrpn_int32 num,
190  vrpn_float64* vals);
191 };
192 
193 #endif
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
class VRPN_API vrpn_Analog_Output_Remote
vrpn_int32 request_channels_m_id
const vrpn_uint32 vrpn_CONNECTION_RELIABLE
Classes of service for messages, specify multiple by ORing them together Priority of satisfying these...
void(VRPN_CALLBACK * vrpn_ANALOGOUTPUTCHANGEHANDLER)(void *userdata, const vrpn_ANALOGOUTPUTCB info)
vrpn_float64 o_channel[vrpn_CHANNEL_MAX]
vrpn_Callback_List< vrpn_ANALOGOUTPUTCB > d_callback_list
List of user-level routines that need to be called back to let them know that the values have changed...
Generic connection class not specific to the transport mechanism.
virtual int register_types(void)=0
Register the types of messages this device sends/receives. Return 0 on success, -1 on fail.
const vrpn_float64 * o_channels(void) const
Exposes an array of values for the user to read from.
#define VRPN_CALLBACK
#define VRPN_API
All types of client/server/peer objects in VRPN should be derived from the vrpn_BaseClass type descri...
virtual void mainloop()=0
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
vrpn_int32 getNumChannels() const
const vrpn_float64 * channel
virtual int register_change_handler(void *userdata, vrpn_ANALOGOUTPUTCHANGEHANDLER handler)
This structure is what is passed to a vrpn_Connection message callback.
vrpn_int32 got_connection_m_id
vrpn_int32 report_num_channels_m_id
#define vrpn_CHANNEL_MAX
Definition: vrpn_Analog.h:16
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
virtual int unregister_change_handler(void *userdata, vrpn_ANALOGOUTPUTCHANGEHANDLER handler)
virtual void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...