vrpn 07.35
Virtual Reality Peripheral Network
Loading...
Searching...
No Matches
vrpn_Griffin.C
Go to the documentation of this file.
1// vrpn_Griffin.C: VRPN driver for Griffin Technologies devices
2
3#include <stdio.h> // for fprintf, stderr, NULL
4#include <string.h> // for memset
5#include <math.h> // for fabs
6
7#include "vrpn_Griffin.h"
8
10
11#if defined(VRPN_USE_HID)
12
13static const double POLL_INTERVAL = 1e+6 / 30.0; // If we have not heard, ask.
14
15// USB vendor and product IDs for the models we support
16static const vrpn_uint16 GRIFFIN_VENDOR = 0x077d;
17static const vrpn_uint16 GRIFFIN_POWERMATE = 0x0410;
18
19static void normalize_axis(const unsigned int value, const short deadzone, const vrpn_float64 scale, vrpn_float64& channel) {
20 channel = (static_cast<float>(value) - 128.0f);
21 if (fabs(channel) < deadzone)
22 {
23 channel = 0.0f;
24 }
25 else
26 {
27 channel /= 128.0f;
28 }
29 channel *= scale;
30 if (channel < -1.0) { channel = -1.0; }
31 if (channel > 1.0) { channel = 1.0; }
32}
33
35 vrpn_uint16 vendor, vrpn_uint16 product)
36 : vrpn_BaseClass(name, c)
37 , vrpn_HidInterface(filter, vendor, product)
38 , _filter(filter)
39{
40 init_hid();
41}
42
44{
45 try {
46 delete _filter;
47 } catch (...) {
48 fprintf(stderr, "vrpn_Griffin::~vrpn_Griffin(): delete failed\n");
49 return;
50 }
51}
52
54 // Get notifications when clients connect and disconnect
57}
58
59void vrpn_Griffin::on_data_received(size_t bytes, vrpn_uint8 *buffer)
60{
61 decodePacket(bytes, buffer);
62}
63
65{
66 return 0;
67}
68
69int vrpn_Griffin::on_connect(void* /*thisPtr*/, vrpn_HANDLERPARAM /*p*/)
70{
71 return 0;
72}
73
75 : vrpn_Griffin(new vrpn_HidProductAcceptor(GRIFFIN_VENDOR, GRIFFIN_POWERMATE), name, c, GRIFFIN_VENDOR, GRIFFIN_POWERMATE)
76 , vrpn_Analog(name, c)
77 , vrpn_Button_Filter(name, c)
78 , vrpn_Dial(name, c)
79{
83
84 // Initialize the state of all the analogs, buttons, and dials
85 _lastDial = 0;
86 memset(buttons, 0, sizeof(buttons));
87 memset(lastbuttons, 0, sizeof(lastbuttons));
88 memset(channel, 0, sizeof(channel));
89 memset(last, 0, sizeof(last));
90}
91
93{
94 update();
96 struct timeval current_time;
97 vrpn_gettimeofday(&current_time, NULL);
98 if (vrpn_TimevalDuration(current_time, _timestamp) > POLL_INTERVAL ) {
99 _timestamp = current_time;
101
102 // Call the server_mainloop on our unique base class.
104 }
105}
106
107void vrpn_Griffin_PowerMate::report(vrpn_uint32 class_of_service) {
109 {
111 }
113 {
115 }
116 if (vrpn_Dial::num_dials > 0)
117 {
119 }
120
122 {
123 vrpn_Analog::report(class_of_service);
124 }
126 {
128 }
129 if (vrpn_Dial::num_dials > 0)
130 {
132 }
133}
134
135void vrpn_Griffin_PowerMate::report_changes(vrpn_uint32 class_of_service) {
137 {
139 }
141 {
143 }
144 if (vrpn_Dial::num_dials > 0)
145 {
147 }
148
150 {
151 vrpn_Analog::report(class_of_service);
152 }
154 {
156 }
157 if (vrpn_Dial::num_dials > 0)
158 {
160 }
161}
162
163void vrpn_Griffin_PowerMate::decodePacket(size_t bytes, vrpn_uint8 *buffer) {
164 // Decode all full reports, each of which is 8 bytes long.
165 // Because there is only one type of report, the initial "0" report-type
166 // byte is removed by the HIDAPI driver.
167 // XXX Check to see that this works with HIDAPI, there may be two smaller reports.
168 if (bytes == 6) {
169
170 if (vrpn_Dial::num_dials > 0) {
171 // dial (2nd byte)
172 // Do the unsigned/signed conversion at the last minute so the
173 // signed values work properly.
174 dials[0] = static_cast<vrpn_int8>(buffer[1]);
175 } else {
176 // dial (2nd byte)
177 normalize_axis(buffer[1], 5, 1.0f, channel[0]);
178 }
179
180 vrpn_uint8 value;
181 // switches (1st byte):
182 value = buffer[0];
183 // button #0: 01 button
184 for (int btn = 0; btn < 1; btn++) {
185 vrpn_uint8 mask = static_cast<vrpn_uint8>(1 << (btn % 8));
186 buttons[btn] = ((value & mask) != 0);
187 }
188 } else {
189 fprintf(stderr, "vrpn_Griffin_PowerMate: Found a corrupted report; # total bytes = %u\n", static_cast<unsigned>(bytes));
190 }
191}
192
193// End of VRPN_USE_HID
194#endif
vrpn_float64 last[vrpn_CHANNEL_MAX]
Definition: vrpn_Analog.h:39
vrpn_float64 channel[vrpn_CHANNEL_MAX]
Definition: vrpn_Analog.h:38
struct timeval timestamp
Definition: vrpn_Analog.h:41
vrpn_int32 num_channel
Definition: vrpn_Analog.h:40
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
int register_autodeleted_handler(vrpn_int32 type, vrpn_MESSAGEHANDLER handler, void *userdata, vrpn_int32 sender=vrpn_ANY_SENDER)
Registers a handler with the connection, and remembers to delete at destruction.
vrpn_Connection * d_connection
Connection that this object talks to.
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
All button servers should derive from this class, which provides the ability to turn any of the butto...
Definition: vrpn_Button.h:66
virtual void report_changes(void)
Definition: vrpn_Button.C:383
vrpn_int32 num_buttons
Definition: vrpn_Button.h:48
struct timeval timestamp
Definition: vrpn_Button.h:49
virtual void report_changes(void)
Definition: vrpn_Button.C:423
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.
virtual vrpn_int32 register_message_type(const char *name)
struct timeval timestamp
Definition: vrpn_Dial.h:28
vrpn_float64 dials[vrpn_DIAL_MAX]
Definition: vrpn_Dial.h:26
virtual void report(void)
Definition: vrpn_Dial.C:82
vrpn_int32 num_dials
Definition: vrpn_Dial.h:27
virtual void mainloop(void)
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
Definition: vrpn_Griffin.C:92
void decodePacket(size_t bytes, vrpn_uint8 *buffer)
Definition: vrpn_Griffin.C:163
vrpn_Griffin_PowerMate(const char *name, vrpn_Connection *c=0)
Definition: vrpn_Griffin.C:74
void on_data_received(size_t bytes, vrpn_uint8 *buffer)
Derived class reimplements this callback.
Definition: vrpn_Griffin.C:59
virtual void decodePacket(size_t bytes, vrpn_uint8 *buffer)=0
vrpn_Griffin(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c=0, vrpn_uint16 vendor=0, vrpn_uint16 product=0)
Definition: vrpn_Griffin.C:34
vrpn_HidAcceptor * _filter
Definition: vrpn_Griffin.h:43
static int VRPN_CALLBACK on_last_disconnect(void *thisPtr, vrpn_HANDLERPARAM p)
Definition: vrpn_Griffin.C:64
static int VRPN_CALLBACK on_connect(void *thisPtr, vrpn_HANDLERPARAM p)
Definition: vrpn_Griffin.C:69
struct timeval _timestamp
Definition: vrpn_Griffin.h:42
virtual ~vrpn_Griffin(void)
Definition: vrpn_Griffin.C:43
void init_hid(void)
Definition: vrpn_Griffin.C:53
virtual void update()
Polls the device buffers and causes on_data_received callbacks if appropriate You NEED to call this f...
Accepts any device with the given vendor and product IDs.
This structure is what is passed to a vrpn_Connection message callback.
#define VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
const char * vrpn_dropped_last_connection
const char * vrpn_got_connection
#define POLL_INTERVAL
Definition: vrpn_IDEA.C:26
unsigned long vrpn_TimevalDuration(struct timeval endT, struct timeval startT)
Return number of microseconds between startT and endT.
Definition: vrpn_Shared.C:138
#define vrpn_gettimeofday
Definition: vrpn_Shared.h:99