1 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
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
13 Kristian Lauszus, TKJ Electronics
14 Web : http://www.tkjelectronics.com
15 e-mail : kristianl@tkjelectronics.com
21 #include "hiduniversal.h"
22 #include "controllerEnums.h"
24 #define PSBUZZ_VID 0x054C // Sony Corporation
25 #define PSBUZZ_PID 0x1000 // PS Buzz Controller
27 /** Struct used to easily read the different buttons on the controllers */
35 } __attribute__((packed
)) btn
[4];
37 } __attribute__((packed
));
40 * This class implements support for the PS Buzz controllers via USB.
41 * It uses the HIDUniversal class for all the USB communication.
43 class PSBuzz
: public HIDUniversal
{
46 * Constructor for the PSBuzz class.
47 * @param p Pointer to the USB class instance.
55 * Used to check if a PS Buzz controller is connected.
56 * @return Returns true if it is connected.
59 return HIDUniversal::isReady() && HIDUniversal::VID
== PSBUZZ_VID
&& HIDUniversal::PID
== PSBUZZ_PID
;
63 * Used to call your own function when the device is successfully initialized.
64 * @param funcOnInit Function to call.
66 void attachOnInit(void (*funcOnInit
)(void)) {
67 pFuncOnInit
= funcOnInit
;
70 /** @name PS Buzzer Controller functions */
72 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
74 * While getButtonClick(ButtonEnum b) will only return it once.
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.
82 bool getButtonPress(ButtonEnum b
, uint8_t controller
= 0);
83 bool getButtonClick(ButtonEnum b
, uint8_t controller
= 0);
85 /** @name PS Buzzer Controller functions */
87 * Set LED value without using ::LEDEnum.
88 * @param value See: ::LEDEnum.
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.
95 void setLedRaw(bool value
, uint8_t controller
= 0);
97 /** Turn all LEDs off. */
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
105 * Turn the LED off on a specific controller.
106 * @param controller The controller to turn off. Defaults to 0.
108 void setLedOff(uint8_t controller
= 0) {
109 setLedRaw(false, controller
);
113 /** Turn all LEDs on. */
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
121 * Turn the LED on on a specific controller.
122 * @param controller The controller to turn off. Defaults to 0.
124 void setLedOn(uint8_t controller
= 0) {
125 setLedRaw(true, controller
);
129 * Toggle the LED on a specific controller.
130 * @param controller The controller to turn off. Defaults to 0.
132 void setLedToggle(uint8_t controller
= 0) {
133 setLedRaw(!ledState
[controller
], controller
);
138 /** @name HIDUniversal implementation */
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.
146 void ParseHIDData(HID
*hid
, bool is_rpt_id
, uint8_t len
, uint8_t *buf
);
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.
153 uint8_t OnInitSuccessful();
156 /** Used to reset the different buffers to their default values */
158 psbuzzButtons
.val
= 0;
159 oldButtonState
.val
= 0;
160 buttonClickState
.val
= 0;
161 for (uint8_t i
= 0; i
< sizeof(ledState
); i
++)
165 /** @name USBDeviceConfig implementation */
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.
172 virtual bool VIDPIDOK(uint16_t vid
, uint16_t pid
) {
173 return (vid
== PSBUZZ_VID
&& pid
== PSBUZZ_PID
);
178 void (*pFuncOnInit
)(void); // Pointer to function called in onInit()
180 void PSBuzz_Command(uint8_t *data
, uint16_t nbytes
);
182 PSBUZZButtons psbuzzButtons
, oldButtonState
, buttonClickState
;