m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

Revision:
1:76cff32344d1
Child:
2:70240992e869
diff -r ef54cd039c52 -r 76cff32344d1 LightCommunication.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightCommunication.cpp	Tue May 09 23:02:13 2017 +0200
@@ -0,0 +1,85 @@
+//
+// Created by Kristof on 5/9/2017.
+//
+
+#include "LightCommunication.h"
+
+LightCommunication::LightCommunication() {
+    left,right = 0;
+    device = new Serial(p14,p15);
+    ir_rx = new ReceiverIR(p21);
+}
+
+LightCommunication::~LightCommunication() {
+
+}
+
+char * LightCommunication::getSerial() {
+    char test[3];
+    if(device->readable()) {
+       device->scanf("%s",test);
+    }
+    return test;
+}
+
+uint8_t * LightCommunication::getIRStyle() {
+    RemoteIR::Format format;
+    uint8_t buf[32];
+    int bitcount;
+    if (ir_rx->getState() == ReceiverIR::Received) {
+        bitcount = ir_rx->getData(&format, buf, sizeof(buf) * 8);
+    }
+    return buf;
+}
+
+void LightCommunication::receiveData() {
+    mut.lock();
+    //uint8_t *  data = LightCommunication::getIRStyle();
+    char *  data = LightCommunication::getSerial();
+
+    printf("%X \n",data[2]);
+    printf("%d \n",data[2]);
+    if (OWN_ID == (int) data[0]) {
+        left = LightCommunication::toFloat(data[1]);
+        right = LightCommunication::toFloat(data[2]);
+    }
+    mut.unlock();
+}
+
+float LightCommunication::getRight(float cur_right) const {
+    float r = right - cur_right;
+    if (r>0){
+        return (cur_right + 0.005);
+    }else if (r<0){
+
+        return (cur_right -0.005);
+    }else{
+        return cur_right;
+    }
+}
+
+float LightCommunication::getLeft(float cur_left) const {
+
+    mut.lock();
+    float l = left - cur_left;
+    if (l>0){
+        return (cur_left + 0.005);
+    }else if (l<0){
+        return (cur_left -0.005);
+    }else{
+        return cur_left;
+    }
+}
+
+float LightCommunication::toFloat(int8_t a) {
+    return ((float)a)/100;
+}
+
+bool LightCommunication::needsToStop() {
+    if (right == 0 && left == 0){
+        return true;
+    }
+    return false;
+}
+
+