check_timer option to return ping_result NO_ECHO
Issue #32
resolved
In order to determine when the background ping_timer has finished, I propose to return false only when the time limit has not been exceeded. This brings the function in line with the ping(), returning ping_result = 0 for no echo. In order not to break existing code, it could be implemented by a define, here #define DIST_NO_ECHO true, which could be default false.
#define DIST_NO_ECHO true
boolean NewPing::check_timer() {
if (micros() > _max_time) { // Outside the time-out limit.
timer_stop(); // Disable timer interrupt
#if DIST_NO_ECHO == true
ping_result = NO_ECHO;
return true;
#endif
return false; // Cancel ping timer.
}
#if URM37_ENABLED == false
if (!(*_echoInput & _echoBit)) { // Ping echo received.
#else
if (*_echoInput & _echoBit) { // Ping echo received.
#endif
timer_stop(); // Disable timer interrupt
ping_result = (micros() - (_max_time - _maxEchoTime) - PING_TIMER_OVERHEAD); // Calculate ping time including overhead.
return true; // Return ping echo true.
}
return false; // Return false because there's no ping echo yet.
}
Comments (3)
-
-
repo owner If it returns a ping, it's in range. If it doesn't return a ping, there's nothing in range. ping_result doesn't have a value so you also know this. There's no reason to have additional information to let you know what you already know.
It was programmed in this way on purpose. If you feel you need something special, creating a private fork would be the way to go.
-
repo owner - changed status to resolved
- Log in to comment
Having looked deeper into the NewPing code, I realised that the ping_result is a public member. Hence it can be assigned an out of range value before calling ping_timer(). And as the check_timer() is an interrupt function, the NewPingEventTimer ought to handle the printing in the loop. With this in mind, the option to let check_timer() return true, but without touching ping_result would be appreciated. With multiple sensors, a flag to indicate end can be used to quickly fire one after the other.
As an example, here is a modified NewPingEventTimer using the option for check_timer to return true in case of out of range:
I do apologise in case you feel this is a waste of time.