formatting errors

pull/68/head
Wilke, Louis 2 years ago
parent 36bf6f98cf
commit 6ecf4e1ab5

@ -102,10 +102,15 @@ func (d *Dev) Sense(e *physic.Env) error {
// read measurement
if err := d.d.Tx(nil, data); err != nil {
return err
} else if d.opts.ValidateData && calculateCRC8(data[0:6]) != data[6] {
return &DataCorruptionError{}
}
// validate data
dataCrc := calculateCRC8(data[0:6])
if d.opts.ValidateData && dataCrc != data[6] {
return &DataCorruptionError{Received: data[6], Calculated: dataCrc}
}
// check if measurement is ready
if data[0]&bitInitialized == 0 {
return &NotInitializedError{}
} else if data[0]&bitBusy == 0 {
@ -122,7 +127,7 @@ func (d *Dev) Sense(e *physic.Env) error {
time.Sleep(d.opts.MeasurementWaitInterval) // wait until measurement is ready
}
return &ReadTimeoutError{}
return &ReadTimeoutError{Timeout: d.opts.MeasurementReadTimeout}
}
// SenseContinuous implements physic.SenseEnv. It returns a channel that will

@ -1,5 +1,10 @@
package aht20
import (
"fmt"
"time"
)
// NotInitializedError is returned when the sensor is not initialized but a measurement is requested.
type NotInitializedError struct{}
@ -8,15 +13,23 @@ func (e *NotInitializedError) Error() string {
}
// ReadTimeoutError is returned when the sensor does not finish a measurement in time.
type ReadTimeoutError struct{}
type ReadTimeoutError struct {
// Timeout is the configured timeout.
Timeout time.Duration
}
func (e *ReadTimeoutError) Error() string {
return "Read timeout. AHT20 did not finish measurement in time."
return fmt.Sprintf("Read timeout after %s. AHT20 did not finish measurement in time.", e.Timeout)
}
// DataCorruptionError is returned when the data from the sensor does not match the CRC8 hash.
type DataCorruptionError struct{}
type DataCorruptionError struct {
// Calculated is the calculated CRC8 hash using the received data bytes.
Calculated uint8
// Received is the CRC8 hash received from the sensor.
Received uint8
}
func (e *DataCorruptionError) Error() string {
return "Data is corrupt. The CRC8 hashes did not match."
return fmt.Sprintf("Data is corrupt. The CRC8 hashes did not match. Calculated: 0x%X, Received: 0x%X", e.Calculated, e.Received)
}

Loading…
Cancel
Save