gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/pca9685_driver_test.go (about) 1 package i2c 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "gobot.io/x/gobot/v2" 9 "gobot.io/x/gobot/v2/drivers/gpio" 10 "gobot.io/x/gobot/v2/gobottest" 11 ) 12 13 // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver 14 // and tests all implementations, so no further tests needed here for gobot.Driver interface 15 var _ gobot.Driver = (*PCA9685Driver)(nil) 16 17 // and also the PwmWriter and ServoWriter interfaces 18 var _ gpio.PwmWriter = (*PCA9685Driver)(nil) 19 var _ gpio.ServoWriter = (*PCA9685Driver)(nil) 20 21 func initTestPCA9685DriverWithStubbedAdaptor() (*PCA9685Driver, *i2cTestAdaptor) { 22 a := newI2cTestAdaptor() 23 d := NewPCA9685Driver(a) 24 if err := d.Start(); err != nil { 25 panic(err) 26 } 27 return d, a 28 } 29 30 func TestNewPCA9685Driver(t *testing.T) { 31 var di interface{} = NewPCA9685Driver(newI2cTestAdaptor()) 32 d, ok := di.(*PCA9685Driver) 33 if !ok { 34 t.Errorf("NewPCA9685Driver() should have returned a *PCA9685Driver") 35 } 36 gobottest.Refute(t, d.Driver, nil) 37 gobottest.Assert(t, strings.HasPrefix(d.Name(), "PCA9685"), true) 38 gobottest.Assert(t, d.defaultAddress, 0x40) 39 } 40 41 func TestPCA9685Options(t *testing.T) { 42 // This is a general test, that options are applied in constructor by using the common WithBus() option and 43 // least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)". 44 d := NewPCA9685Driver(newI2cTestAdaptor(), WithBus(2)) 45 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 46 } 47 48 func TestPCA9685Start(t *testing.T) { 49 a := newI2cTestAdaptor() 50 d := NewPCA9685Driver(a) 51 a.i2cReadImpl = func(b []byte) (int, error) { 52 copy(b, []byte{0x01}) 53 return 1, nil 54 } 55 gobottest.Assert(t, d.Start(), nil) 56 } 57 58 func TestPCA9685Halt(t *testing.T) { 59 d, a := initTestPCA9685DriverWithStubbedAdaptor() 60 a.i2cReadImpl = func(b []byte) (int, error) { 61 copy(b, []byte{0x01}) 62 return 1, nil 63 } 64 gobottest.Assert(t, d.Start(), nil) 65 gobottest.Assert(t, d.Halt(), nil) 66 } 67 68 func TestPCA9685SetPWM(t *testing.T) { 69 d, a := initTestPCA9685DriverWithStubbedAdaptor() 70 a.i2cReadImpl = func(b []byte) (int, error) { 71 copy(b, []byte{0x01}) 72 return 1, nil 73 } 74 gobottest.Assert(t, d.Start(), nil) 75 gobottest.Assert(t, d.SetPWM(0, 0, 256), nil) 76 } 77 78 func TestPCA9685SetPWMError(t *testing.T) { 79 d, a := initTestPCA9685DriverWithStubbedAdaptor() 80 a.i2cReadImpl = func(b []byte) (int, error) { 81 copy(b, []byte{0x01}) 82 return 1, nil 83 } 84 gobottest.Assert(t, d.Start(), nil) 85 a.i2cWriteImpl = func([]byte) (int, error) { 86 return 0, errors.New("write error") 87 } 88 gobottest.Assert(t, d.SetPWM(0, 0, 256), errors.New("write error")) 89 } 90 91 func TestPCA9685SetPWMFreq(t *testing.T) { 92 d, a := initTestPCA9685DriverWithStubbedAdaptor() 93 a.i2cReadImpl = func(b []byte) (int, error) { 94 copy(b, []byte{0x01}) 95 return 1, nil 96 } 97 gobottest.Assert(t, d.Start(), nil) 98 99 a.i2cReadImpl = func(b []byte) (int, error) { 100 copy(b, []byte{0x01}) 101 return 1, nil 102 } 103 gobottest.Assert(t, d.SetPWMFreq(60), nil) 104 } 105 106 func TestPCA9685SetPWMFreqReadError(t *testing.T) { 107 d, a := initTestPCA9685DriverWithStubbedAdaptor() 108 a.i2cReadImpl = func(b []byte) (int, error) { 109 copy(b, []byte{0x01}) 110 return 1, nil 111 } 112 gobottest.Assert(t, d.Start(), nil) 113 114 a.i2cReadImpl = func(b []byte) (int, error) { 115 return 0, errors.New("read error") 116 } 117 gobottest.Assert(t, d.SetPWMFreq(60), errors.New("read error")) 118 } 119 120 func TestPCA9685SetPWMFreqWriteError(t *testing.T) { 121 d, a := initTestPCA9685DriverWithStubbedAdaptor() 122 a.i2cReadImpl = func(b []byte) (int, error) { 123 copy(b, []byte{0x01}) 124 return 1, nil 125 } 126 gobottest.Assert(t, d.Start(), nil) 127 128 a.i2cWriteImpl = func([]byte) (int, error) { 129 return 0, errors.New("write error") 130 } 131 gobottest.Assert(t, d.SetPWMFreq(60), errors.New("write error")) 132 } 133 134 func TestPCA9685Commands(t *testing.T) { 135 d, a := initTestPCA9685DriverWithStubbedAdaptor() 136 a.i2cReadImpl = func(b []byte) (int, error) { 137 copy(b, []byte{0x01}) 138 return 1, nil 139 } 140 d.Start() 141 142 err := d.Command("PwmWrite")(map[string]interface{}{"pin": "1", "val": "1"}) 143 gobottest.Assert(t, err, nil) 144 145 err = d.Command("ServoWrite")(map[string]interface{}{"pin": "1", "val": "1"}) 146 gobottest.Assert(t, err, nil) 147 148 err = d.Command("SetPWM")(map[string]interface{}{"channel": "1", "on": "0", "off": "1024"}) 149 gobottest.Assert(t, err, nil) 150 151 err = d.Command("SetPWMFreq")(map[string]interface{}{"freq": "60"}) 152 gobottest.Assert(t, err, nil) 153 }