99package dht // import "tinygo.org/x/drivers/dht"
1010
1111import (
12- "machine"
1312 "runtime/interrupt"
1413 "time"
14+
15+ "tinygo.org/x/drivers"
1516)
1617
1718// DummyDevice provides a basic interface for DHT devices.
@@ -30,7 +31,7 @@ type DummyDevice interface {
3031// Since taking measurements from the sensor is time consuming procedure and blocks interrupts,
3132// user can avoid any hidden calls to the sensor.
3233type device struct {
33- pin machine .Pin
34+ pin drivers .Pin
3435
3536 measurements DeviceType
3637 initialized bool
@@ -44,7 +45,9 @@ type device struct {
4445func (t * device ) ReadMeasurements () error {
4546 // initial waiting
4647 state := powerUp (t .pin )
47- defer t .pin .Set (state )
48+ defer func () {
49+ t .pin .Set (state )
50+ }()
4851 err := t .read ()
4952 if err == nil {
5053 t .initialized = true
@@ -93,14 +96,12 @@ func (t *device) HumidityFloat() (float32, error) {
9396// Perform initialization of the communication protocol.
9497// Device lowers the voltage on pin for startingLow=20ms and starts listening for response
9598// Section 5.2 in [1]
96- func initiateCommunication (p machine .Pin ) {
99+ func initiateCommunication (p drivers .Pin ) {
97100 // Send low signal to the device
98- p .Configure (machine.PinConfig {Mode : machine .PinOutput })
99- p .Low ()
101+ p .Set (false )
100102 time .Sleep (startingLow )
101103 // Set pin to high and wait for reply
102- p .High ()
103- p .Configure (machine.PinConfig {Mode : machine .PinInput })
104+ p .Set (true )
104105}
105106
106107// Measurements returns both measurements: temperature and humidity as they sent by the device.
@@ -158,7 +159,7 @@ func (t *device) read() error {
158159
159160// receiveSignals counts number of low and high cycles. The execution is time critical, so the function disables
160161// interrupts
161- func receiveSignals (pin machine. Pin , result []counter ) {
162+ func receiveSignals (pin drivers. PinInput , result []counter ) {
162163 i := uint8 (0 )
163164 mask := interrupt .Disable ()
164165 defer interrupt .Restore (mask )
@@ -189,7 +190,7 @@ func (t *device) extractData(signals []counter, buf []uint8) error {
189190// waitForDataTransmission waits for reply from the sensor.
190191// If no reply received, returns NoSignalError.
191192// For more details, see section 5.2 in [1]
192- func waitForDataTransmission (p machine. Pin ) error {
193+ func waitForDataTransmission (p drivers. PinInput ) error {
193194 // wait for thermometer to pull down
194195 if expectChange (p , true ) == timeout {
195196 return NoSignalError
@@ -209,10 +210,10 @@ func waitForDataTransmission(p machine.Pin) error {
209210// This device provides full control to the user.
210211// It does not do any hidden measurements calls and does not check
211212// 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 )
214215 return & device {
215- pin : pin ,
216+ pin : drivers . SafePin ( pin ) ,
216217 measurements : deviceType ,
217218 initialized : false ,
218219 temperature : 0 ,
0 commit comments