mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 06:03:57 +00:00
i2c iface refactor: Resolve 559
This commit is contained in:
+9
-6
@@ -4,7 +4,10 @@
|
||||
// Datasheet: https://www.nxp.com/docs/en/data-sheet/MAG3110.pdf
|
||||
package mag3110 // import "tinygo.org/x/drivers/mag3110"
|
||||
|
||||
import "tinygo.org/x/drivers"
|
||||
import (
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
// Device wraps an I2C connection to a MAG3110 device.
|
||||
type Device struct {
|
||||
@@ -24,22 +27,22 @@ 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(uint8(d.Address), WHO_AM_I, data)
|
||||
legacy.ReadRegister(d.bus, uint8(d.Address), WHO_AM_I, data)
|
||||
return data[0] == 0xC4
|
||||
}
|
||||
|
||||
// Configure sets up the device for communication.
|
||||
func (d Device) Configure() {
|
||||
d.bus.WriteRegister(uint8(d.Address), CTRL_REG2, []uint8{0x80}) // Power down when not used
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), CTRL_REG2, []uint8{0x80}) // Power down when not used
|
||||
}
|
||||
|
||||
// ReadMagnetic reads the vectors of the magnetic field of the device and
|
||||
// returns it.
|
||||
func (d Device) ReadMagnetic() (x int16, y int16, z int16) {
|
||||
d.bus.WriteRegister(uint8(d.Address), CTRL_REG1, []uint8{0x1a}) // Request a measurement
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), CTRL_REG1, []uint8{0x1a}) // Request a measurement
|
||||
|
||||
data := make([]byte, 6)
|
||||
d.bus.ReadRegister(uint8(d.Address), OUT_X_MSB, data)
|
||||
legacy.ReadRegister(d.bus, uint8(d.Address), OUT_X_MSB, data)
|
||||
x = int16((uint16(data[0]) << 8) | uint16(data[1]))
|
||||
y = int16((uint16(data[2]) << 8) | uint16(data[3]))
|
||||
z = int16((uint16(data[4]) << 8) | uint16(data[5]))
|
||||
@@ -50,6 +53,6 @@ func (d Device) ReadMagnetic() (x int16, y int16, z int16) {
|
||||
// celsius milli degrees (°C/1000).
|
||||
func (d Device) ReadTemperature() (int32, error) {
|
||||
data := make([]byte, 1)
|
||||
d.bus.ReadRegister(uint8(d.Address), DIE_TEMP, data)
|
||||
legacy.ReadRegister(d.bus, uint8(d.Address), DIE_TEMP, data)
|
||||
return int32(data[0]) * 1000, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user