vrpn 07.35
Virtual Reality Peripheral Network
 
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
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
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?
79
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++ ) {
93 }
94}
95
96
101
103{
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
161 state = STATE_SYNCHING;
162 }
163}
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
vrpn_Button_Filter(const char *, vrpn_Connection *c=NULL)
vrpn_int32 num_buttons
Definition vrpn_Button.h:48
struct timeval timestamp
Definition vrpn_Button.h:49
virtual void report_changes(void)
unsigned char lastbuttons[vrpn_BUTTON_MAX_BUTTONS]
Definition vrpn_Button.h:46
unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS]
Definition vrpn_Button.h:45
Generic connection class not specific to the transport mechanism.
vrpn_VPJoystick(char *name, vrpn_Connection *c, const char *port="/dev/ttyS0", long baud=9600)
void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
#define VRPN_BUTTON_ON
#define VRPN_BUTTON_OFF
#define VRPN_API
int vrpn_close_commport(int comm)
int vrpn_read_available_characters(int comm, unsigned char *buffer, size_t bytes)
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: Pulls all the serial port routines into one file to make porting to new operating system...
#define vrpn_gettimeofday
Definition vrpn_Shared.h:99
#define STATE_RECEIVED
#define VP_HAT_ALL
#define VP_BUTTON_1
#define VP_BUTTON_5
#define VP_BUTTON_4
#define VP_BUTTON_3
#define STATE_SYNCHING
#define VP_BUTTON_ALL
#define VP_BUTTON_6
#define STATE_READING
#define SYNC_BYTE
#define VP_BUTTON_2
#define VP_BUTTON_7
#define VP_BUTTON_8
#define vrpn_VPJOY_MESSAGE_LENGTH
#define vrpn_VPJOY_NUM_BUTTONS
class VRPN_API vrpn_Connection