-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathlps22hb.go
75 lines (62 loc) · 2.17 KB
/
lps22hb.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
// Package lps22hb implements a driver for LPS22HB, a MEMS nano pressure sensor.
//
// Datasheet: https://www.st.com/resource/en/datasheet/dm00140895.pdf
package lps22hb
import (
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
)
// Device wraps an I2C connection to a HTS221 device.
type Device struct {
bus drivers.I2C
Address uint8
}
// New creates a new LPS22HB connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: LPS22HB_ADDRESS}
}
// ReadPressure returns the pressure in milli pascals (mPa).
func (d *Device) ReadPressure() (pressure int32, err error) {
d.waitForOneShot()
// read data
data := []byte{0, 0, 0}
legacy.ReadRegister(d.bus, d.Address, LPS22HB_PRESS_OUT_REG, data[:1])
legacy.ReadRegister(d.bus, d.Address, LPS22HB_PRESS_OUT_REG+1, data[1:2])
legacy.ReadRegister(d.bus, d.Address, LPS22HB_PRESS_OUT_REG+2, data[2:])
pValue := float32(uint32(data[2])<<16|uint32(data[1])<<8|uint32(data[0])) / 4096.0
return int32(pValue * 1000), nil
}
// Connected returns whether LPS22HB has been found.
// It does a "who am I" request and checks the response.
func (d *Device) Connected() bool {
data := []byte{0}
legacy.ReadRegister(d.bus, d.Address, LPS22HB_WHO_AM_I_REG, data)
return data[0] == 0xB1
}
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *Device) ReadTemperature() (temperature int32, err error) {
d.waitForOneShot()
// read data
data := []byte{0, 0}
legacy.ReadRegister(d.bus, d.Address, LPS22HB_TEMP_OUT_REG, data[:1])
legacy.ReadRegister(d.bus, d.Address, LPS22HB_TEMP_OUT_REG+1, data[1:])
tValue := float32(int16(uint16(data[1])<<8|uint16(data[0]))) / 100.0
return int32(tValue * 1000), nil
}
// private functions
// wait and trigger one shot in block update
func (d *Device) waitForOneShot() {
// trigger one shot
legacy.WriteRegister(d.bus, d.Address, LPS22HB_CTRL2_REG, []byte{0x01})
// wait until one shot is cleared
data := []byte{1}
for {
legacy.ReadRegister(d.bus, d.Address, LPS22HB_CTRL2_REG, data)
if data[0]&0x01 == 0 {
break
}
}
}