-
Notifications
You must be signed in to change notification settings - Fork 180
/
sht4x.go
76 lines (61 loc) · 2.12 KB
/
sht4x.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Package sht4x provides a driver for the SHT4x digital humidity sensor series by Sensirion.
// Datasheet: https://www.sensirion.com/media/documents/33FD6951/64D3B030/Sensirion_Datasheet_SHT4x.pdf
package sht4x
import (
"time"
"tinygo.org/x/drivers"
)
const DefaultAddress = 0x44
const (
// single-shot, high-repeatability measurement
commandMeasurement = 0xfd
)
// Device represents a SHT4x sensor
type Device struct {
bus drivers.I2C
Address uint8
}
// New creates a new SHT4x connection. The I2C bus must already be
// configured.
func New(bus drivers.I2C) Device {
return Device{
bus: bus,
Address: DefaultAddress,
}
}
// ReadTemperatureHumidity starts a measurement and then reads out the results. This function blocks
// while the measurement is in progress.
//
// Temperature is returned in [degree Celsius], multiplied by 1000,
// and relative humidity in [percent relative humidity], multiplied by 1000.
func (d *Device) ReadTemperatureHumidity() (temperatureMilliCelsius int32, relativeHumidityMilliPercent int32, err error) {
rawTemp, rawHum, err := d.rawReadings()
if err != nil {
return 0, 0, err
}
// from the reference driver: https://github.com/Sensirion/embedded-sht/blob/fcc8a523210cc1241a2750899ff6b0f68f3ed212/sht4x/sht4x.c#L81
temperatureMilliCelsius = ((21875 * int32(rawTemp)) >> 13) - 45000
relativeHumidityMilliPercent = ((15625 * int32(rawHum)) >> 13) - 6000
return temperatureMilliCelsius, relativeHumidityMilliPercent, err
}
// rawReadings returns the sensor's raw values of the temperature and humidity
func (d *Device) rawReadings() (uint16, uint16, error) {
err := d.bus.Tx(uint16(d.Address), []byte{commandMeasurement}, nil)
if err != nil {
return 0, 0, err
}
// max time for measurement according to datasheet
time.Sleep(10 * time.Millisecond)
var data [6]byte
err = d.bus.Tx(uint16(d.Address), nil, data[:])
if err != nil {
return 0, 0, err
}
tTicks := readUint(data[0], data[1])
rhTicks := readUint(data[3], data[4])
return tTicks, rhTicks, nil
}
// readUint converts two bytes to uint16
func readUint(msb byte, lsb byte) uint16 {
return (uint16(msb) << 8) | uint16(lsb)
}