vrpn  07.33
Virtual Reality Peripheral Network
vrpn_Button.h
Go to the documentation of this file.
1 #ifndef VRPN_BUTTON_H
2 #include <stddef.h> // for NULL
3 
4 #include "vrpn_BaseClass.h" // for vrpn_Callback_List, etc
5 #include "vrpn_Configure.h" // for VRPN_API, VRPN_CALLBACK
6 #include "vrpn_Shared.h" // for timeval
7 #include "vrpn_Types.h" // for vrpn_int32, vrpn_float64, etc
8 
10 struct vrpn_HANDLERPARAM;
11 
12 const int vrpn_BUTTON_MAX_BUTTONS = 256;
13 const int VRPN_BUTTON_BUF_SIZE = 256;
14 
15 // Base class for buttons. Definition
16 // of remote button class for the user is at the end.
17 
18 const int vrpn_BUTTON_MOMENTARY = 10;
19 const int vrpn_BUTTON_TOGGLE_OFF = 20;
20 const int vrpn_BUTTON_TOGGLE_ON = 21;
21 const int vrpn_BUTTON_LIGHT_OFF = 30;
22 const int vrpn_BUTTON_LIGHT_ON = 31;
23 const int vrpn_ALL_ID = -99;
24 
31 public:
32  vrpn_Button(const char *name, vrpn_Connection *c = NULL);
33  virtual ~vrpn_Button(void);
34 
35  // Print the status of the button
36  void print(void);
37 
38  virtual void set_momentary(vrpn_int32 which_button);
39  virtual void set_toggle(vrpn_int32 which_button, vrpn_int32 current_state);
40  virtual void set_all_momentary(void);
41  virtual void set_all_toggle(vrpn_int32 default_state);
42 
43 protected:
44  unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS];
45  unsigned char lastbuttons[vrpn_BUTTON_MAX_BUTTONS];
46  vrpn_int32 minrate[vrpn_BUTTON_MAX_BUTTONS];
47  vrpn_int32 num_buttons;
48  struct timeval timestamp;
49  vrpn_int32 change_message_id; // ID of change button message to connection
50  vrpn_int32 states_message_id; // ID of button-states message to connection
51  vrpn_int32 admin_message_id; // ID of admin button message to connection
52 
53  virtual int register_types(void);
54  virtual void report_changes(void);
55  virtual void report_states(void); // Calls Button or Button_Filter encode
56  virtual vrpn_int32 encode_to(char *buf, vrpn_int32 button,
57  vrpn_int32 state);
58  virtual vrpn_int32 encode_states_to(char *buf);
59 };
60 
66 public:
67  vrpn_int32 buttonstate[vrpn_BUTTON_MAX_BUTTONS];
68  virtual void set_momentary(vrpn_int32 which_button);
69  virtual void set_toggle(vrpn_int32 which_button, vrpn_int32 current_state);
70  virtual void set_all_momentary(void);
71  virtual void set_all_toggle(vrpn_int32 default_state);
72  void set_alerts(vrpn_int32);
73 
74 protected:
76  vrpn_Button_Filter(const char *, vrpn_Connection *c = NULL);
77  vrpn_int32
78  alert_message_id; // used to send back to alert button box for lights
79  virtual vrpn_int32 encode_states_to(char *buf);
80  virtual void report_changes(void);
81 
82  // This method makes sure we send a states message whenever we get a ping
83  // from
84  // a client object or a new connection.
85  static int VRPN_CALLBACK
86  handle_ping_message(void *userdata, vrpn_HANDLERPARAM p);
87 };
88 
89 #ifndef VRPN_CLIENT_ONLY
90 
91 // Button server that lets you set the values for the buttons directly and
92 // then have it update if needed. This class should be used by devices that
93 // can have several sets of buttons in them and don't want to derive from the
94 // Button class themselves. An example is the InterSense 900 features found in
95 // the Fastrak server (which may have several button devices, one for each
96 // sensor).
97 
99 public:
100  vrpn_Button_Server(const char *name, vrpn_Connection *c,
101  int numbuttons = 1);
102 
104  int number_of_buttons(void);
105 
108  virtual void mainloop();
109 
111  int set_button(int button, int new_value);
112 };
113 
114 // Example button server code. This button device causes its buttons to
115 // be pressed and released at the interval specified (default 1/sec). It
116 // has the specified number of buttons (default 1).
117 // This class is derived from the vrpn_Button_Filter class, so that it
118 // can be made to toggle its buttons using messages from the client.
119 
121 public:
122  vrpn_Button_Example_Server(const char *name, vrpn_Connection *c,
123  int numbuttons = 1, vrpn_float64 rate = 1.0);
124 
125  virtual void mainloop();
126 
127 protected:
128  vrpn_float64 _update_rate; // How often to toggle
129 };
130 
131 // Button device that is connected to a parallel port and uses the
132 // status bits to read from the buttons. There can be up to 5 buttons
133 // read this way.
135 public:
136  // Open a button connected to the local machine, talk to the
137  // outside world through the connection.
138  vrpn_Button_Parallel(const char *name, vrpn_Connection *connection,
139  int portno, unsigned porthex = 0);
141 
142 protected:
143  int port;
144  int status;
145 
146  virtual void read(void) = 0;
147 #ifdef _WIN32
148  int openGiveIO(void);
149 #endif // _WIN32
150 };
151 
152 // Open a Python (or Hiball Button) that is connected to a parallel port.
153 // See www.vrpn.org/UNC_python.html for a description of how to make
154 // a connector that uses the parallel port this way. Note that this
155 // use of a parallel port can result in damage to the motherboard if
156 // voltage spikes (static) are passed through if care is not taken.
157 // This interface is intended for use at UNC. No warranty is expressed
158 // or implied for use elsewhere (use at your own risk).
160 public:
161  vrpn_Button_Python(const char *name, vrpn_Connection *c, int p);
162  vrpn_Button_Python(const char *name, vrpn_Connection *c, int p,
163  unsigned ph);
164 
165  virtual void mainloop();
166 
167 protected:
168  virtual void read(void);
170 };
171 
172 // Button device that is connected to the serial port.
174 public:
175  vrpn_Button_Serial(const char *name, vrpn_Connection *c,
176  const char *port = "/dev/ttyS1/", long baud = 38400);
177  virtual ~vrpn_Button_Serial();
178 
179 protected:
180  char portname[VRPN_BUTTON_BUF_SIZE];
181  long baudrate;
183  int status;
184 
185  unsigned char
186  buffer[VRPN_BUTTON_BUF_SIZE]; // char read from the button so far
187  vrpn_uint32 bufcount; // number of char in the buffer
188 
189  virtual void read() = 0;
190 };
191 
192 // Open a Fakespace Pinch Glove System that is connected to a serial port. There
193 // are total of 10 buttons. Buttons 0-4 are fingers for the right hand-thumb
194 // first and pinkie last-while buttons 5-9 are for the left hand-thumb first.
195 // The report you get back is the finger is touching. So you will not have a
196 // state where only one button is ON.
198 public:
199  vrpn_Button_PinchGlove(const char *name, vrpn_Connection *c,
200  const char *port = "/dev/ttyS1/", long baud = 38400);
201 
202  virtual void mainloop();
203 
204 protected:
206  virtual void read();
207  void
208  report_no_timestamp(); // set the glove to report data without timestamp
209 };
210 
211 #endif // VRPN_CLIENT_ONLY
212 
213 //----------------------------------------------------------
214 //************** Users deal with the following *************
215 
216 // User routine to handle a change in button state. This is called when
217 // the button callback is called (when a message from its counterpart
218 // across the connection arrives). The pinch glove has 5 different states of on
219 // since it knows which fingers are touching. This pinch glove behavior is
220 // non-standard and will be removed in a future version. Button states should
221 // be considered like booleans.
222 #define VRPN_BUTTON_OFF (0)
223 #define VRPN_BUTTON_ON (1)
224 
225 typedef struct _vrpn_BUTTONCB {
226  struct timeval msg_time; // Time of button press/release
227  vrpn_int32 button; // Which button (numbered from zero)
228  vrpn_int32 state; // button state (0 = off, 1 = on)
229 } vrpn_BUTTONCB;
230 typedef void(VRPN_CALLBACK *vrpn_BUTTONCHANGEHANDLER)(void *userdata,
231  const vrpn_BUTTONCB info);
232 
233 // This is a new button callback type that was added in VRPN 7.31. It
234 // tells the current state of all of the buttons on the device. It is
235 // called whenever a button server receives a new connection request. It
236 // is intended to deal with the issue of not knowing what state toggled
237 // buttons are in when a client connects.
238 typedef struct _vrpn_BUTTONSTATECB {
239  struct timeval msg_time; // Timestamp of analog data
240  vrpn_int32 num_buttons; // how many buttons
241  vrpn_int32 states[vrpn_BUTTON_MAX_BUTTONS]; // button state values
244  void *userdata, const vrpn_BUTTONSTATESCB info);
245 
246 // Open a button that is on the other end of a connection
247 // and handle updates from it. This is the type of button that user code will
248 // deal with.
249 
251 public:
252  // The name of the button device to connect to. Optional second
253  // argument is used when you already have an open connection you
254  // want it to listen on.
255  vrpn_Button_Remote(const char *name, vrpn_Connection *cn = NULL);
256  virtual ~vrpn_Button_Remote(void);
257 
258  // This routine calls the mainloop of the connection it's on
259  virtual void mainloop();
260 
261  // (un)Register a callback handler to handle a button state change
262  virtual int register_change_handler(void *userdata,
263  vrpn_BUTTONCHANGEHANDLER handler)
264  {
265  return d_callback_list.register_handler(userdata, handler);
266  };
267  virtual int unregister_change_handler(void *userdata,
268  vrpn_BUTTONCHANGEHANDLER handler)
269  {
270  return d_callback_list.unregister_handler(userdata, handler);
271  }
272 
273  // (un)Register a callback handler to handle buttons states reports
274  virtual int register_states_handler(void *userdata,
275  vrpn_BUTTONSTATESHANDLER handler)
276  {
277  return d_states_callback_list.register_handler(userdata, handler);
278  };
279  virtual int unregister_states_handler(void *userdata,
280  vrpn_BUTTONSTATESHANDLER handler)
281  {
282  return d_states_callback_list.unregister_handler(userdata, handler);
283  }
284 
285 protected:
287  static int VRPN_CALLBACK
288  handle_change_message(void *userdata, vrpn_HANDLERPARAM p);
289 
291  static int VRPN_CALLBACK
292  handle_states_message(void *userdata, vrpn_HANDLERPARAM p);
293 };
294 
295 #define VRPN_BUTTON_H
296 #endif
vrpn_BUTTON_LIGHT_ON
const int vrpn_BUTTON_LIGHT_ON
Definition: vrpn_Button.h:22
vrpn_Button_Serial::read
virtual void read()=0
vrpn_Button::report_changes
virtual void report_changes(void)
Definition: vrpn_Button.C:422
vrpn_Button::admin_message_id
vrpn_int32 admin_message_id
Definition: vrpn_Button.h:51
vrpn_BaseClass.h
vrpn_BUTTON_MAX_BUTTONS
const int vrpn_BUTTON_MAX_BUTTONS
Definition: vrpn_Button.h:12
vrpn_Button::change_message_id
vrpn_int32 change_message_id
Definition: vrpn_Button.h:49
vrpn_Types.h
vrpn_Button_Remote::d_callback_list
vrpn_Callback_List< vrpn_BUTTONCB > d_callback_list
Definition: vrpn_Button.h:286
vrpn_Button::states_message_id
vrpn_int32 states_message_id
Definition: vrpn_Button.h:50
vrpn_Button_Serial::serial_fd
int serial_fd
Definition: vrpn_Button.h:182
vrpn_BUTTON_TOGGLE_ON
const int vrpn_BUTTON_TOGGLE_ON
Definition: vrpn_Button.h:20
vrpn_Button_Serial::status
int status
Definition: vrpn_Button.h:183
vrpn_Button::set_toggle
virtual void set_toggle(vrpn_int32 which_button, vrpn_int32 current_state)
Definition: vrpn_Button.C:226
vrpn_Button_Python::d_first_fail
bool d_first_fail
Definition: vrpn_Button.h:169
vrpn_Button_Remote
Definition: vrpn_Button.h:250
vrpn_BUTTONCB::state
vrpn_int32 state
Definition: vrpn_Button.h:228
vrpn_Callback_List< vrpn_BUTTONCB >
vrpn_ALL_ID
const int vrpn_ALL_ID
Definition: vrpn_Button.h:23
vrpn_Button_Parallel::port
int port
Definition: vrpn_Button.h:143
vrpn_Button::num_buttons
vrpn_int32 num_buttons
Definition: vrpn_Button.h:47
vrpn_Button_PinchGlove
Definition: vrpn_Button.h:197
vrpn_Button::set_all_momentary
virtual void set_all_momentary(void)
Definition: vrpn_Button.C:254
vrpn_Button_Server
Definition: vrpn_Button.h:98
vrpn_Button::set_all_toggle
virtual void set_all_toggle(vrpn_int32 default_state)
Definition: vrpn_Button.C:259
vrpn_Button_Serial::bufcount
vrpn_uint32 bufcount
Definition: vrpn_Button.h:187
vrpn_Button_Parallel::read
virtual void read(void)=0
vrpn_BUTTONCB::button
vrpn_int32 button
Definition: vrpn_Button.h:227
vrpn_HANDLERPARAM
This structure is what is passed to a vrpn_Connection message callback.
Definition: vrpn_Connection.h:44
vrpn_Shared.h
vrpn_BUTTONCHANGEHANDLER
void(VRPN_CALLBACK * vrpn_BUTTONCHANGEHANDLER)(void *userdata, const vrpn_BUTTONCB info)
Definition: vrpn_Button.h:230
vrpn_BUTTONSTATESHANDLER
void(VRPN_CALLBACK * vrpn_BUTTONSTATESHANDLER)(void *userdata, const vrpn_BUTTONSTATESCB info)
Definition: vrpn_Button.h:243
vrpn_Button::set_momentary
virtual void set_momentary(vrpn_int32 which_button)
Definition: vrpn_Button.C:191
vrpn_Button_Filter::alert_message_id
vrpn_int32 alert_message_id
Definition: vrpn_Button.h:78
vrpn_BaseClass::mainloop
virtual void mainloop()=0
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
vrpn_Button_Python
Definition: vrpn_Button.h:159
vrpn_BUTTONCB
Definition: vrpn_Button.h:225
vrpn_Button_Remote::register_change_handler
virtual int register_change_handler(void *userdata, vrpn_BUTTONCHANGEHANDLER handler)
Definition: vrpn_Button.h:262
vrpn_BUTTONSTATESCB
Definition: vrpn_Button.h:238
vrpn_Button_Remote::register_states_handler
virtual int register_states_handler(void *userdata, vrpn_BUTTONSTATESHANDLER handler)
Definition: vrpn_Button.h:274
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition: vrpn_Connection.h:510
vrpn_Button_PinchGlove::reported_failure
bool reported_failure
Definition: vrpn_Button.h:205
vrpn_Button_Serial
Definition: vrpn_Button.h:173
vrpn_Button_Remote::d_states_callback_list
vrpn_Callback_List< vrpn_BUTTONSTATESCB > d_states_callback_list
Definition: vrpn_Button.h:290
vrpn_Button
This is the base class for both the client and server for a button device (a device with one or more ...
Definition: vrpn_Button.h:30
VRPN_BUTTON_BUF_SIZE
const int VRPN_BUTTON_BUF_SIZE
Definition: vrpn_Button.h:13
vrpn_Button_Example_Server::_update_rate
vrpn_float64 _update_rate
Definition: vrpn_Button.h:128
vrpn_Button::encode_states_to
virtual vrpn_int32 encode_states_to(char *buf)
Encode a message describing the state of all buttons.
Definition: vrpn_Button.C:317
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_BUTTONSTATESCB::num_buttons
vrpn_int32 num_buttons
Definition: vrpn_Button.h:240
vrpn_Button_Serial::baudrate
long baudrate
Definition: vrpn_Button.h:181
vrpn_Button_Server
class VRPN_API vrpn_Button_Server
Definition: vrpn_Tracker_Fastrak.h:23
VRPN_CALLBACK
#define VRPN_CALLBACK
Definition: vrpn_Configure.h:647
vrpn_BUTTON_TOGGLE_OFF
const int vrpn_BUTTON_TOGGLE_OFF
Definition: vrpn_Button.h:19
vrpn_BUTTON_MOMENTARY
const int vrpn_BUTTON_MOMENTARY
Definition: vrpn_Button.h:18
vrpn_Button_Filter::send_alerts
int send_alerts
Definition: vrpn_Button.h:75
vrpn_Configure.h
vrpn_Button_Example_Server
Definition: vrpn_Button.h:120
vrpn_Button_Remote::unregister_states_handler
virtual int unregister_states_handler(void *userdata, vrpn_BUTTONSTATESHANDLER handler)
Definition: vrpn_Button.h:279
vrpn_Button_Parallel::status
int status
Definition: vrpn_Button.h:144
VRPN_API
#define VRPN_API
Definition: vrpn_Configure.h:646
vrpn_Button_Remote::unregister_change_handler
virtual int unregister_change_handler(void *userdata, vrpn_BUTTONCHANGEHANDLER handler)
Definition: vrpn_Button.h:267
vrpn_Button_Parallel
Definition: vrpn_Button.h:134
vrpn_BaseClass
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
Definition: vrpn_BaseClass.h:313
vrpn_BUTTON_LIGHT_OFF
const int vrpn_BUTTON_LIGHT_OFF
Definition: vrpn_Button.h:21
vrpn_Button_Filter
All button servers should derive from this class, which provides the ability to turn any of the butto...
Definition: vrpn_Button.h:65