新部内対抗A班 / Mbed 2 deprecated PS3_BlueUSB_user_ver_circlewar_A

Dependencies:   mbed

Fork of PS3_BlueUSB_user_ver_circlewar by 新部内対抗A班

User.cpp

Committer:
jack0325suzu
Date:
2016-03-10
Revision:
18:299810a6dcb4
Parent:
17:ae30e74e9eba

File content as of revision 18:299810a6dcb4:


/*分かれ!J!*/

#include "Utils.h"
#include "USBHost.h"
#include "hci.h"
#include "ps3.h"
#include "User.h"
#include "mbed.h"
#include "S9706.h"
#include "SerialServo.h"
//#define DEBUG_MODE
#define ID 0

#define duty1 0.1
#define duty2 0.1
int servoflag = 0;
int RSX,RSY,LSX,LSY,BSU,BSL;
//これより下に関数外に書く要素を記入する
DigitalOut LA(p18);
DigitalOut RA(p21);
DigitalOut LH(p19);
DigitalOut RH(p20);
DigitalOut CH(p23);
PwmOut motor[2] = {p25,p26};
SPI m(p5, p6, p7);
DigitalOut cs(p8);
//Serial pc(USBTX,USBRX);

DigitalOut leds[4] = {LED1,LED2,LED3,LED4};
int countA=0,countB=0,countC=0,countD=0;


//
void UserLoopSetting()
{
    //一度だけ行いたい初期設定をここに書く
    LA=0;
    RA=0;
    LH=0;
    RH=0;
    CH=0;
    initSerialServo();
}

void UserLoop(char n,const u8* data)
{
    u16 ButtonState;
    if(n==0) { //有線Ps3USB.cpp
        RSX = ((ps3report*)data)->RightStickX;
        RSY = ((ps3report*)data)->RightStickY;
        LSX = ((ps3report*)data)->LeftStickX;
        LSY = ((ps3report*)data)->LeftStickY;
        BSU = (u8)(((ps3report*)data)->ButtonState & 0x00ff);
        BSL = (u8)(((ps3report*)data)->ButtonState >> 8);
        //ボタンの処理
        ButtonState =  ((ps3report*)data)->ButtonState;
    } else {//無線TestShell.cpp
        RSX = ((ps3report*)(data + 1))->RightStickX;
        RSY = ((ps3report*)(data + 1))->RightStickY;
        LSX = ((ps3report*)(data + 1))->LeftStickX;
        LSY = ((ps3report*)(data + 1))->LeftStickY;
        BSU = (u8)(((ps3report*)(data + 1))->ButtonState & 0x00ff);
        BSL = (u8)(((ps3report*)(data + 1))->ButtonState >> 8);
        //ボタンの処理
        ButtonState =  ((ps3report*)(data + 1))->ButtonState;
    }
    //ここより下にプログラムを書く
    //データ取得例

    int M=0;
    m.format(16,3);
    m.frequency(1000000);
    cs=0;
    /*    if((ButtonState >> BUTTONCIRCLE)&1) {
            //○が押されたとき
        }
        */
    if((ButtonState >> BUTTONUP)&1) {
        motor[0]=0;
        motor[1]=0.1;
    } else if((ButtonState >> BUTTONDOWN)&1) {
        motor[1]=0;
        motor[0]=0.1;
    } else {
        motor[0]=0;
        motor[1]=0;
    }
    /*    if((ButtonState >> BUTTONRIGHT)&1){
            set_degree(ID,50);
        }else if((ButtonState >> BUTTONLEFT)&1){
            set_degree(ID,200);
        }else{*/
    if(servoflag == 0) {
        set_degree(ID,135);
        servoflag= 1;
    }
//    }
    if((ButtonState >> BUTTONL2)&1) {
        countA++;
        if(countA>50) {
            LA=!LA;
            RA=!RA;
            countA=0;
        }
    } else if((ButtonState >> BUTTONR2)&1) {
        countA++;
        if(countA>50) {
            RA=!RA;
            LA=!LA;
            countA=0;
        }
    } else if((ButtonState >> BUTTONCROSS)&1) {
        countA++;
        if(countA>50) {
            RA=!RA;
            LA=!LA;
            countA=0;
        }
    } else if((ButtonState >> BUTTONCIRCLE)&1) {
        countB++;
        if(countB>50) {
            RH=!RH;
            countB=0;
        }
    } else if((ButtonState >> BUTTONTRIANGEL)&1) {
        countC++;
        if(countC>50) {
            CH=!CH;
            LH=!LH;
            countC=0;
        }
    } else if((ButtonState >> BUTTONSQUARE)&1) {
        countD++;
        if(countD>50) {
            LH=!LH;
            CH=!CH;
            countD=0;
        }
    }
    if((ButtonState >> BUTTONR1)&1) {
        M+=1 << 15;
    } else if((ButtonState >> BUTTONL1)&1) {
        M+=1 << 14;
    }
    if(LSX-128>=0) {
        M+=((LSX-128)/2) << 7;
    } else if(LSX-128<0) {
        M+=1 << 13;
        if(128-LSX==128)M+=63 << 7;
        else M+=((128-LSX)/2) << 7;
    }
    if(LSY-127<=0) {
        M+=((127-LSY)/2);
    } else if(LSY-127>0) {
        M+=1 << 6;
        if(LSY-127==128)M+=63;
        else M+=(LSY-127)/2;
    }
#ifdef DEBUG_MODE
    int MM,i,MA[16];
    MM=M;
    for(i=0; MM>0; i++) {
        MA[i]=MM % 2;
        MM=MM/2;
    }
    for(i=i+1; i<16; i++) {
        MA[i]=0;
    }
    for(i=15; i>=0; i--) {
        printf("%d",MA[i]);
    }
    printf("\r\n");
#endif
    //printf("LSX=%d,LSY=%d\r\n",LSX,LSY);
    printf("M=%d \r\n",M);

    m.write(M);

    /*
        led1=LSX/256.0f;
        led2=LSY/256.0f;
        led3=ButtonState & 0x0400;  //L1の状態
        led4=ButtonState & 0x0800;  //R1の状態
        */

    //値の取得はps3.hを参照
    cs=1;
//    setID(ID);
//    readID();
//    readID();

}