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 (
8
8
"time"
9
9
10
10
"tinygo.org/x/drivers/dht"
11
+ "tinygo.org/x/drivers/tinygo"
11
12
)
12
13
13
14
func main () {
14
- pin := & machinePin { pin : machine .D6 }
15
+ pin := tinygo . New ( machine .D6 )
15
16
dhtSensor := dht .New (pin , dht .DHT11 )
16
17
for {
17
18
temp , hum , err := dhtSensor .Measurements ()
@@ -24,27 +25,3 @@ func main() {
24
25
time .Sleep (time .Second * 2 )
25
26
}
26
27
}
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 (
5
5
"time"
6
6
7
7
"tinygo.org/x/drivers/hcsr04"
8
+ "tinygo.org/x/drivers/tinygo"
8
9
)
9
10
10
11
func 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 )
12
15
sensor .Configure ()
13
16
14
17
println ("Ultrasonic starts" )
Original file line number Diff line number Diff line change 5
5
package hcsr04
6
6
7
7
import (
8
- "machine"
9
8
"time"
9
+
10
+ "tinygo.org/x/drivers"
10
11
)
11
12
12
13
const TIMEOUT = 23324 // max sensing distance (4m)
13
14
14
15
// Device holds the pins
15
16
type Device struct {
16
- trigger machine. Pin
17
- echo machine. Pin
17
+ trigger drivers. PinOutput
18
+ echo drivers. PinInput
18
19
}
19
20
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 {
22
24
return Device {
23
25
trigger : trigger ,
24
26
echo : echo ,
@@ -27,8 +29,7 @@ func New(trigger, echo machine.Pin) Device {
27
29
28
30
// Configure configures the pins of the Device
29
31
func (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
32
33
}
33
34
34
35
// ReadDistance returns the distance of the object in mm
@@ -45,11 +46,11 @@ func (d *Device) ReadDistance() int32 {
45
46
// ReadPulse returns the time of the pulse (roundtrip) in microseconds
46
47
func (d * Device ) ReadPulse () int32 {
47
48
t := time .Now ()
48
- d .trigger .Low ( )
49
+ d .trigger .Set ( false )
49
50
time .Sleep (2 * time .Microsecond )
50
- d .trigger .High ( )
51
+ d .trigger .Set ( true )
51
52
time .Sleep (10 * time .Microsecond )
52
- d .trigger .Low ( )
53
+ d .trigger .Set ( false )
53
54
i := uint8 (0 )
54
55
for {
55
56
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