Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tlv493d: Auto-reset TLV493D on error #52

Merged
merged 4 commits into from
Oct 1, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions tlv493d/tlv493d.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,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 +204,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 +215,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 +403,15 @@ 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
fmt.Println("Error reading from TLV493D sensor:", err)
maruel marked this conversation as resolved.
Show resolved Hide resolved
if err := d.initialize(true); err == nil {
if err := d.SetMode(newMode); err != nil {
fmt.Println("Unable to reset TLV493D mode:", err)
} else {
fmt.Println("Sensor reset successfully")
}
}
continue
}
reading <- value
Expand Down