EEP fORK
Dependencies: BLE_API mbed nRF51822
Fork of MCS_LRF by
Diff: laser.cpp
- Revision:
- 11:0dafbbb3a686
- Parent:
- 10:d37cd13dd529
- Child:
- 12:cf8af0b4e0d2
diff -r d37cd13dd529 -r 0dafbbb3a686 laser.cpp --- a/laser.cpp Tue Dec 22 03:46:12 2015 +0000 +++ b/laser.cpp Tue Dec 22 04:22:12 2015 +0000 @@ -12,80 +12,8 @@ char cmd[] = "0 -1 10 doMeasDistExt\n"; // single reading averaged over 10 measurements //char cmd[] = "0 -1 -1 doMeasDistExt\n"; // single reading of single measurement - This could make the laser to lock up and may need reseting - sendCommand(cmd); - processResponse(); -// float distance = -5; -// vector<char*> v; -// -// split(buf, ' ', v); -// -// if (v.size() != 6 || atoi(v[1]) != 0 || strcmp(v[5], "Reply") != 0) { -// // there is an error -// distanceCallback(-1.0); -// } else { -// distance = atoi(v[2]) / 1000000.0; // distance in m -// distanceCallback(distance); -// } -// -// -// // processBuffer(); - -// float distance = getDistance(); -// distanceCallback(distance); -} - -void Laser::processBuffer() -{ - //float distance; -// vector<char*> v; -// // debugCallback(buf); -// -// split(buf, ' ', v); -// -// if (v.size() != 6 || atoi(v[1]) != 0 || strcmp(v[5], "Reply") != 0) { -// // there is an error -// distanceCallback(-1.0); -// } else -// { -// distance = atoi(v[2]) / 1000000.0; // distance in m -// distanceCallback(distance); -// } - - - float distance = -5; - vector<char*> v; - - split(buf, ' ', v); - - if (v.size() != 6 || atoi(v[1]) != 0 || strcmp(v[5], "Reply") != 0) { - // there is an error - distanceCallback(-1.0, 0.0); - } else { - float elapsed = (float)(timer.read_us()/1000.0); // elapsed in ms - distance = atoi(v[2]) / 1000000.0; // distance in m - distanceCallback(distance, elapsed); - } -} - - - -void Laser::enableMeasurement(bool enable) -{ - if (enable) - sendCommand(";\n"); - else - sendCommand("switchMeasOff\n"); -} - -void Laser::setRedDot(bool on) -{ - if(on) { - char cmd[] = "0 -1 10 doMeasDistExt\n"; // doing a measurement turns the redDot on - sendCommand(cmd); - } else - sendCommand("switchMeasOff\n"); } bool Laser::sendCommand(char cmd[]) @@ -100,34 +28,10 @@ } return true; - - // return processResponse(); } bool Laser::processResponse() { - - // // Note: Need to actually read from the serial to clear the RX interrupt -// char c = 0; -// uint16_t count = 0; -// idx = 0; -// do { -// if(serial.readable()) { -// buf[idx++] = serial.getc(); -// } -// else{wait_us(100);} -// } while (c != '\n' && idx < bufSize && count++ < 5000); // timeout after about 500ms and ensure no overflow -// -// if (count >= 5000 || idx == 0 || idx >= bufSize) return false; // timeout or overflow -// -// // now that we have the reply parse it -// buf[idx - 1] = 0; // null terminate the string -// -// // processBuffer(); -// return true; - - - int i = 0; char c = 0; uint16_t count = 0; @@ -166,10 +70,25 @@ distanceCallback(distance, elapsed); } -//processBuffer(); return true; } +void Laser::enableMeasurement(bool enable) +{ + if (enable) + sendCommand(";\n"); + else + sendCommand("switchMeasOff\n"); +} + +void Laser::setRedDot(bool on) +{ + if(on) { + char cmd[] = "0 -1 10 doMeasDistExt\n"; // doing a measurement turns the redDot on + sendCommand(cmd); + } else + sendCommand("switchMeasOff\n"); +} void Laser::split(char str[], char c, std::vector<char*>& v) { @@ -182,18 +101,6 @@ } } -void Laser::processRxData(char c) -{ - if(c != '\n') { - buf[idx++] = c; - if(idx == bufSize) idx = 0; // avoid overflow - } else { - buf[idx] = 0; // null terminate the string - processBuffer(); - idx = 0; - } -} - void Laser::setDistaceCallback(void (*callback)(float, float)) { distanceCallback = callback; @@ -204,7 +111,6 @@ debugCallback = callback; } - Laser::~Laser() { enableMeasurement(false); @@ -212,6 +118,19 @@ + +//void Laser::processRxData(char c) +//{ +// if(c != '\n') { +// buf[idx++] = c; +// if(idx == bufSize) idx = 0; // avoid overflow +// } else { +// buf[idx] = 0; // null terminate the string +// processBuffer(); +// idx = 0; +// } +//} + //float Laser::getDistance() //{ // float distance = -4.0; @@ -255,3 +174,36 @@ // return distance; //} +//void Laser::processBuffer() +//{ +// //float distance; +//// vector<char*> v; +//// // debugCallback(buf); +//// +//// split(buf, ' ', v); +//// +//// if (v.size() != 6 || atoi(v[1]) != 0 || strcmp(v[5], "Reply") != 0) { +//// // there is an error +//// distanceCallback(-1.0); +//// } else +//// { +//// distance = atoi(v[2]) / 1000000.0; // distance in m +//// distanceCallback(distance); +//// } +// +// +// float distance = -5; +// vector<char*> v; +// +// split(buf, ' ', v); +// +// if (v.size() != 6 || atoi(v[1]) != 0 || strcmp(v[5], "Reply") != 0) { +// // there is an error +// distanceCallback(-1.0, 0.0); +// } else { +// float elapsed = (float)(timer.read_us()/1000.0); // elapsed in ms +// distance = atoi(v[2]) / 1000000.0; // distance in m +// distanceCallback(distance, elapsed); +// } +//} +