You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PSBuzz.h 6.7KB

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