sdaitzman / arduino-new-ping

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

check_ping() doesn't indicate difference between timeout and still-waiting #3

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I think a simple tweak would make NewPing's timer-driven mode easier to work 
with.

Inside my echoCheck() function, I do nothing but update a global variable with 
the ping distance result.  Because check_ping() returns same value for "still 
waiting" and "timed out", I can't use that to update the global to zero after a 
timeout. This means the global always reflects the last *good* ping attempt, so 
I have to clear it (set it to zero) every time I test it.

NewPing is already keeping track of the last good ping attempt in ping_result, 
but it's doing exactly what I'm doing... just updating when there's a 
successful ping and never setting it back to zero.

If check_ping()'s timeout code would set ping_result to zero, then I could 
eliminate my global variable (and the need to zero it out) and simply look at 
ping_result.  ping_result is used nowhere else in the code, so setting it to 
zero should have no undesirable side-effects inside NewPing.

boolean NewPing::check_timer() {
        if (micros() > _max_time) { // Outside the timeout limit.
                timer_stop();           // Disable timer interrupt
                ping_result = 0;        // Last ping timed out
                return false;           // Cancel ping timer.
        }
...

Alternately, check_timer() could return an int, returning -1 for "still 
waiting", 0 for "timed out" and a positive number for distance.  But this would 
change the API and affect existing code.

Original issue reported on code.google.com by raven...@gmail.com on 22 Feb 2013 at 10:49

GoogleCodeExporter commented 9 years ago
There's no way to know the difference between "still waiting" and no ping 
result.  If there's no ping result, it's still waiting.  All that we know is 
that EITHER it's out of range OR no ping result.

The current code already tracks "still waiting" when sonar.check_timer() 
returns false.  This needs to be polled anyway, so there's no saving and you 
can't access ping_result directly as a result.  It could be done to set 
ping_result to zero if the ping times out.  But, I feel that's wasted code as 
all you know is that it's "all clear".

Basically, the only time you should be doing something with the ping_result 
value is when check_timer() returns a true value.  False values tell you that 
it hasn't got a ping yet.  This is designed to alert you when something is in 
range, not inform you of the status of the ping.  If you need to know the 
status of the ping, you will need outside code as you're doing now.

Original comment by eckel.tim on 15 Jul 2013 at 1:56

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Please report this in a more appropriate place.

Original comment by eckel.tim on 19 Sep 2013 at 6:58