mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 04:43:25 +00:00
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:
+53
-46
@@ -57,6 +57,14 @@ type enableConfig struct {
|
|||||||
PON bool
|
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.
|
// Connected returns whether APDS-9960 has been found.
|
||||||
// It does a "who am I" request and checks the response.
|
// It does a "who am I" request and checks the response.
|
||||||
func (d *Device) Connected() bool {
|
func (d *Device) Connected() bool {
|
||||||
@@ -65,52 +73,6 @@ func (d *Device) Connected() bool {
|
|||||||
return data[0] == 0xAB
|
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
|
// GetMode returns current engine mode
|
||||||
func (d *Device) GetMode() uint8 {
|
func (d *Device) GetMode() uint8 {
|
||||||
return d.mode
|
return d.mode
|
||||||
@@ -353,6 +315,51 @@ func (d *Device) ReadGesture() (gesture int32) {
|
|||||||
|
|
||||||
// private functions
|
// 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) {
|
func (d *Device) enable(cfg enableConfig) {
|
||||||
var gen, pien, aien, wen, pen, aen, pon uint8
|
var gen, pien, aien, wen, pen, aen, pon uint8
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ package apds9960
|
|||||||
|
|
||||||
import "tinygo.org/x/drivers"
|
import "tinygo.org/x/drivers"
|
||||||
|
|
||||||
// New creates a new APDS-9960 connection. The I2C bus must already be
|
// Configure sets up the APDS-9960 device.
|
||||||
// configured.
|
func (d *Device) Configure(cfg Configuration) {
|
||||||
//
|
// configure device
|
||||||
// This function only creates the Device object, it does not touch the device.
|
d.configureDevice(cfg)
|
||||||
func New(bus drivers.I2C) Device {
|
|
||||||
return Device{bus: bus, Address: ADPS9960_ADDRESS, mode: MODE_NONE}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,19 @@ package apds9960
|
|||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new APDS-9960 connection. The I2C bus must already be
|
// Configure sets up the APDS-9960 device.
|
||||||
// configured.
|
func (d *Device) Configure(cfg Configuration) {
|
||||||
//
|
|
||||||
// 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)
|
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,21 +9,22 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// use Nano 33 BLE Sense's internal I2C bus
|
||||||
machine.I2C1.Configure(machine.I2CConfig{
|
machine.I2C1.Configure(machine.I2CConfig{
|
||||||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
|
SCL: machine.SCL1_PIN,
|
||||||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
|
SDA: machine.SDA1_PIN,
|
||||||
Frequency: machine.TWI_FREQ_400KHZ,
|
Frequency: machine.TWI_FREQ_400KHZ,
|
||||||
})
|
})
|
||||||
|
|
||||||
sensor := apds9960.New(machine.I2C1)
|
sensor := apds9960.New(machine.I2C1)
|
||||||
|
|
||||||
|
sensor.Configure(apds9960.Configuration{}) // use default settings
|
||||||
|
|
||||||
if !sensor.Connected() {
|
if !sensor.Connected() {
|
||||||
println("APDS-9960 not connected!")
|
println("APDS-9960 not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor.Configure(apds9960.Configuration{}) // use default settings
|
|
||||||
|
|
||||||
sensor.EnableColor() // enable color engine
|
sensor.EnableColor() // enable color engine
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -9,21 +9,22 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// use Nano 33 BLE Sense's internal I2C bus
|
||||||
machine.I2C1.Configure(machine.I2CConfig{
|
machine.I2C1.Configure(machine.I2CConfig{
|
||||||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
|
SCL: machine.SCL1_PIN,
|
||||||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
|
SDA: machine.SDA1_PIN,
|
||||||
Frequency: machine.TWI_FREQ_400KHZ,
|
Frequency: machine.TWI_FREQ_400KHZ,
|
||||||
})
|
})
|
||||||
|
|
||||||
sensor := apds9960.New(machine.I2C1)
|
sensor := apds9960.New(machine.I2C1)
|
||||||
|
|
||||||
|
sensor.Configure(apds9960.Configuration{}) // use default settings
|
||||||
|
|
||||||
if !sensor.Connected() {
|
if !sensor.Connected() {
|
||||||
println("APDS-9960 not connected!")
|
println("APDS-9960 not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor.Configure(apds9960.Configuration{}) // use default settings
|
|
||||||
|
|
||||||
sensor.EnableGesture() // enable gesture engine
|
sensor.EnableGesture() // enable gesture engine
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -34,7 +35,7 @@ func main() {
|
|||||||
gesture := sensor.ReadGesture()
|
gesture := sensor.ReadGesture()
|
||||||
print("Detected gesture: ")
|
print("Detected gesture: ")
|
||||||
switch gesture {
|
switch gesture {
|
||||||
case apds9960.GESTURE_UP:
|
case apds9960.GESTURE_UP: // the nRF52 chip is "up"
|
||||||
println("Up")
|
println("Up")
|
||||||
case apds9960.GESTURE_DOWN:
|
case apds9960.GESTURE_DOWN:
|
||||||
println("Down")
|
println("Down")
|
||||||
|
|||||||
@@ -9,21 +9,23 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// use Nano 33 BLE Sense's internal I2C bus
|
||||||
machine.I2C1.Configure(machine.I2CConfig{
|
machine.I2C1.Configure(machine.I2CConfig{
|
||||||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
|
SCL: machine.SCL1_PIN,
|
||||||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
|
SDA: machine.SDA1_PIN,
|
||||||
Frequency: machine.TWI_FREQ_400KHZ,
|
Frequency: machine.TWI_FREQ_400KHZ,
|
||||||
})
|
})
|
||||||
|
|
||||||
sensor := apds9960.New(machine.I2C1)
|
sensor := apds9960.New(machine.I2C1)
|
||||||
|
|
||||||
|
// use default settings
|
||||||
|
sensor.Configure(apds9960.Configuration{})
|
||||||
|
|
||||||
if !sensor.Connected() {
|
if !sensor.Connected() {
|
||||||
println("APDS-9960 not connected!")
|
println("APDS-9960 not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor.Configure(apds9960.Configuration{}) // use default settings
|
|
||||||
|
|
||||||
sensor.EnableProximity() // enable proximity engine
|
sensor.EnableProximity() // enable proximity engine
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -9,28 +9,27 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// use Nano 33 BLE Sense's internal I2C bus
|
||||||
machine.I2C1.Configure(machine.I2CConfig{
|
machine.I2C1.Configure(machine.I2CConfig{
|
||||||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
|
SCL: machine.SCL1_PIN,
|
||||||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
|
SDA: machine.SDA1_PIN,
|
||||||
Frequency: machine.TWI_FREQ_400KHZ,
|
Frequency: machine.TWI_FREQ_400KHZ,
|
||||||
})
|
})
|
||||||
|
|
||||||
sensor := hts221.New(machine.I2C1)
|
sensor := hts221.New(machine.I2C1)
|
||||||
|
|
||||||
|
sensor.Configure() // power on and calibrate
|
||||||
|
|
||||||
if !sensor.Connected() {
|
if !sensor.Connected() {
|
||||||
println("HTS221 not connected!")
|
println("HTS221 not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor.Configure() // power on and calibrate
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
h, _ := sensor.ReadHumidity()
|
h, _ := sensor.ReadHumidity()
|
||||||
t, _ := sensor.ReadTemperature()
|
t, _ := sensor.ReadTemperature()
|
||||||
println("h =", float32(h)/100.0, "% / t =", float32(t)/1000.0, "*C")
|
println("h =", float32(h)/100.0, "% / t =", float32(t)/1000.0, "*C")
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,29 +9,27 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
// use Nano 33 BLE Sense's internal I2C bus
|
||||||
machine.I2C1.Configure(machine.I2CConfig{
|
machine.I2C1.Configure(machine.I2CConfig{
|
||||||
SCL: machine.P0_15, // SCL1 on Nano 33 BLE Sense
|
SCL: machine.SCL1_PIN,
|
||||||
SDA: machine.P0_14, // SDA1 on Nano 33 BLE Sense
|
SDA: machine.SDA1_PIN,
|
||||||
Frequency: machine.TWI_FREQ_400KHZ,
|
Frequency: machine.TWI_FREQ_400KHZ,
|
||||||
})
|
})
|
||||||
|
|
||||||
sensor := lps22hb.New(machine.I2C1)
|
sensor := lps22hb.New(machine.I2C1)
|
||||||
|
sensor.Configure()
|
||||||
|
|
||||||
if !sensor.Connected() {
|
if !sensor.Connected() {
|
||||||
println("LPS22HB not connected!")
|
println("LPS22HB not connected!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor.Configure()
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
p, _ := sensor.ReadPressure()
|
p, _ := sensor.ReadPressure()
|
||||||
t, _ := sensor.ReadTemperature()
|
t, _ := sensor.ReadTemperature()
|
||||||
println("p =", float32(p)/1000.0, "hPa / t =", float32(t)/1000.0, "*C")
|
println("p =", float32(p)/1000.0, "hPa / t =", float32(t)/1000.0, "*C")
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
// note: the device would power down itself after each query
|
// note: the device would power down itself after each query
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -21,6 +21,14 @@ type Device struct {
|
|||||||
temperatureZero float32
|
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.
|
// Connected returns whether HTS221 has been found.
|
||||||
// It does a "who am I" request and checks the response.
|
// It does a "who am I" request and checks the response.
|
||||||
func (d *Device) Connected() bool {
|
func (d *Device) Connected() bool {
|
||||||
@@ -29,14 +37,6 @@ func (d *Device) Connected() bool {
|
|||||||
return data[0] == 0xBC
|
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
|
// Power is for turn on/off the HTS221 device
|
||||||
func (d *Device) Power(status bool) {
|
func (d *Device) Power(status bool) {
|
||||||
data := []byte{0}
|
data := []byte{0}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ package hts221
|
|||||||
|
|
||||||
import "tinygo.org/x/drivers"
|
import "tinygo.org/x/drivers"
|
||||||
|
|
||||||
// New creates a new HTS221 connection. The I2C bus must already be
|
// Configure sets up the HTS221 device for communication.
|
||||||
// configured.
|
func (d *Device) Configure() {
|
||||||
//
|
// read calibration data
|
||||||
// This function only creates the Device object, it does not touch the device.
|
d.calibration()
|
||||||
func New(bus drivers.I2C) Device {
|
// activate device and use block data update mode
|
||||||
return Device{bus: bus, Address: HTS221_ADDRESS}
|
d.Power(true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,20 @@ package hts221
|
|||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new HTS221 connection. The I2C bus must already be
|
// Configure sets up the HTS221 device for communication.
|
||||||
// configured.
|
func (d *Device) Configure() {
|
||||||
//
|
// Following lines are Nano 33 BLE specific, they have nothing to do with sensor per se
|
||||||
// This function only creates the Device object, it does not touch the device.
|
machine.HTS_PWR.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
func New(bus drivers.I2C) Device {
|
machine.HTS_PWR.High()
|
||||||
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00)
|
machine.I2C_PULLUP.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
// and wait a moment.
|
machine.I2C_PULLUP.High()
|
||||||
ENV := machine.P0_22
|
// Wait a moment
|
||||||
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
time.Sleep(10 * time.Millisecond)
|
||||||
ENV.High()
|
|
||||||
R := machine.P1_00
|
|
||||||
R.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
R.High()
|
|
||||||
time.Sleep(time.Millisecond * 10)
|
|
||||||
|
|
||||||
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
@@ -14,18 +14,12 @@ type Device struct {
|
|||||||
Address uint8
|
Address uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connected returns whether LPS22HB has been found.
|
// New creates a new LPS22HB connection. The I2C bus must already be
|
||||||
// It does a "who am I" request and checks the response.
|
// configured.
|
||||||
func (d *Device) Connected() bool {
|
//
|
||||||
data := []byte{0}
|
// This function only creates the Device object, it does not touch the device.
|
||||||
d.bus.ReadRegister(d.Address, LPS22HB_WHO_AM_I_REG, data)
|
func New(bus drivers.I2C) Device {
|
||||||
return data[0] == 0xB1
|
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})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadPressure returns the pressure in milli pascals (mPa).
|
// 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
|
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).
|
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
|
||||||
func (d *Device) ReadTemperature() (temperature int32, err error) {
|
func (d *Device) ReadTemperature() (temperature int32, err error) {
|
||||||
d.waitForOneShot()
|
d.waitForOneShot()
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ package lps22hb
|
|||||||
|
|
||||||
import "tinygo.org/x/drivers"
|
import "tinygo.org/x/drivers"
|
||||||
|
|
||||||
// New creates a new LPS22HB connection. The I2C bus must already be
|
// Configure sets up the LPS22HB device for communication.
|
||||||
// configured.
|
func (d *Device) Configure() {
|
||||||
//
|
// set to block update mode
|
||||||
// This function only creates the Device object, it does not touch the device.
|
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||||
func New(bus drivers.I2C) Device {
|
|
||||||
return Device{bus: bus, Address: LPS22HB_ADDRESS}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,18 @@ package lps22hb
|
|||||||
import (
|
import (
|
||||||
"machine"
|
"machine"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tinygo.org/x/drivers"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new LPS22HB connection. The I2C bus must already be
|
// Configure sets up the LPS22HB device for communication.
|
||||||
// configured.
|
func (d *Device) Configure() {
|
||||||
//
|
// Following lines are Nano 33 BLE specific, they have nothing to do with sensor per se
|
||||||
// This function only creates the Device object, it does not touch the device.
|
machine.LSP_PWR.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
func New(bus drivers.I2C) Device {
|
machine.LSP_PWR.High()
|
||||||
// turn on internal power pin (machine.P0_22) and I2C1 pullups power pin (machine.P1_00)
|
machine.I2C_PULLUP.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
// and wait a moment.
|
machine.I2C_PULLUP.High()
|
||||||
ENV := machine.P0_22
|
// Wait a moment
|
||||||
ENV.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
time.Sleep(10 * time.Millisecond)
|
||||||
ENV.High()
|
|
||||||
R := machine.P1_00
|
|
||||||
R.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
|
||||||
R.High()
|
|
||||||
time.Sleep(time.Millisecond * 10)
|
|
||||||
|
|
||||||
return Device{bus: bus, Address: LPS22HB_ADDRESS}
|
// set to block update mode
|
||||||
|
d.bus.WriteRegister(d.Address, LPS22HB_CTRL1_REG, []byte{0x02})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user