1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/i2c.go

52 lines
1.3 KiB
Go
Raw Normal View History

2014-03-03 00:51:23 +05:30
package embd
2013-12-07 23:11:06 +05:30
2014-03-03 00:51:23 +05:30
type I2CBus interface {
// ReadByte reads a byte from the given address.
2013-12-07 23:11:06 +05:30
ReadByte(addr byte) (value byte, err error)
// WriteByte writes a byte to the given address.
2013-12-07 23:11:06 +05:30
WriteByte(addr, value byte) error
// WriteBytes writes a slice bytes to the given address.
2013-12-07 23:11:06 +05:30
WriteBytes(addr byte, value []byte) error
// ReadFromReg reads n (len(value)) bytes from the given address and register.
ReadFromReg(addr, reg byte, value []byte) error
// ReadByteFromReg reads a byte from the given address and register.
2013-12-07 23:11:06 +05:30
ReadByteFromReg(addr, reg byte) (value byte, err error)
// ReadU16FromReg reads a unsigned 16 bit integer from the given address and register.
ReadWordFromReg(addr, reg byte) (value uint16, err error)
// WriteToReg writes len(value) bytes to the given address and register.
WriteToReg(addr, reg byte, value []byte) error
// WriteByteToReg writes a byte to the given address and register.
WriteByteToReg(addr, reg, value byte) error
// WriteU16ToReg
WriteWordToReg(addr, reg byte, value uint16) error
2013-12-07 23:11:06 +05:30
}
type I2C interface {
2014-03-03 00:51:23 +05:30
Bus(l byte) I2CBus
Close() error
}
2014-03-03 00:51:23 +05:30
var i2cInstance I2C
2014-03-03 00:51:23 +05:30
func InitI2C() error {
desc, err := DescribeHost()
if err != nil {
return err
}
2014-03-03 00:51:23 +05:30
i2cInstance = desc.I2C()
return nil
}
2014-03-03 00:51:23 +05:30
func CloseI2C() error {
return i2cInstance.Close()
}
2014-03-03 00:51:23 +05:30
func NewI2CBus(l byte) I2CBus {
return i2cInstance.Bus(l)
}