Fix Nano 33 BLE drivers (#351)

Fix HTS221, LPS22HB and APDS9960 drivers and their examples for Arduino Nano 33 BLE:

* Update hts221_nano_33_ble.go
* Update lps22hb_nano_33_ble.go
* Update apds9960_nano_33_ble.go
This commit is contained in:
Alan Wang
2021-12-20 16:57:37 +08:00
committed by GitHub
parent 43099c5d5f
commit bee7422c3a
14 changed files with 151 additions and 160 deletions
+53 -46
View File
@@ -57,6 +57,14 @@ type enableConfig struct {
PON bool
}
// New creates a new APDS-9960 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: ADPS9960_ADDRESS, mode: MODE_NONE}
}
// Connected returns whether APDS-9960 has been found.
// It does a "who am I" request and checks the response.
func (d *Device) Connected() bool {
@@ -65,52 +73,6 @@ func (d *Device) Connected() bool {
return data[0] == 0xAB
}
// Configure sets up the APDS-9960 device.
func (d *Device) Configure(cfg Configuration) {
d.DisableAll() // turn off everything
// "default" settings
if cfg.ProximityPulseLength == 0 {
cfg.ProximityPulseLength = 16
}
if cfg.ProximityPulseCount == 0 {
cfg.ProximityPulseCount = 64
}
if cfg.GesturePulseLength == 0 {
cfg.GesturePulseLength = 16
}
if cfg.GesturePulseCount == 0 {
cfg.GesturePulseCount = 64
}
if cfg.ProximityGain == 0 {
cfg.ProximityGain = 1
}
if cfg.GestureGain == 0 {
cfg.GestureGain = 1
}
if cfg.ColorGain == 0 {
cfg.ColorGain = 4
}
if cfg.ADCIntegrationCycles == 0 {
cfg.ADCIntegrationCycles = 4
}
if cfg.threshold == 0 {
d.gesture.threshold = 30
}
if cfg.sensitivity == 0 {
d.gesture.sensitivity = 20
}
d.SetProximityPulse(cfg.ProximityPulseLength, cfg.ProximityPulseCount)
d.SetGesturePulse(cfg.GesturePulseLength, cfg.GesturePulseCount)
d.SetGains(cfg.ProximityGain, cfg.GestureGain, cfg.ColorGain)
d.SetADCIntegrationCycles(cfg.ADCIntegrationCycles)
if cfg.LEDBoost > 0 {
d.LEDBoost(cfg.LEDBoost)
}
}
// GetMode returns current engine mode
func (d *Device) GetMode() uint8 {
return d.mode
@@ -353,6 +315,51 @@ func (d *Device) ReadGesture() (gesture int32) {
// private functions
func (d *Device) configureDevice(cfg Configuration) {
d.DisableAll() // turn off everything
// "default" settings
if cfg.ProximityPulseLength == 0 {
cfg.ProximityPulseLength = 16
}
if cfg.ProximityPulseCount == 0 {
cfg.ProximityPulseCount = 64
}
if cfg.GesturePulseLength == 0 {
cfg.GesturePulseLength = 16
}
if cfg.GesturePulseCount == 0 {
cfg.GesturePulseCount = 64
}
if cfg.ProximityGain == 0 {
cfg.ProximityGain = 1
}
if cfg.GestureGain == 0 {
cfg.GestureGain = 1
}
if cfg.ColorGain == 0 {
cfg.ColorGain = 4
}
if cfg.ADCIntegrationCycles == 0 {
cfg.ADCIntegrationCycles = 4
}
if cfg.threshold == 0 {
d.gesture.threshold = 30
}
if cfg.sensitivity == 0 {
d.gesture.sensitivity = 20
}
d.SetProximityPulse(cfg.ProximityPulseLength, cfg.ProximityPulseCount)
d.SetGesturePulse(cfg.GesturePulseLength, cfg.GesturePulseCount)
d.SetGains(cfg.ProximityGain, cfg.GestureGain, cfg.ColorGain)
d.SetADCIntegrationCycles(cfg.ADCIntegrationCycles)
if cfg.LEDBoost > 0 {
d.LEDBoost(cfg.LEDBoost)
}
}
func (d *Device) enable(cfg enableConfig) {
var gen, pien, aien, wen, pen, aen, pon uint8
+4 -6
View File
@@ -5,10 +5,8 @@ package apds9960
import "tinygo.org/x/drivers"
// New creates a new APDS-9960 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: ADPS9960_ADDRESS, mode: MODE_NONE}
// Configure sets up the APDS-9960 device.
func (d *Device) Configure(cfg Configuration) {
// configure device
d.configureDevice(cfg)
}
+12 -17
View File
@@ -6,24 +6,19 @@ package apds9960
import (
"machine"
"time"
"tinygo.org/x/drivers"
)
// New creates a new APDS-9960 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00)
// and wait a moment.
ENV := machine.P0_22
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput})
ENV.High()
R := machine.P1_00
R.Configure(machine.PinConfig{Mode: machine.PinOutput})
R.High()
time.Sleep(time.Millisecond * 10)
// Configure sets up the APDS-9960 device.
func (d *Device) Configure(cfg Configuration) {
return Device{bus: bus, Address: ADPS9960_ADDRESS, mode: MODE_NONE}
// Following lines are Nano 33 BLE specific, they have nothing to do with sensor per se
machine.LSM_PWR.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.LSM_PWR.High()
machine.I2C_PULLUP.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.I2C_PULLUP.High()
// Wait a moment
time.Sleep(10 * time.Millisecond)
// configure device
d.configureDevice(cfg)
}
+5 -4
View File
@@ -9,21 +9,22 @@ import (
func main() {
// use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
SCL: machine.SCL1_PIN,
SDA: machine.SDA1_PIN,
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := apds9960.New(machine.I2C1)
sensor.Configure(apds9960.Configuration{}) // use default settings
if !sensor.Connected() {
println("APDS-9960 not connected!")
return
}
sensor.Configure(apds9960.Configuration{}) // use default settings
sensor.EnableColor() // enable color engine
for {
+6 -5
View File
@@ -9,21 +9,22 @@ import (
func main() {
// use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
SCL: machine.SCL1_PIN,
SDA: machine.SDA1_PIN,
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := apds9960.New(machine.I2C1)
sensor.Configure(apds9960.Configuration{}) // use default settings
if !sensor.Connected() {
println("APDS-9960 not connected!")
return
}
sensor.Configure(apds9960.Configuration{}) // use default settings
sensor.EnableGesture() // enable gesture engine
for {
@@ -34,7 +35,7 @@ func main() {
gesture := sensor.ReadGesture()
print("Detected gesture: ")
switch gesture {
case apds9960.GESTURE_UP:
case apds9960.GESTURE_UP: // the nRF52 chip is "up"
println("Up")
case apds9960.GESTURE_DOWN:
println("Down")
+6 -4
View File
@@ -9,21 +9,23 @@ import (
func main() {
// use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
SCL: machine.SCL1_PIN,
SDA: machine.SDA1_PIN,
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := apds9960.New(machine.I2C1)
// use default settings
sensor.Configure(apds9960.Configuration{})
if !sensor.Connected() {
println("APDS-9960 not connected!")
return
}
sensor.Configure(apds9960.Configuration{}) // use default settings
sensor.EnableProximity() // enable proximity engine
for {
+5 -6
View File
@@ -9,28 +9,27 @@ import (
func main() {
// use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
SCL: machine.SCL1_PIN,
SDA: machine.SDA1_PIN,
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := hts221.New(machine.I2C1)
sensor.Configure() // power on and calibrate
if !sensor.Connected() {
println("HTS221 not connected!")
return
}
sensor.Configure() // power on and calibrate
for {
h, _ := sensor.ReadHumidity()
t, _ := sensor.ReadTemperature()
println("h =", float32(h)/100.0, "% / t =", float32(t)/1000.0, "*C")
time.Sleep(time.Second)
}
}
+4 -6
View File
@@ -9,29 +9,27 @@ import (
func main() {
// use Nano 33 BLE Sense's internal I2C bus
machine.I2C1.Configure(machine.I2CConfig{
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
SCL: machine.SCL1_PIN,
SDA: machine.SDA1_PIN,
Frequency: machine.TWI_FREQ_400KHZ,
})
sensor := lps22hb.New(machine.I2C1)
sensor.Configure()
if !sensor.Connected() {
println("LPS22HB not connected!")
return
}
sensor.Configure()
for {
p, _ := sensor.ReadPressure()
t, _ := sensor.ReadTemperature()
println("p =", float32(p)/1000.0, "hPa / t =", float32(t)/1000.0, "*C")
time.Sleep(time.Second)
// note: the device would power down itself after each query
}
}
+8 -8
View File
@@ -21,6 +21,14 @@ type Device struct {
temperatureZero float32
}
// New creates a new HTS221 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: HTS221_ADDRESS}
}
// Connected returns whether HTS221 has been found.
// It does a "who am I" request and checks the response.
func (d *Device) Connected() bool {
@@ -29,14 +37,6 @@ func (d *Device) Connected() bool {
return data[0] == 0xBC
}
// Configure sets up the HTS221 device for communication.
func (d *Device) Configure() {
// read calibration data
d.calibration()
// activate device and use block data update mode
d.Power(true)
}
// Power is for turn on/off the HTS221 device
func (d *Device) Power(status bool) {
data := []byte{0}
+6 -6
View File
@@ -5,10 +5,10 @@ package hts221
import "tinygo.org/x/drivers"
// New creates a new HTS221 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: HTS221_ADDRESS}
// Configure sets up the HTS221 device for communication.
func (d *Device) Configure() {
// read calibration data
d.calibration()
// activate device and use block data update mode
d.Power(true)
}
+13 -17
View File
@@ -6,24 +6,20 @@ package hts221
import (
"machine"
"time"
"tinygo.org/x/drivers"
)
// New creates a new HTS221 connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00)
// and wait a moment.
ENV := machine.P0_22
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput})
ENV.High()
R := machine.P1_00
R.Configure(machine.PinConfig{Mode: machine.PinOutput})
R.High()
time.Sleep(time.Millisecond * 10)
// Configure sets up the HTS221 device for communication.
func (d *Device) Configure() {
// Following lines are Nano 33 BLE specific, they have nothing to do with sensor per se
machine.HTS_PWR.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.HTS_PWR.High()
machine.I2C_PULLUP.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.I2C_PULLUP.High()
// Wait a moment
time.Sleep(10 * time.Millisecond)
return Device{bus: bus, Address: HTS221_ADDRESS}
// read calibration data
d.calibration()
// activate device and use block data update mode
d.Power(true)
}
+14 -12
View File
@@ -14,18 +14,12 @@ type Device struct {
Address uint8
}
// Connected returns whether LPS22HB has been found.
// 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)
return data[0] == 0xB1
}
// 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})
// New creates a new LPS22HB connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: LPS22HB_ADDRESS}
}
// ReadPressure returns the pressure in milli pascals (mPa).
@@ -42,6 +36,14 @@ func (d *Device) ReadPressure() (pressure int32, err error) {
return int32(pValue * 1000), nil
}
// Connected returns whether LPS22HB has been found.
// 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)
return data[0] == 0xB1
}
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *Device) ReadTemperature() (temperature int32, err error) {
d.waitForOneShot()
+4 -6
View File
@@ -5,10 +5,8 @@ package lps22hb
import "tinygo.org/x/drivers"
// New creates a new LPS22HB connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: LPS22HB_ADDRESS}
// 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})
}
+11 -17
View File
@@ -6,24 +6,18 @@ package lps22hb
import (
"machine"
"time"
"tinygo.org/x/drivers"
)
// New creates a new LPS22HB connection. The I2C bus must already be
// configured.
//
// This function only creates the Device object, it does not touch the device.
func New(bus drivers.I2C) Device {
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00)
// and wait a moment.
ENV := machine.P0_22
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput})
ENV.High()
R := machine.P1_00
R.Configure(machine.PinConfig{Mode: machine.PinOutput})
R.High()
time.Sleep(time.Millisecond * 10)
// Configure sets up the LPS22HB device for communication.
func (d *Device) Configure() {
// Following lines are Nano 33 BLE specific, they have nothing to do with sensor per se
machine.LSP_PWR.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.LSP_PWR.High()
machine.I2C_PULLUP.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.I2C_PULLUP.High()
// Wait a moment
time.Sleep(10 * time.Millisecond)
return Device{bus: bus, Address: LPS22HB_ADDRESS}
// set to block update mode
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
}