vrpn 07.35
Virtual Reality Peripheral Network
Loading...
Searching...
No Matches
vrpn_DreamCheeky.C
Go to the documentation of this file.
1// vrpn_dreamcheeky.C: VRPN driver for Dream Cheeky USB roll-up drum kit
2
3#include <string.h> // for memset
4
5#include "vrpn_DreamCheeky.h"
6
8
10
11#if defined(VRPN_USE_HID)
12
13// USB vendor and product IDs for the models we support
14static const vrpn_uint16 DREAMCHEEKY_VENDOR = 6465;
15static const vrpn_uint16 USB_ROLL_UP_DRUM_KIT = 32801;
16
18 vrpn_uint16 vendor, vrpn_uint16 product)
19 : vrpn_BaseClass(name, c)
20 , vrpn_HidInterface(filter, vendor, product)
21 , _filter(filter)
22{
24}
25
27{
28 try {
29 delete _filter;
30 } catch (...) {
31 fprintf(stderr, "vrpn_DreamCheeky::~vrpn_DreamCheeky(): delete failed\n");
32 return;
33 }
34}
35
36void vrpn_DreamCheeky::on_data_received(size_t bytes, vrpn_uint8 *buffer)
37{
38 decodePacket(bytes, buffer);
39}
40
42 bool debounce)
43 : vrpn_DreamCheeky(new vrpn_HidProductAcceptor(DREAMCHEEKY_VENDOR, USB_ROLL_UP_DRUM_KIT), name, c, DREAMCHEEKY_VENDOR, USB_ROLL_UP_DRUM_KIT)
44 , vrpn_Button_Filter(name, c)
45 , d_debounce(debounce)
46{
48
49 // Initialize the state of all the buttons
50 memset(buttons, 0, sizeof(buttons));
51 memset(lastbuttons, 0, sizeof(lastbuttons));
52}
53
63
69
75
76void vrpn_DreamCheeky_Drum_Kit::decodePacket(size_t bytes, vrpn_uint8 *buffer)
77{
78 // The reports are each 8 bytes long. Since there is only one type of
79 // report for this device, the report type is not included (stripped by
80 // the HIDAPI driver). The bytes are 8 identical reports.
81 // There is one byte per report, and it holds a binary encoding of
82 // the buttons. Button 0 is in the LSB, and the others proceed up
83 // the bit chain. Parse each report and then send any changes on.
84 // need to send between each report so we don't miss a button press/release
85 // all in one packet.
86
87 size_t i, r;
88 // Truncate the count to an even number of 8 bytes. This will
89 // throw out any partial reports (which is not necessarily what
90 // we want, because this will start us off parsing at the wrong
91 // place if the rest of the report comes next, but it is not
92 // clear how to handle that cleanly).
93 bytes -= (bytes % 8);
94
95 // Decode all full reports, each of which is 8 bytes long.
96 for (i = 0; i < (bytes / 8); i++) {
97
98 // If we're debouncing the buttons, then we set the button
99 // to "pressed" if it has 4 or more pressed events in the
100 // set of 8 and to "released" if it has less than 4.
101 if (d_debounce) {
102 int btn;
103 for (btn = 0; btn < vrpn_Button::num_buttons; btn++) {
104 unsigned count = 0;
105 vrpn_uint8 mask = 1 << btn;
106 for (r = 0; r < 8; r++) { // Skip the all-zeroes byte
107 vrpn_uint8 *report = buffer + 9*i + r;
108 count += ((*report & mask) != 0);
109 }
110 buttons[btn] = (count >= 4);
113 }
114
115 // If we're not debouncing, then we report each button event
116 // independently.
117 }else {
118 for (r = 0; r < 8; r++) { // Skip the all-zeroes byte
119 vrpn_uint8 *report = buffer + 9*i + r;
120 int btn;
121 for (btn = 0; btn < vrpn_Button::num_buttons; btn++) {
122 vrpn_uint8 mask = 1 << btn;
123 buttons[btn] = ((*report & mask) != 0);
124 }
127 }
128 }
129 }
130}
131
132#endif
133
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
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.
void decodePacket(size_t bytes, vrpn_uint8 *buffer)
virtual void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
vrpn_DreamCheeky_Drum_Kit(const char *name, vrpn_Connection *c=0, bool debounce=true)
struct timeval _timestamp
vrpn_HidAcceptor * _filter
void on_data_received(size_t bytes, vrpn_uint8 *buffer)
Derived class reimplements this callback.
virtual ~vrpn_DreamCheeky()
virtual void decodePacket(size_t bytes, vrpn_uint8 *buffer)=0
vrpn_DreamCheeky(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c=0, vrpn_uint16 vendor=0, vrpn_uint16 product=0)
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.
#define VRPN_API
#define VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
#define vrpn_gettimeofday
Definition vrpn_Shared.h:99