gobot.io/x/gobot@v1.16.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" 9 "gobot.io/x/gobot/drivers/gpio" 10 "gobot.io/x/gobot/gobottest" 11 ) 12 13 // ensure that PCA9685Driver fulfills Gobot Driver interface 14 var _ gobot.Driver = (*PCA9685Driver)(nil) 15 16 // and also the PwmWriter and ServoWriter interfaces 17 var _ gpio.PwmWriter = (*PCA9685Driver)(nil) 18 var _ gpio.ServoWriter = (*PCA9685Driver)(nil) 19 20 // --------- HELPERS 21 func initTestPCA9685Driver() (driver *PCA9685Driver) { 22 driver, _ = initTestPCA9685DriverWithStubbedAdaptor() 23 return 24 } 25 26 func initTestPCA9685DriverWithStubbedAdaptor() (*PCA9685Driver, *i2cTestAdaptor) { 27 adaptor := newI2cTestAdaptor() 28 return NewPCA9685Driver(adaptor), adaptor 29 } 30 31 // --------- TESTS 32 33 func TestNewPCA9685Driver(t *testing.T) { 34 // Does it return a pointer to an instance of PCA9685Driver? 35 var pca interface{} = NewPCA9685Driver(newI2cTestAdaptor()) 36 _, ok := pca.(*PCA9685Driver) 37 if !ok { 38 t.Errorf("NewPCA9685Driver() should have returned a *PCA9685Driver") 39 } 40 } 41 42 func TestPCA9685DriverName(t *testing.T) { 43 pca := initTestPCA9685Driver() 44 gobottest.Refute(t, pca.Connection(), nil) 45 gobottest.Assert(t, strings.HasPrefix(pca.Name(), "PCA9685"), true) 46 } 47 48 func TestPCA9685DriverOptions(t *testing.T) { 49 pca := NewPCA9685Driver(newI2cTestAdaptor(), WithBus(2)) 50 gobottest.Assert(t, pca.GetBusOrDefault(1), 2) 51 } 52 53 // Methods 54 func TestPCA9685DriverStart(t *testing.T) { 55 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 56 adaptor.i2cReadImpl = func(b []byte) (int, error) { 57 copy(b, []byte{0x01}) 58 return 1, nil 59 } 60 gobottest.Assert(t, pca.Start(), nil) 61 } 62 63 func TestPCA9685DriverStartConnectError(t *testing.T) { 64 d, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 65 adaptor.Testi2cConnectErr(true) 66 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 67 } 68 69 func TestPCA9685DriverStartWriteError(t *testing.T) { 70 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 71 adaptor.i2cWriteImpl = func([]byte) (int, error) { 72 return 0, errors.New("write error") 73 } 74 gobottest.Assert(t, pca.Start(), errors.New("write error")) 75 } 76 77 func TestPCA9685DriverHalt(t *testing.T) { 78 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 79 adaptor.i2cReadImpl = func(b []byte) (int, error) { 80 copy(b, []byte{0x01}) 81 return 1, nil 82 } 83 gobottest.Assert(t, pca.Start(), nil) 84 gobottest.Assert(t, pca.Halt(), nil) 85 } 86 87 func TestPCA9685DriverSetPWM(t *testing.T) { 88 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 89 adaptor.i2cReadImpl = func(b []byte) (int, error) { 90 copy(b, []byte{0x01}) 91 return 1, nil 92 } 93 gobottest.Assert(t, pca.Start(), nil) 94 gobottest.Assert(t, pca.SetPWM(0, 0, 256), nil) 95 } 96 97 func TestPCA9685DriverSetPWMError(t *testing.T) { 98 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 99 adaptor.i2cReadImpl = func(b []byte) (int, error) { 100 copy(b, []byte{0x01}) 101 return 1, nil 102 } 103 gobottest.Assert(t, pca.Start(), nil) 104 adaptor.i2cWriteImpl = func([]byte) (int, error) { 105 return 0, errors.New("write error") 106 } 107 gobottest.Assert(t, pca.SetPWM(0, 0, 256), errors.New("write error")) 108 } 109 110 func TestPCA9685DriverSetPWMFreq(t *testing.T) { 111 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 112 adaptor.i2cReadImpl = func(b []byte) (int, error) { 113 copy(b, []byte{0x01}) 114 return 1, nil 115 } 116 gobottest.Assert(t, pca.Start(), nil) 117 118 adaptor.i2cReadImpl = func(b []byte) (int, error) { 119 copy(b, []byte{0x01}) 120 return 1, nil 121 } 122 gobottest.Assert(t, pca.SetPWMFreq(60), nil) 123 } 124 125 func TestPCA9685DriverSetPWMFreqReadError(t *testing.T) { 126 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 127 adaptor.i2cReadImpl = func(b []byte) (int, error) { 128 copy(b, []byte{0x01}) 129 return 1, nil 130 } 131 gobottest.Assert(t, pca.Start(), nil) 132 133 adaptor.i2cReadImpl = func(b []byte) (int, error) { 134 return 0, errors.New("read error") 135 } 136 gobottest.Assert(t, pca.SetPWMFreq(60), errors.New("read error")) 137 } 138 139 func TestPCA9685DriverSetPWMFreqWriteError(t *testing.T) { 140 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 141 adaptor.i2cReadImpl = func(b []byte) (int, error) { 142 copy(b, []byte{0x01}) 143 return 1, nil 144 } 145 gobottest.Assert(t, pca.Start(), nil) 146 147 adaptor.i2cWriteImpl = func([]byte) (int, error) { 148 return 0, errors.New("write error") 149 } 150 gobottest.Assert(t, pca.SetPWMFreq(60), errors.New("write error")) 151 } 152 153 func TestPCA9685DriverSetName(t *testing.T) { 154 pca := initTestPCA9685Driver() 155 pca.SetName("TESTME") 156 gobottest.Assert(t, pca.Name(), "TESTME") 157 } 158 159 func TestPCA9685DriverCommands(t *testing.T) { 160 pca, adaptor := initTestPCA9685DriverWithStubbedAdaptor() 161 adaptor.i2cReadImpl = func(b []byte) (int, error) { 162 copy(b, []byte{0x01}) 163 return 1, nil 164 } 165 pca.Start() 166 167 err := pca.Command("PwmWrite")(map[string]interface{}{"pin": "1", "val": "1"}) 168 gobottest.Assert(t, err, nil) 169 170 err = pca.Command("ServoWrite")(map[string]interface{}{"pin": "1", "val": "1"}) 171 gobottest.Assert(t, err, nil) 172 173 err = pca.Command("SetPWM")(map[string]interface{}{"channel": "1", "on": "0", "off": "1024"}) 174 gobottest.Assert(t, err, nil) 175 176 err = pca.Command("SetPWMFreq")(map[string]interface{}{"freq": "60"}) 177 gobottest.Assert(t, err, nil) 178 }