forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcp9808smoketest.go
76 lines (64 loc) · 1.59 KB
/
mcp9808smoketest.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
// 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 mcp9808smoketest
import (
"errors"
"flag"
"fmt"
"periph.io/x/periph/conn/i2c/i2creg"
"periph.io/x/periph/experimental/devices/mcp9808"
"periph.io/x/periph/host"
)
// SmokeTest is imported by periph-smoketest.
type SmokeTest struct {
}
// Name implements the SmokeTest interface.
func (s *SmokeTest) Name() string {
return "mcp9808"
}
// Description implements the SmokeTest interface.
func (s *SmokeTest) Description() string {
return "Tests MCP9808 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", 0x18, "I²C bus address use: 0x18 to 0x1f")
if err := f.Parse(args); err != nil {
return err
}
if f.NArg() != 0 {
f.Usage()
return errors.New("unrecognized arguments")
}
fmt.Println("Starting MCP9808 Temperature 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 temperature sensor a with maximum resolution.
config := mcp9808.Opts{
Addr: *i2cAddr,
Res: mcp9808.Maximum,
}
sensor, err := mcp9808.New(bus, &config)
if err != nil {
return err
}
t, err := sensor.SenseTemp()
if err != nil {
return err
}
fmt.Println(t)
return nil
}