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) 2012 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 _ps3usb_h_
hudakz 0:84353c479782 19 #define _ps3usb_h_
hudakz 0:84353c479782 20
hudakz 0:84353c479782 21 #include "Usb.h"
hudakz 0:84353c479782 22 #include "usbhid.h"
hudakz 0:84353c479782 23 #include "PS3Enums.h"
hudakz 0:84353c479782 24
hudakz 0:84353c479782 25 /* PS3 data taken from descriptors */
hudakz 0:84353c479782 26 #define EP_MAXPKTSIZE 64 // max size for data via USB
hudakz 0:84353c479782 27
hudakz 0:84353c479782 28 /* Names we give to the 3 ps3 pipes - this is only used for setting the bluetooth address into the ps3 controllers */
hudakz 0:84353c479782 29 #define PS3_CONTROL_PIPE 0
hudakz 0:84353c479782 30 #define PS3_OUTPUT_PIPE 1
hudakz 0:84353c479782 31 #define PS3_INPUT_PIPE 2
hudakz 0:84353c479782 32
hudakz 0:84353c479782 33 //PID and VID of the different devices
hudakz 0:84353c479782 34 #define PS3_VID 0x054C // Sony Corporation
hudakz 0:84353c479782 35 #define PS3_PID 0x0268 // PS3 Controller DualShock 3
hudakz 0:84353c479782 36 #define PS3NAVIGATION_PID 0x042F // Navigation controller
hudakz 0:84353c479782 37 #define PS3MOVE_PID 0x03D5 // Motion controller
hudakz 0:84353c479782 38
hudakz 0:84353c479782 39 #define PS3_MAX_ENDPOINTS 3
hudakz 0:84353c479782 40
hudakz 0:84353c479782 41 /**
hudakz 0:84353c479782 42 * This class implements support for all the official PS3 Controllers:
hudakz 0:84353c479782 43 * Dualshock 3, Navigation or a Motion controller via USB.
hudakz 0:84353c479782 44 *
hudakz 0:84353c479782 45 * One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB on the Move controller.
hudakz 0:84353c479782 46 *
hudakz 0:84353c479782 47 * Information about the protocol can be found at the wiki: https://github.com/felis/USB_Host_Shield_2.0/wiki/PS3-Information.
hudakz 0:84353c479782 48 */
hudakz 0:84353c479782 49 class PS3USB : public USBDeviceConfig {
hudakz 0:84353c479782 50 public:
hudakz 0:84353c479782 51 /**
hudakz 0:84353c479782 52 * Constructor for the PS3USB class.
hudakz 0:84353c479782 53 * @param pUsb Pointer to USB class instance.
hudakz 0:84353c479782 54 * @param btadr5,btadr4,btadr3,btadr2,btadr1,btadr0
hudakz 0:84353c479782 55 * Pass your dongles Bluetooth address into the constructor,
hudakz 0:84353c479782 56 * so you are able to pair the controller with a Bluetooth dongle.
hudakz 0:84353c479782 57 */
hudakz 0:84353c479782 58 PS3USB(USB *pUsb, uint8_t btadr5 = 0, uint8_t btadr4 = 0, uint8_t btadr3 = 0, uint8_t btadr2 = 0, uint8_t btadr1 = 0, uint8_t btadr0 = 0);
hudakz 0:84353c479782 59
hudakz 0:84353c479782 60 /** @name USBDeviceConfig implementation */
hudakz 0:84353c479782 61 /**
hudakz 0:84353c479782 62 * Initialize the PS3 Controller.
hudakz 0:84353c479782 63 * @param parent Hub number.
hudakz 0:84353c479782 64 * @param port Port number on the hub.
hudakz 0:84353c479782 65 * @param lowspeed Speed of the device.
hudakz 0:84353c479782 66 * @return 0 on success.
hudakz 0:84353c479782 67 */
hudakz 0:84353c479782 68 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
hudakz 0:84353c479782 69 /**
hudakz 0:84353c479782 70 * Release the USB device.
hudakz 0:84353c479782 71 * @return 0 on success.
hudakz 0:84353c479782 72 */
hudakz 0:84353c479782 73 uint8_t Release();
hudakz 0:84353c479782 74 /**
hudakz 0:84353c479782 75 * Poll the USB Input endpoins and run the state machines.
hudakz 0:84353c479782 76 * @return 0 on success.
hudakz 0:84353c479782 77 */
hudakz 0:84353c479782 78 uint8_t Poll();
hudakz 0:84353c479782 79
hudakz 0:84353c479782 80 /**
hudakz 0:84353c479782 81 * Get the device address.
hudakz 0:84353c479782 82 * @return The device address.
hudakz 0:84353c479782 83 */
hudakz 0:84353c479782 84 virtual uint8_t GetAddress() {
hudakz 0:84353c479782 85 return bAddress;
hudakz 0:84353c479782 86 };
hudakz 0:84353c479782 87
hudakz 0:84353c479782 88 /**
hudakz 0:84353c479782 89 * Used to check if the controller has been initialized.
hudakz 0:84353c479782 90 * @return True if it's ready.
hudakz 0:84353c479782 91 */
hudakz 0:84353c479782 92 virtual bool isReady() {
hudakz 0:84353c479782 93 return bPollEnable;
hudakz 0:84353c479782 94 };
hudakz 0:84353c479782 95
hudakz 0:84353c479782 96 /**
hudakz 0:84353c479782 97 * Used by the USB core to check what this driver support.
hudakz 0:84353c479782 98 * @param vid The device's VID.
hudakz 0:84353c479782 99 * @param pid The device's PID.
hudakz 0:84353c479782 100 * @return Returns true if the device's VID and PID matches this driver.
hudakz 0:84353c479782 101 */
hudakz 0:84353c479782 102 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
hudakz 0:84353c479782 103 return (vid == PS3_VID && (pid == PS3_PID || pid == PS3NAVIGATION_PID || pid == PS3MOVE_PID));
hudakz 0:84353c479782 104 };
hudakz 0:84353c479782 105 /**@}*/
hudakz 0:84353c479782 106
hudakz 0:84353c479782 107 /**
hudakz 0:84353c479782 108 * Used to set the Bluetooth address inside the Dualshock 3 and Navigation controller.
hudakz 0:84353c479782 109 * Set using LSB first.
hudakz 0:84353c479782 110 * @param bdaddr Your dongles Bluetooth address.
hudakz 0:84353c479782 111 */
hudakz 0:84353c479782 112 void setBdaddr(uint8_t *bdaddr);
hudakz 0:84353c479782 113 /**
hudakz 0:84353c479782 114 * Used to get the Bluetooth address inside the Dualshock 3 and Navigation controller.
hudakz 0:84353c479782 115 * Will return LSB first.
hudakz 0:84353c479782 116 * @param bdaddr Your dongles Bluetooth address.
hudakz 0:84353c479782 117 */
hudakz 0:84353c479782 118 void getBdaddr(uint8_t *bdaddr);
hudakz 0:84353c479782 119
hudakz 0:84353c479782 120 /**
hudakz 0:84353c479782 121 * Used to set the Bluetooth address inside the Move controller.
hudakz 0:84353c479782 122 * Set using LSB first.
hudakz 0:84353c479782 123 * @param bdaddr Your dongles Bluetooth address.
hudakz 0:84353c479782 124 */
hudakz 0:84353c479782 125 void setMoveBdaddr(uint8_t *bdaddr);
hudakz 0:84353c479782 126 /**
hudakz 0:84353c479782 127 * Used to get the Bluetooth address inside the Move controller.
hudakz 0:84353c479782 128 * Will return LSB first.
hudakz 0:84353c479782 129 * @param bdaddr Your dongles Bluetooth address.
hudakz 0:84353c479782 130 */
hudakz 0:84353c479782 131 void getMoveBdaddr(uint8_t *bdaddr);
hudakz 0:84353c479782 132 /**
hudakz 0:84353c479782 133 * Used to get the calibration data inside the Move controller.
hudakz 0:84353c479782 134 * @param data Buffer to store data in. Must be at least 147 bytes
hudakz 0:84353c479782 135 */
hudakz 0:84353c479782 136 void getMoveCalibration(uint8_t *data);
hudakz 0:84353c479782 137
hudakz 0:84353c479782 138 /** @name PS3 Controller functions */
hudakz 0:84353c479782 139 /**
hudakz 0:84353c479782 140 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
hudakz 0:84353c479782 141 *
hudakz 0:84353c479782 142 * While getButtonClick(ButtonEnum b) will only return it once.
hudakz 0:84353c479782 143 *
hudakz 0:84353c479782 144 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
hudakz 0:84353c479782 145 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
hudakz 0:84353c479782 146 * @param b ::ButtonEnum to read.
hudakz 0:84353c479782 147 * @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 148 */
hudakz 0:84353c479782 149 bool getButtonPress(ButtonEnum b);
hudakz 0:84353c479782 150 bool getButtonClick(ButtonEnum b);
hudakz 0:84353c479782 151 /**@}*/
hudakz 0:84353c479782 152 /** @name PS3 Controller functions */
hudakz 0:84353c479782 153 /**
hudakz 0:84353c479782 154 * Used to get the analog value from button presses.
hudakz 0:84353c479782 155 * @param a The ::ButtonEnum to read.
hudakz 0:84353c479782 156 * The supported buttons are:
hudakz 0:84353c479782 157 * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
hudakz 0:84353c479782 158 * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
hudakz 0:84353c479782 159 * @return Analog value in the range of 0-255.
hudakz 0:84353c479782 160 */
hudakz 0:84353c479782 161 uint8_t getAnalogButton(ButtonEnum a);
hudakz 0:84353c479782 162 /**
hudakz 0:84353c479782 163 * Used to read the analog joystick.
hudakz 0:84353c479782 164 * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
hudakz 0:84353c479782 165 * @return Return the analog value in the range of 0-255.
hudakz 0:84353c479782 166 */
hudakz 0:84353c479782 167 uint8_t getAnalogHat(AnalogHatEnum a);
hudakz 0:84353c479782 168 /**
hudakz 0:84353c479782 169 * Used to read the sensors inside the Dualshock 3 controller.
hudakz 0:84353c479782 170 * @param a
hudakz 0:84353c479782 171 * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
hudakz 0:84353c479782 172 * @return Return the raw sensor value.
hudakz 0:84353c479782 173 */
hudakz 0:84353c479782 174 uint16_t getSensor(SensorEnum a);
hudakz 0:84353c479782 175 /**
hudakz 0:84353c479782 176 * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
hudakz 0:84353c479782 177 * @param a Either ::Pitch or ::Roll.
hudakz 0:84353c479782 178 * @return Return the angle in the range of 0-360.
hudakz 0:84353c479782 179 */
hudakz 0:84353c479782 180 float getAngle(AngleEnum a);
hudakz 0:84353c479782 181 /**
hudakz 0:84353c479782 182 * Get the ::StatusEnum from the controller.
hudakz 0:84353c479782 183 * @param c The ::StatusEnum you want to read.
hudakz 0:84353c479782 184 * @return True if correct and false if not.
hudakz 0:84353c479782 185 */
hudakz 0:84353c479782 186 bool getStatus(StatusEnum c);
hudakz 0:84353c479782 187 /** Read all the available statuses from the controller and prints it as a nice formated string. */
hudakz 0:84353c479782 188 void printStatusString();
hudakz 0:84353c479782 189
hudakz 0:84353c479782 190 /** Used to set all LEDs and rumble off. */
hudakz 0:84353c479782 191 void setAllOff();
hudakz 0:84353c479782 192 /** Turn off rumble. */
hudakz 0:84353c479782 193 void setRumbleOff();
hudakz 0:84353c479782 194 /**
hudakz 0:84353c479782 195 * Turn on rumble.
hudakz 0:84353c479782 196 * @param mode Either ::RumbleHigh or ::RumbleLow.
hudakz 0:84353c479782 197 */
hudakz 0:84353c479782 198 void setRumbleOn(RumbleEnum mode);
hudakz 0:84353c479782 199 /**
hudakz 0:84353c479782 200 * Turn on rumble using custom duration and power.
hudakz 0:84353c479782 201 * @param rightDuration The duration of the right/low rumble effect.
hudakz 0:84353c479782 202 * @param rightPower The intensity of the right/low rumble effect.
hudakz 0:84353c479782 203 * @param leftDuration The duration of the left/high rumble effect.
hudakz 0:84353c479782 204 * @param leftPower The intensity of the left/high rumble effect.
hudakz 0:84353c479782 205 */
hudakz 0:84353c479782 206 void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
hudakz 0:84353c479782 207
hudakz 0:84353c479782 208 /**
hudakz 0:84353c479782 209 * Set LED value without using the ::LEDEnum.
hudakz 0:84353c479782 210 * @param value See: ::LEDEnum.
hudakz 0:84353c479782 211 */
hudakz 0:84353c479782 212 void setLedRaw(uint8_t value);
hudakz 0:84353c479782 213
hudakz 0:84353c479782 214 /** Turn all LEDs off. */
hudakz 0:84353c479782 215 void setLedOff() {
hudakz 0:84353c479782 216 setLedRaw(0);
hudakz 0:84353c479782 217 }
hudakz 0:84353c479782 218 /**
hudakz 0:84353c479782 219 * Turn the specific ::LEDEnum off.
hudakz 0:84353c479782 220 * @param a The ::LEDEnum to turn off.
hudakz 0:84353c479782 221 */
hudakz 0:84353c479782 222 void setLedOff(LEDEnum a);
hudakz 0:84353c479782 223 /**
hudakz 0:84353c479782 224 * Turn the specific ::LEDEnum on.
hudakz 0:84353c479782 225 * @param a The ::LEDEnum to turn on.
hudakz 0:84353c479782 226 */
hudakz 0:84353c479782 227 void setLedOn(LEDEnum a);
hudakz 0:84353c479782 228 /**
hudakz 0:84353c479782 229 * Toggle the specific ::LEDEnum.
hudakz 0:84353c479782 230 * @param a The ::LEDEnum to toggle.
hudakz 0:84353c479782 231 */
hudakz 0:84353c479782 232 void setLedToggle(LEDEnum a);
hudakz 0:84353c479782 233
hudakz 0:84353c479782 234 /**
hudakz 0:84353c479782 235 * Use this to set the Color using RGB values.
hudakz 0:84353c479782 236 * @param r,g,b RGB value.
hudakz 0:84353c479782 237 */
hudakz 0:84353c479782 238 void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
hudakz 0:84353c479782 239 /**
hudakz 0:84353c479782 240 * Use this to set the color using the predefined colors in ::ColorsEnum.
hudakz 0:84353c479782 241 * @param color The desired color.
hudakz 0:84353c479782 242 */
hudakz 0:84353c479782 243 void moveSetBulb(ColorsEnum color);
hudakz 0:84353c479782 244 /**
hudakz 0:84353c479782 245 * Set the rumble value inside the Move controller.
hudakz 0:84353c479782 246 * @param rumble The desired value in the range from 64-255.
hudakz 0:84353c479782 247 */
hudakz 0:84353c479782 248 void moveSetRumble(uint8_t rumble);
hudakz 0:84353c479782 249
hudakz 0:84353c479782 250 /**
hudakz 0:84353c479782 251 * Used to call your own function when the controller is successfully initialized.
hudakz 0:84353c479782 252 * @param funcOnInit Function to call.
hudakz 0:84353c479782 253 */
hudakz 0:84353c479782 254 void attachOnInit(void (*funcOnInit)(void)) {
hudakz 0:84353c479782 255 pFuncOnInit = funcOnInit;
hudakz 0:84353c479782 256 };
hudakz 0:84353c479782 257 /**@}*/
hudakz 0:84353c479782 258
hudakz 0:84353c479782 259 /** Variable used to indicate if the normal playstation controller is successfully connected. */
hudakz 0:84353c479782 260 bool PS3Connected;
hudakz 0:84353c479782 261 /** Variable used to indicate if the move controller is successfully connected. */
hudakz 0:84353c479782 262 bool PS3MoveConnected;
hudakz 0:84353c479782 263 /** Variable used to indicate if the navigation controller is successfully connected. */
hudakz 0:84353c479782 264 bool PS3NavigationConnected;
hudakz 0:84353c479782 265
hudakz 0:84353c479782 266 protected:
hudakz 0:84353c479782 267 /** Pointer to USB class instance. */
hudakz 0:84353c479782 268 USB *pUsb;
hudakz 0:84353c479782 269 /** Device address. */
hudakz 0:84353c479782 270 uint8_t bAddress;
hudakz 0:84353c479782 271 /** Endpoint info structure. */
hudakz 0:84353c479782 272 EpInfo epInfo[PS3_MAX_ENDPOINTS];
hudakz 0:84353c479782 273
hudakz 0:84353c479782 274 private:
hudakz 0:84353c479782 275 /**
hudakz 0:84353c479782 276 * Called when the controller is successfully initialized.
hudakz 0:84353c479782 277 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
hudakz 0:84353c479782 278 * This is useful for instance if you want to set the LEDs in a specific way.
hudakz 0:84353c479782 279 */
hudakz 0:84353c479782 280 void onInit();
hudakz 0:84353c479782 281 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
hudakz 0:84353c479782 282
hudakz 0:84353c479782 283 bool bPollEnable;
hudakz 0:84353c479782 284
hudakz 0:84353c479782 285 uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values
hudakz 0:84353c479782 286
hudakz 0:84353c479782 287 uint32_t ButtonState;
hudakz 0:84353c479782 288 uint32_t OldButtonState;
hudakz 0:84353c479782 289 uint32_t ButtonClickState;
hudakz 0:84353c479782 290
hudakz 0:84353c479782 291 uint8_t my_bdaddr[6]; // Change to your dongles Bluetooth address in the constructor
hudakz 0:84353c479782 292 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
hudakz 0:84353c479782 293 uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
hudakz 0:84353c479782 294
hudakz 0:84353c479782 295 void readReport(); // read incoming data
hudakz 0:84353c479782 296 void printReport(); // print incoming date - Uncomment for debugging
hudakz 0:84353c479782 297
hudakz 0:84353c479782 298 /* Private commands */
hudakz 0:84353c479782 299 void PS3_Command(uint8_t *data, uint16_t nbytes);
hudakz 0:84353c479782 300 void enable_sixaxis(); // Command used to enable the Dualshock 3 and Navigation controller to send data via USB
hudakz 0:84353c479782 301 void Move_Command(uint8_t *data, uint16_t nbytes);
hudakz 0:84353c479782 302 };
hudakz 0:84353c479782 303 #endif