mirror of https://github.com/periph/devices
Introduces driver for Sensirion SHT-4X Temperature/Humidity Sensors.
Move CRC8 logic into common package for resuability.pull/109/head
parent
f009056300
commit
8a85f99127
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2025 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package common contains functions used across multiple packages. For
|
||||||
|
// example, a CRC8 calculation
|
||||||
|
package common
|
||||||
|
|
||||||
|
// CRC8 calculates the 8-bit CRC of the byte slice parameter and returns the
|
||||||
|
// calculated value. CRC bytes are used in sensors from TI and Sensirion.
|
||||||
|
func CRC8(bytes []byte) byte {
|
||||||
|
var crc byte = 0xff
|
||||||
|
for _, val := range bytes {
|
||||||
|
crc ^= val
|
||||||
|
for range 8 {
|
||||||
|
if (crc & 0x80) == 0 {
|
||||||
|
crc <<= 1
|
||||||
|
} else {
|
||||||
|
crc = (byte)((crc << 1) ^ 0x31)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return crc
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
// Copyright 2025 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package common
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCRC8(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
bytes []byte
|
||||||
|
result byte
|
||||||
|
}{
|
||||||
|
{bytes: []byte{0xbe, 0xef}, result: 0x92},
|
||||||
|
{bytes: []byte{0x01, 0xa4}, result: 0x4d},
|
||||||
|
{bytes: []byte{0xab, 0xcd}, result: 0x6f},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
res := CRC8(test.bytes)
|
||||||
|
if res != test.result {
|
||||||
|
t.Errorf("CRC8(%#v)!=0x%d received 0x%d", test.bytes, test.result, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright 2025 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package sht4x_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"periph.io/x/conn/v3/i2c/i2creg"
|
||||||
|
"periph.io/x/conn/v3/physic"
|
||||||
|
"periph.io/x/devices/v3/sht4x"
|
||||||
|
"periph.io/x/host/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Example shows creating an SHT-4X sensor and reading from it.
|
||||||
|
func Example() {
|
||||||
|
if _, err := host.Init(); err != nil {
|
||||||
|
log.Fatal("Error calling host.init()")
|
||||||
|
}
|
||||||
|
bus, err := i2creg.Open("")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer bus.Close()
|
||||||
|
|
||||||
|
dev, err := sht4x.New(bus, sht4x.DefaultAddress)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
env := &physic.Env{}
|
||||||
|
|
||||||
|
for range 10 {
|
||||||
|
err = dev.Sense(env)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Temperature: %s Humidity: %s\n", env.Temperature, env.Humidity)
|
||||||
|
}
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,209 @@
|
|||||||
|
// Copyright 2025 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package sht4x
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"periph.io/x/conn/v3/i2c/i2ctest"
|
||||||
|
"periph.io/x/conn/v3/physic"
|
||||||
|
)
|
||||||
|
|
||||||
|
var liveDevice bool
|
||||||
|
|
||||||
|
func getDev(testName string) (*Dev, error) {
|
||||||
|
return New(&i2ctest.Playback{Ops: recordingData[testName], DontPanic: true}, DefaultAddress)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasic(t *testing.T) {
|
||||||
|
t.Logf("liveDevice=%t", liveDevice)
|
||||||
|
dev, err := getDev(t.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test String
|
||||||
|
s := dev.String()
|
||||||
|
if len(s) == 0 {
|
||||||
|
t.Error("string returned empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test Serial Number
|
||||||
|
sn, err := dev.SerialNumber()
|
||||||
|
if err == nil {
|
||||||
|
if sn == 0 {
|
||||||
|
t.Error("invalid serial number")
|
||||||
|
} else {
|
||||||
|
t.Logf("SerialNumber=0x%x", sn)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCountToTemp(t *testing.T) {
|
||||||
|
temp := countToTemp(0)
|
||||||
|
if temp != minTemperature {
|
||||||
|
t.Errorf("invalid temperature %s. Expected -40", temp)
|
||||||
|
}
|
||||||
|
temp = countToTemp(0xffff)
|
||||||
|
if temp != maxTemperature {
|
||||||
|
t.Errorf("invalid temperature %s. Expected 125", temp)
|
||||||
|
}
|
||||||
|
temp = countToTemp(0x8000)
|
||||||
|
tTest := 42.5 + physic.ZeroCelsius.Celsius()
|
||||||
|
diff := physic.Temperature(math.Abs(tTest-float64(temp.Celsius()))) * physic.Kelvin
|
||||||
|
if diff > 2*physic.MilliKelvin {
|
||||||
|
t.Errorf("invalid temperature expected %f. got %s diff=%s", tTest, temp, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCountToHumidity(t *testing.T) {
|
||||||
|
rh := countToHumidity(0)
|
||||||
|
if rh != minRH {
|
||||||
|
t.Errorf("received RH %s expected %s", rh, minRH)
|
||||||
|
}
|
||||||
|
rh = countToHumidity(0xffff)
|
||||||
|
if rh != maxRH {
|
||||||
|
t.Errorf("received RH %s expected %s", rh, maxRH)
|
||||||
|
}
|
||||||
|
rh = countToHumidity(0x8000)
|
||||||
|
expected := physic.RelativeHumidity(56.5 * float64(physic.PercentRH))
|
||||||
|
diff := rh - expected
|
||||||
|
if diff > 2*physic.MilliRH {
|
||||||
|
t.Errorf("received rh %s expected %s diff=%v", rh, expected, diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test turning on the heater at various power levels and durations.
|
||||||
|
func TestHeater(t *testing.T) {
|
||||||
|
dev, err := getDev(t.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Test Invalid parameters
|
||||||
|
_, err = dev.SetHeater(Power20mW, HeaterDuration(10*time.Second))
|
||||||
|
if err == nil {
|
||||||
|
t.Error("SetHeater() invalid duration did not generate error.")
|
||||||
|
}
|
||||||
|
_, err = dev.SetHeater(HeaterPower(500), Duration100ms)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("SetHeater() invalid power level did not generate error.")
|
||||||
|
}
|
||||||
|
initEnv := &physic.Env{}
|
||||||
|
// Iterate over the allowed durations
|
||||||
|
for _, duration := range []HeaterDuration{Duration100ms, Duration1s} {
|
||||||
|
// Iterate over the supported heater power levels
|
||||||
|
for _, power := range []HeaterPower{Power20mW, Power110mW, Power200mW} {
|
||||||
|
// Read the initial temperature at the test start.
|
||||||
|
err := dev.Sense(initEnv)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// Turn the heater on 3 times
|
||||||
|
var diffLast float64
|
||||||
|
for range 3 {
|
||||||
|
env, err := dev.SetHeater(power, duration)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Confirm that the difference between the initial temperature and
|
||||||
|
// the temperature after the heater was turned on is > 0
|
||||||
|
diff := env.Temperature.Celsius() - initEnv.Temperature.Celsius()
|
||||||
|
t.Logf("initTemp=%s heaterTemp=%s, diff=%f", initEnv.Temperature, env.Temperature, diff)
|
||||||
|
if diff <= 0 || diff <= diffLast {
|
||||||
|
t.Errorf("heater error power=%d, Duration=%v diff=%f expected > 0", power, duration, diff)
|
||||||
|
}
|
||||||
|
diffLast = diff
|
||||||
|
}
|
||||||
|
if liveDevice {
|
||||||
|
// Give the thermometer core time to cool off
|
||||||
|
time.Sleep(10 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReset(t *testing.T) {
|
||||||
|
dev, err := getDev(t.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
err = dev.Reset()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSense(t *testing.T) {
|
||||||
|
dev, err := getDev(t.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
env := &physic.Env{}
|
||||||
|
err = dev.Sense(env)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
t.Logf("env=%#v temperature=%s humidity=%s", *env, env.Temperature.String(), env.Humidity.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSenseContinuous(t *testing.T) {
|
||||||
|
readCount := int32(10)
|
||||||
|
dev, err := getDev(t.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err = dev.SenseContinuous(time.Millisecond)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("SenseContinuous() doesn't return an error on too short a duration.")
|
||||||
|
}
|
||||||
|
ch, err := dev.SenseContinuous(100 * time.Millisecond)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = dev.SenseContinuous(time.Second)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected an error for attempting concurrent SenseContinuous")
|
||||||
|
}
|
||||||
|
|
||||||
|
counter := atomic.Int32{}
|
||||||
|
tEnd := time.Now().UnixMilli() + int64(readCount+2)*100
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
// Stay here until we get the expected number of reads, or the time
|
||||||
|
// has expired and when we do, Halt the SenseContinuous.
|
||||||
|
if counter.Load() >= readCount || time.Now().UnixMilli() >= tEnd {
|
||||||
|
t.Logf("calling halt!")
|
||||||
|
err := dev.Halt()
|
||||||
|
t.Logf("halt() returned")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// Iterate over the channel until it's closed.
|
||||||
|
for e := range ch {
|
||||||
|
counter.Add(1)
|
||||||
|
t.Log(time.Now(), e, "count=", counter.Load())
|
||||||
|
}
|
||||||
|
if counter.Load() < readCount || counter.Load() > (readCount+1) {
|
||||||
|
t.Errorf("expected %d readings. received %d", readCount, counter.Load())
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
@ -0,0 +1,94 @@
|
|||||||
|
// Copyright 2025 The Periph Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed under the Apache License, Version 2.0
|
||||||
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package sht4x
|
||||||
|
|
||||||
|
import (
|
||||||
|
"periph.io/x/conn/v3/i2c/i2ctest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Auto-Generated by i2ctest.BusTest
|
||||||
|
|
||||||
|
var recordingData = map[string][]i2ctest.IO{
|
||||||
|
"TestBasic": {
|
||||||
|
{Addr: 0x44, W: []uint8{0x89}},
|
||||||
|
{Addr: 0x44, R: []uint8{0xd, 0x20, 0x47, 0x61, 0x1, 0x11}}},
|
||||||
|
"TestHeater": {
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x63, 0x36, 0x9a, 0x5a, 0x7c, 0xd}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x15}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x67, 0xa0, 0x86, 0x5a, 0xf9, 0x82}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x15}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x69, 0x28, 0x28, 0x5b, 0x27, 0x6d}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x15}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x6a, 0x26, 0x1a, 0x5b, 0x16, 0x99}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x63, 0x4a, 0x1f, 0x5a, 0x53, 0xa5}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x24}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x79, 0x7c, 0xfc, 0x5c, 0x84, 0x6c}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x24}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x81, 0x14, 0xd1, 0x5c, 0x64, 0xad}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x24}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x85, 0xd2, 0xb3, 0x5a, 0x5d, 0xba}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x63, 0x7f, 0x2f, 0x5b, 0x84, 0xc2}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x32}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x89, 0x3c, 0xd9, 0x5e, 0xae, 0xe8}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x32}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x95, 0xe0, 0x7a, 0x5c, 0xbe, 0x72}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x32}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x9d, 0xaa, 0xab, 0x55, 0xc0, 0x56}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x63, 0xbb, 0xac, 0x57, 0xed, 0x45}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x1e}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x6d, 0x4a, 0x72, 0x55, 0x53, 0x3c}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x1e}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x6e, 0x1e, 0xe5, 0x4e, 0x61, 0xef}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x1e}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x6e, 0x7e, 0x5e, 0x49, 0x91, 0xc3}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x63, 0xc5, 0x4b, 0x57, 0xeb, 0xe3}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x2f}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x93, 0x28, 0xde, 0x3b, 0xa5, 0x20}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x2f}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x97, 0x12, 0x43, 0x25, 0x59, 0xdc}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x2f}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x99, 0x2, 0x6d, 0x1f, 0x1f, 0x1b}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x65, 0x48, 0x27, 0x51, 0x9e, 0xb4}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x39}},
|
||||||
|
{Addr: 0x44, R: []uint8{0xb3, 0xb8, 0x3b, 0x21, 0xa1, 0x64}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x39}},
|
||||||
|
{Addr: 0x44, R: []uint8{0xb9, 0xd0, 0xd7, 0x15, 0x79, 0xe8}},
|
||||||
|
{Addr: 0x44, W: []uint8{0x39}},
|
||||||
|
{Addr: 0x44, R: []uint8{0xbc, 0xb8, 0xa2, 0x14, 0x4b, 0xbb}}},
|
||||||
|
"TestReset": {
|
||||||
|
{Addr: 0x44, W: []uint8{0x94}}},
|
||||||
|
"TestSense": {
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xf4, 0xc8, 0x4c, 0x9b, 0x6f}}},
|
||||||
|
"TestSenseContinuous": {
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xee, 0x50, 0x4c, 0xbf, 0x2d}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xda, 0x51, 0x4c, 0xeb, 0x97}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xdb, 0x60, 0x4c, 0xf6, 0x98}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xda, 0x51, 0x4d, 0x24, 0xa}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xd4, 0x4e, 0x4d, 0x3f, 0xa3}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xca, 0x12, 0x4d, 0x50, 0x36}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xba, 0xea, 0x4d, 0x71, 0x81}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xb6, 0x97, 0x4d, 0x99, 0xf9}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xb3, 0x62, 0x4d, 0xb0, 0xf7}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0xb3, 0x62, 0x4d, 0xc3, 0x5c}},
|
||||||
|
{Addr: 0x44, W: []uint8{0xfd}},
|
||||||
|
{Addr: 0x44, R: []uint8{0x66, 0x9e, 0xa8, 0x4d, 0xce, 0x10}}},
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue