mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-17 13:23:26 +00:00
i2c iface refactor: Resolve 559
This commit is contained in:
+25
-24
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
// Device wraps an I2C connection to a HTS221 device.
|
||||
@@ -32,7 +33,7 @@ func New(bus drivers.I2C) Device {
|
||||
// It does a "who am I" request and checks the response.
|
||||
func (d *Device) Connected() bool {
|
||||
data := []byte{0}
|
||||
d.bus.ReadRegister(d.Address, HTS221_WHO_AM_I_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_WHO_AM_I_REG, data)
|
||||
return data[0] == 0xBC
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ func (d *Device) Power(status bool) {
|
||||
if status {
|
||||
data[0] = 0x84
|
||||
}
|
||||
d.bus.WriteRegister(d.Address, HTS221_CTRL1_REG, data)
|
||||
legacy.WriteRegister(d.bus, d.Address, HTS221_CTRL1_REG, data)
|
||||
}
|
||||
|
||||
// ReadHumidity returns the relative humidity in percent * 100.
|
||||
@@ -55,8 +56,8 @@ func (d *Device) ReadHumidity() (humidity int32, err error) {
|
||||
|
||||
// read data and calibrate
|
||||
data := []byte{0, 0}
|
||||
d.bus.ReadRegister(d.Address, HTS221_HUMID_OUT_REG, data[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_HUMID_OUT_REG+1, data[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_HUMID_OUT_REG, data[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_HUMID_OUT_REG+1, data[1:])
|
||||
hValue := readInt(data[1], data[0])
|
||||
hValueCalib := float32(hValue)*d.humiditySlope + d.humidityZero
|
||||
|
||||
@@ -73,8 +74,8 @@ func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||
|
||||
// read data and calibrate
|
||||
data := []byte{0, 0}
|
||||
d.bus.ReadRegister(d.Address, HTS221_TEMP_OUT_REG, data[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_TEMP_OUT_REG+1, data[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_TEMP_OUT_REG, data[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_TEMP_OUT_REG+1, data[1:])
|
||||
tValue := readInt(data[1], data[0])
|
||||
tValueCalib := float32(tValue)*d.temperatureSlope + d.temperatureZero
|
||||
|
||||
@@ -91,7 +92,7 @@ func (d *Device) Resolution(h uint8, t uint8) {
|
||||
if t > 7 {
|
||||
t = 3 // default
|
||||
}
|
||||
d.bus.WriteRegister(d.Address, HTS221_AV_CONF_REG, []byte{h<<3 | t})
|
||||
legacy.WriteRegister(d.bus, d.Address, HTS221_AV_CONF_REG, []byte{h<<3 | t})
|
||||
}
|
||||
|
||||
// private functions
|
||||
@@ -104,19 +105,19 @@ func (d *Device) calibration() {
|
||||
h0t0Out, h1t0Out := []byte{0, 0}, []byte{0, 0}
|
||||
t0Out, t1Out := []byte{0, 0}, []byte{0, 0}
|
||||
|
||||
d.bus.ReadRegister(d.Address, HTS221_H0_rH_x2_REG, h0rH)
|
||||
d.bus.ReadRegister(d.Address, HTS221_H1_rH_x2_REG, h1rH)
|
||||
d.bus.ReadRegister(d.Address, HTS221_T0_degC_x8_REG, t0degC)
|
||||
d.bus.ReadRegister(d.Address, HTS221_T1_degC_x8_REG, t1degC)
|
||||
d.bus.ReadRegister(d.Address, HTS221_T1_T0_MSB_REG, t1t0msb)
|
||||
d.bus.ReadRegister(d.Address, HTS221_H0_T0_OUT_REG, h0t0Out[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_H0_T0_OUT_REG+1, h0t0Out[1:])
|
||||
d.bus.ReadRegister(d.Address, HTS221_H1_T0_OUT_REG, h1t0Out[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_H1_T0_OUT_REG+1, h1t0Out[1:])
|
||||
d.bus.ReadRegister(d.Address, HTS221_T0_OUT_REG, t0Out[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_T0_OUT_REG+1, t0Out[1:])
|
||||
d.bus.ReadRegister(d.Address, HTS221_T1_OUT_REG, t1Out[:1])
|
||||
d.bus.ReadRegister(d.Address, HTS221_T1_OUT_REG+1, t1Out[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H0_rH_x2_REG, h0rH)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H1_rH_x2_REG, h1rH)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T0_degC_x8_REG, t0degC)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T1_degC_x8_REG, t1degC)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T1_T0_MSB_REG, t1t0msb)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H0_T0_OUT_REG, h0t0Out[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H0_T0_OUT_REG+1, h0t0Out[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H1_T0_OUT_REG, h1t0Out[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_H1_T0_OUT_REG+1, h1t0Out[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T0_OUT_REG, t0Out[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T0_OUT_REG+1, t0Out[1:])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T1_OUT_REG, t1Out[:1])
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_T1_OUT_REG+1, t1Out[1:])
|
||||
|
||||
h0rH_v := float32(h0rH[0]) / 2.0
|
||||
h1rH_v := float32(h1rH[0]) / 2.0
|
||||
@@ -138,7 +139,7 @@ func (d *Device) waitForOneShot(filter uint8) error {
|
||||
data := []byte{0}
|
||||
|
||||
// check if the device is on
|
||||
d.bus.ReadRegister(d.Address, HTS221_CTRL1_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_CTRL1_REG, data)
|
||||
if data[0]&0x80 == 0 {
|
||||
return errors.New("device is off, unable to query")
|
||||
}
|
||||
@@ -146,19 +147,19 @@ func (d *Device) waitForOneShot(filter uint8) error {
|
||||
// wait until one shot (one conversion) is ready to go
|
||||
data[0] = 1
|
||||
for {
|
||||
d.bus.ReadRegister(d.Address, HTS221_CTRL2_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_CTRL2_REG, data)
|
||||
if data[0]&0x01 == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// trigger one shot
|
||||
d.bus.WriteRegister(d.Address, HTS221_CTRL2_REG, []byte{0x01})
|
||||
legacy.WriteRegister(d.bus, d.Address, HTS221_CTRL2_REG, []byte{0x01})
|
||||
|
||||
// wait until conversion completed
|
||||
data[0] = 0
|
||||
for {
|
||||
d.bus.ReadRegister(d.Address, HTS221_STATUS_REG, data)
|
||||
legacy.ReadRegister(d.bus, d.Address, HTS221_STATUS_REG, data)
|
||||
if data[0]&filter == filter {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user