1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00
hybridgroup.gobot/sysfs/i2c_device_test.go
Hrishikesh Tapaswi 42475e479d Get I2C functionality before doing SMBus block I/O
In the sysfs i2cDevice implementation, use an ioctl to get the adapter
functionality mask. Prefer SMBus block I/O but if it's not available,
perform read/write calls directly on the file descriptor.

Improve Wiichuck error handling. Add a 1 ms delay between I/O operations
to the Wiichuck; this dramatically improves reliability.

Signed-off-by: Hrishikesh Tapaswi <hrishikesh195@yahoo.com>
2016-02-17 12:25:15 -08:00

50 lines
770 B
Go

package sysfs
import (
"os"
"testing"
"github.com/hybridgroup/gobot"
)
func TestNewI2cDevice(t *testing.T) {
fs := NewMockFilesystem([]string{})
SetFilesystem(fs)
i, err := NewI2cDevice(os.DevNull, 0xff)
gobot.Refute(t, err, nil)
fs = NewMockFilesystem([]string{
"/dev/i2c-1",
})
SetFilesystem(fs)
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
gobot.Refute(t, err, nil)
SetSyscall(&MockSyscall{})
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
var _ I2cDevice = i
gobot.Assert(t, err, nil)
gobot.Assert(t, i.SetAddress(0xff), nil)
buf := []byte{0x01, 0x02, 0x03}
n, err := i.Write(buf)
gobot.Assert(t, n, len(buf))
gobot.Assert(t, err, nil)
buf = make([]byte, 4)
n, err = i.Read(buf)
gobot.Assert(t, n, 3)
gobot.Assert(t, err, nil)
}