File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -4,11 +4,12 @@ import (
44 "fmt"
55 "machine"
66 "time"
7+
78 "tinygo.org/x/drivers/dht"
89)
910
1011func main () {
11- pin := machine .D6
12+ pin := & machinePin { pin : machine .D6 }
1213 dhtSensor := dht .New (pin , dht .DHT11 )
1314 for {
1415 temp , hum , err := dhtSensor .Measurements ()
Original file line number Diff line number Diff line change 1+ //go:build baremetal && tinygo
2+
3+ package main
4+
5+ import (
6+ "machine"
7+ )
8+
9+ // machinePin wraps machine.Pin to ensure correct pin mode is set when Get or Set methods are called.
10+
11+ type machinePin struct {
12+ pin machine.Pin
13+ mode machine.PinMode
14+ modeSet bool
15+ }
16+
17+ func (p * machinePin ) Get () bool {
18+ if ! p .modeSet || p .mode != machine .PinInput {
19+ p .pin .Configure (machine.PinConfig {Mode : machine .PinInput })
20+ p .mode = machine .PinInput
21+ }
22+ return p .pin .Get ()
23+ }
24+
25+ func (p * machinePin ) Set (high bool ) {
26+ if ! p .modeSet || p .mode != machine .PinOutput {
27+ p .pin .Configure (machine.PinConfig {Mode : machine .PinOutput })
28+ p .mode = machine .PinOutput
29+ }
30+ p .pin .Set (high )
31+ }
You can’t perform that action at this time.
0 commit comments