diff --git a/controller/hd44780/hd44780_test.go b/controller/hd44780/hd44780_test.go index b05dddc..eee375d 100644 --- a/controller/hd44780/hd44780_test.go +++ b/controller/hd44780/hd44780_test.go @@ -131,6 +131,7 @@ type mockI2CBus struct { closed bool } +func (bus *mockI2CBus) ReadBytes(addr byte, num int) ([]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 dc8e74c..45c5870 100644 --- a/host/generic/i2cbus.go +++ b/host/generic/i2cbus.go @@ -101,6 +101,28 @@ func (b *i2cBus) ReadByte(addr byte) (byte, error) { 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 { b.mu.Lock() defer b.mu.Unlock() diff --git a/i2c.go b/i2c.go index 8bdb876..6a5d1ec 100644 --- a/i2c.go +++ b/i2c.go @@ -6,6 +6,8 @@ package embd type I2CBus interface { // 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. + ReadBytes(addr byte, num int) (value []byte, err error) // WriteByte writes a byte to the given address. WriteByte(addr, value byte) error // WriteBytes writes a slice bytes to the given address.