aaa

Dependencies:   mbed ArduinoUsbHostShield

Committer:
hamaken1018
Date:
Fri Jun 04 14:43:00 2021 +0000
Revision:
0:12b41747ca26
aaa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hamaken1018 0:12b41747ca26 1 #include "mbed.h"
hamaken1018 0:12b41747ca26 2 #include <PS5BT.h>
hamaken1018 0:12b41747ca26 3 #include <usbhub.h>
hamaken1018 0:12b41747ca26 4
hamaken1018 0:12b41747ca26 5 Serial pc(USBTX, USBRX, 115200);
hamaken1018 0:12b41747ca26 6
hamaken1018 0:12b41747ca26 7 //Nucleo f303k8用
hamaken1018 0:12b41747ca26 8 USB Usb(A6, A5, A4, A3, A2); // mosi, miso, sclk, ssel, intr
hamaken1018 0:12b41747ca26 9 BTD Btd(&Usb);
hamaken1018 0:12b41747ca26 10 PS5BT PS5(&Btd);
hamaken1018 0:12b41747ca26 11
hamaken1018 0:12b41747ca26 12 int main()
hamaken1018 0:12b41747ca26 13 {
hamaken1018 0:12b41747ca26 14 bool printAngle = false, printTouch = false;
hamaken1018 0:12b41747ca26 15 uint8_t oldL2Value, oldR2Value;
hamaken1018 0:12b41747ca26 16 bool reportChanged;
hamaken1018 0:12b41747ca26 17 if(Usb.Init() == -1){
hamaken1018 0:12b41747ca26 18 pc.printf("\r\nOSC did not start");
hamaken1018 0:12b41747ca26 19 while(1); // Halt
hamaken1018 0:12b41747ca26 20 }
hamaken1018 0:12b41747ca26 21 pc.printf("\r\nPS5 USB Library Started");
hamaken1018 0:12b41747ca26 22
hamaken1018 0:12b41747ca26 23 while(1){
hamaken1018 0:12b41747ca26 24 Usb.Task();
hamaken1018 0:12b41747ca26 25 if (PS5.connected()) {
hamaken1018 0:12b41747ca26 26 lastMessageCounter = PS5.getMessageCounter();
hamaken1018 0:12b41747ca26 27 if(PS5.getAnalogHat(LeftHatX) > 137 || PS5.getAnalogHat(LeftHatX) < 117 || PS5.getAnalogHat(LeftHatY) > 137 || PS5.getAnalogHat(LeftHatY) < 117 || PS5.getAnalogHat(RightHatX) > 137 || PS5.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) {
hamaken1018 0:12b41747ca26 28 pc.printf("\tLeftHatX: %d",PS5.getAnalogHat(LeftHatX));
hamaken1018 0:12b41747ca26 29 pc.printf("\tLeftHatY: %d",PS5.getAnalogHat(LeftHatY));
hamaken1018 0:12b41747ca26 30 if(PS5.connected()) {
hamaken1018 0:12b41747ca26 31 pc.printf("\tRightHatX: %d",PS5.getAnalogHat(RightHatX));
hamaken1018 0:12b41747ca26 32 pc.printf("\tRightHatY: %d",PS5.getAnalogHat(RightHatY));
hamaken1018 0:12b41747ca26 33 }
hamaken1018 0:12b41747ca26 34 }
hamaken1018 0:12b41747ca26 35 if(PS5.getAnalogButton(L2) != oldL2Value || PS5.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
hamaken1018 0:12b41747ca26 36 PS5.setRumbleOn(PS5.getAnalogButton(L2), PS5.getAnalogButton(R2));
hamaken1018 0:12b41747ca26 37 oldL2Value = PS5.getAnalogButton(L2);
hamaken1018 0:12b41747ca26 38 oldR2Value = PS5.getAnalogButton(R2);
hamaken1018 0:12b41747ca26 39
hamaken1018 0:12b41747ca26 40 if(PS5.getAnalogButton(L2))
hamaken1018 0:12b41747ca26 41 pc.printf("\r\nL2");
hamaken1018 0:12b41747ca26 42 if(PS5.getAnalogButton(R2))
hamaken1018 0:12b41747ca26 43 pc.printf("\r\nR2");
hamaken1018 0:12b41747ca26 44
hamaken1018 0:12b41747ca26 45
hamaken1018 0:12b41747ca26 46 if (PS5.getButtonClick(PS)){
hamaken1018 0:12b41747ca26 47 PS5.disconnect();
hamaken1018 0:12b41747ca26 48 pc.printf("\r\nPS");
hamaken1018 0:12b41747ca26 49 }
hamaken1018 0:12b41747ca26 50
hamaken1018 0:12b41747ca26 51 if (PS5.getButtonClick(TRIANGLE))
hamaken1018 0:12b41747ca26 52 pc.printf("\r\nTriangle");
hamaken1018 0:12b41747ca26 53 if (PS5.getButtonClick(CIRCLE))
hamaken1018 0:12b41747ca26 54 pc.printf("\r\nCircle");
hamaken1018 0:12b41747ca26 55 if (PS5.getButtonClick(CROSS))
hamaken1018 0:12b41747ca26 56 pc.printf("\r\nCross");
hamaken1018 0:12b41747ca26 57 if (PS5.getButtonClick(SQUARE))
hamaken1018 0:12b41747ca26 58 pc.printf("\r\nSquare");
hamaken1018 0:12b41747ca26 59
hamaken1018 0:12b41747ca26 60 if (PS5.getButtonClick(UP)){
hamaken1018 0:12b41747ca26 61 pc.printf("\r\nUp");
hamaken1018 0:12b41747ca26 62 PS5.setLedOff();
hamaken1018 0:12b41747ca26 63 // PS5.setLedOn(CONTROLLER_LED4);
hamaken1018 0:12b41747ca26 64 }
hamaken1018 0:12b41747ca26 65 if(PS5.getButtonClick(RIGHT)){
hamaken1018 0:12b41747ca26 66 pc.printf("\r\nRight");
hamaken1018 0:12b41747ca26 67 PS5.setLedOff();
hamaken1018 0:12b41747ca26 68 // PS5.setLedOn(CONTROLLER_LED1);
hamaken1018 0:12b41747ca26 69 }
hamaken1018 0:12b41747ca26 70 if(PS5.getButtonClick(DOWN)){
hamaken1018 0:12b41747ca26 71 pc.printf("\r\nDown");
hamaken1018 0:12b41747ca26 72 PS5.setLedOff();
hamaken1018 0:12b41747ca26 73 // PS5.setLedOn(CONTROLLER_LED2);
hamaken1018 0:12b41747ca26 74 }
hamaken1018 0:12b41747ca26 75 if(PS5.getButtonClick(LEFT)){
hamaken1018 0:12b41747ca26 76 pc.printf("\r\nLeft");
hamaken1018 0:12b41747ca26 77 PS5.setLedOff();
hamaken1018 0:12b41747ca26 78 // PS5.setLedOn(CONTROLLER_LED3);
hamaken1018 0:12b41747ca26 79 }
hamaken1018 0:12b41747ca26 80
hamaken1018 0:12b41747ca26 81 if(PS5.getButtonClick(L1))
hamaken1018 0:12b41747ca26 82 pc.printf("\r\nL1");
hamaken1018 0:12b41747ca26 83 if(PS5.getButtonClick(L3))
hamaken1018 0:12b41747ca26 84 pc.printf("\r\nL3");
hamaken1018 0:12b41747ca26 85 if(PS5.getButtonClick(R1))
hamaken1018 0:12b41747ca26 86 pc.printf("\r\nR1");
hamaken1018 0:12b41747ca26 87 if(PS5.getButtonClick(R3))
hamaken1018 0:12b41747ca26 88 pc.printf("\r\nR3");
hamaken1018 0:12b41747ca26 89 /*
hamaken1018 0:12b41747ca26 90 if(PS5.getButtonClick(SELECT)){
hamaken1018 0:12b41747ca26 91 pc.printf("\r\nSelect - ");
hamaken1018 0:12b41747ca26 92 PS5.printStatusString();
hamaken1018 0:12b41747ca26 93 }
hamaken1018 0:12b41747ca26 94 */
hamaken1018 0:12b41747ca26 95 if(PS5.getButtonClick(START)){
hamaken1018 0:12b41747ca26 96 pc.printf("\r\nStart");
hamaken1018 0:12b41747ca26 97 printAngle = !printAngle;
hamaken1018 0:12b41747ca26 98 }
hamaken1018 0:12b41747ca26 99 }
hamaken1018 0:12b41747ca26 100 else{
hamaken1018 0:12b41747ca26 101 pc.printf("not connect\n");
hamaken1018 0:12b41747ca26 102 }
hamaken1018 0:12b41747ca26 103 }
hamaken1018 0:12b41747ca26 104 }
hamaken1018 0:12b41747ca26 105
hamaken1018 0:12b41747ca26 106
hamaken1018 0:12b41747ca26 107 /*
hamaken1018 0:12b41747ca26 108
hamaken1018 0:12b41747ca26 109 #include <PS5USB.h>
hamaken1018 0:12b41747ca26 110
hamaken1018 0:12b41747ca26 111 // Satisfy the IDE, which needs to see the include statment in the ino too.
hamaken1018 0:12b41747ca26 112 #ifdef dobogusinclude
hamaken1018 0:12b41747ca26 113 #include <spi4teensy3.h>
hamaken1018 0:12b41747ca26 114 #endif
hamaken1018 0:12b41747ca26 115 #include <SPI.h>
hamaken1018 0:12b41747ca26 116
hamaken1018 0:12b41747ca26 117 USB Usb;
hamaken1018 0:12b41747ca26 118 PS5USB PS5(&Usb);
hamaken1018 0:12b41747ca26 119
hamaken1018 0:12b41747ca26 120 bool printAngle = false, printTouch = false;
hamaken1018 0:12b41747ca26 121 uint16_t lastMessageCounter = -1;
hamaken1018 0:12b41747ca26 122 uint8_t player_led_mask = 0;
hamaken1018 0:12b41747ca26 123 bool microphone_led = false;
hamaken1018 0:12b41747ca26 124
hamaken1018 0:12b41747ca26 125 void setup() {
hamaken1018 0:12b41747ca26 126 Serial.begin(115200);
hamaken1018 0:12b41747ca26 127 #if !defined(__MIPSEL__)
hamaken1018 0:12b41747ca26 128 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
hamaken1018 0:12b41747ca26 129 #endif
hamaken1018 0:12b41747ca26 130 if (Usb.Init() == -1) {
hamaken1018 0:12b41747ca26 131 Serial.print(F("\r\nOSC did not start"));
hamaken1018 0:12b41747ca26 132 while (1); // Halt
hamaken1018 0:12b41747ca26 133 }
hamaken1018 0:12b41747ca26 134 Serial.print(F("\r\nPS5 USB Library Started"));
hamaken1018 0:12b41747ca26 135 }
hamaken1018 0:12b41747ca26 136
hamaken1018 0:12b41747ca26 137 void loop() {
hamaken1018 0:12b41747ca26 138 Usb.Task();
hamaken1018 0:12b41747ca26 139
hamaken1018 0:12b41747ca26 140 if (PS5.connected() && lastMessageCounter != PS5.getMessageCounter()) {
hamaken1018 0:12b41747ca26 141 lastMessageCounter = PS5.getMessageCounter();
hamaken1018 0:12b41747ca26 142
hamaken1018 0:12b41747ca26 143 if (PS5.getAnalogHat(LeftHatX) > 137 || PS5.getAnalogHat(LeftHatX) < 117 || PS5.getAnalogHat(LeftHatY) > 137 || PS5.getAnalogHat(LeftHatY) < 117 || PS5.getAnalogHat(RightHatX) > 137 || PS5.getAnalogHat(RightHatX) < 117 || PS5.getAnalogHat(RightHatY) > 137 || PS5.getAnalogHat(RightHatY) < 117) {
hamaken1018 0:12b41747ca26 144 Serial.print(F("\r\nLeftHatX: "));
hamaken1018 0:12b41747ca26 145 Serial.print(PS5.getAnalogHat(LeftHatX));
hamaken1018 0:12b41747ca26 146 Serial.print(F("\tLeftHatY: "));
hamaken1018 0:12b41747ca26 147 Serial.print(PS5.getAnalogHat(LeftHatY));
hamaken1018 0:12b41747ca26 148 Serial.print(F("\tRightHatX: "));
hamaken1018 0:12b41747ca26 149 Serial.print(PS5.getAnalogHat(RightHatX));
hamaken1018 0:12b41747ca26 150 Serial.print(F("\tRightHatY: "));
hamaken1018 0:12b41747ca26 151 Serial.print(PS5.getAnalogHat(RightHatY));
hamaken1018 0:12b41747ca26 152 }
hamaken1018 0:12b41747ca26 153
hamaken1018 0:12b41747ca26 154 if (PS5.getAnalogButton(L2) || PS5.getAnalogButton(R2)) { // These are the only analog buttons on the PS5 controller
hamaken1018 0:12b41747ca26 155 Serial.print(F("\r\nL2: "));
hamaken1018 0:12b41747ca26 156 Serial.print(PS5.getAnalogButton(L2));
hamaken1018 0:12b41747ca26 157 Serial.print(F("\tR2: "));
hamaken1018 0:12b41747ca26 158 Serial.print(PS5.getAnalogButton(R2));
hamaken1018 0:12b41747ca26 159 }
hamaken1018 0:12b41747ca26 160
hamaken1018 0:12b41747ca26 161 // Set the left trigger to resist at the right trigger's level
hamaken1018 0:12b41747ca26 162 static uint8_t oldR2Value = 0xFF;
hamaken1018 0:12b41747ca26 163 if (PS5.getAnalogButton(R2) != oldR2Value) {
hamaken1018 0:12b41747ca26 164 oldR2Value = PS5.getAnalogButton(R2);
hamaken1018 0:12b41747ca26 165 PS5.leftTrigger.setTriggerForce(oldR2Value, 255);
hamaken1018 0:12b41747ca26 166 }
hamaken1018 0:12b41747ca26 167
hamaken1018 0:12b41747ca26 168 if (PS5.getButtonClick(PS))
hamaken1018 0:12b41747ca26 169 Serial.print(F("\r\nPS"));
hamaken1018 0:12b41747ca26 170 if (PS5.getButtonClick(TRIANGLE)) {
hamaken1018 0:12b41747ca26 171 Serial.print(F("\r\nTriangle"));
hamaken1018 0:12b41747ca26 172 PS5.setRumbleOn(RumbleLow);
hamaken1018 0:12b41747ca26 173 }
hamaken1018 0:12b41747ca26 174 if (PS5.getButtonClick(CIRCLE)) {
hamaken1018 0:12b41747ca26 175 Serial.print(F("\r\nCircle"));
hamaken1018 0:12b41747ca26 176 PS5.setRumbleOn(RumbleHigh);
hamaken1018 0:12b41747ca26 177 }
hamaken1018 0:12b41747ca26 178 if (PS5.getButtonClick(CROSS)) {
hamaken1018 0:12b41747ca26 179 Serial.print(F("\r\nCross"));
hamaken1018 0:12b41747ca26 180
hamaken1018 0:12b41747ca26 181 // Set the player LEDs
hamaken1018 0:12b41747ca26 182 player_led_mask = (player_led_mask << 1) | 1;
hamaken1018 0:12b41747ca26 183 if (player_led_mask > 0x1F)
hamaken1018 0:12b41747ca26 184 player_led_mask = 0;
hamaken1018 0:12b41747ca26 185 PS5.setPlayerLed(player_led_mask); // The bottom 5 bits set player LEDs
hamaken1018 0:12b41747ca26 186 }
hamaken1018 0:12b41747ca26 187 if (PS5.getButtonClick(SQUARE)) {
hamaken1018 0:12b41747ca26 188 Serial.print(F("\r\nSquare"));
hamaken1018 0:12b41747ca26 189 PS5.setRumbleOff();
hamaken1018 0:12b41747ca26 190 }
hamaken1018 0:12b41747ca26 191
hamaken1018 0:12b41747ca26 192 if (PS5.getButtonClick(UP)) {
hamaken1018 0:12b41747ca26 193 Serial.print(F("\r\nUp"));
hamaken1018 0:12b41747ca26 194 PS5.setLed(Red);
hamaken1018 0:12b41747ca26 195 } if (PS5.getButtonClick(RIGHT)) {
hamaken1018 0:12b41747ca26 196 Serial.print(F("\r\nRight"));
hamaken1018 0:12b41747ca26 197 PS5.setLed(Blue);
hamaken1018 0:12b41747ca26 198 } if (PS5.getButtonClick(DOWN)) {
hamaken1018 0:12b41747ca26 199 Serial.print(F("\r\nDown"));
hamaken1018 0:12b41747ca26 200 PS5.setLed(Yellow);
hamaken1018 0:12b41747ca26 201 } if (PS5.getButtonClick(LEFT)) {
hamaken1018 0:12b41747ca26 202 Serial.print(F("\r\nLeft"));
hamaken1018 0:12b41747ca26 203 PS5.setLed(Green);
hamaken1018 0:12b41747ca26 204 }
hamaken1018 0:12b41747ca26 205
hamaken1018 0:12b41747ca26 206 if (PS5.getButtonClick(L1))
hamaken1018 0:12b41747ca26 207 Serial.print(F("\r\nL1"));
hamaken1018 0:12b41747ca26 208 if (PS5.getButtonClick(L3))
hamaken1018 0:12b41747ca26 209 Serial.print(F("\r\nL3"));
hamaken1018 0:12b41747ca26 210 if (PS5.getButtonClick(R1))
hamaken1018 0:12b41747ca26 211 Serial.print(F("\r\nR1"));
hamaken1018 0:12b41747ca26 212 if (PS5.getButtonClick(R3))
hamaken1018 0:12b41747ca26 213 Serial.print(F("\r\nR3"));
hamaken1018 0:12b41747ca26 214
hamaken1018 0:12b41747ca26 215 if (PS5.getButtonClick(CREATE))
hamaken1018 0:12b41747ca26 216 Serial.print(F("\r\nCreate"));
hamaken1018 0:12b41747ca26 217 if (PS5.getButtonClick(OPTIONS)) {
hamaken1018 0:12b41747ca26 218 Serial.print(F("\r\nOptions"));
hamaken1018 0:12b41747ca26 219 printAngle = !printAngle;
hamaken1018 0:12b41747ca26 220 }
hamaken1018 0:12b41747ca26 221 if (PS5.getButtonClick(TOUCHPAD)) {
hamaken1018 0:12b41747ca26 222 Serial.print(F("\r\nTouchpad"));
hamaken1018 0:12b41747ca26 223 printTouch = !printTouch;
hamaken1018 0:12b41747ca26 224 }
hamaken1018 0:12b41747ca26 225 if (PS5.getButtonClick(MICROPHONE)) {
hamaken1018 0:12b41747ca26 226 Serial.print(F("\r\nMicrophone"));
hamaken1018 0:12b41747ca26 227 microphone_led = !microphone_led;
hamaken1018 0:12b41747ca26 228 PS5.setMicLed(microphone_led);
hamaken1018 0:12b41747ca26 229 }
hamaken1018 0:12b41747ca26 230
hamaken1018 0:12b41747ca26 231 if (printAngle) { // Print angle calculated using the accelerometer only
hamaken1018 0:12b41747ca26 232 Serial.print(F("\r\nPitch: "));
hamaken1018 0:12b41747ca26 233 Serial.print(PS5.getAngle(Pitch));
hamaken1018 0:12b41747ca26 234 Serial.print(F("\tRoll: "));
hamaken1018 0:12b41747ca26 235 Serial.print(PS5.getAngle(Roll));
hamaken1018 0:12b41747ca26 236 }
hamaken1018 0:12b41747ca26 237
hamaken1018 0:12b41747ca26 238 if (printTouch) { // Print the x, y coordinates of the touchpad
hamaken1018 0:12b41747ca26 239 if (PS5.isTouching(0) || PS5.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
hamaken1018 0:12b41747ca26 240 Serial.print(F("\r\n"));
hamaken1018 0:12b41747ca26 241 for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
hamaken1018 0:12b41747ca26 242 if (PS5.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
hamaken1018 0:12b41747ca26 243 Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
hamaken1018 0:12b41747ca26 244 Serial.print(PS5.getX(i));
hamaken1018 0:12b41747ca26 245 Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
hamaken1018 0:12b41747ca26 246 Serial.print(PS5.getY(i));
hamaken1018 0:12b41747ca26 247 Serial.print(F("\t"));
hamaken1018 0:12b41747ca26 248 }
hamaken1018 0:12b41747ca26 249 }
hamaken1018 0:12b41747ca26 250 }
hamaken1018 0:12b41747ca26 251 }
hamaken1018 0:12b41747ca26 252 }