-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
120 lines (110 loc) · 2.64 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 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.
// i2c-io communicates to an I²C device.
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strconv"
"periph.io/x/conn/v3/i2c"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/conn/v3/physic"
"periph.io/x/host/v3"
)
func mainImpl() error {
addr := flag.Int("a", -1, "I²C device address to query")
busName := flag.String("b", "", "I²C bus to use")
verbose := flag.Bool("v", false, "verbose mode")
// TODO(maruel): This is not generic enough.
write := flag.Bool("w", false, "write instead of reading")
reg := flag.Int("r", -1, "register to address")
var hz physic.Frequency
flag.Var(&hz, "hz", "I²C bus speed (may require root)")
l := flag.Int("l", 1, "length of data to read; ignored if -w is specified")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
if !*write && flag.NArg() != 0 {
return errors.New("unexpected argument, try -help")
}
if *addr < 0 || *addr >= 1<<9 {
return fmt.Errorf("-a is required and must be between 0 and %d", 1<<9-1)
}
if *reg < 0 || *reg > 255 {
return errors.New("-r must be between 0 and 255")
}
if *l <= 0 || *l > 255 {
return errors.New("-l must be between 1 and 255")
}
if _, err := host.Init(); err != nil {
return err
}
var buf []byte
if *write {
if flag.NArg() == 0 {
return errors.New("specify data to write as a list of hex encoded bytes")
}
buf = make([]byte, 1, flag.NArg()+1)
buf[0] = byte(*reg)
for _, a := range flag.Args() {
b, err := strconv.ParseUint(a, 0, 8)
if err != nil {
return err
}
buf = append(buf, byte(b))
}
} else {
if flag.NArg() != 0 {
return errors.New("do not specify bytes when reading")
}
buf = make([]byte, *l)
}
bus, err := i2creg.Open(*busName)
if err != nil {
return err
}
defer bus.Close()
if hz != 0 {
if err = bus.SetSpeed(hz); err != nil {
return err
}
}
if *verbose {
if p, ok := bus.(i2c.Pins); ok {
log.Printf("Using pins SCL: %s SDA: %s", p.SCL(), p.SDA())
}
}
d := i2c.Dev{Bus: bus, Addr: uint16(*addr)}
if *write {
_, err = d.Write(buf)
} else {
if err = d.Tx([]byte{byte(*reg)}, buf); err != nil {
return err
}
for i, b := range buf {
if i != 0 {
if _, err = fmt.Print(", "); err != nil {
break
}
}
if _, err = fmt.Printf("0x%02X", b); err != nil {
break
}
}
_, err = fmt.Print("\n")
}
return err
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "i2c-io: %s.\n", err)
os.Exit(1)
}
}