2014-10-29 17:51:47 -07:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-12-31 06:12:25 -08:00
|
|
|
// I2CSlave is the linux default ioctrl request code
|
2014-10-29 17:51:47 -07:00
|
|
|
const I2CSlave = 0x0703
|
|
|
|
|
2014-12-31 06:12:25 -08:00
|
|
|
// NewI2cDevice returns an io.ReadWriteCloser with the proper ioctrl given
|
|
|
|
// an i2c bus location and device address
|
2014-10-29 17:51:47 -07:00
|
|
|
func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
|
2014-11-07 16:21:39 -08:00
|
|
|
file, err := OpenFile(location, os.O_RDWR, os.ModeExclusive)
|
2014-10-29 17:51:47 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-07 16:21:39 -08:00
|
|
|
|
2014-11-07 16:56:13 -08:00
|
|
|
_, _, errno := Syscall(
|
|
|
|
syscall.SYS_IOCTL,
|
|
|
|
file.Fd(),
|
|
|
|
I2CSlave,
|
|
|
|
uintptr(address),
|
|
|
|
)
|
2014-10-29 17:51:47 -07:00
|
|
|
|
|
|
|
if errno != 0 {
|
2014-12-31 06:12:25 -08:00
|
|
|
return nil, fmt.Errorf("Failed with syscall.Errno %v", errno)
|
2014-10-29 17:51:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|