Skip to content

Commit

Permalink
tlv493d: Auto-reset TLV493D on error (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezineb5 authored Oct 1, 2022
1 parent a6bfa87 commit 43cd795
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tlv493d/tlv493d.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tlv493d
import (
"errors"
"fmt"
"log"
"sync"
"time"

Expand Down Expand Up @@ -185,7 +186,7 @@ func New(i i2c.Bus, opts *Opts) (*Dev, error) {
temperatureOffsetCompensation: opts.TemperatureOffsetCompensation,
registersBuffer: make([]byte, numberOfReadRegisters),
}
if err := d.initialize(opts); err != nil {
if err := d.initialize(opts.Reset); err != nil {
return nil, err
}
return d, nil
Expand All @@ -204,7 +205,7 @@ func (d *Dev) Halt() error {
return d.SetMode(PowerDownMode)
}

func (d *Dev) initialize(opts *Opts) error {
func (d *Dev) initialize(reset bool) error {
d.mu.Lock()
defer d.mu.Unlock()

Expand All @@ -215,7 +216,7 @@ func (d *Dev) initialize(opts *Opts) error {
return err
}

if opts.Reset {
if reset {
// Reset I2C address
var resetAddress byte = 0x00
if d.i2c.Addr == I2CAddr1 {
Expand Down Expand Up @@ -403,7 +404,14 @@ func (d *Dev) ReadContinuous(frequency physic.Frequency, precision Precision) (<
case <-t.C:
value, err := d.Read(precision)
if err != nil {
// In continuous mode, we'll ignore errors silently.
// Try resetting the sensor to recover from errors
if err := d.initialize(true); err == nil {
if err := d.SetMode(newMode); err != nil {
log.Println("Unable to reset TLV493D mode:", err)
} else {
log.Println("Sensor reset successfully")
}
}
continue
}
reading <- value
Expand Down

0 comments on commit 43cd795

Please sign in to comment.