mirror of
https://github.com/kidoman/embd
synced 2025-07-03 03:47:33 +02:00
gpio: provide an api to allow performance timing of pulses
the us020 driver is the first consumer of this functionality
This commit is contained in:
parent
af23a999ba
commit
5b852d9f42
3 changed files with 87 additions and 43 deletions
|
@ -91,36 +91,11 @@ func (d *US020) Distance() (float64, error) {
|
|||
|
||||
glog.V(2).Infof("us020: waiting for echo to go high")
|
||||
|
||||
// Wait until ECHO goes high
|
||||
for {
|
||||
v, err := d.EchoPin.Read()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if v == embd.High {
|
||||
break
|
||||
}
|
||||
duration, err := d.EchoPin.TimePulse(embd.High)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
startTime := time.Now() // Record time when ECHO goes high
|
||||
|
||||
glog.V(2).Infof("us020: waiting for echo to go low")
|
||||
|
||||
// Wait until ECHO goes low
|
||||
for {
|
||||
v, err := d.EchoPin.Read()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if v == embd.Low {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
duration := time.Since(startTime) // Calculate time lapsed for ECHO to transition from high to low
|
||||
|
||||
// Calculate the distance based on the time computed
|
||||
distance := float64(duration.Nanoseconds()) / 10000000 * (d.speedSound / 2)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue