mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +01:00
i2c: added ReadBytes to I2CBus
This commit is contained in:
parent
5065e49386
commit
2e6bd46354
@ -101,6 +101,28 @@ func (b *i2cBus) ReadByte(addr byte) (byte, error) {
|
|||||||
return bytes[0], nil
|
return bytes[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *i2cBus) ReadBytes(addr byte, num int) ([]byte, error) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
|
if err := b.init(); err != nil {
|
||||||
|
return []byte{0}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := b.setAddress(addr); err != nil {
|
||||||
|
return []byte{0}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes := make([]byte, num)
|
||||||
|
n, _ := b.file.Read(bytes)
|
||||||
|
|
||||||
|
if n != num {
|
||||||
|
return []byte{0}, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n)
|
||||||
|
}
|
||||||
|
|
||||||
|
return bytes, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *i2cBus) WriteByte(addr, value byte) error {
|
func (b *i2cBus) WriteByte(addr, value byte) error {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
2
i2c.go
2
i2c.go
@ -4,6 +4,8 @@ package embd
|
|||||||
|
|
||||||
// I2CBus interface is used to interact with the I2C bus.
|
// I2CBus interface is used to interact with the I2C bus.
|
||||||
type I2CBus interface {
|
type I2CBus interface {
|
||||||
|
// ReadByte reads a byte from the given address.
|
||||||
|
ReadBytes(addr byte, num int) (value []byte, err error)
|
||||||
// ReadByte reads a byte from the given address.
|
// ReadByte reads a byte from the given address.
|
||||||
ReadByte(addr byte) (value byte, err error)
|
ReadByte(addr byte) (value byte, err error)
|
||||||
// WriteByte writes a byte to the given address.
|
// WriteByte writes a byte to the given address.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user