remove last traces of legacy I2C calls

This commit is contained in:
soypat
2023-12-26 08:48:49 -08:00
parent 5dfacc3c73
commit ca37c1c461
2 changed files with 74 additions and 50 deletions
+54 -39
View File
@@ -8,17 +8,16 @@ import (
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
) )
// Device wraps an I2C connection to a APDS-9960 device. // Device wraps an I2C connection to a APDS-9960 device.
type Device struct { type Device struct {
bus drivers.I2C bus drivers.I2C
_txerr error
gesture gestureData
buf [8]byte
Address uint8 Address uint8
mode uint8 mode uint8
gesture gestureData
buf [32]byte
_txerr error
} }
// Configuration for APDS-9960 device. // Configuration for APDS-9960 device.
@@ -48,17 +47,7 @@ type gestureData struct {
received bool received bool
} }
// for enabling various device function // for enabling various device functions.
type enableConfig struct {
GEN bool
PIEN bool
AIEN bool
WEN bool
PEN bool
AEN bool
PON bool
}
type encfg uint8 type encfg uint8
// data := []byte{gen<<6 | pien<<5 | aien<<4 | wen<<3 | pen<<2 | aen<<1 | pon} // data := []byte{gen<<6 | pien<<5 | aien<<4 | wen<<3 | pen<<2 | aen<<1 | pon}
@@ -113,7 +102,7 @@ func (d *Device) DisableAll() error {
return err return err
} }
// SetProximityPulse sets proximity pulse length (4, 8, 16, 32) and count (1~64) // SetProximityPulse sets proximity pulse length (4, 8, 16, 32) and count (1..64)
// default: 16, 64 // default: 16, 64
func (d *Device) SetProximityPulse(length, count uint8) error { func (d *Device) SetProximityPulse(length, count uint8) error {
d.txNew() d.txNew()
@@ -121,7 +110,7 @@ func (d *Device) SetProximityPulse(length, count uint8) error {
return d.txErr() return d.txErr()
} }
// SetGesturePulse sets gesture pulse length (4, 8, 16, 32) and count (1~64) // SetGesturePulse sets gesture pulse length (4, 8, 16, 32) and count (1..64)
// default: 16, 64 // default: 16, 64
func (d *Device) SetGesturePulse(length, count uint8) error { func (d *Device) SetGesturePulse(length, count uint8) error {
d.txNew() d.txNew()
@@ -129,8 +118,8 @@ func (d *Device) SetGesturePulse(length, count uint8) error {
return d.txErr() return d.txErr()
} }
// SetADCIntegrationCycles sets ALS/color ADC internal integration cycles (1~256, 1 cycle = 2.78 ms) // SetADCIntegrationCycles sets ALS/color ADC internal integration cycles (1..256, 1 cycle = 2.78 ms)
// default: 4 (~10 ms) // default: 4 (approx. 10 ms)
func (d *Device) SetADCIntegrationCycles(cycles uint16) error { func (d *Device) SetADCIntegrationCycles(cycles uint16) error {
if cycles > 256 { if cycles > 256 {
cycles = 256 cycles = 256
@@ -168,13 +157,13 @@ func (d *Device) LEDBoost(percent uint16) error {
return d.txErr() return d.txErr()
} }
// Setthreshold sets threshold (0~255) for detecting gestures // Setthreshold sets threshold (0..255) for detecting gestures
// default: 30 // default: 30
func (d *Device) Setthreshold(t uint8) { func (d *Device) Setthreshold(t uint8) {
d.gesture.threshold = t d.gesture.threshold = t
} }
// Setsensitivity sets sensivity (0~100) for detecting gestures // Setsensitivity sets sensivity (0..100) for detecting gestures
// default: 20 // default: 20
func (d *Device) Setsensitivity(s uint8) { func (d *Device) Setsensitivity(s uint8) {
if s > 100 { if s > 100 {
@@ -198,6 +187,14 @@ func (d *Device) EnableProximity() error {
return err return err
} }
// Err returns the current error state of the device if encountered during I2C communication.
// After a call to Err the error is cleared.
func (d *Device) Err() error {
err := d.txErr()
d.txNew()
return err
}
// ProximityAvailable reports if proximity data is available // ProximityAvailable reports if proximity data is available
func (d *Device) ProximityAvailable() bool { func (d *Device) ProximityAvailable() bool {
if d.mode != MODE_PROXIMITY { if d.mode != MODE_PROXIMITY {
@@ -207,7 +204,7 @@ func (d *Device) ProximityAvailable() bool {
return err == nil && status.PVALID() return err == nil && status.PVALID()
} }
// ReadProximity reads proximity data (0~255) // ReadProximity reads proximity data (0..255)
func (d *Device) ReadProximity() (proximity int32) { func (d *Device) ReadProximity() (proximity int32) {
if d.mode != MODE_PROXIMITY { if d.mode != MODE_PROXIMITY {
return 0 return 0
@@ -218,11 +215,14 @@ func (d *Device) ReadProximity() (proximity int32) {
} }
// EnableColor starts the color engine // EnableColor starts the color engine
func (d *Device) EnableColor() error { func (d *Device) EnableColor() (err error) {
if d.mode != MODE_NONE { if d.mode != MODE_NONE {
d.DisableAll() err = d.DisableAll()
if err != nil {
return err
}
} }
err := d.enable(enPON | enAEN | enWEN) err = d.enable(enPON | enAEN | enWEN)
if err == nil { if err == nil {
d.mode = MODE_COLOR d.mode = MODE_COLOR
} }
@@ -300,11 +300,14 @@ func (d *Device) GestureAvailable() bool {
data := d.buf[:] data := d.buf[:]
// read up, down, left and right proximity data from FIFO // read up, down, left and right proximity data from FIFO
var dataSets [32][4]uint8 var dataSets [32][4]uint8
const numAddrs = APDS9960_GFIFO_R_REG - APDS9960_GFIFO_U_REG + 1
for i := uint8(0); i < availableDataSets; i++ { for i := uint8(0); i < availableDataSets; i++ {
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_U_REG, data[:1]) for j := uint8(0); j < numAddrs; j++ {
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_D_REG, data[1:2]) data[j] = d.txRead8(j + APDS9960_GFIFO_U_REG)
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_L_REG, data[2:3]) }
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_R_REG, data[3:4]) if d.txErr() != nil {
return false
}
for j := uint8(0); j < 4; j++ { for j := uint8(0); j < 4; j++ {
dataSets[i][j] = data[j] dataSets[i][j] = data[j]
} }
@@ -366,9 +369,11 @@ func (d *Device) ReadGesture() (gesture int32) {
// private functions // private functions
func (d *Device) configureDevice(cfg Configuration) { func (d *Device) configureDevice(cfg Configuration) error {
d.DisableAll() // turn off everything err := d.DisableAll() // turn off everything
if err != nil {
return err
}
// "default" settings // "default" settings
if cfg.ProximityPulseLength == 0 { if cfg.ProximityPulseLength == 0 {
cfg.ProximityPulseLength = 16 cfg.ProximityPulseLength = 16
@@ -401,14 +406,23 @@ func (d *Device) configureDevice(cfg Configuration) {
d.gesture.sensitivity = 20 d.gesture.sensitivity = 20
} }
d.SetProximityPulse(cfg.ProximityPulseLength, cfg.ProximityPulseCount) err = d.SetProximityPulse(cfg.ProximityPulseLength, cfg.ProximityPulseCount)
d.SetGesturePulse(cfg.GesturePulseLength, cfg.GesturePulseCount) if err != nil {
d.SetGains(cfg.ProximityGain, cfg.GestureGain, cfg.ColorGain) return err
d.SetADCIntegrationCycles(cfg.ADCIntegrationCycles)
if cfg.LEDBoost > 0 {
d.LEDBoost(cfg.LEDBoost)
} }
err = d.SetGesturePulse(cfg.GesturePulseLength, cfg.GesturePulseCount)
if err != nil {
return err
}
err = d.SetGains(cfg.ProximityGain, cfg.GestureGain, cfg.ColorGain)
if err != nil {
return err
}
err = d.SetADCIntegrationCycles(cfg.ADCIntegrationCycles)
if err == nil && cfg.LEDBoost > 0 {
err = d.LEDBoost(cfg.LEDBoost)
}
return err
} }
func (d *Device) enable(cfg encfg) error { func (d *Device) enable(cfg encfg) error {
@@ -434,6 +448,7 @@ func (d *Device) txRead8(addr uint8) uint8 {
d._txerr = d.bus.Tx(uint16(d.Address), d.buf[:1], d.buf[1:2]) d._txerr = d.bus.Tx(uint16(d.Address), d.buf[:1], d.buf[1:2])
return d.buf[1] return d.buf[1]
} }
func (d *Device) txWrite8(addr uint8, val uint8) { func (d *Device) txWrite8(addr uint8, val uint8) {
if d._txerr != nil { if d._txerr != nil {
return return
+20 -11
View File
@@ -8,33 +8,42 @@ import (
) )
func main() { func main() {
// Sleep to catch any errors through the serial monitor.
time.Sleep(1000 * time.Millisecond)
bus := machine.I2C0
// use Nano 33 BLE Sense's internal I2C bus // use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{ err := bus.Configure(machine.I2CConfig{
SCL: machine.SCL1_PIN, SCL: machine.GP1,
SDA: machine.SDA1_PIN, SDA: machine.GP0,
Frequency: machine.TWI_FREQ_400KHZ, Frequency: 400 * machine.KHz,
}) })
if err != nil {
panic(err.Error())
}
sensor := apds9960.New(machine.I2C1) sensor := apds9960.New(bus)
// use default settings // use default settings
sensor.Configure(apds9960.Configuration{}) sensor.Configure(apds9960.Configuration{})
if !sensor.Connected() { if !sensor.Connected() {
println("APDS-9960 not connected!") println("APDS-9960 not connected!")
println("err:", sensor.Err())
return return
} }
println("APDS connected!")
sensor.EnableProximity() // enable proximity engine err = sensor.EnableProximity() // enable proximity engine
if err != nil {
panic(err.Error())
}
for { for {
if sensor.ProximityAvailable() { if sensor.ProximityAvailable() {
p := sensor.ReadProximity() p := sensor.ReadProximity()
println("Proximity:", p) println("Proximity:", p)
} }
if err := sensor.Err(); err != nil {
println(err.Error())
}
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
} }
} }