FEPとdualshock3

Dependents:   PS3_sample Nucleo_NHK_2018_syudo_wheel kourobo kourobo5 ... more

Files at this revision

API Documentation at this revision

Comitter:
number_key
Date:
Fri Sep 14 04:43:51 2018 +0000
Parent:
2:f41f9d145429
Commit message:
ring buffer

Changed in this revision

PS3.cpp Show annotated file Show diff for this revision Revisions of this file
PS3.h Show annotated file Show diff for this revision Revisions of this file
diff -r f41f9d145429 -r 78827486d24f PS3.cpp
--- a/PS3.cpp	Tue Sep 11 08:26:51 2018 +0000
+++ b/PS3.cpp	Fri Sep 14 04:43:51 2018 +0000
@@ -5,15 +5,27 @@
     timeout(PS3_TIMEOUT_COUNT),
     status(false)
 {
+    bufferPoint = 0;
+    receivedBytes = 0;
+    buffer = new uint8_t[PS3_BUFFER_SIZE];
     attach(callback(this, &PS3::receiveByte));
-    assembleTicker.attach(callback(this, &PS3::assembleLoop), 0.01);
-    // thread.start(callback(this, &PS3::assembleLoop));
+    timeoutTicker.attach(callback(this, &PS3::timeoutLoop), 0.1);
 }
 
 void PS3::receiveByte()
 {
-    buf.push_back(getc());
+    buffer[bufferPoint % PS3_BUFFER_SIZE] = getc();
     timeout = 0;
+ 
+    if(bufferPoint != 0xff) {
+        ++bufferPoint;
+    } else {
+        bufferPoint = (255%PS3_BUFFER_SIZE)+1;
+    }
+ 
+    ++receivedBytes;
+ 
+    if(receivedBytes >= PS3_BUFFER_SIZE) checkData();
 }
 
 void PS3::assemble()
@@ -21,41 +33,50 @@
     uint8_t data, i;
 
     for(i = 0; i < 4; i++) {
-        stick[i] = buf[i+9];
+        stick[i] = buffer[i+9];
     }
     for(i = 0; i < 2; i++) {
-        trigger[i] = buf[i+13];
+        trigger[i] = buffer[i+13];
     }
-    data = buf[15];
+    data = buffer[15];
     for(i = 0; i < 6; i++) {
         button[i] = data & 0x01;
         data >>= 1;
     }
-    data = buf[16];
+    data = buffer[16];
     for( ; i < 12; i++) {
         button[i] = data & 0x01;
         data >>= 1;
     }
 }
 
-void PS3::assembleLoop()
+void PS3::checkData()
 {
-    // while(true) {
+    for(int i = 0; i < PS3_BUFFER_SIZE; i++) {
+        if(buffer[i % PS3_BUFFER_SIZE] == PS3_HEADER0 && buffer[(i + 1) % PS3_BUFFER_SIZE] == PS3_HEADER1 && buffer[(i + 2) % PS3_BUFFER_SIZE] == PS3_HEADER2) {
+            uint8_t checksum = 0x00;
+            for(int j = 9; j < PS3_BUFFER_SIZE - 3; j++) {
+                checksum += buffer[(i + j)% PS3_BUFFER_SIZE];
+ 
+            }
+            if(checksum == buffer[(i + PS3_BUFFER_SIZE - 3)% PS3_BUFFER_SIZE]) {
+                assemble();
+                receivedBytes = 0;
+                
+                return;
+            }
+        }
+    }
+}
+
+void PS3::timeoutLoop()
+{
     if(timeout >= PS3_TIMEOUT_COUNT) {
         status = false;
         return;
     }
     status = true;
-    if(buf.size() > PS3_BUFFER_SIZE) {
-        if(buf[0] == PS3_HEADER0 && buf[1] == PS3_HEADER1 && buf[2] == PS3_HEADER2) {
-            assemble();
-            buf.erase(buf.begin(), buf.begin() + (PS3_BUFFER_SIZE - 1));
-        } else {
-            buf.erase(buf.begin());
-        }
-    }
-    timeout++;
-    // }
+    timeout++;   
 }
 
 bool PS3::getButton(int n)
diff -r f41f9d145429 -r 78827486d24f PS3.h
--- a/PS3.h	Tue Sep 11 08:26:51 2018 +0000
+++ b/PS3.h	Fri Sep 14 04:43:51 2018 +0000
@@ -2,10 +2,9 @@
 #define PS3_H
 
 #include "mbed.h"
-#include <vector>
 
-#define PS3_TIMEOUT_COUNT 200
-#define PS3_BUFFER_SIZE 22
+#define PS3_TIMEOUT_COUNT 10
+#define PS3_BUFFER_SIZE 20
 #define PS3_HEADER0 'R'
 #define PS3_HEADER1 'B'
 #define PS3_HEADER2 'N'
@@ -40,16 +39,19 @@
     bool getButton(int n);
     uint8_t getStick(int n);
     uint8_t getTrigger(int n);
-    // Thread thread;
     bool status;
     
 private :
     void receiveByte();
     void assemble();
-    void assembleLoop();
+    void checkData();
+    void timeoutLoop();
 
-    Ticker assembleTicker;
-    std::vector<unsigned char> buf;
+    uint8_t *buffer;
+    uint8_t bufferPoint;
+    uint8_t receivedBytes;
+    
+    Ticker timeoutTicker;
     int timeout;
 
     bool button[12];