vrpn  07.33
Virtual Reality Peripheral Network
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
14 static const vrpn_uint16 DREAMCHEEKY_VENDOR = 6465;
15 static const vrpn_uint16 USB_ROLL_UP_DRUM_KIT = 32801;
16 
18  : vrpn_HidInterface(filter)
19  , vrpn_BaseClass(name, c)
20  , _filter(filter)
21 {
23 }
24 
26 {
27  delete _filter;
28 }
29 
30 void vrpn_DreamCheeky::on_data_received(size_t bytes, vrpn_uint8 *buffer)
31 {
32  decodePacket(bytes, buffer);
33 }
34 
36  bool debounce)
37  : vrpn_DreamCheeky(_filter = new vrpn_HidProductAcceptor(DREAMCHEEKY_VENDOR, USB_ROLL_UP_DRUM_KIT), name, c)
38  , vrpn_Button_Filter(name, c)
39  , d_debounce(debounce)
40 {
42 
43  // Initialize the state of all the buttons
44  memset(buttons, 0, sizeof(buttons));
45  memset(lastbuttons, 0, sizeof(lastbuttons));
46 }
47 
49 {
50  update();
54 
56 }
57 
61 }
62 
66 }
67 
68 void vrpn_DreamCheeky_Drum_Kit::decodePacket(size_t bytes, vrpn_uint8 *buffer)
69 {
70  // The reports are each 8 bytes long. Since there is only one type of
71  // report for this device, the report type is not included (stripped by
72  // the HIDAPI driver). The bytes are 8 identical reports.
73  // There is one byte per report, and it holds a binary encoding of
74  // the buttons. Button 0 is in the LSB, and the others proceed up
75  // the bit chain. Parse each report and then send any changes on.
76  // need to send between each report so we don't miss a button press/release
77  // all in one packet.
78 
79  size_t i, r;
80  // Truncate the count to an even number of 8 bytes. This will
81  // throw out any partial reports (which is not necessarily what
82  // we want, because this will start us off parsing at the wrong
83  // place if the rest of the report comes next, but it is not
84  // clear how to handle that cleanly).
85  bytes -= (bytes % 8);
86 
87  // Decode all full reports, each of which is 8 bytes long.
88  for (i = 0; i < (bytes / 8); i++) {
89 
90  // If we're debouncing the buttons, then we set the button
91  // to "pressed" if it has 4 or more pressed events in the
92  // set of 8 and to "released" if it has less than 4.
93  if (d_debounce) {
94  int btn;
95  for (btn = 0; btn < vrpn_Button::num_buttons; btn++) {
96  unsigned count = 0;
97  vrpn_uint8 mask = 1 << btn;
98  for (r = 0; r < 8; r++) { // Skip the all-zeroes byte
99  vrpn_uint8 *report = buffer + 9*i + r;
100  count += ((*report & mask) != 0);
101  }
102  buttons[btn] = (count >= 4);
104  report_changes();
105  }
106 
107  // If we're not debouncing, then we report each button event
108  // independently.
109  }else {
110  for (r = 0; r < 8; r++) { // Skip the all-zeroes byte
111  vrpn_uint8 *report = buffer + 9*i + r;
112  int btn;
113  for (btn = 0; btn < vrpn_Button::num_buttons; btn++) {
114  vrpn_uint8 mask = 1 << btn;
115  buttons[btn] = ((*report & mask) != 0);
116  }
118  report_changes();
119  }
120  }
121  }
122 }
123 
124 #endif
125 
void server_mainloop(void)
Handles functions that all servers should provide in their mainloop() (ping/pong, for example) Should...
virtual ~vrpn_DreamCheeky()
vrpn_int32 num_buttons
Definition: vrpn_Button.h:47
void on_data_received(size_t bytes, vrpn_uint8 *buffer)
Derived class reimplements this callback.
Accepts any device with the given vendor and product IDs.
Generic connection class not specific to the transport mechanism.
#define VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
#define VRPN_API
vrpn_DreamCheeky_Drum_Kit(const char *name, vrpn_Connection *c=0, bool debounce=true)
virtual void report_changes(void)
Definition: vrpn_Button.C:422
void decodePacket(size_t bytes, vrpn_uint8 *buffer)
vrpn_DreamCheeky(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c=0)
virtual void decodePacket(size_t bytes, vrpn_uint8 *buffer)=0
vrpn_HidAcceptor * _filter
struct timeval _timestamp
struct timeval timestamp
Definition: vrpn_Button.h:48
#define vrpn_gettimeofday
Definition: vrpn_Shared.h:89
virtual void update()
Polls the device buffers and causes on_data_received callbacks if appropriate You NEED to call this f...
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:65
unsigned char lastbuttons[vrpn_BUTTON_MAX_BUTTONS]
Definition: vrpn_Button.h:45
unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS]
Definition: vrpn_Button.h:44
virtual void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...