From 840a355281210c1bf798d355073f48f9f5b3001c Mon Sep 17 00:00:00 2001 From: George Sexton Date: Sat, 23 Nov 2024 16:07:24 -0700 Subject: [PATCH] AM2320 Temp/Humidity Sensor - Initial Add --- am2320/am2320.go | 2 +- am2320/am2320_test.go | 1 + am2320/example_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 am2320/example_test.go diff --git a/am2320/am2320.go b/am2320/am2320.go index 01bc5ba..f4d1b76 100644 --- a/am2320/am2320.go +++ b/am2320/am2320.go @@ -3,7 +3,7 @@ // that can be found in the LICENSE file. // // This package provides a driver for the AOSONG AM2320 Temperature/Humidity -// Sensor. This sensor is a basic, inexpensive sensor with reasonably good +// Sensor. This sensor is a basic, inexpensive i2c sensor with reasonably good // accuracy for both temperature and humidity. // // The datasheet is located at: diff --git a/am2320/am2320_test.go b/am2320/am2320_test.go index e07c13f..c5431dd 100644 --- a/am2320/am2320_test.go +++ b/am2320/am2320_test.go @@ -150,6 +150,7 @@ func TestSense(t *testing.T) { func TestSenseContinuous(t *testing.T) { readCount := 10 + // make 10 copies of the single reading playback data. pb := make([]i2ctest.IO, 0, len(pbSense)*10) for range readCount { pb = append(pb, pbSense...) diff --git a/am2320/example_test.go b/am2320/example_test.go new file mode 100644 index 0000000..abaa26c --- /dev/null +++ b/am2320/example_test.go @@ -0,0 +1,41 @@ +// Copyright 2024 The Periph Authors. All rights reserved. +// Use of this source code is governed under the Apache License, Version 2.0 +// that can be found in the LICENSE file. + +package am2320 + +import ( + "fmt" + "log" + "periph.io/x/conn/v3/i2c/i2creg" + "periph.io/x/conn/v3/physic" + "periph.io/x/host/v3" +) + +func Example() { + // Make sure periph is initialized. + if _, err := host.Init(); err != nil { + log.Fatal(err) + } + + // Open default I²C bus. + bus, err := i2creg.Open("") + if err != nil { + log.Fatalf("failed to open I²C: %v", err) + } + defer bus.Close() + + // Create the Sensor + sensor, err := NewI2C(bus, SensorAddress) + if err != nil { + log.Fatal(err) + } + + // Take a reading + env := physic.Env{} + err = sensor.Sense(&env) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Sensor Output: %s\n", env) +}