PS3の移動ライブラリ(バグあり動作不可)です。 誠意製作中です。

Dependents:   NHK2018_BM

PS3_S.cpp

Committer:
tektomo
Date:
2018-09-22
Revision:
0:1e79ec3fb922
Child:
1:5bb9feb40944

File content as of revision 0:1e79ec3fb922:

#include "PS3_S.h"

PS3s::PS3s(PinName TX, PinName RX) : Serial(TX, RX)
{
    baud(2400);
    Rdata[0] = 0;
    Rdata[1] = 0;
    Rdata[2] = 0;
    Rdata[3] = 0;
    Rdata[4] = 0;
    Rdata[5] = 0;
    Rdata[6] = 0;
    Rdata[7] = 0;
    for(int i = 0; i < 18; i++) {
        condata[i] = 0;
    }
}

void PS3s::getdata()
{
    
    if (Serial::readable() == true) {
        databuffer = Serial::getc();
        if(databuffer == 128) {
            endval = 1;
            dataval = 0;
            checkval = 1;
            Rdata[dataval] = databuffer;
            dataval++;
        }else if(endval == 1){
            Rdata[dataval] = databuffer;
            dataval++;
            if(dataval == 7) {
                endval = 0;
                checkval = 0;
                dataval = 0;
            }
        }
    }
}

void PS3s::checkdata()
{
    if (checkval == false) {
        for (int i = 1; i < 7; i++) {
            checksum_buffer += Rdata[i];
        }
        checksum_buffer = checksum_buffer & 0x0000007F;
        checkval = 1;
        if(checksum_buffer == (Rdata[7] & 0x0000007F)) {
            button1_buffer = Rdata[1];
            button2_buffer = Rdata[2];
            condata[14] = Rdata[3];
            condata[15] = Rdata[4];
            condata[16] = Rdata[5];
            condata[17] = Rdata[6];

            int num1 = 16;
            int num2 = 64;
            int value1 = 0;
            int value2 = 0;

            for(int i = 5; i >= 1; i--) {
                value1 = i + 6;
                if (button1_buffer >= num1) {
                    condata[value1] = 1;
                    button1_buffer -= num1;
                } else {
                    condata[value1] = 0;
                }
                num1 /= 2;
            }

            for(int j = 7; j >= 1; j--) {
                value2 = j - 1;
                if (button2_buffer - num2 >= 0) {
                    condata[value2] = 1;
                    button2_buffer -= num2;
                } else {
                    condata[value2] = 0;
                }
                num2 /= 2;
            }
            if(condata[0]!=0 && condata[1]!=0) {
                condata[0] = 0;
                condata[1] = 0;
                condata[13] = 1;
            } else if(condata[2]!=0 && condata[3]!=0) {
                condata[2] = 0;
                condata[3] = 0;
                condata[12] = 1;
            } else {
                condata[12] = 0;
                condata[13] = 0;
            }
        }
    }
}