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 
vrpn_Button::report_changes
virtual void report_changes(void)
Definition: vrpn_Button.C:422
vrpn_DreamCheeky::~vrpn_DreamCheeky
virtual ~vrpn_DreamCheeky()
Definition: vrpn_DreamCheeky.C:25
vrpn_HidInterface
Definition: vrpn_HumanInterface.h:68
vrpn_DreamCheeky_Drum_Kit::report
void report(void)
Definition: vrpn_DreamCheeky.C:58
vrpn_DreamCheeky::on_data_received
void on_data_received(size_t bytes, vrpn_uint8 *buffer)
Derived class reimplements this callback.
Definition: vrpn_DreamCheeky.C:30
vrpn_HidInterface::update
virtual void update()
Polls the device buffers and causes on_data_received callbacks if appropriate You NEED to call this f...
Definition: vrpn_HumanInterface.C:140
vrpn_HidAcceptor
Definition: vrpn_HumanInterface.h:54
vrpn_Button::num_buttons
vrpn_int32 num_buttons
Definition: vrpn_Button.h:47
vrpn_Button::buttons
unsigned char buttons[vrpn_BUTTON_MAX_BUTTONS]
Definition: vrpn_Button.h:44
vrpn_DreamCheeky_Drum_Kit::d_debounce
bool d_debounce
Definition: vrpn_DreamCheeky.h:58
vrpn_DreamCheeky_Drum_Kit::vrpn_DreamCheeky_Drum_Kit
vrpn_DreamCheeky_Drum_Kit(const char *name, vrpn_Connection *c=0, bool debounce=true)
Definition: vrpn_DreamCheeky.C:35
vrpn_DreamCheeky::decodePacket
virtual void decodePacket(size_t bytes, vrpn_uint8 *buffer)=0
vrpn_DreamCheeky
Definition: vrpn_DreamCheeky.h:26
vrpn_DreamCheeky_Drum_Kit::decodePacket
void decodePacket(size_t bytes, vrpn_uint8 *buffer)
Definition: vrpn_DreamCheeky.C:68
vrpn_HidProductAcceptor
Accepts any device with the given vendor and product IDs.
Definition: vrpn_HumanInterface.h:150
vrpn_Button::timestamp
struct timeval timestamp
Definition: vrpn_Button.h:48
vrpn_Connection
Generic connection class not specific to the transport mechanism.
Definition: vrpn_Connection.h:510
vrpn_DreamCheeky::vrpn_DreamCheeky
vrpn_DreamCheeky(vrpn_HidAcceptor *filter, const char *name, vrpn_Connection *c=0)
Definition: vrpn_DreamCheeky.C:17
vrpn_DreamCheeky.h
vrpn_gettimeofday
#define vrpn_gettimeofday
Definition: vrpn_Shared.h:89
vrpn_Button::lastbuttons
unsigned char lastbuttons[vrpn_BUTTON_MAX_BUTTONS]
Definition: vrpn_Button.h:45
vrpn_DreamCheeky_Drum_Kit::mainloop
virtual void mainloop()
Called once through each main loop iteration to handle updates. Remote object mainloop() should call ...
Definition: vrpn_DreamCheeky.C:48
vrpn_DreamCheeky_Drum_Kit::report_changes
void report_changes(void)
Definition: vrpn_DreamCheeky.C:63
vrpn_DreamCheeky::_filter
vrpn_HidAcceptor * _filter
Definition: vrpn_DreamCheeky.h:39
vrpn_DreamCheeky::_timestamp
struct timeval _timestamp
Definition: vrpn_DreamCheeky.h:38
VRPN_SUPPRESS_EMPTY_OBJECT_WARNING
#define VRPN_SUPPRESS_EMPTY_OBJECT_WARNING()
Definition: vrpn_Configure.h:495
VRPN_API
#define VRPN_API
Definition: vrpn_Configure.h:646
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:603
vrpn_BaseClass
Class from which all user-level (and other) classes that communicate with vrpn_Connections should der...
Definition: vrpn_BaseClass.h:313
vrpn_Button_Filter
All button servers should derive from this class, which provides the ability to turn any of the butto...
Definition: vrpn_Button.h:65