PS3 Bluetooth For Arduino Uno or Arduino Pro mini
main.cpp@0:f678509106d4, 2022-06-25 (annotated)
- Committer:
- piroro4560
- Date:
- Sat Jun 25 18:50:19 2022 +0000
- Revision:
- 0:f678509106d4
make
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
piroro4560 | 0:f678509106d4 | 1 | /* |
piroro4560 | 0:f678509106d4 | 2 | Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus |
piroro4560 | 0:f678509106d4 | 3 | For more information visit my blog: http://blog.tkjelectronics.dk/ or |
piroro4560 | 0:f678509106d4 | 4 | send me an e-mail: kristianl@tkjelectronics.com |
piroro4560 | 0:f678509106d4 | 5 | ハセオムニ:00:02:5B:00:A5:A5, 0x00, 0x02, 0x5B, 0x00, 0xA5, 0xA5 |
piroro4560 | 0:f678509106d4 | 6 | 一番ドングル:00:1B:DC:F4:41:E8, 0x00, 0x1B, 0xDC, 0xF4, 0x41, 0xE |
piroro4560 | 0:f678509106d4 | 7 | 二番ドングル:00:1B:DC:F4:43:A0, 0x00, 0x1B, 0xDC, 0xF4, 0x43, 0xA0 |
piroro4560 | 0:f678509106d4 | 8 | */ |
piroro4560 | 0:f678509106d4 | 9 | |
piroro4560 | 0:f678509106d4 | 10 | #include <PS3BT.h> |
piroro4560 | 0:f678509106d4 | 11 | #include <usbhub.h> |
piroro4560 | 0:f678509106d4 | 12 | |
piroro4560 | 0:f678509106d4 | 13 | // Satisfy the IDE, which needs to see the include statment in the ino too. |
piroro4560 | 0:f678509106d4 | 14 | #ifdef dobogusinclude |
piroro4560 | 0:f678509106d4 | 15 | #include <spi4teensy3.h> |
piroro4560 | 0:f678509106d4 | 16 | #endif |
piroro4560 | 0:f678509106d4 | 17 | #include <SPI.h> |
piroro4560 | 0:f678509106d4 | 18 | |
piroro4560 | 0:f678509106d4 | 19 | USB Usb; |
piroro4560 | 0:f678509106d4 | 20 | //USBHub Hub1(&Usb); // Some dongles have a hub inside |
piroro4560 | 0:f678509106d4 | 21 | |
piroro4560 | 0:f678509106d4 | 22 | BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so |
piroro4560 | 0:f678509106d4 | 23 | /* You can create the instance of the class in two ways */ |
piroro4560 | 0:f678509106d4 | 24 | PS3BT PS3(&Btd); // This will just create the instance |
piroro4560 | 0:f678509106d4 | 25 | // PS3BT PS3(&Btd, 0x00, 0x1B, 0xDC, 0xF4, 0x41, 0xE8); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch |
piroro4560 | 0:f678509106d4 | 26 | |
piroro4560 | 0:f678509106d4 | 27 | uint8_t header0 = 0xFF; |
piroro4560 | 0:f678509106d4 | 28 | uint8_t header1 = 0xEE; |
piroro4560 | 0:f678509106d4 | 29 | bool button[12] = {}; |
piroro4560 | 0:f678509106d4 | 30 | uint8_t button0 = 0; |
piroro4560 | 0:f678509106d4 | 31 | uint8_t button1 = 0; |
piroro4560 | 0:f678509106d4 | 32 | uint8_t trigger[2] = {}; |
piroro4560 | 0:f678509106d4 | 33 | uint8_t stick[4] = {}; |
piroro4560 | 0:f678509106d4 | 34 | uint8_t count_timeout=0; |
piroro4560 | 0:f678509106d4 | 35 | |
piroro4560 | 0:f678509106d4 | 36 | bool printTemperature, printAngle; |
piroro4560 | 0:f678509106d4 | 37 | |
piroro4560 | 0:f678509106d4 | 38 | void setup() { |
piroro4560 | 0:f678509106d4 | 39 | Serial.begin(115200); |
piroro4560 | 0:f678509106d4 | 40 | #if !defined(__MIPSEL__) |
piroro4560 | 0:f678509106d4 | 41 | while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection |
piroro4560 | 0:f678509106d4 | 42 | #endif |
piroro4560 | 0:f678509106d4 | 43 | if (Usb.Init() == -1) { |
piroro4560 | 0:f678509106d4 | 44 | Serial.print(F("\r\nOSC did not start PS3BT")); |
piroro4560 | 0:f678509106d4 | 45 | while (1); //halt |
piroro4560 | 0:f678509106d4 | 46 | } |
piroro4560 | 0:f678509106d4 | 47 | Serial.print(F("\rNo.1 Test005 send all data nodelay\n")); |
piroro4560 | 0:f678509106d4 | 48 | } |
piroro4560 | 0:f678509106d4 | 49 | void loop() { |
piroro4560 | 0:f678509106d4 | 50 | Usb.Task(); |
piroro4560 | 0:f678509106d4 | 51 | |
piroro4560 | 0:f678509106d4 | 52 | if (PS3.PS3Connected || PS3.PS3NavigationConnected) { |
piroro4560 | 0:f678509106d4 | 53 | for (int i = 0; i < 8; i++) { |
piroro4560 | 0:f678509106d4 | 54 | button[i] = PS3.getButtonPress((ButtonEnum)(i)); |
piroro4560 | 0:f678509106d4 | 55 | button1 = button1 << 1; |
piroro4560 | 0:f678509106d4 | 56 | button1 += (button[i]); |
piroro4560 | 0:f678509106d4 | 57 | } |
piroro4560 | 0:f678509106d4 | 58 | for (int i = 8; i < 12; i++) { |
piroro4560 | 0:f678509106d4 | 59 | button[i] = PS3.getButtonPress((ButtonEnum)(i+2)); |
piroro4560 | 0:f678509106d4 | 60 | button0 = button0 << 1; |
piroro4560 | 0:f678509106d4 | 61 | button0 += button[i]; |
piroro4560 | 0:f678509106d4 | 62 | } |
piroro4560 | 0:f678509106d4 | 63 | trigger[0] = PS3.getAnalogButton(L2); |
piroro4560 | 0:f678509106d4 | 64 | trigger[1] = PS3.getAnalogButton(R2); |
piroro4560 | 0:f678509106d4 | 65 | stick[0] = PS3.getAnalogHat(LeftHatX); |
piroro4560 | 0:f678509106d4 | 66 | stick[1] = PS3.getAnalogHat(LeftHatY); |
piroro4560 | 0:f678509106d4 | 67 | stick[2] = PS3.getAnalogHat(RightHatX); |
piroro4560 | 0:f678509106d4 | 68 | stick[3] = PS3.getAnalogHat(RightHatY); |
piroro4560 | 0:f678509106d4 | 69 | |
piroro4560 | 0:f678509106d4 | 70 | #if 0 |
piroro4560 | 0:f678509106d4 | 71 | Serial.print(header0); |
piroro4560 | 0:f678509106d4 | 72 | Serial.print(header1); |
piroro4560 | 0:f678509106d4 | 73 | Serial.print(button0); |
piroro4560 | 0:f678509106d4 | 74 | Serial.print(button1); |
piroro4560 | 0:f678509106d4 | 75 | Serial.print(trigger[0]); |
piroro4560 | 0:f678509106d4 | 76 | Serial.print(trigger[1]); |
piroro4560 | 0:f678509106d4 | 77 | Serial.print(stick[0]); |
piroro4560 | 0:f678509106d4 | 78 | Serial.print(stick[1]); |
piroro4560 | 0:f678509106d4 | 79 | Serial.print(stick[2]); |
piroro4560 | 0:f678509106d4 | 80 | Serial.print(stick[3]); |
piroro4560 | 0:f678509106d4 | 81 | Serial.print(count_timeout); |
piroro4560 | 0:f678509106d4 | 82 | Serial.println(); |
piroro4560 | 0:f678509106d4 | 83 | #else |
piroro4560 | 0:f678509106d4 | 84 | Serial.write(header0); |
piroro4560 | 0:f678509106d4 | 85 | Serial.write(header1); |
piroro4560 | 0:f678509106d4 | 86 | Serial.write(button0); |
piroro4560 | 0:f678509106d4 | 87 | Serial.write(button1); |
piroro4560 | 0:f678509106d4 | 88 | Serial.write(trigger[0]); |
piroro4560 | 0:f678509106d4 | 89 | Serial.write(trigger[1]); |
piroro4560 | 0:f678509106d4 | 90 | Serial.write(stick[0]); |
piroro4560 | 0:f678509106d4 | 91 | Serial.write(stick[1]); |
piroro4560 | 0:f678509106d4 | 92 | Serial.write(stick[2]); |
piroro4560 | 0:f678509106d4 | 93 | Serial.write(stick[3]); |
piroro4560 | 0:f678509106d4 | 94 | Serial.write(count_timeout); |
piroro4560 | 0:f678509106d4 | 95 | #endif |
piroro4560 | 0:f678509106d4 | 96 | |
piroro4560 | 0:f678509106d4 | 97 | count_timeout++; |
piroro4560 | 0:f678509106d4 | 98 | if (count_timeout > 255) count_timeout=0; |
piroro4560 | 0:f678509106d4 | 99 | } |
piroro4560 | 0:f678509106d4 | 100 | } |