forked from periph/devices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ina219smoketest.go
79 lines (67 loc) · 1.7 KB
/
ina219smoketest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 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 ina219smoketest tests the ina219 device.
package ina219smoketest
import (
"errors"
"flag"
"fmt"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/conn/v3/physic"
"periph.io/x/devices/v3/ina219"
"periph.io/x/host/v3"
)
// SmokeTest is imported by periph-smoketest.
type SmokeTest struct {
}
// Name implements the SmokeTest interface.
func (s *SmokeTest) Name() string {
return "ina219"
}
// Description implements the SmokeTest interface.
func (s *SmokeTest) Description() string {
return "Tests INA219 over I²C"
}
// Run implements the SmokeTest interface.
func (s *SmokeTest) Run(f *flag.FlagSet, args []string) (err error) {
i2cID := f.String("i2c", "", "I²C bus to use")
i2cAddr := f.Int("ia", 0x40, "I²C bus address use: 0x40 to 0x4f")
if err = f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
}
fmt.Println("Starting INA219 Current Sensor\nctrl+c to exit")
if _, err = host.Init(); err != nil {
return err
}
// Open default i2c bus.
bus, err := i2creg.Open(*i2cID)
if err != nil {
return err
}
defer func() {
if err2 := bus.Close(); err == nil {
err = err2
}
}()
// Create a new power sensor a sense resistor of 100 mΩ.
config := &ina219.Opts{
Address: *i2cAddr,
SenseResistor: 100 * physic.MilliOhm,
MaxCurrent: 3200 * physic.MilliAmpere,
}
sensor, err := ina219.New(bus, config)
if err != nil {
return err
}
pm, err := sensor.Sense()
if err != nil {
return err
}
fmt.Println(pm)
return nil
}