tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/dht/constants.go (about)

     1  //go:build tinygo
     2  
     3  // Package dht provides a driver for DHTXX family temperature and humidity sensors.
     4  //
     5  // [1] Datasheet DHT11: https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf
     6  // [2] Datasheet DHT22: https://cdn-shop.adafruit.com/datasheets/Digital+humidity+and+temperature+sensor+AM2302.pdf
     7  // Adafruit C++ driver: https://github.com/adafruit/DHT-sensor-library
     8  
     9  package dht // import "tinygo.org/x/drivers/dht"
    10  
    11  import (
    12  	"machine"
    13  	"time"
    14  )
    15  
    16  // Celsius and Fahrenheit temperature scales
    17  type TemperatureScale uint8
    18  
    19  func (t TemperatureScale) convertToFloat(temp int16) float32 {
    20  	if t == C {
    21  		return float32(temp) / 10
    22  	} else {
    23  		// Fahrenheit
    24  		return float32(temp)*(9.0/50.) + 32.
    25  	}
    26  }
    27  
    28  // All functions return ErrorCode instance as error. This class can be used for more efficient error processing
    29  type ErrorCode uint8
    30  
    31  const (
    32  	startTimeout = time.Millisecond * 200
    33  	startingLow  = time.Millisecond * 20
    34  
    35  	C TemperatureScale = iota
    36  	F
    37  
    38  	ChecksumError ErrorCode = iota
    39  	NoSignalError
    40  	NoDataError
    41  	UpdateError
    42  	UninitializedDataError
    43  )
    44  
    45  // error interface implementation for ErrorCode
    46  func (e ErrorCode) Error() string {
    47  	switch e {
    48  	case ChecksumError:
    49  		// DHT returns ChecksumError if all the data from the sensor was received, but the checksum does not match.
    50  		return "checksum mismatch"
    51  	case NoSignalError:
    52  		// DHT returns NoSignalError if there was no reply from the sensor. Check sensor connection or the correct pin
    53  		// sis chosen,
    54  		return "no signal"
    55  	case NoDataError:
    56  		// DHT returns NoDataError if the connection was successfully initialized, but not all 40 bits from
    57  		// the sensor is received
    58  		return "no data"
    59  	case UpdateError:
    60  		// DHT returns UpdateError if ReadMeasurements function is called before time specified in UpdatePolicy or
    61  		// less than 2 seconds after past measurement
    62  		return "cannot update now"
    63  	case UninitializedDataError:
    64  		// DHT returns UninitializedDataError if user attempts to access data before first measurement
    65  		return "no measurements done"
    66  	}
    67  	// should never be reached
    68  	return "unknown error"
    69  }
    70  
    71  // Update policy of the DHT device. UpdateTime cannot be shorter than 2 seconds. According to dht specification sensor
    72  // will return undefined data if update requested less than 2 seconds before last usage
    73  type UpdatePolicy struct {
    74  	UpdateTime          time.Duration
    75  	UpdateAutomatically bool
    76  }
    77  
    78  var (
    79  	// timeout counter equal to number of ticks per 1 millisecond
    80  	timeout counter
    81  )
    82  
    83  func init() {
    84  	timeout = cyclesPerMillisecond()
    85  }
    86  
    87  func cyclesPerMillisecond() counter {
    88  	freq := machine.CPUFrequency()
    89  	freq /= 1000
    90  	return counter(freq)
    91  }