-
Notifications
You must be signed in to change notification settings - Fork 28
/
i2c.go
64 lines (55 loc) · 1.3 KB
/
i2c.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
// Package i2c provides low level control over the linux i2c bus.
//
// Before usage you should load the i2c-dev kenel module
//
// sudo modprobe i2c-dev
//
// Each i2c bus can address 127 independent i2c devices, and most
// linux systems contain several buses.
package i2c
import (
"fmt"
"os"
"syscall"
)
const (
i2c_SLAVE = 0x0703
)
// I2C represents a connection to an i2c device.
type I2C struct {
rc *os.File
}
// New opens a connection to an i2c device.
func New(addr uint8, bus int) (*I2C, error) {
f, err := os.OpenFile(fmt.Sprintf("/dev/i2c-%d", bus), os.O_RDWR, 0600)
if err != nil {
return nil, err
}
if err := ioctl(f.Fd(), i2c_SLAVE, uintptr(addr)); err != nil {
return nil, err
}
return &I2C{f}, nil
}
// Write sends buf to the remote i2c device. The interpretation of
// the message is implementation dependant.
func (i2c *I2C) Write(buf []byte) (int, error) {
return i2c.rc.Write(buf)
}
func (i2c *I2C) WriteByte(b byte) (int, error) {
var buf [1]byte
buf[0] = b
return i2c.rc.Write(buf[:])
}
func (i2c *I2C) Read(p []byte) (int, error) {
return i2c.rc.Read(p)
}
func (i2c *I2C) Close() error {
return i2c.rc.Close()
}
func ioctl(fd, cmd, arg uintptr) (err error) {
_, _, e1 := syscall.Syscall6(syscall.SYS_IOCTL, fd, cmd, arg, 0, 0, 0)
if e1 != 0 {
err = e1
}
return
}