vrpn  07.33
Virtual Reality Peripheral Network
vrpn_sgibox.C
Go to the documentation of this file.
1 #ifdef sgi
2 
3 #include "vrpn_sgibox.h"
4 #include <stdio.h>
5 
6 // all for gethostbyname();
7 #include <unistd.h>
8 static int sgibox_con_cb(void * userdata, vrpn_HANDLERPARAM p);
9 static int sgibox_alert_handler(void * userdata, vrpn_HANDLERPARAM);
10 
11 
12 vrpn_SGIBox::vrpn_SGIBox(char * name, vrpn_Connection * c):
13  vrpn_Analog(name, c), vrpn_Button_Filter(name, c) {
14  int ret;
15  char hn[128];
16  sid = -1;
17  winid = -1;
18 
19  if (winid != -1) {
20  printf("Closing previous windows Winid=%d, sid = %d:\n",winid, sid);
21  winclose(winid);
22  //dglclose(sid);
23  dglclose(-1); // close all dgl connection;
24  }
25 
26  ret = gethostname(hn, 100);
27  if (ret < 0) {
28  fprintf(stderr, "vrpn_SGIBox: error in gethostname()\n");
29  return;
30  }
31  sid = ret = dglopen(hn,DGLLOCAL);
32  if (ret < 0) {
33  fprintf(stderr, "vrpn_SGIBox: error in dglopen()\n");
34  return;
35  }
36  noport();
37  winid = winopen("");
38  reset();
39  num_channel = NUM_DIALS;
40  num_buttons = NUM_BUTTONS;
41 
42  register_autodeleted_handler(c->register_message_type( vrpn_got_first_connection), sgibox_con_cb, this);
43  register_autodeleted_handler(alert_message_id,sgibox_alert_handler, this);
44  set_alerts(1); //turn on alerts from toggle filter class to notify
45  //local sgibox that lights should be turned on/off
46 }
47 
48 void vrpn_SGIBox::reset() { /* Button/Dial box setup */
49  int i;
50 
51  for (i=0; i<NUM_DIALS; i++) devs[i] = DIAL0+i;
52  for (i=0; i<NUM_BUTTONS; i++) devs[i+NUM_DIALS] = SW0+i;
53  btstat = 0; /* Set all on/off buttons to off */
54  //fprintf(stderr, "vrpn_SGIBox::reset %d\n", __LINE__);
55  setdblights(btstat); /* Make the lights reflect this */
56  //fprintf(stderr, "vrpn_SGIBox::reset %d\n", __LINE__);
57  getdev(NUMDEVS, devs, vals1); /* Get initial values */
58 
59  for (i=0; i<NUM_BUTTONS; i++)
60  lastbuttons[i] = vals1[NUM_DIALS+i];
61 
62  for (i=0; i<NUM_DIALS; i++) mid_values[i] = vals1[i], last[i] = 0;
63 }
64 
65 void vrpn_SGIBox::get_report() {
66  int i;
67  getdev(NUMDEVS, devs, vals1); /* read button/dial boxes */
68  //fprintf(stderr, "Button=");
69  for (i=0; i< NUM_BUTTONS; i++) {
70  buttons[i] = vals1[NUM_DIALS+i];
71  }
72  for (i=0; i< NUM_DIALS; i++) {
73  int temp = vals1[i] -mid_values[i];
74  if (temp > 200) channel[i] = 0.5, mid_values[i] = vals1[i] - 200;
75  else if (temp < -200) channel[i] = -0.5, mid_values[i] = vals1[i]
76  + 200;
77  else
78  channel[i] = temp/400.0;
79  //fprintf(stderr, " %d", vals1[i]);
80  }
81  //fprintf(stderr, "\n");
82 
85 }
86 
87 void vrpn_SGIBox::mainloop() {
88  server_mainloop();
89  get_report();
90 }
91 
92 
93 static int sgibox_alert_handler(void * userdata, vrpn_HANDLERPARAM){
94  int i;
95  long lights;
96  vrpn_SGIBox*me=(vrpn_SGIBox *)userdata;
97 
98  lights=0;
99  for(i=0;i<NUM_BUTTONS;i++){
100  if(me->buttonstate[i]==vrpn_BUTTON_TOGGLE_ON) lights=lights|1<<i;
101  }
102  setdblights(lights);
103  return 0;
104 
105 }
106 
107 static int sgibox_con_cb(void * userdata, vrpn_HANDLERPARAM)
108 {
109 
110  printf("vrpn_SGIBox::Get first new connection, reset the box\n");
111  ((vrpn_SGIBox *)userdata) ->reset();
112  return 0;
113 }
114 
115 #endif // sgi
116 
117 
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
const char * vrpn_got_first_connection
These are the strings that define the system-generated message types that tell when connections are r...
Generic connection class not specific to the transport mechanism.
virtual void report_changes(void)
Definition: vrpn_Button.C:382
This structure is what is passed to a vrpn_Connection message callback.
const int vrpn_BUTTON_TOGGLE_ON
Definition: vrpn_Button.h:20
All button servers should derive from this class, which provides the ability to turn any of the butto...
Definition: vrpn_Button.h:65
virtual vrpn_int32 register_message_type(const char *name)