Library&Source / PS3_to_Arduino

Dependents:   throw_1

Arduino_PRO.txt

Committer:
kitta5687
Date:
2019-03-01
Revision:
1:b7d47b40707d
Child:
2:422d5d7602cc

File content as of revision 1:b7d47b40707d:

#include <PS3BT.h>
#include <usbhub.h>

#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3(&Btd); // This will just create the instance
//PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

unsigned long botton_val = 0;

int sthick[6] = { 0, 0, 0, 0, 0, 0 };//アナログ値格納用:左スティック,右スティック,L2,R2
int check = 0;
uint8_t str[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };//送信データ格納用

void setup() {
  Serial.begin(57600);
  //Serial.begin(115200);
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    while (1); //halt
  }
}
void loop() {
  Usb.Task();
  if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
    sthick[0] = PS3.getAnalogHat(LeftHatX);
    sthick[1] = PS3.getAnalogHat(LeftHatY);
    sthick[2] = PS3.getAnalogHat(RightHatX);
    sthick[3] = PS3.getAnalogHat(RightHatY);
    sthick[4] = PS3.getAnalogButton(L2);
    sthick[5] = PS3.getAnalogButton(R2);

    if (PS3.getButtonClick(PS))       bitSet(botton_val, 0);  //PS3.disconnect();
    if (PS3.getButtonClick(TRIANGLE)) bitSet(botton_val, 1);
    if (PS3.getButtonClick(CIRCLE))   bitSet(botton_val, 2);
    if (PS3.getButtonClick(CROSS))    bitSet(botton_val, 3);
    if (PS3.getButtonClick(SQUARE))   bitSet(botton_val, 4);

    if (PS3.getButtonClick(UP))       bitSet(botton_val, 5);
    if (PS3.getButtonClick(RIGHT))    bitSet(botton_val, 6);
    if (PS3.getButtonClick(DOWN))     bitSet(botton_val, 7);
    if (PS3.getButtonClick(LEFT))     bitSet(botton_val, 8);

    if (PS3.getButtonClick(L1))       bitSet(botton_val, 9);
    if (PS3.getButtonClick(L3))       bitSet(botton_val, 10);
    if (PS3.getButtonClick(R1))       bitSet(botton_val, 11);
    if (PS3.getButtonClick(R3))       bitSet(botton_val, 12);

    if (PS3.getButtonClick(SELECT))   bitSet(botton_val, 13);
    if (PS3.getButtonClick(START))    bitSet(botton_val, 14);

    /***********************************************/
    str[0] = botton_val & 0x00ff;
    str[1] = botton_val >> 8;
    botton_val = 0;

    for (int i = 2; i < 8; i++) {
      str[i] = sthick[i - 2];
      }

    //str[8] = 0;
    str[8] = PS3.PS3Connected;//コントローラが接続されているか確認
    /***********************************************/
    check = Serial.read();//nucleoからの受信データ確認
    
    if (check == 1) Serial.write(str, 9);
    else{}
     
    Serial.flush();
     check = 0;
  }
}