Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PSBuzz.h Source File

PSBuzz.h

00001 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. All rights reserved.
00002 
00003  This software may be distributed and modified under the terms of the GNU
00004  General Public License version 2 (GPL2) as published by the Free Software
00005  Foundation and appearing in the file GPL2.TXT included in the packaging of
00006  this file. Please note that GPL2 Section 2[b] requires that all works based
00007  on this software must also be made publicly available under the terms of
00008  the GPL2 ("Copyleft").
00009 
00010  Contact information
00011  -------------------
00012 
00013  Kristian Lauszus, TKJ Electronics
00014  Web      :  http://www.tkjelectronics.com
00015  e-mail   :  kristianl@tkjelectronics.com
00016  */
00017 
00018 #ifndef _psbuzz_h_
00019 #define _psbuzz_h_
00020 
00021 #include "hiduniversal.h"
00022 #include "controllerEnums.h"
00023 
00024 #define PSBUZZ_VID 0x054C // Sony Corporation
00025 #define PSBUZZ_PID 0x1000 // PS Buzz Controller
00026 
00027 /** Struct used to easily read the different buttons on the controllers */
00028 union PSBUZZButtons {
00029         struct {
00030                 uint8_t red : 1;
00031                 uint8_t yellow : 1;
00032                 uint8_t green : 1;
00033                 uint8_t orange : 1;
00034                 uint8_t blue : 1;
00035         } __attribute__((packed)) btn[4];
00036         uint32_t val : 20;
00037 } __attribute__((packed));
00038 
00039 /**
00040  * This class implements support for the PS Buzz controllers via USB.
00041  * It uses the HIDUniversal class for all the USB communication.
00042  */
00043 class PSBuzz : public HIDUniversal {
00044 public:
00045         /**
00046          * Constructor for the PSBuzz class.
00047          * @param  p   Pointer to the USB class instance.
00048          */
00049         PSBuzz(Usb *p) :
00050         HIDUniversal(p) {
00051                 Reset();
00052         };
00053 
00054         /**
00055          * Used to check if a PS Buzz controller is connected.
00056          * @return Returns true if it is connected.
00057          */
00058         bool connected() {
00059                 return HIDUniversal::isReady() && HIDUniversal::VID == PSBUZZ_VID && HIDUniversal::PID == PSBUZZ_PID;
00060         };
00061 
00062         /**
00063          * Used to call your own function when the device is successfully initialized.
00064          * @param funcOnInit Function to call.
00065          */
00066         void attachOnInit(void (*funcOnInit)(void)) {
00067                 pFuncOnInit = funcOnInit;
00068         };
00069 
00070         /** @name PS Buzzer Controller functions */
00071         /**
00072          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
00073          *
00074          * While getButtonClick(ButtonEnum b) will only return it once.
00075          *
00076          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
00077          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
00078          * @param  b          ::ButtonEnum to read.
00079          * @param  controller The controller to read from. Default to 0.
00080          * @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.
00081          */
00082         bool getButtonPress(ButtonEnum b, uint8_t controller = 0);
00083         bool getButtonClick(ButtonEnum b, uint8_t controller = 0);
00084         /**@}*/
00085         /** @name PS Buzzer Controller functions */
00086         /**
00087          * Set LED value without using ::LEDEnum.
00088          * @param value See: ::LEDEnum.
00089          */
00090         /**
00091          * Set LED values directly.
00092          * @param value      Used to set whenever the LED should be on or off
00093          * @param controller The controller to control. Defaults to 0.
00094          */
00095         void setLedRaw(bool value, uint8_t controller = 0);
00096 
00097         /** Turn all LEDs off. */
00098         void setLedOffAll() {
00099                 for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw
00100                         ledState[i] = false; // Just an easy way to set all four off at the same time
00101                 setLedRaw(false); // Turn the LED off, on all four controllers
00102         };
00103 
00104         /**
00105          * Turn the LED off on a specific controller.
00106          * @param controller The controller to turn off. Defaults to 0.
00107          */
00108         void setLedOff(uint8_t controller = 0) {
00109                 setLedRaw(false, controller);
00110         };
00111 
00112 
00113         /** Turn all LEDs on. */
00114         void setLedOnAll() {
00115                 for (uint8_t i = 1; i < 4; i++) // Skip first as it will be set in setLedRaw
00116                         ledState[i] = true; // Just an easy way to set all four off at the same time
00117                 setLedRaw(true); // Turn the LED on, on all four controllers
00118         };
00119 
00120         /**
00121          * Turn the LED on on a specific controller.
00122          * @param controller The controller to turn off. Defaults to 0.
00123          */
00124         void setLedOn(uint8_t controller = 0) {
00125                 setLedRaw(true, controller);
00126         };
00127 
00128         /**
00129          * Toggle the LED on a specific controller.
00130          * @param controller The controller to turn off. Defaults to 0.
00131          */
00132         void setLedToggle(uint8_t controller = 0) {
00133                 setLedRaw(!ledState[controller], controller);
00134         };
00135         /**@}*/
00136 
00137 protected:
00138         /** @name HIDUniversal implementation */
00139         /**
00140          * Used to parse USB HID data.
00141          * @param hid       Pointer to the HID class.
00142          * @param is_rpt_id Only used for Hubs.
00143          * @param len       The length of the incoming data.
00144          * @param buf       Pointer to the data buffer.
00145          */
00146         void ParseHIDData(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
00147 
00148         /**
00149          * Called when a device is successfully initialized.
00150          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00151          * This is useful for instance if you want to set the LEDs in a specific way.
00152          */
00153         uint8_t OnInitSuccessful();
00154         /**@}*/
00155 
00156         /** Used to reset the different buffers to their default values */
00157         void Reset() {
00158                 psbuzzButtons.val = 0;
00159                 oldButtonState.val = 0;
00160                 buttonClickState.val = 0;
00161                 for (uint8_t i = 0; i < sizeof(ledState); i++)
00162                         ledState[i] = 0;
00163         };
00164 
00165         /** @name USBDeviceConfig implementation */
00166         /**
00167          * Used by the USB core to check what this driver support.
00168          * @param  vid The device's VID.
00169          * @param  pid The device's PID.
00170          * @return     Returns true if the device's VID and PID matches this driver.
00171          */
00172         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
00173                 return (vid == PSBUZZ_VID && pid == PSBUZZ_PID);
00174         };
00175         /**@}*/
00176 
00177 private:
00178         void (*pFuncOnInit)(void); // Pointer to function called in onInit()
00179 
00180         void PSBuzz_Command(uint8_t *data, uint16_t nbytes);
00181 
00182         PSBUZZButtons psbuzzButtons, oldButtonState, buttonClickState;
00183         bool ledState[4];
00184 };
00185 #endif