mirror of https://github.com/kidoman/embd
Merge 97b1dae347
into d3d8c0c5c6
This commit is contained in:
commit
1078122bac
|
@ -16,6 +16,8 @@ import (
|
|||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const timeoutInterval = 10000000
|
||||
|
||||
type digitalPin struct {
|
||||
id string
|
||||
n int
|
||||
|
@ -169,6 +171,8 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
|||
aroundState = embd.High
|
||||
}
|
||||
|
||||
initTime := time.Now()
|
||||
|
||||
// Wait for any previous pulse to end
|
||||
for {
|
||||
v, err := p.read()
|
||||
|
@ -179,6 +183,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
|||
if v == aroundState {
|
||||
break
|
||||
}
|
||||
|
||||
if time.Since(initTime).Nanoseconds() > timeoutInterval {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until ECHO goes high
|
||||
|
@ -191,6 +199,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
|||
if v == state {
|
||||
break
|
||||
}
|
||||
|
||||
if time.Since(initTime).Nanoseconds() > timeoutInterval {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
startTime := time.Now() // Record time when ECHO goes high
|
||||
|
@ -205,6 +217,10 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
|||
if v == aroundState {
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue