diff options
Diffstat (limited to 'lib/usbhost/USB_Host_Shield_2.0/PSBuzz.h')
-rw-r--r-- | lib/usbhost/USB_Host_Shield_2.0/PSBuzz.h | 185 |
1 files changed, 185 insertions, 0 deletions
diff --git a/lib/usbhost/USB_Host_Shield_2.0/PSBuzz.h b/lib/usbhost/USB_Host_Shield_2.0/PSBuzz.h new file mode 100644 index 000000000..8880d9e50 --- /dev/null +++ b/lib/usbhost/USB_Host_Shield_2.0/PSBuzz.h | |||
@@ -0,0 +1,185 @@ | |||
1 | /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved. | ||
2 | |||
3 | This software may be distributed and modified under the terms of the GNU | ||
4 | General Public License version 2 (GPL2) as published by the Free Software | ||
5 | Foundation and appearing in the file GPL2.TXT included in the packaging of | ||
6 | this file. Please note that GPL2 Section 2[b] requires that all works based | ||
7 | on this software must also be made publicly available under the terms of | ||
8 | the GPL2 ("Copyleft"). | ||
9 | |||
10 | Contact information | ||
11 | ------------------- | ||
12 | |||
13 | Kristian Lauszus, TKJ Electronics | ||
14 | Web : http://www.tkjelectronics.com | ||
15 | e-mail : kristianl@tkjelectronics.com | ||
16 | */ | ||
17 | |||
18 | #ifndef _psbuzz_h_ | ||
19 | #define _psbuzz_h_ | ||
20 | |||
21 | #include "hiduniversal.h" | ||
22 | #include "controllerEnums.h" | ||
23 | |||
24 | #define PSBUZZ_VID 0x054C // Sony Corporation | ||
25 | #define PSBUZZ_PID 0x1000 // PS Buzz Controller | ||
26 | |||
27 | /** Struct used to easily read the different buttons on the controllers */ | ||
28 | union PSBUZZButtons { | ||
29 | struct { | ||
30 | uint8_t red : 1; | ||
31 | uint8_t yellow : 1; | ||
32 | uint8_t green : 1; | ||
33 | uint8_t orange : 1; | ||
34 | uint8_t blue : 1; | ||
35 | } __attribute__((packed)) btn[4]; | ||
36 | uint32_t val : 20; | ||
37 | } __attribute__((packed)); | ||
38 | |||
39 | /** | ||
40 | * This class implements support for the PS Buzz controllers via USB. | ||
41 | * It uses the HIDUniversal class for all the USB communication. | ||
42 | */ | ||
43 | class PSBuzz : public HIDUniversal { | ||
44 | public: | ||
45 | /** | ||
46 | * Constructor for the PSBuzz class. | ||
47 | * @param p Pointer to the USB class instance. | ||
48 | */ | ||
49 | PSBuzz(USB *p) : | ||
50 | HIDUniversal(p) { | ||
51 | Reset(); | ||
52 | }; | ||
53 | |||
54 | /** | ||
55 | * Used to check if a PS Buzz controller is connected. | ||
56 | * @return Returns true if it is connected. | ||
57 | */ | ||
58 | bool connected() { | ||
59 | return HIDUniversal::isReady() && HIDUniversal::VID == PSBUZZ_VID && HIDUniversal::PID == PSBUZZ_PID; | ||
60 | }; | ||
61 | |||
62 | /** | ||
63 | * Used to call your own function when the device is successfully initialized. | ||
64 | * @param funcOnInit Function to call. | ||
65 | */ | ||
66 | void attachOnInit(void (*funcOnInit)(void)) { | ||
67 | pFuncOnInit = funcOnInit; | ||
68 | }; | ||
69 | |||
70 | /** @name PS Buzzer Controller functions */ | ||
71 | /** | ||
72 | * getButtonPress(ButtonEnum b) will return true as long as the button is held down. | ||
73 | * | ||
74 | * While getButtonClick(ButtonEnum b) will only return it once. | ||
75 | * | ||
76 | * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b), | ||
77 | * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b). | ||
78 | * @param b ::ButtonEnum to read. | ||
79 | * @param controller The controller to read from. Default to 0. | ||
80 | * @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press. | ||
81 | */ | ||
82 | bool getButtonPress(ButtonEnum b, uint8_t controller = 0); | ||
83 | bool getButtonClick(ButtonEnum b, uint8_t controller = 0); | ||
84 | /**@}*/ | ||
85 | /** @name PS Buzzer Controller functions */ | ||
86 | /** | ||
87 | * Set LED value without using ::LEDEnum. | ||
88 | * @param value See: ::LEDEnum. | ||
89 | */ | ||
90 | /** | ||
91 | * Set LED values directly. | ||
92 | * @param value Used to set whenever the LED should be on or off | ||
93 | * @param controller The controller to control. Defaults to 0. | ||
94 | */ | ||
95 | void setLedRaw(bool value, uint8_t controller = 0); | ||
96 | |||
97 | /** Turn all LEDs off. */ | ||
98 | void setLedOffAll() { | ||
99 | for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw | ||
100 | ledState[i] = false; // Just an easy way to set all four off at the same time | ||
101 | setLedRaw(false); // Turn the LED off, on all four controllers | ||
102 | }; | ||
103 | |||
104 | /** | ||
105 | * Turn the LED off on a specific controller. | ||
106 | * @param controller The controller to turn off. Defaults to 0. | ||
107 | */ | ||
108 | void setLedOff(uint8_t controller = 0) { | ||
109 | setLedRaw(false, controller); | ||
110 | }; | ||
111 | |||
112 | |||
113 | /** Turn all LEDs on. */ | ||
114 | void setLedOnAll() { | ||
115 | for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw | ||
116 | ledState[i] = true; // Just an easy way to set all four off at the same time | ||
117 | setLedRaw(true); // Turn the LED on, on all four controllers | ||
118 | }; | ||
119 | |||
120 | /** | ||
121 | * Turn the LED on on a specific controller. | ||
122 | * @param controller The controller to turn off. Defaults to 0. | ||
123 | */ | ||
124 | void setLedOn(uint8_t controller = 0) { | ||
125 | setLedRaw(true, controller); | ||
126 | }; | ||
127 | |||
128 | /** | ||
129 | * Toggle the LED on a specific controller. | ||
130 | * @param controller The controller to turn off. Defaults to 0. | ||
131 | */ | ||
132 | void setLedToggle(uint8_t controller = 0) { | ||
133 | setLedRaw(!ledState[controller], controller); | ||
134 | }; | ||
135 | /**@}*/ | ||
136 | |||
137 | protected: | ||
138 | /** @name HIDUniversal implementation */ | ||
139 | /** | ||
140 | * Used to parse USB HID data. | ||
141 | * @param hid Pointer to the HID class. | ||
142 | * @param is_rpt_id Only used for Hubs. | ||
143 | * @param len The length of the incoming data. | ||
144 | * @param buf Pointer to the data buffer. | ||
145 | */ | ||
146 | void ParseHIDData(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); | ||
147 | |||
148 | /** | ||
149 | * Called when a device is successfully initialized. | ||
150 | * Use attachOnInit(void (*funcOnInit)(void)) to call your own function. | ||
151 | * This is useful for instance if you want to set the LEDs in a specific way. | ||
152 | */ | ||
153 | uint8_t OnInitSuccessful(); | ||
154 | /**@}*/ | ||
155 | |||
156 | /** Used to reset the different buffers to their default values */ | ||
157 | void Reset() { | ||
158 | psbuzzButtons.val = 0; | ||
159 | oldButtonState.val = 0; | ||
160 | buttonClickState.val = 0; | ||
161 | for (uint8_t i = 0; i < sizeof(ledState); i++) | ||
162 | ledState[i] = 0; | ||
163 | }; | ||
164 | |||
165 | /** @name USBDeviceConfig implementation */ | ||
166 | /** | ||
167 | * Used by the USB core to check what this driver support. | ||
168 | * @param vid The device's VID. | ||
169 | * @param pid The device's PID. | ||
170 | * @return Returns true if the device's VID and PID matches this driver. | ||
171 | */ | ||
172 | virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) { | ||
173 | return (vid == PSBUZZ_VID && pid == PSBUZZ_PID); | ||
174 | }; | ||
175 | /**@}*/ | ||
176 | |||
177 | private: | ||
178 | void (*pFuncOnInit)(void); // Pointer to function called in onInit() | ||
179 | |||
180 | void PSBuzz_Command(uint8_t *data, uint16_t nbytes); | ||
181 | |||
182 | PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState; | ||
183 | bool ledState[4]; | ||
184 | }; | ||
185 | #endif | ||