use PS4

PS4Serial.cpp

Committer:
yopcyuuu
Date:
2018-08-20
Revision:
1:f953b9de204e
Parent:
0:f907f82d58dc
Child:
2:ebf134f6c2ef

File content as of revision 1:f953b9de204e:

#include "PS4Serial.h"

PS4Serial::PS4Serial(PinName tx, PinName rx) : WirelessSerial(tx, rx, 115200), rp(0)
{
    for(int i = 0; i < 8; i++) ControllerData[i] = 2 <= i && i <= 5 ? 127 : 0;
    WirelessSerial.attach(this, &PS4Serial::WirelessRecive, RawSerial::RxIrq);
}

void PS4Serial::WirelessRecive()
{
    t.stop();
    t.reset();
    char data = WirelessSerial.getc();
    if(data == 0xff) {
        rp = 0;
    } else if(rp > 8) {
        //nothing
    } else {
        ControllerData[rp] = data;
        rp++;
    }
    t.start();
}

int PS4Serial::getButtonPress(PS4Button button)
{
    if(button < 7) {
        return (ControllerData[0] >> button) & 1;
    } else if(button < 14) {
        return (ControllerData[1] >> (button - 7)) & 1;
    } else {
        return ControllerData[button - 12];
    }
}

bool PS4Serial::connected(){
    return t.read_ms() < 500;
}