address code review comments

pull/82/head
George Sexton 1 year ago
parent 840a355281
commit d9959d66e0

@ -1,12 +1,12 @@
// Copyright 2024 The Periph Authors. All rights reserved. // Copyright 2024 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0 // Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
//
// This package provides a driver for the AOSONG AM2320 Temperature/Humidity // This package provides a driver for the AOSONG AM2320 Temperature/Humidity
// Sensor. This sensor is a basic, inexpensive i2c sensor with reasonably good // Sensor. This sensor is a basic, inexpensive i2c sensor with reasonably good
// accuracy for both temperature and humidity. // accuracy for both temperature and humidity.
// //
// The datasheet is located at: // # Datasheet
// //
// https://cdn-shop.adafruit.com/product-files/3721/AM2320.pdf // https://cdn-shop.adafruit.com/product-files/3721/AM2320.pdf
package am2320 package am2320
@ -74,28 +74,26 @@ func checkCRC(bytes []byte) bool {
// readCommand provides the logic of communicating with the sensor. According // readCommand provides the logic of communicating with the sensor. According
// to the datasheet, it tries to stay in low-power as much as possible to // to the datasheet, it tries to stay in low-power as much as possible to
// avoid self-heating the sensors. This makes finicky to talk to. On success, // avoid self-heating the sensors. This makes it finicky to talk to. On success,
// returns a slice of registerCount bytes starting from registerAddress. // returns a slice of registerCount bytes starting from registerAddress.
func (dev *Dev) readCommand(registerAddress, registerCount byte) ([]byte, error) { func (dev *Dev) readCommand(registerAddress, registerCount byte) ([]byte, error) {
// Send a wake-up call to the device. // Send a wake-up call to the device.
var err error
for range 5 { for range 5 {
err := dev.d.Tx([]byte{0}, nil) err = dev.d.Tx([]byte{0}, nil)
if err == nil { if err == nil {
break break
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
} }
w := make([]byte, 3) w := []byte{0x3, registerAddress, registerCount}
w[0] = 0x3 // Read Operation
w[1] = registerAddress
w[2] = registerCount
// The read return format is: // The read return format is:
// //
// {operation,registerCount,requested registers...,crc low, crc high} // {operation,registerCount,requested registers...,crc low, crc high}
r := make([]byte, registerCount+4) r := make([]byte, registerCount+4)
for range 10 { for range 10 {
err := dev.d.Tx(w, r) err = dev.d.Tx(w, r)
if err == nil && if err == nil &&
w[0] == r[0] && w[2] == r[1] && w[0] == r[0] && w[2] == r[1] &&
checkCRC(r) { checkCRC(r) {
@ -104,8 +102,10 @@ func (dev *Dev) readCommand(registerAddress, registerCount byte) ([]byte, error)
} }
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
} }
if err == nil {
return nil, errors.New("am2320: error sending read command") err = errors.New("invalid return values or crc from sensor")
}
return nil, fmt.Errorf("am2320 error sending read command: %w", err)
} }
// Sense queries the sensor for the current temperature and humidity. Note that // Sense queries the sensor for the current temperature and humidity. Note that

Loading…
Cancel
Save