From 2e6bd46354a851f46de6efd5b587182c703819d4 Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sat, 29 Aug 2015 00:42:44 -0400 Subject: [PATCH 1/6] i2c: added ReadBytes to I2CBus --- host/generic/i2cbus.go | 22 ++++++++++++++++++++++ i2c.go | 2 ++ 2 files changed, 24 insertions(+) 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..d9922ed 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 { + // 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(addr byte) (value byte, err error) // WriteByte writes a byte to the given address. From afa49bb2bc5488863b3b97944eb51c602ac6398d Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sat, 29 Aug 2015 00:53:34 -0400 Subject: [PATCH 2/6] build: fixed broken tests --- controller/hd44780/hd44780_test.go | 1 + i2c.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/controller/hd44780/hd44780_test.go b/controller/hd44780/hd44780_test.go index b05dddc..40b3e2a 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/i2c.go b/i2c.go index d9922ed..41244ea 100644 --- a/i2c.go +++ b/i2c.go @@ -6,7 +6,7 @@ package embd 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 slice of bytes from the given address. ReadByte(addr byte) (value byte, err error) // WriteByte writes a byte to the given address. WriteByte(addr, value byte) error From db5c9abb2a12eca86ba44f2d1690a62ffe476d7f Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sat, 29 Aug 2015 00:57:25 -0400 Subject: [PATCH 3/6] ran gofmt --- controller/hd44780/hd44780_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/hd44780/hd44780_test.go b/controller/hd44780/hd44780_test.go index 40b3e2a..eee375d 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, 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 } From 48c26358bdbce029d3e614635bd435d9dbd439c9 Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sun, 30 Aug 2015 09:48:43 -0400 Subject: [PATCH 4/6] doc: fixed mixed up comments --- i2c.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i2c.go b/i2c.go index 41244ea..6a5d1ec 100644 --- a/i2c.go +++ b/i2c.go @@ -5,9 +5,9 @@ package embd // I2CBus interface is used to interact with the I2C bus. type I2CBus interface { // ReadByte reads a byte from the given address. - ReadBytes(addr byte, num int) (value []byte, err error) - // ReadByte reads a slice of bytes 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. From 9d8285ca0169f361264fd951cb137227b06989d9 Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sat, 10 Oct 2015 23:23:40 -0400 Subject: [PATCH 5/6] changed ReadBytes to get file size from stat --- controller/hd44780/hd44780_test.go | 2 +- host/generic/i2cbus.go | 12 +++++++++--- i2c.go | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) 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. From b296368a0557615cbc3ba19bd21965d364d44dba Mon Sep 17 00:00:00 2001 From: Gavin Cabbage Date: Sat, 10 Oct 2015 23:58:24 -0400 Subject: [PATCH 6/6] removing failed Stat use --- controller/hd44780/hd44780_test.go | 2 +- host/generic/i2cbus.go | 12 +++--------- i2c.go | 2 -- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/controller/hd44780/hd44780_test.go b/controller/hd44780/hd44780_test.go index 9542bf7..eee375d 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) ([]byte, error) { return []byte{0x00}, nil } +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 63b6eb1..45c5870 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) ([]byte, error) { +func (b *i2cBus) ReadBytes(addr byte, num int) ([]byte, error) { b.mu.Lock() defer b.mu.Unlock() @@ -113,16 +113,10 @@ func (b *i2cBus) ReadBytes(addr byte) ([]byte, error) { return []byte{0}, err } - info, err := b.file.Stat() - if err != nil { - return []byte{0}, err - } - - size := int(info.Size()) - bytes := make([]byte, size) + bytes := make([]byte, num) n, _ := b.file.Read(bytes) - if n != size { + if n != num { return []byte{0}, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n) } diff --git a/i2c.go b/i2c.go index f0c79da..6a5d1ec 100644 --- a/i2c.go +++ b/i2c.go @@ -4,8 +4,6 @@ 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.