sdaitzman / arduino-new-ping

Automatically exported from code.google.com/p/arduino-new-ping
0 stars 0 forks source link

Can't tell difference between still waiting and ping timeout #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
When using ping_timer(), then check_timer(), it only returns true once a ping 
is received, requiring the timeout to be tracked both in the library and 
outside the library, requiring extra ram and processing cycles.

Changing the check_timer() method to type int8_t instead of boolean, then in 
the "Outside the timeout limit" block return -1, and in the "Ping echo 
received" return 1, and default return 0 allows the sketch to piggyback off the 
library code, saving ram and extra conditional checks.  

new method would look like:
int8_t NewPing::check_timer() {
    if (micros() > _max_time) { // Outside the timeout limit.
        timer_stop();           // Disable timer interrupt
        return -1;           // return ping echo timed out.
    }

    if (!(*_echoInput & _echoBit)) { // Ping echo received.
        timer_stop();                // Disable timer interrupt
        ping_result = (micros() - (_max_time - _maxEchoTime) - 13); // Calculate ping time, 13uS of overhead.
        return 1;                 // Return ping echo positive.
    }

    return 0; // Return 0 because there's no ping echo yet.
}

Original issue reported on code.google.com by cwe...@gmail.com on 28 Feb 2014 at 4:18

GoogleCodeExporter commented 9 years ago
For support please visit the official forum at: 
http://forum.arduino.cc/index.php/topic,106043.0.html

Original comment by eckel.tim on 25 Apr 2014 at 8:28