vrpn
07.35
Virtual Reality Peripheral Network
Toggle main menu visibility
Loading...
Searching...
No Matches
vrpn_VPJoystick.C
Go to the documentation of this file.
1
26
27
#include "
vrpn_Serial.h
"
28
#include "
vrpn_Shared.h
"
// for timeval, vrpn_gettimeofday
29
#include "
vrpn_VPJoystick.h
"
30
31
class
VRPN_API
vrpn_Connection
;
32
33
#define STATE_SYNCHING (0)
34
#define STATE_READING (1)
35
#define STATE_RECEIVED (2)
36
37
#define SYNC_BYTE (0xff)
38
39
#define VP_BUTTON_1 4096
40
#define VP_BUTTON_2 256
41
#define VP_BUTTON_3 1
42
#define VP_BUTTON_4 512
43
#define VP_BUTTON_5 2048
44
#define VP_BUTTON_6 2
45
#define VP_BUTTON_7 1024
46
#define VP_BUTTON_8 4
47
48
#define VP_HAT_UP VP_BUTTON_5
49
#define VP_HAT_DOWN VP_BUTTON_7
50
#define VP_HAT_LEFT VP_BUTTON_8
51
#define VP_HAT_RIGHT VP_BUTTON_6
52
53
#define VP_TRIGGER VP_BUTTON_4
54
#define VP_BUTTON_TOP VP_BUTTON_3
55
#define VP_BUTTON_MIDDLE VP_BUTTON_2
56
#define VP_BUTTON_BOTTOM VP_BUTTON_1
57
58
59
#define VP_HAT_ALL ( VP_HAT_UP | VP_HAT_DOWN | VP_HAT_LEFT | VP_HAT_RIGHT )
60
#define VP_BUTTON_ALL ( VP_TRIGGER | VP_BUTTON_TOP | VP_BUTTON_MIDDLE | VP_BUTTON_BOTTOM )
61
62
#include <stdio.h>
// for fprintf, stderr
63
64
vrpn_VPJoystick::vrpn_VPJoystick
(
char
* name,
vrpn_Connection
*c,
65
const
char
*port,
long
baud)
66
:
vrpn_Button_Filter
(name, c),
67
serial_fd(0), state(
STATE_SYNCHING
)
68
{
69
int
i;
70
71
// Open the serial port
72
if
( (serial_fd=
vrpn_open_commport
(port, baud)) == -1) {
73
fprintf(stderr,
"vrpn_VPJoystick: Cannot Open serial port\n"
);
74
}
75
76
// find out what time it is - needed?
77
vrpn_gettimeofday
(&
timestamp
, 0);
78
vrpn_Button::timestamp
=
timestamp
;
79
80
num_buttons
=
vrpn_VPJOY_NUM_BUTTONS
;
81
82
button_masks[0] =
VP_BUTTON_1
;
83
button_masks[1] =
VP_BUTTON_2
;
84
button_masks[2] =
VP_BUTTON_3
;
85
button_masks[3] =
VP_BUTTON_4
;
86
button_masks[4] =
VP_BUTTON_5
;
87
button_masks[5] =
VP_BUTTON_6
;
88
button_masks[6] =
VP_BUTTON_7
;
89
button_masks[7] =
VP_BUTTON_8
;
90
91
for
( i=0; i<
num_buttons
; i++ ) {
92
buttons
[i] =
lastbuttons
[i] =
VRPN_BUTTON_OFF
;
93
}
94
}
95
96
97
vrpn_VPJoystick::~vrpn_VPJoystick
()
98
{
99
vrpn_close_commport
(serial_fd);
100
}
101
102
void
vrpn_VPJoystick::mainloop
()
103
{
104
server_mainloop
();
105
106
// XXX Why not have a timeout of zero? This would cause faster
107
// update rates for other devices in the same server.
108
struct
timeval timeout = { 0,200000 };
109
110
if
(serial_fd == -1) {
111
fprintf(stderr,
"vrpn_VPJOystick::mainloop(): Bad serial port descriptor\n"
);
112
return
;
113
}
114
115
if
( state ==
STATE_SYNCHING
) {
116
117
// Read bytes from the incoming stream until
118
// the synch byte is received
119
120
message_buffer[0] = 0;
121
if
(
vrpn_read_available_characters
( serial_fd, message_buffer, 1, &timeout ) ) {
122
if
( message_buffer[0] ==
SYNC_BYTE
) {
123
state =
STATE_READING
;
124
bytes_read = 1;
125
}
126
}
127
128
}
129
130
// The state may just have been set to this, so don't do this in a switch
131
// or if-then-else statement because we may take another whole loop iteration
132
// before checking it again.
133
if
( state ==
STATE_READING
) {
134
135
// Read the remaining bytes of the packet
136
//
137
138
bytes_read +=
vrpn_read_available_characters
( serial_fd, message_buffer + bytes_read,
vrpn_VPJOY_MESSAGE_LENGTH
- bytes_read, &timeout );
139
if
( bytes_read ==
vrpn_VPJOY_MESSAGE_LENGTH
) {
140
state =
STATE_RECEIVED
;
141
}
142
143
}
144
145
// The state may just have been set to this, so don't do this in a switch
146
// or if-then-else statement because we may take another whole loop iteration
147
// before checking it again.
148
if
( state ==
STATE_RECEIVED
) {
149
150
// decode a received packet
151
152
int
i;
153
int
flag = ((int) message_buffer[1])*256 + message_buffer[2];
154
flag = (~flag) & (
VP_BUTTON_ALL
|
VP_HAT_ALL
);
155
156
for
( i = 0 ; i <
num_buttons
; i++ ) {
157
buttons
[ i ] =
static_cast<
unsigned
char
>
( ( ( flag & button_masks[i] ) == button_masks[i] ) ?
VRPN_BUTTON_ON
:
VRPN_BUTTON_OFF
);
158
}
159
160
vrpn_Button::report_changes
();
161
state =
STATE_SYNCHING
;
162
}
163
}
vrpn_BaseClassUnique::server_mainloop
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
Definition
vrpn_BaseClass.C:604
vrpn_Button_Filter::vrpn_Button_Filter
vrpn_Button_Filter(const char *, vrpn_Connection *c=NULL)
Definition
vrpn_Button.C:129
vrpn_Button::num_buttons
vrpn_int32 num_buttons
Definition
vrpn_Button.h:48
vrpn_Button::timestamp
struct timeval timestamp
Definition
vrpn_Button.h:49
vrpn_Button::report_changes
virtual void report_changes(void)
Definition
vrpn_Button.C:423
vrpn_Button::lastbuttons
unsigned char lastbuttons[vrpn_BUTTON_MAX_BUTTONS]
Definition
vrpn_Button.h:46
vrpn_Button::buttons
unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS]
Definition
vrpn_Button.h:45
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition
vrpn_Connection.h:561
vrpn_VPJoystick::~vrpn_VPJoystick
~vrpn_VPJoystick()
Definition
vrpn_VPJoystick.C:97
vrpn_VPJoystick::vrpn_VPJoystick
vrpn_VPJoystick(char *name, vrpn_Connection *c, const char *port="/dev/ttyS0", long baud=9600)
Definition
vrpn_VPJoystick.C:64
vrpn_VPJoystick::mainloop
void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
Definition
vrpn_VPJoystick.C:102
VRPN_BUTTON_ON
#define VRPN_BUTTON_ON
Definition
vrpn_Button.h:224
VRPN_BUTTON_OFF
#define VRPN_BUTTON_OFF
Definition
vrpn_Button.h:223
VRPN_API
#define VRPN_API
Definition
vrpn_Configure.h:648
vrpn_close_commport
int vrpn_close_commport(int comm)
Definition
vrpn_Serial.C:348
vrpn_read_available_characters
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
Definition
vrpn_Serial.C:515
vrpn_open_commport
int vrpn_open_commport(const char *portname, long baud, int charsize, vrpn_SER_PARITY parity, bool rts_flow)
Open a serial port, given its name and baud rate.
Definition
vrpn_Serial.C:54
vrpn_Serial.h
vrpn_Serial: Pulls all the serial port routines into one file to make porting to new operating system...
vrpn_Shared.h
vrpn_gettimeofday
#define vrpn_gettimeofday
Definition
vrpn_Shared.h:99
STATE_RECEIVED
#define STATE_RECEIVED
Definition
vrpn_VPJoystick.C:35
VP_HAT_ALL
#define VP_HAT_ALL
Definition
vrpn_VPJoystick.C:59
VP_BUTTON_1
#define VP_BUTTON_1
Definition
vrpn_VPJoystick.C:39
VP_BUTTON_5
#define VP_BUTTON_5
Definition
vrpn_VPJoystick.C:43
VP_BUTTON_4
#define VP_BUTTON_4
Definition
vrpn_VPJoystick.C:42
VP_BUTTON_3
#define VP_BUTTON_3
Definition
vrpn_VPJoystick.C:41
STATE_SYNCHING
#define STATE_SYNCHING
Definition
vrpn_VPJoystick.C:33
VP_BUTTON_ALL
#define VP_BUTTON_ALL
Definition
vrpn_VPJoystick.C:60
VP_BUTTON_6
#define VP_BUTTON_6
Definition
vrpn_VPJoystick.C:44
STATE_READING
#define STATE_READING
Definition
vrpn_VPJoystick.C:34
SYNC_BYTE
#define SYNC_BYTE
Definition
vrpn_VPJoystick.C:37
VP_BUTTON_2
#define VP_BUTTON_2
Definition
vrpn_VPJoystick.C:40
VP_BUTTON_7
#define VP_BUTTON_7
Definition
vrpn_VPJoystick.C:45
VP_BUTTON_8
#define VP_BUTTON_8
Definition
vrpn_VPJoystick.C:46
vrpn_VPJoystick.h
vrpn_VPJOY_MESSAGE_LENGTH
#define vrpn_VPJOY_MESSAGE_LENGTH
Definition
vrpn_VPJoystick.h:11
vrpn_VPJOY_NUM_BUTTONS
#define vrpn_VPJOY_NUM_BUTTONS
Definition
vrpn_VPJoystick.h:12
vrpn_Connection
class VRPN_API vrpn_Connection
Definition
vrpn_VPJoystick.h:8
vrpn_VPJoystick.C
Generated by
1.17.0