mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-21 06:59:00 +00:00
i2c iface refactor: Resolve 559
This commit is contained in:
+9
-8
@@ -5,6 +5,7 @@ package lps22hb
|
||||
|
||||
import (
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
// Device wraps an I2C connection to a HTS221 device.
|
||||
@@ -27,9 +28,9 @@ func (d *Device) ReadPressure() (pressure int32, err error) {
|
||||
|
||||
// read data
|
||||
data := []byte{0, 0, 0}
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG, data[:1])
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG+1, data[1:2])
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_PRESS_OUT_REG+2, data[2:])
|
||||
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
|
||||
@@ -39,7 +40,7 @@ func (d *Device) ReadPressure() (pressure int32, err error) {
|
||||
// It does a "who am I" request and checks the response.
|
||||
func (d *Device) Connected() bool {
|
||||
data := []byte{0}
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_WHO_AM_I_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, LPS22HB_WHO_AM_I_REG, data)
|
||||
return data[0] == 0xB1
|
||||
}
|
||||
|
||||
@@ -49,8 +50,8 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||
|
||||
// read data
|
||||
data := []byte{0, 0}
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_TEMP_OUT_REG, data[:1])
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_TEMP_OUT_REG+1, data[1:])
|
||||
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
|
||||
@@ -61,12 +62,12 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||
// wait and trigger one shot in block update
|
||||
func (d *Device) waitForOneShot() {
|
||||
// trigger one shot
|
||||
d.bus.WriteRegister(d.Address, LPS22HB_CTRL2_REG, []byte{0x01})
|
||||
legacy.WriteRegister(d.bus, d.Address, LPS22HB_CTRL2_REG, []byte{0x01})
|
||||
|
||||
// wait until one shot is cleared
|
||||
data := []byte{1}
|
||||
for {
|
||||
d.bus.ReadRegister(d.Address, LPS22HB_CTRL2_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, LPS22HB_CTRL2_REG, data)
|
||||
if data[0]&0x01 == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
package lps22hb
|
||||
|
||||
import "tinygo.org/x/drivers"
|
||||
import "tinygo.org/x/drivers/internal/legacy"
|
||||
|
||||
// Configure sets up the LPS22HB device for communication.
|
||||
func (d *Device) Configure() {
|
||||
// set to block update mode
|
||||
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||
legacy.WriteRegister(d.bus, d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ func (d *Device) Configure() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
// set to block update mode
|
||||
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||
legacy.WriteRegister(d.bus, d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user