Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: PS4.cpp
- Revision:
- 6:0b7476f64352
- Parent:
- 5:51d531c885ba
- Child:
- 7:ffa4066a789d
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PS4.cpp Tue Oct 10 09:28:44 2017 +0000
@@ -0,0 +1,79 @@
+#include "PS4.h"
+
+PS4::PS4(I2C *i, char addr)
+{
+ i2c = i;
+ ADDR = addr;
+
+ for(int i=0; i<21; i++) {
+ DATA_CONTROLLER[i] = 0;
+ DATA_CONTROLLER_OLD[i] = 0;
+ }
+}
+
+bool PS4::UpDate()
+{
+ //Make backup
+ memcpy( DATA_CONTROLLER_OLD, DATA_CONTROLLER, sizeof(DATA_CONTROLLER) );
+
+ //Get data by I2C
+ int s = i2c->read((ADDR | 1), data, 8);
+
+ if(s == 0) {
+
+ //UpDate
+ DATA_CONTROLLER[CONNECTION] = (data[1] >> 1) & 0x01;
+ DATA_CONTROLLER[UP] = (data[0] >> 7) & 0x01;
+ DATA_CONTROLLER[RIGHT] = (data[0] >> 6) & 0x01;
+ DATA_CONTROLLER[DOWN] = (data[0] >> 5) & 0x01;
+ DATA_CONTROLLER[LEFT] = (data[0] >> 4) & 0x01;
+ DATA_CONTROLLER[TRIANGLE] = (data[0] >> 3) & 0x01;
+ DATA_CONTROLLER[CIRCLE] = (data[0] >> 2) & 0x01;
+ DATA_CONTROLLER[CROSS] = (data[0] >> 1) & 0x01;
+ DATA_CONTROLLER[SQUARE] = (data[0] >> 0) & 0x01;
+ DATA_CONTROLLER[L1] = (data[1] >> 7) & 0x01;
+ DATA_CONTROLLER[R1] = (data[1] >> 6) & 0x01;
+ DATA_CONTROLLER[SHARE] = (data[1] >> 5) & 0x01;
+ DATA_CONTROLLER[OPTIONS] = (data[1] >> 4) & 0x01;
+ DATA_CONTROLLER[L3] = (data[1] >> 3) & 0x01;
+ DATA_CONTROLLER[R3] = (data[1] >> 2) & 0x01;
+ DATA_CONTROLLER[LeftHatX] = data[2];
+ DATA_CONTROLLER[LeftHatY] = data[3];
+ DATA_CONTROLLER[RightHatX] = data[4];
+ DATA_CONTROLLER[RightHatY] = data[5];
+ DATA_CONTROLLER[L2] = data[6];
+ DATA_CONTROLLER[R2] = data[7];
+ } else {
+ for(int i = 0; i < 21; i++) {
+ DATA_CONTROLLER[i] = i >= 15 && i <= 18 ? 127 : 0;
+ }
+ }
+ return s;
+}
+
+int PS4::getPress( CONTROLLER buttom )
+{
+ if(buttom < 21) {
+ return DATA_CONTROLLER[buttom];
+ } else {
+ return 0;
+ }
+}
+
+int PS4::getClick( CONTROLLER button, bool mode )
+{
+ //mode:0 -> Rising
+ //mode:1 -> Falling
+
+ if(button <= 13) {
+ return mode*(DATA_CONTROLLER[button]*!DATA_CONTROLLER_OLD[button])
+ + !mode*(!DATA_CONTROLLER[button]*DATA_CONTROLLER_OLD[button]);
+
+ } else if( (19<=button) && (button<=20) ) {
+ return mode*((DATA_CONTROLLER[button]>127)*!(DATA_CONTROLLER_OLD[button]>127))
+ + !mode*(!(DATA_CONTROLLER[button]>127)*(DATA_CONTROLLER_OLD[button]>127));
+
+ } else {
+ return 0;
+ }
+}