use PS4
PS4Serial.cpp@0:f907f82d58dc, 2018-08-16 (annotated)
- Committer:
- yopcyuuu
- Date:
- Thu Aug 16 07:36:23 2018 +0000
- Revision:
- 0:f907f82d58dc
- Child:
- 1:f953b9de204e
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yopcyuuu | 0:f907f82d58dc | 1 | #include "PS4Serial.h" |
yopcyuuu | 0:f907f82d58dc | 2 | |
yopcyuuu | 0:f907f82d58dc | 3 | PS4Serial::PS4Serial(PinName tx, PinName rx) : WirelessSerial(tx, rx, 115200), rp(0), dif(0) |
yopcyuuu | 0:f907f82d58dc | 4 | { |
yopcyuuu | 0:f907f82d58dc | 5 | for(int i = 0; i < 8; i++) ControllerData[i] = 2 <= i && i <= 5 ? 127 : 0; |
yopcyuuu | 0:f907f82d58dc | 6 | WirelessSerial.attach(this, &PS4Serial::WirelessRecive, RawSerial::RxIrq); |
yopcyuuu | 0:f907f82d58dc | 7 | } |
yopcyuuu | 0:f907f82d58dc | 8 | |
yopcyuuu | 0:f907f82d58dc | 9 | void PS4Serial::WirelessRecive() |
yopcyuuu | 0:f907f82d58dc | 10 | { |
yopcyuuu | 0:f907f82d58dc | 11 | t.stop(); |
yopcyuuu | 0:f907f82d58dc | 12 | t.reset(); |
yopcyuuu | 0:f907f82d58dc | 13 | char data = WirelessSerial.getc(); |
yopcyuuu | 0:f907f82d58dc | 14 | if(data == 0xff) { |
yopcyuuu | 0:f907f82d58dc | 15 | rp = 0; |
yopcyuuu | 0:f907f82d58dc | 16 | } else if(rp > 8) { |
yopcyuuu | 0:f907f82d58dc | 17 | //nothing |
yopcyuuu | 0:f907f82d58dc | 18 | } else { |
yopcyuuu | 0:f907f82d58dc | 19 | ControllerData[rp] = data; |
yopcyuuu | 0:f907f82d58dc | 20 | rp++; |
yopcyuuu | 0:f907f82d58dc | 21 | } |
yopcyuuu | 0:f907f82d58dc | 22 | t.start(); |
yopcyuuu | 0:f907f82d58dc | 23 | } |
yopcyuuu | 0:f907f82d58dc | 24 | |
yopcyuuu | 0:f907f82d58dc | 25 | int PS4Serial::getButtonPress(PS4Button button) |
yopcyuuu | 0:f907f82d58dc | 26 | { |
yopcyuuu | 0:f907f82d58dc | 27 | if(button < 7) { |
yopcyuuu | 0:f907f82d58dc | 28 | return (ControllerData[0] >> button) & 1; |
yopcyuuu | 0:f907f82d58dc | 29 | } else if(button < 14) { |
yopcyuuu | 0:f907f82d58dc | 30 | return (ControllerData[1] >> (button - 7)) & 1; |
yopcyuuu | 0:f907f82d58dc | 31 | } else { |
yopcyuuu | 0:f907f82d58dc | 32 | return ControllerData[button - 12]; |
yopcyuuu | 0:f907f82d58dc | 33 | } |
yopcyuuu | 0:f907f82d58dc | 34 | } |
yopcyuuu | 0:f907f82d58dc | 35 | |
yopcyuuu | 0:f907f82d58dc | 36 | bool PS4Serial::connected(){ |
yopcyuuu | 0:f907f82d58dc | 37 | return t.read_ms() < 500; |
yopcyuuu | 0:f907f82d58dc | 38 | } |