From 6ecf4e1ab55d9fd0abf632c38b254ef31a4f4b0d Mon Sep 17 00:00:00 2001 From: "Wilke, Louis" Date: Mon, 15 Jan 2024 19:28:37 +0100 Subject: [PATCH] formatting errors --- aht20/aht20.go | 11 ++++++++--- aht20/error.go | 21 +++++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/aht20/aht20.go b/aht20/aht20.go index ddb00c4..ddd8b50 100644 --- a/aht20/aht20.go +++ b/aht20/aht20.go @@ -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 diff --git a/aht20/error.go b/aht20/error.go index b65221f..832033d 100644 --- a/aht20/error.go +++ b/aht20/error.go @@ -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) }