use PS4
Diff: PS4Serial.cpp
- Revision:
- 0:f907f82d58dc
- Child:
- 1:f953b9de204e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PS4Serial.cpp Thu Aug 16 07:36:23 2018 +0000 @@ -0,0 +1,38 @@ +#include "PS4Serial.h" + +PS4Serial::PS4Serial(PinName tx, PinName rx) : WirelessSerial(tx, rx, 115200), rp(0), dif(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; +} \ No newline at end of file