vrpn  07.33
Virtual Reality Peripheral Network
vrpn_Analog.h
Go to the documentation of this file.
1 #ifndef VRPN_ANALOG_H
2 #define VRPN_ANALOG_H
3 
4 #include <stddef.h> // for NULL
5 
6 #include "vrpn_BaseClass.h" // for vrpn_Callback_List, etc
7 #include "vrpn_Configure.h" // for VRPN_API, VRPN_CALLBACK
8 #include "vrpn_Connection.h" // for vrpn_CONNECTION_LOW_LATENCY, etc
9 #include "vrpn_Shared.h" // for timeval
10 #include "vrpn_Types.h" // for vrpn_int32, vrpn_float64, etc
11 
12 #ifndef VRPN_CLIENT_ONLY
13 #include "vrpn_Serial.h" // for ::vrpn_SER_PARITY_NONE, etc
14 #endif
15 
16 #define vrpn_CHANNEL_MAX 128
17 
18 // analog status flags
19 const int vrpn_ANALOG_SYNCING = (2);
20 const int vrpn_ANALOG_REPORT_READY = (1);
21 const int vrpn_ANALOG_PARTIAL = (0);
22 const int vrpn_ANALOG_RESETTING = (-1);
23 const int vrpn_ANALOG_FAIL = (-2);
24 
25 // Analog time value meaning "go find out what time it is right now"
26 const struct timeval vrpn_ANALOG_NOW = {0, 0};
27 
29 public:
30  vrpn_Analog(const char *name, vrpn_Connection *c = NULL);
31 
32  // Print the status of the analog device
33  void print(void);
34 
35  vrpn_int32 getNumChannels(void) const;
36 
37 protected:
38  vrpn_float64 channel[vrpn_CHANNEL_MAX];
39  vrpn_float64 last[vrpn_CHANNEL_MAX];
40  vrpn_int32 num_channel;
41  struct timeval timestamp;
42  vrpn_int32 channel_m_id; //< channel message id (message from server)
43  int status;
44 
45  virtual int register_types(void);
46 
47  //------------------------------------------------------------------
48  // Routines used to send data from the server
49  virtual vrpn_int32 encode_to(char *buf);
52  virtual void
53  report_changes(vrpn_uint32 class_of_service = vrpn_CONNECTION_LOW_LATENCY,
54  const struct timeval time = vrpn_ANALOG_NOW);
57  virtual void
58  report(vrpn_uint32 class_of_service = vrpn_CONNECTION_LOW_LATENCY,
59  const struct timeval time = vrpn_ANALOG_NOW);
60 };
61 
62 #ifndef VRPN_CLIENT_ONLY
64 public:
65  vrpn_Serial_Analog(const char *name, vrpn_Connection *connection,
66  const char *port, int baud = 9600, int bits = 8,
68  bool rts_flow = false);
70 
71 protected:
72  int serial_fd;
73  char portname[1024];
74  int baudrate;
75  unsigned char buffer[1024];
77 
78  int read_available_characters(char *buffer, int bytes);
79 };
80 #endif
81 
82 // vrpn_Analog_Server
83 // Tom Hudson, March 1999
84 //
85 // A *Sample* Analog server. Use this or derive your own from vrpn_Analog with
86 // this as a guide.
87 //
88 // Write whatever values you want into channels(), then call report()
89 // or report_changes(). (Original spec only called for report_changes(),
90 // but vrpn_Analog's assumption that "no new data = same data" doesn't
91 // match the BLT stripchart assumption of "no intervening data = ramp".
92 //
93 // For a sample application, see server_src/sample_analog.C
94 
96 
97 public:
98  vrpn_Analog_Server(const char *name, vrpn_Connection *c,
99  vrpn_int32 numChannels = vrpn_CHANNEL_MAX);
100 
102  virtual void
103  report_changes(vrpn_uint32 class_of_service = vrpn_CONNECTION_LOW_LATENCY,
104  const struct timeval time = vrpn_ANALOG_NOW);
105 
107  virtual void
108  report(vrpn_uint32 class_of_service = vrpn_CONNECTION_LOW_LATENCY,
109  const struct timeval time = vrpn_ANALOG_NOW);
110 
114  virtual void mainloop() { server_mainloop(); };
115 
117  vrpn_float64 *channels(void) { return channel; }
118 
122  vrpn_int32 setNumChannels(vrpn_int32 sizeRequested);
123 };
124 
126 // This is useful for joysticks, to allow them to be centered and
127 // scaled to cover the whole range. Rather than writing directly
128 // into the channels array, call the setChannel() method.
129 
131 public:
132  vrpn_Clipping_Analog_Server(const char *name, vrpn_Connection *c,
133  vrpn_int32 numChannels = vrpn_CHANNEL_MAX);
134 
142  int setClipValues(int channel, double min, double lowzero, double highzero,
143  double max);
144 
148  int setChannelValue(int channel, double value);
149 
150 protected:
151  typedef struct {
152  double minimum_val; // Value mapped to -1
153  double lower_zero; // Minimum value mapped to 0
154  double upper_zero; // Maximum value mapped to 0
155  double maximum_val; // Value mapped to 1
156  } clipvals_struct;
157 
159 };
160 
161 //----------------------------------------------------------
162 //************** Users deal with the following *************
163 
164 // User routine to handle a change in analog values. This is called when
165 // the analog callback is called (when a message from its counterpart
166 // across the connection arrives).
167 
168 typedef struct _vrpn_ANALOGCB {
169  struct timeval msg_time; // Timestamp of analog data
170  vrpn_int32 num_channel; // how many channels
171  vrpn_float64 channel[vrpn_CHANNEL_MAX]; // analog values
172 } vrpn_ANALOGCB;
173 
174 typedef void(VRPN_CALLBACK *vrpn_ANALOGCHANGEHANDLER)(void *userdata,
175  const vrpn_ANALOGCB info);
176 
177 // Open an analog device that is on the other end of a connection
178 // and handle updates from it. This is the type of analog device
179 // that user code will deal with.
180 
182 public:
183  // The name of the analog device to connect to
184  // Optional argument to be used when the Remote should listen on
185  // a connection that is already open.
186  vrpn_Analog_Remote(const char *name, vrpn_Connection *c = NULL);
187 
188  // This routine calls the mainloop of the connection it's on
189  virtual void mainloop();
190 
191  // (un)Register a callback handler to handle analog value change
192  virtual int register_change_handler(void *userdata,
193  vrpn_ANALOGCHANGEHANDLER handler)
194  {
195  return d_callback_list.register_handler(userdata, handler);
196  };
197  virtual int unregister_change_handler(void *userdata,
198  vrpn_ANALOGCHANGEHANDLER handler)
199  {
200  return d_callback_list.unregister_handler(userdata, handler);
201  }
202 
203 protected:
205 
206  static int VRPN_CALLBACK
207  handle_change_message(void *userdata, vrpn_HANDLERPARAM p);
208 };
209 
210 #endif
#define min(x, y)
Definition: vrpn_WiiMote.C:47
const struct timeval vrpn_ANALOG_NOW
Definition: vrpn_Analog.h:26
const vrpn_uint32 vrpn_CONNECTION_LOW_LATENCY
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
const int vrpn_ANALOG_PARTIAL
Definition: vrpn_Analog.h:21
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...
Definition: vrpn_Analog.C:71
vrpn_int32 num_channel
Definition: vrpn_Analog.h:170
class VRPN_API vrpn_Clipping_Analog_Server
vrpn_float64 * channels(void)
Exposes an array of values for the user to write into.
Definition: vrpn_Analog.h:117
vrpn_SER_PARITY
Definition: vrpn_Serial.h:15
vrpn_int32 channel_m_id
Definition: vrpn_Analog.h:42
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_float64 channel[vrpn_CHANNEL_MAX]
Definition: vrpn_Analog.h:38
Generic connection class not specific to the transport mechanism.
virtual void mainloop()
For this server, the user must normally call report() or report_changes() directly. This mainloop() only takes care of the things any server object should do.
Definition: vrpn_Analog.h:114
virtual int register_types(void)=0
Register the types of messages this device sends/receives. Return 0 on success, -1 on fail...
Analog server that can scale and clip its range to -1..1.
Definition: vrpn_Analog.h:130
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 ...
Definition: vrpn_Analog.C:94
#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 num_channel
Definition: vrpn_Analog.h:40
vrpn_Callback_List< vrpn_ANALOGCB > d_callback_list
Definition: vrpn_Analog.h:204
This structure is what is passed to a vrpn_Connection message callback.
virtual int register_change_handler(void *userdata, vrpn_ANALOGCHANGEHANDLER handler)
Definition: vrpn_Analog.h:192
const int vrpn_ANALOG_SYNCING
Definition: vrpn_Analog.h:19
virtual int unregister_change_handler(void *userdata, vrpn_ANALOGCHANGEHANDLER handler)
Definition: vrpn_Analog.h:197
#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...
const int vrpn_ANALOG_FAIL
Definition: vrpn_Analog.h:23
const int vrpn_ANALOG_RESETTING
Definition: vrpn_Analog.h:22
void(VRPN_CALLBACK * vrpn_ANALOGCHANGEHANDLER)(void *userdata, const vrpn_ANALOGCB info)
Definition: vrpn_Analog.h:174
const int vrpn_ANALOG_REPORT_READY
Definition: vrpn_Analog.h:20