謙一 永井 / Mbed 2 deprecated 303_DS

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
kenboh
Date:
Wed Sep 11 07:12:08 2019 +0000
Commit message:
0911

Changed in this revision

DualShock.cpp Show annotated file Show diff for this revision Revisions of this file
DualShock.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DualShock.cpp	Wed Sep 11 07:12:08 2019 +0000
@@ -0,0 +1,79 @@
+#include "DualShock.h"
+
+tDSParm hDS;
+static uint8_t recv_data, check = 0;
+static Serial* ds_serial;
+static float DimensionlessAndBacklash(int8_t dat);
+static uint8_t getsum(void *buf, size_t size);
+
+void ReStartDS(void) {
+    check = 0;
+    recv_data = (*ds_serial).getc();
+}
+
+uint8_t InitDS(Serial* f_serial) {
+
+    hDS.BUTTON.ButtonData = 0;
+    hDS.ANALOG.LX = 0;
+    hDS.ANALOG.LY = 0;
+    hDS.ANALOG.RX = 0;
+    hDS.ANALOG.RY = 0;
+    //huartDS = huart;
+    ds_serial = f_serial;
+
+    printf("DualShock connecting check...\r\n");
+    ReStartDS();
+    printf("DualShock connect successful...\r\n");
+    return 0;
+}
+//&getDSdata
+void getDSdata(void) {
+    static uint8_t dat[8] = {0};
+
+    recv_data = (*ds_serial).getc();
+    dat[check] = recv_data;
+    switch (check) {
+    case 0:
+        if (dat[0] == 0x80) {
+            check++;
+        }
+        break;
+    case 7:
+        if (dat[7] == (getsum(dat, 7) & 0x7f)) {
+            hDS.BUTTON.ButtonHighData = dat[1];
+            hDS.BUTTON.ButtonLowData = dat[2];
+            hDS.ANALOG.LX = DimensionlessAndBacklash(dat[3]);
+            hDS.ANALOG.LY = DimensionlessAndBacklash(dat[4]);
+            hDS.ANALOG.RX = DimensionlessAndBacklash(dat[5]);
+            hDS.ANALOG.RY = DimensionlessAndBacklash(dat[6]);
+        }
+        dat[0] = 0;
+        check = 0;
+        break;
+    default:
+        check++;
+        break;
+    }
+}
+
+static float DimensionlessAndBacklash(int8_t dat) {
+    float val;
+
+    if ((dat < BACKLASH) && (dat > -BACKLASH)) {
+        dat = 0;
+    } else {
+        dat += (dat > 0) ? (-BACKLASH) : (BACKLASH);
+    }
+    val = (float) dat / (float) (128 - BACKLASH);
+    return val;
+}
+
+static uint8_t getsum(void *buf, size_t size) {
+    const uint8_t *p = (uint8_t*) buf;
+    uint8_t ret = 0;
+
+    for (; size; size--) {
+        ret += *p++;
+    }
+    return ret;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DualShock.h	Wed Sep 11 07:12:08 2019 +0000
@@ -0,0 +1,64 @@
+#ifndef DUALSHOCK_H_
+#define DUALSHOCK_H_
+
+#include "mbed.h"
+
+#define BACKLASH 28
+
+typedef struct st_ds {
+    union {
+        uint16_t ButtonData;
+        struct {
+            union {
+                uint8_t ButtonLowData   :8;
+                struct {
+                    uint8_t L2          :1;
+                    uint8_t R2          :1;
+                    uint8_t L1          :1;
+                    uint8_t R1          :1;
+                    uint8_t TRIANGLE    :1;
+                    uint8_t CIRCLE      :1;
+                    uint8_t CROSS       :1;
+                    uint8_t SQUARE      :1;
+                };
+            };
+            union {
+                uint8_t ButtonHighData  :8;
+                struct {
+                    uint8_t SELECT      :1;
+                    uint8_t L3          :1;
+                    uint8_t R3          :1;
+                    uint8_t START       :1;
+                    uint8_t UP          :1;
+                    uint8_t RIGHT       :1;
+                    uint8_t DOWN        :1;
+                    uint8_t LEFT        :1;
+                };
+            };
+        };
+    }BUTTON;
+    struct {
+        float LY;
+        float LX;
+        float RY;
+        float RX;
+    }ANALOG;
+} tDSParm;
+
+extern tDSParm hDS;
+/*
+if(hDS.BUTTON.L1)
+if(-1<=hDS.ANALOG.LY<=1)*/
+
+/**
+ * @brief Function to initialize the encoder
+ * @param mode      see definition above
+ */
+extern uint8_t InitDS(Serial* f_serial);
+extern void ReStartDS(void);
+/**
+ * @brief Function to initialize the encoder
+ * @retval *pParm   see definition above
+ */
+extern void getDSdata(void);
+#endif /* DUALSHOCK_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 11 07:12:08 2019 +0000
@@ -0,0 +1,21 @@
+#include "mbed.h"
+#include "DualShock.h"
+
+Serial DS_serial(PA_9, PA_10);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+int main() {
+    DS_serial.baud(115200);     //通信速度設定
+    InitDS(&DS_serial);         //受信データ用変数を初期化する
+    DS_serial.attach(&getDSdata, Serial::RxIrq);    //「受信したら割り込みして」の宣言
+
+    while (1) {
+        pc.printf("%d\t %d\t\t %d\t%d\r\n",hDS.BUTTON.L2, hDS.BUTTON.UP, hDS.BUTTON.TRIANGLE, hDS.BUTTON.R2);
+        pc.printf("%d\t%d %d\t\t%d %d\t%d\r\n",hDS.BUTTON.L1, hDS.BUTTON.LEFT, hDS.BUTTON.RIGHT, hDS.BUTTON.SQUARE, hDS.BUTTON.CIRCLE, hDS.BUTTON.R1);
+        pc.printf("\t %d\t  %d/%d\t %d\r\n",hDS.BUTTON.DOWN, hDS.BUTTON.SELECT, hDS.BUTTON.START, hDS.BUTTON.CROSS);
+        pc.printf("LX:%.3f,LY:%.3f\tRX:%.3f,RY:%.3f\r\n",hDS.ANALOG.LX, hDS.ANALOG.LY, hDS.ANALOG.RX, hDS.ANALOG.RY);
+        pc.printf("\r\n");
+        wait_ms(400);
+    }
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 11 07:12:08 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file