1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/sysfs/i2c_device.go
2014-10-29 17:51:47 -07:00

32 lines
572 B
Go

package sysfs
import (
"errors"
"fmt"
"io"
"os"
"syscall"
)
const I2CSlave = 0x0703
/// NewI2cDevice creates a new i2c device given a device location and address
func NewI2cDevice(location string, address byte) (io.ReadWriteCloser, error) {
file, err := os.OpenFile(location, os.O_RDWR, os.ModeExclusive)
if err != nil {
return nil, err
}
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
file.Fd(),
I2CSlave, uintptr(address),
)
if errno != 0 {
return nil, errors.New(fmt.Sprintf("Failed with syscall.Errno %v", errno))
}
return file, nil
}