legacy.PinOutput->pin.Output and add High+Low methods on drivers.PinOutput and use them in drivers

This commit is contained in:
Patricio Whittingslow
2025-09-16 17:53:18 -03:00
committed by deadprogram
parent 2e007a6de7
commit 8ac285e821
30 changed files with 280 additions and 245 deletions
+5 -4
View File
@@ -9,6 +9,7 @@ import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
const TIMEOUT = 23324 // max sensing distance (4m)
@@ -21,7 +22,7 @@ type Device struct {
}
// New returns a new ultrasonic driver given 2 pins
func New(trigger legacy.PinOutput, echo legacy.PinInput) Device {
func New(trigger pin.Output, echo pin.Input) Device {
return Device{
trigger: trigger.Set,
echo: echo.Get,
@@ -54,11 +55,11 @@ func (d *Device) ReadDistance() int32 {
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
func (d *Device) ReadPulse() int32 {
t := time.Now()
d.trigger(false)
d.trigger.Low()
time.Sleep(2 * time.Microsecond)
d.trigger(true)
d.trigger.High()
time.Sleep(10 * time.Microsecond)
d.trigger(false)
d.trigger.Low()
i := uint8(0)
for {
if d.echo() {