2014-10-30 08:51:47 +08:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const I2CSlave = 0x0703
|
|
|
|
|
2014-10-31 07:22:25 +08:00
|
|
|
// NewI2cDevice creates a new io.ReadWriteCloser with the proper ioctrl given an i2c bus location and device address
|
2014-10-30 08:51:47 +08:00
|
|
|
func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
|
2014-11-08 08:21:39 +08:00
|
|
|
file, err := OpenFile(location, os.O_RDWR, os.ModeExclusive)
|
2014-10-30 08:51:47 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-08 08:21:39 +08:00
|
|
|
|
2014-11-08 08:56:13 +08:00
|
|
|
_, _, errno := Syscall(
|
|
|
|
syscall.SYS_IOCTL,
|
|
|
|
file.Fd(),
|
|
|
|
I2CSlave,
|
|
|
|
uintptr(address),
|
|
|
|
)
|
2014-10-30 08:51:47 +08:00
|
|
|
|
|
|
|
if errno != 0 {
|
|
|
|
return nil, errors.New(fmt.Sprintf("Failed with syscall.Errno %v", errno))
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|