File tree Expand file tree Collapse file tree 4 files changed +56
-36
lines changed Expand file tree Collapse file tree 4 files changed +56
-36
lines changed Original file line number Diff line number Diff line change @@ -8,10 +8,11 @@ import (
88 "time"
99
1010 "tinygo.org/x/drivers/dht"
11+ "tinygo.org/x/drivers/tinygo"
1112)
1213
1314func main () {
14- pin := & machinePin { pin : machine .D6 }
15+ pin := tinygo . New ( machine .D6 )
1516 dhtSensor := dht .New (pin , dht .DHT11 )
1617 for {
1718 temp , hum , err := dhtSensor .Measurements ()
@@ -24,27 +25,3 @@ func main() {
2425 time .Sleep (time .Second * 2 )
2526 }
2627}
27-
28- // machinePin wraps machine.Pin to ensure correct pin mode is set when Get or Set methods are called.
29-
30- type machinePin struct {
31- pin machine.Pin
32- mode machine.PinMode
33- modeSet bool
34- }
35-
36- func (p * machinePin ) Get () bool {
37- if ! p .modeSet || p .mode != machine .PinInput {
38- p .pin .Configure (machine.PinConfig {Mode : machine .PinInput })
39- p .mode = machine .PinInput
40- }
41- return p .pin .Get ()
42- }
43-
44- func (p * machinePin ) Set (high bool ) {
45- if ! p .modeSet || p .mode != machine .PinOutput {
46- p .pin .Configure (machine.PinConfig {Mode : machine .PinOutput })
47- p .mode = machine .PinOutput
48- }
49- p .pin .Set (high )
50- }
Original file line number Diff line number Diff line change @@ -5,10 +5,13 @@ import (
55 "time"
66
77 "tinygo.org/x/drivers/hcsr04"
8+ "tinygo.org/x/drivers/tinygo"
89)
910
1011func main () {
11- sensor := hcsr04 .New (machine .D10 , machine .D9 )
12+ trigger := tinygo .New (machine .D10 ) // automatically configures pin as output
13+ echo := tinygo .New (machine .D9 ) // automatically configures pin as input
14+ sensor := hcsr04 .New (trigger , echo )
1215 sensor .Configure ()
1316
1417 println ("Ultrasonic starts" )
Original file line number Diff line number Diff line change 55package hcsr04
66
77import (
8- "machine"
98 "time"
9+
10+ "tinygo.org/x/drivers"
1011)
1112
1213const TIMEOUT = 23324 // max sensing distance (4m)
1314
1415// Device holds the pins
1516type Device struct {
16- trigger machine. Pin
17- echo machine. Pin
17+ trigger drivers. PinOutput
18+ echo drivers. PinInput
1819}
1920
20- // New returns a new ultrasonic driver given 2 pins
21- func New (trigger , echo machine.Pin ) Device {
21+ // New returns a new ultrasonic driver given 2 pins.
22+ // Pins must be configured as output (trigger) and input (echo).
23+ func New (trigger drivers.PinOutput , echo drivers.PinInput ) Device {
2224 return Device {
2325 trigger : trigger ,
2426 echo : echo ,
@@ -27,8 +29,7 @@ func New(trigger, echo machine.Pin) Device {
2729
2830// Configure configures the pins of the Device
2931func (d * Device ) Configure () {
30- d .trigger .Configure (machine.PinConfig {Mode : machine .PinOutput })
31- d .echo .Configure (machine.PinConfig {Mode : machine .PinInput })
32+ // no-op, left for API compatibility
3233}
3334
3435// ReadDistance returns the distance of the object in mm
@@ -45,11 +46,11 @@ func (d *Device) ReadDistance() int32 {
4546// ReadPulse returns the time of the pulse (roundtrip) in microseconds
4647func (d * Device ) ReadPulse () int32 {
4748 t := time .Now ()
48- d .trigger .Low ( )
49+ d .trigger .Set ( false )
4950 time .Sleep (2 * time .Microsecond )
50- d .trigger .High ( )
51+ d .trigger .Set ( true )
5152 time .Sleep (10 * time .Microsecond )
52- d .trigger .Low ( )
53+ d .trigger .Set ( false )
5354 i := uint8 (0 )
5455 for {
5556 if d .echo .Get () {
Original file line number Diff line number Diff line change 1+ //go:build baremetal && tinygo
2+
3+ package tinygo // import "tinygo.org/x/drivers/tinygo"
4+
5+ import "machine"
6+
7+ // tinygo.Pin wraps machine.Pin to ensure correct pin mode is set when Get or Set methods are called.
8+
9+ type Pin struct {
10+ pin machine.Pin
11+ mode machine.PinMode
12+
13+ modeSet bool
14+ inputMode machine.PinMode
15+ }
16+
17+ func New (pin machine.Pin ) * Pin {
18+ return & Pin {pin : pin , inputMode : machine .PinInput }
19+ }
20+
21+ func NewWithInputMode (pin machine.Pin , inputMode machine.PinMode ) * Pin {
22+ return & Pin {pin : pin , inputMode : inputMode }
23+ }
24+
25+ func (p * Pin ) Get () bool {
26+ if ! p .modeSet || p .mode != p .inputMode {
27+ p .pin .Configure (machine.PinConfig {Mode : p .inputMode })
28+ p .mode = machine .PinInput
29+ }
30+ return p .pin .Get ()
31+ }
32+
33+ func (p * Pin ) Set (high bool ) {
34+ if ! p .modeSet || p .mode != machine .PinOutput {
35+ p .pin .Configure (machine.PinConfig {Mode : machine .PinOutput })
36+ p .mode = machine .PinOutput
37+ }
38+ p .pin .Set (high )
39+ }
You can’t perform that action at this time.
0 commit comments