9
9
package dht // import "tinygo.org/x/drivers/dht"
10
10
11
11
import (
12
- "machine"
13
12
"runtime/interrupt"
14
13
"time"
14
+
15
+ "tinygo.org/x/drivers"
15
16
)
16
17
17
18
// DummyDevice provides a basic interface for DHT devices.
@@ -30,7 +31,7 @@ type DummyDevice interface {
30
31
// Since taking measurements from the sensor is time consuming procedure and blocks interrupts,
31
32
// user can avoid any hidden calls to the sensor.
32
33
type device struct {
33
- pin machine .Pin
34
+ pin drivers .Pin
34
35
35
36
measurements DeviceType
36
37
initialized bool
@@ -44,7 +45,9 @@ type device struct {
44
45
func (t * device ) ReadMeasurements () error {
45
46
// initial waiting
46
47
state := powerUp (t .pin )
47
- defer t .pin .Set (state )
48
+ defer func () {
49
+ t .pin .Set (state )
50
+ }()
48
51
err := t .read ()
49
52
if err == nil {
50
53
t .initialized = true
@@ -93,14 +96,12 @@ func (t *device) HumidityFloat() (float32, error) {
93
96
// Perform initialization of the communication protocol.
94
97
// Device lowers the voltage on pin for startingLow=20ms and starts listening for response
95
98
// Section 5.2 in [1]
96
- func initiateCommunication (p machine .Pin ) {
99
+ func initiateCommunication (p drivers .Pin ) {
97
100
// Send low signal to the device
98
- p .Configure (machine.PinConfig {Mode : machine .PinOutput })
99
- p .Low ()
101
+ p .Set (false )
100
102
time .Sleep (startingLow )
101
103
// Set pin to high and wait for reply
102
- p .High ()
103
- p .Configure (machine.PinConfig {Mode : machine .PinInput })
104
+ p .Set (true )
104
105
}
105
106
106
107
// Measurements returns both measurements: temperature and humidity as they sent by the device.
@@ -158,7 +159,7 @@ func (t *device) read() error {
158
159
159
160
// receiveSignals counts number of low and high cycles. The execution is time critical, so the function disables
160
161
// interrupts
161
- func receiveSignals (pin machine. Pin , result []counter ) {
162
+ func receiveSignals (pin drivers. PinInput , result []counter ) {
162
163
i := uint8 (0 )
163
164
mask := interrupt .Disable ()
164
165
defer interrupt .Restore (mask )
@@ -189,7 +190,7 @@ func (t *device) extractData(signals []counter, buf []uint8) error {
189
190
// waitForDataTransmission waits for reply from the sensor.
190
191
// If no reply received, returns NoSignalError.
191
192
// For more details, see section 5.2 in [1]
192
- func waitForDataTransmission (p machine. Pin ) error {
193
+ func waitForDataTransmission (p drivers. PinInput ) error {
193
194
// wait for thermometer to pull down
194
195
if expectChange (p , true ) == timeout {
195
196
return NoSignalError
@@ -209,10 +210,10 @@ func waitForDataTransmission(p machine.Pin) error {
209
210
// This device provides full control to the user.
210
211
// It does not do any hidden measurements calls and does not check
211
212
// for 2 seconds delay between measurements.
212
- func NewDummyDevice (pin machine .Pin , deviceType DeviceType ) DummyDevice {
213
- pin .High ( )
213
+ func NewDummyDevice (pin drivers .Pin , deviceType DeviceType ) DummyDevice {
214
+ pin .Set ( true )
214
215
return & device {
215
- pin : pin ,
216
+ pin : drivers . SafePin ( pin ) ,
216
217
measurements : deviceType ,
217
218
initialized : false ,
218
219
temperature : 0 ,
0 commit comments