more drivers are now cross platform

This commit is contained in:
soypat
2025-07-02 22:03:58 -03:00
committed by deadprogram
parent 7d3404f060
commit cd37668592
20 changed files with 481 additions and 325 deletions
+24 -20
View File
@@ -11,12 +11,12 @@ import (
// also an I2C interface, but it is not yet supported.
type DeviceSPI struct {
// Chip select pin
CSB drivers.PinOutput
csb drivers.PinOutput
buf [7]byte
// SPI bus (requires chip select to be usable).
Bus drivers.SPI
bus drivers.SPI
config func()
}
@@ -25,8 +25,8 @@ type DeviceSPI struct {
// using this device.
func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
CSB: csb.Set, // chip select
Bus: spi,
csb: csb.Set, // chip select
bus: spi,
config: func() {
legacy.ConfigurePinOut(csb)
},
@@ -37,7 +37,11 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
// configures the BMI160, but it does not configure the SPI interface (it is
// assumed to be up and running).
func (d *DeviceSPI) Configure() error {
d.CSB(true)
if d.config == nil {
return legacy.ErrConfigBeforeInstantiated
}
d.config()
d.csb(true)
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
@@ -89,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.CSB(false)
err = d.Bus.Tx(data, data)
d.CSB(true)
d.csb(false)
err = d.bus.Tx(data, data)
d.csb(true)
if err != nil {
return
}
@@ -126,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB(false)
err = d.Bus.Tx(data, data)
d.CSB(true)
d.csb(false)
err = d.bus.Tx(data, data)
d.csb(true)
if err != nil {
return
}
@@ -156,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB(false)
err = d.Bus.Tx(data, data)
d.CSB(true)
d.csb(false)
err = d.bus.Tx(data, data)
d.csb(true)
if err != nil {
return
}
@@ -204,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.CSB(false)
d.Bus.Tx(data, data)
d.CSB(true)
d.csb(false)
d.bus.Tx(data, data)
d.csb(true)
return data[1]
}
@@ -220,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address
buf[1] = data
d.CSB(false)
d.Bus.Tx(buf, buf)
d.CSB(true)
d.csb(false)
d.bus.Tx(buf, buf)
d.csb(true)
}