mirror of https://github.com/kidoman/embd
Added timeout to TimePulse
This commit is contained in:
parent
bfcd1345fe
commit
97b1dae347
|
@ -15,6 +15,8 @@ import (
|
||||||
"github.com/kidoman/embd"
|
"github.com/kidoman/embd"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const timeoutInterval = 10000000
|
||||||
|
|
||||||
type digitalPin struct {
|
type digitalPin struct {
|
||||||
id string
|
id string
|
||||||
n int
|
n int
|
||||||
|
@ -165,6 +167,8 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
||||||
aroundState = embd.High
|
aroundState = embd.High
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initTime := time.Now()
|
||||||
|
|
||||||
// Wait for any previous pulse to end
|
// Wait for any previous pulse to end
|
||||||
for {
|
for {
|
||||||
v, err := p.read()
|
v, err := p.read()
|
||||||
|
@ -175,6 +179,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
||||||
if v == aroundState {
|
if v == aroundState {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if time.Since(initTime).Nanoseconds() > timeoutInterval {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait until ECHO goes high
|
// Wait until ECHO goes high
|
||||||
|
@ -187,6 +195,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
||||||
if v == state {
|
if v == state {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if time.Since(initTime).Nanoseconds() > timeoutInterval {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
startTime := time.Now() // Record time when ECHO goes high
|
startTime := time.Now() // Record time when ECHO goes high
|
||||||
|
@ -201,6 +213,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
||||||
if v == aroundState {
|
if v == aroundState {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if time.Since(initTime).Nanoseconds() > timeoutInterval {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return time.Since(startTime), nil // Calculate time lapsed for ECHO to transition from high to low
|
return time.Since(startTime), nil // Calculate time lapsed for ECHO to transition from high to low
|
||||||
|
|
Loading…
Reference in New Issue