fix pulseInLong considering overflow

fixes #3830
This commit is contained in:
Martino Facchin 2015-09-21 11:59:20 +02:00
parent 2453337d85
commit e5685758e3
1 changed files with 8 additions and 6 deletions

View File

@ -69,22 +69,24 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0);
unsigned long maxMicros = micros() + timeout;
unsigned long startMicros = micros();
// wait for any previous pulse to end
while ((*portInputRegister(port) & bit) == stateMask)
if (micros() > maxMicros)
while ((*portInputRegister(port) & bit) == stateMask) {
if (micros() - startMicros > timeout)
return 0;
}
// wait for the pulse to start
while ((*portInputRegister(port) & bit) != stateMask)
if (micros() > maxMicros)
while ((*portInputRegister(port) & bit) != stateMask) {
if (micros() - startMicros > timeout)
return 0;
}
unsigned long start = micros();
// wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) {
if (micros() > maxMicros)
if (micros() - startMicros > timeout)
return 0;
}
return micros() - start;