diff --git a/controller/hd44780/hd44780_test.go b/controller/hd44780/hd44780_test.go index eee375d..9542bf7 100644 --- a/controller/hd44780/hd44780_test.go +++ b/controller/hd44780/hd44780_test.go @@ -131,7 +131,7 @@ type mockI2CBus struct { closed bool } -func (bus *mockI2CBus) ReadBytes(addr byte, num int) ([]byte, error) { return []byte{0x00}, nil } +func (bus *mockI2CBus) ReadBytes(addr byte) ([]byte, error) { return []byte{0x00}, nil } func (bus *mockI2CBus) ReadByte(addr byte) (byte, error) { return 0x00, nil } func (bus *mockI2CBus) WriteBytes(addr byte, value []byte) error { return nil } func (bus *mockI2CBus) ReadFromReg(addr, reg byte, value []byte) error { return nil } diff --git a/host/generic/i2cbus.go b/host/generic/i2cbus.go index 45c5870..63b6eb1 100644 --- a/host/generic/i2cbus.go +++ b/host/generic/i2cbus.go @@ -101,7 +101,7 @@ func (b *i2cBus) ReadByte(addr byte) (byte, error) { return bytes[0], nil } -func (b *i2cBus) ReadBytes(addr byte, num int) ([]byte, error) { +func (b *i2cBus) ReadBytes(addr byte) ([]byte, error) { b.mu.Lock() defer b.mu.Unlock() @@ -113,10 +113,16 @@ func (b *i2cBus) ReadBytes(addr byte, num int) ([]byte, error) { return []byte{0}, err } - bytes := make([]byte, num) + info, err := b.file.Stat() + if err != nil { + return []byte{0}, err + } + + size := int(info.Size()) + bytes := make([]byte, size) n, _ := b.file.Read(bytes) - if n != num { + if n != size { return []byte{0}, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n) } diff --git a/i2c.go b/i2c.go index 6a5d1ec..f0c79da 100644 --- a/i2c.go +++ b/i2c.go @@ -4,6 +4,8 @@ package embd // I2CBus interface is used to interact with the I2C bus. type I2CBus interface { + // ReadBytes reads a slice of bytes from the given address. + ReadBytes(addr byte) (value []byte, err error) // ReadByte reads a byte from the given address. ReadByte(addr byte) (value byte, err error) // ReadBytes reads a slice of bytes from the given address.