m3pi for proj2

Dependencies:   RemoteIR m3pi mbed-rtos mbed

LightCommunication.cpp

Committer:
Kristof@LAPTOP-FT09DA9V.home
Date:
2017-05-09
Revision:
4:9580760cdd1f
Parent:
3:ecb3fa0406e8
Child:
5:5d2beac511c1

File content as of revision 4:9580760cdd1f:

//
// Created by Kristof on 5/9/2017.
//

#include "LightCommunication.h"

float LightCommunication::left = 0;
float LightCommunication::right = 0;
Serial LightCommunication::device(p14,p15);
ReceiverIR LightCommunication::ir_rx(p21);
Mutex LightCommunication::mut;


LightCommunication::LightCommunication() {
    left,right = 0;
}

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){
    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){

    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;
}