マイコン間通信用

Files at this revision

API Documentation at this revision

Comitter:
tanabe2000
Date:
Fri Oct 12 04:39:03 2018 +0000
Commit message:
ver1

Changed in this revision

linemiconget.cpp Show annotated file Show diff for this revision Revisions of this file
linemiconget.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 72a36a529ad4 linemiconget.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/linemiconget.cpp	Fri Oct 12 04:39:03 2018 +0000
@@ -0,0 +1,52 @@
+#include "linemiconget.h"
+
+linemiconget::linemiconget(PinName linegettx, PinName linegetrx):
+    serial(linegettx, linegetrx)
+{
+    serial.baud(115200);
+    serial.setHeaders(FIRST_HEDDER, SECOND_HEDDER);
+    serial.startReceive(BUFFER_SIZE);
+}
+
+void linemiconget::receiveState()
+{
+    serial.getData(rxdata);
+    for(int i = 0; i < TWO_BYTE_DATA; i++) {
+        //if((((rxdata[2*i]<<8)|rxdata[2*i+1]) - 32768 > 1200) || (((rxdata[2*i]<<8)|rxdata[2*i+1]) - 32768 < -1)) {
+//        } else {
+            data[i] = ((rxdata[2*i]<<8)|rxdata[2*i+1]) - 32768;
+//        }
+    }
+        for(int i = 0; i < ONE_BYTE_DATA; i++)data[TWO_BYTE_DATA + i] = rxdata[2*TWO_BYTE_DATA + i];
+    for(int i=0; i<5; i++) {
+        line1[i] = data[5] % 2;
+        data[5] /= 2;
+        line2[i] = data[6] % 2;
+        data[6] /= 2;
+
+    }
+}
+
+int linemiconget::getDistance(int number) const
+{
+    if((data[number] > 1200) || (data[number] < -1)) {
+        return -1;
+    } else {
+        return data[number];
+    }
+}
+
+int linemiconget::getEncoder(int number) const
+{
+    return data[number + 2];
+}
+
+bool linemiconget::getLine1(int number) const
+{
+    return line1[number];
+}
+
+bool linemiconget::getLine2(int number) const
+{
+    return line2[number];
+}
diff -r 000000000000 -r 72a36a529ad4 linemiconget.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/linemiconget.h	Fri Oct 12 04:39:03 2018 +0000
@@ -0,0 +1,34 @@
+#ifndef LINEMICONGET_H
+#define LINEMICONGET_H
+
+#include "mbed.h"
+#include "SerialMultiByte.h"
+
+#define FIRST_HEDDER 0xEE
+#define SECOND_HEDDER 0xFF
+#define BUFFER_SIZE 12
+#define TWO_BYTE_DATA 5
+#define ONE_BYTE_DATA 2 
+
+
+class linemiconget
+{
+
+public:
+
+    linemiconget(PinName linegettx, PinName linegetrx);
+    void receiveState();
+    int getDistance(int number) const;
+    int getEncoder(int number) const;
+    bool getLine1(int number) const;
+    bool getLine2(int number) const;
+
+private:
+
+    uint8_t rxdata[BUFFER_SIZE];
+    int data[TWO_BYTE_DATA + ONE_BYTE_DATA];
+    bool line1[5], line2[5];
+    SerialMultiByte serial;
+};
+
+#endif
\ No newline at end of file