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

     1  //go:build tinygo
     2  
     3  package dht // import "tinygo.org/x/drivers/dht"
     4  
     5  import (
     6  	"machine"
     7  	"time"
     8  )
     9  
    10  // Check if the pin is disabled
    11  func powerUp(p machine.Pin) bool {
    12  	state := p.Get()
    13  	if !state {
    14  		p.High()
    15  		time.Sleep(startTimeout)
    16  	}
    17  	return state
    18  }
    19  
    20  func expectChange(p machine.Pin, oldState bool) counter {
    21  	cnt := counter(0)
    22  	for ; p.Get() == oldState && cnt != timeout; cnt++ {
    23  	}
    24  	return cnt
    25  }
    26  
    27  func checksum(buf []uint8) uint8 {
    28  	return buf[4]
    29  }
    30  func computeChecksum(buf []uint8) uint8 {
    31  	return buf[0] + buf[1] + buf[2] + buf[3]
    32  }
    33  
    34  func isValid(buf []uint8) bool {
    35  	return checksum(buf) == computeChecksum(buf)
    36  }