kopia lustrzana https://github.com/cyoung/stratux
cleanup retry connection and error handeling
rodzic
ee439340a9
commit
379b56403b
|
@ -31,8 +31,8 @@ const (
|
|||
MPUREG_WHO_AM_I_VAL_60X0 = 0x68 // Expected value for MPU6000 and MPU6050 (and MPU9150)
|
||||
MPUREG_WHO_AM_I_VAL_UNKNOWN = 0x75 // Unknown MPU found on recent batch of gy91 boards see discussion 182
|
||||
ICMREG_WHO_AM_I = 0x00
|
||||
ICMREG_WHO_AM_I_VAL = 0xEA // Expected value.
|
||||
PRESSURE_WHO_AM_I = 0x77 // Expected value for bosch pressure sensors bmpXXX.
|
||||
ICMREG_WHO_AM_I_VAL = 0xEA // Expected value.
|
||||
PRESSURE_WHO_AM_I = bmp388.Address // Expected address for bosch pressure sensors bmpXXX.
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -88,8 +88,12 @@ func initPressureSensor() (ok bool) {
|
|||
return false
|
||||
}
|
||||
if v == bmp388.ChipId {
|
||||
myPressureReader = sensors.NewBMP388(&i2cbus)
|
||||
return true
|
||||
log.Printf("BMP-388 detected")
|
||||
bmp, err := sensors.NewBMP388(&i2cbus)
|
||||
if err == nil {
|
||||
myPressureReader = bmp
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
bmp, err := sensors.NewBMP280(&i2cbus, 100*time.Millisecond)
|
||||
if err == nil {
|
||||
|
|
|
@ -13,13 +13,24 @@ type BMP388 struct {
|
|||
running bool
|
||||
}
|
||||
|
||||
func NewBMP388(i2cbus *embd.I2CBus) *BMP388 {
|
||||
func NewBMP388(i2cbus *embd.I2CBus) (*BMP388, error) {
|
||||
|
||||
bmp := bmp388.BMP388{Address: bmp388.Address, Config: bmp388.Config{}, Bus: i2cbus}
|
||||
newbmp := BMP388{
|
||||
sensor: &bmp}
|
||||
go newbmp.run()
|
||||
return &newbmp
|
||||
bmp := bmp388.BMP388{Address: bmp388.Address, Config: bmp388.Config{}, Bus: i2cbus} //new sensor
|
||||
// retry to connect until sensor connected
|
||||
var connected bool
|
||||
for n := 0; n < 5; n++ {
|
||||
if bmp.Connected() {
|
||||
connected = true
|
||||
} else {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}
|
||||
if !connected {
|
||||
return nil, bmp388.ErrNotConnected
|
||||
}
|
||||
newBmp := BMP388{sensor: &bmp}
|
||||
go newBmp.run()
|
||||
return &newBmp, nil
|
||||
}
|
||||
func (bmp *BMP388) run() {
|
||||
bmp.running = true
|
||||
|
@ -35,24 +46,24 @@ func (bmp *BMP388) run() {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *BMP388) Close() {
|
||||
d.running = false
|
||||
d.sensor.Config.Mode = bmp388.Sleep
|
||||
_ = d.sensor.Configure(d.sensor.Config)
|
||||
func (bmp *BMP388) Close() {
|
||||
bmp.running = false
|
||||
bmp.sensor.Config.Mode = bmp388.Sleep
|
||||
_ = bmp.sensor.Configure(bmp.sensor.Config)
|
||||
}
|
||||
|
||||
// Temperature returns the current temperature in degrees C measured by the BMP280
|
||||
func (d *BMP388) Temperature() (float64, error) {
|
||||
if !d.running {
|
||||
func (bmp *BMP388) Temperature() (float64, error) {
|
||||
if !bmp.running {
|
||||
return 0, bmp388.ErrNotConnected
|
||||
}
|
||||
|
||||
return d.temperature, nil
|
||||
return bmp.temperature, nil
|
||||
}
|
||||
|
||||
func (d *BMP388) Pressure() (float64, error) {
|
||||
if !d.running {
|
||||
func (bmp *BMP388) Pressure() (float64, error) {
|
||||
if !bmp.running {
|
||||
return 0, bmp388.ErrNotConnected
|
||||
}
|
||||
return d.pressure, nil
|
||||
return bmp.pressure, nil
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue