Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Committer:
hudakz
Date:
Sun Jul 12 20:39:26 2020 +0000
Revision:
0:84353c479782
Child:
1:2263e77400e9
MAX3421E-based USB Host Shield Library

Who changed what in which revision?

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