gobot.io/x/gobot@v1.16.0/platforms/digispark/digispark_i2c_test.go (about) 1 package digispark 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "gobot.io/x/gobot/drivers/i2c" 9 "gobot.io/x/gobot/gobottest" 10 ) 11 12 const availableI2cAddress = 0x40 13 const maxUint8 = ^uint8(0) 14 15 var _ i2c.Connector = (*Adaptor)(nil) 16 var i2cData = []byte{5, 4, 3, 2, 1, 0} 17 18 type i2cMock struct { 19 address int 20 duration uint 21 direction uint8 22 dataWritten []byte 23 startWasSend bool 24 stopWasSend bool 25 } 26 27 func initTestAdaptorI2c() *Adaptor { 28 a := NewAdaptor() 29 a.connect = func(a *Adaptor) (err error) { return nil } 30 a.littleWire = new(i2cMock) 31 return a 32 } 33 34 func TestDigisparkAdaptorI2cGetConnection(t *testing.T) { 35 // arrange 36 var c i2c.Connection 37 var err error 38 a := initTestAdaptorI2c() 39 40 // act 41 c, err = a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 42 43 // assert 44 gobottest.Assert(t, err, nil) 45 gobottest.Refute(t, c, nil) 46 } 47 48 func TestDigisparkAdaptorI2cGetConnectionFailWithInvalidBus(t *testing.T) { 49 // arrange 50 a := initTestAdaptorI2c() 51 52 // act 53 c, err := a.GetConnection(0x40, 1) 54 55 // assert 56 gobottest.Assert(t, err, errors.New("Invalid bus number 1, only 0 is supported")) 57 gobottest.Assert(t, c, nil) 58 } 59 60 func TestDigisparkAdaptorI2cStartFailWithWrongAddress(t *testing.T) { 61 // arrange 62 data := []byte{0, 1, 2, 3, 4} 63 a := initTestAdaptorI2c() 64 c, err := a.GetConnection(0x39, a.GetDefaultBus()) 65 66 // act 67 count, err := c.Write(data) 68 69 // assert 70 gobottest.Assert(t, count, 0) 71 gobottest.Assert(t, err, fmt.Errorf("Invalid address, only %d is supported", availableI2cAddress)) 72 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, maxUint8) 73 } 74 75 func TestDigisparkAdaptorI2cWrite(t *testing.T) { 76 // arrange 77 data := []byte{0, 1, 2, 3, 4} 78 dataLen := len(data) 79 a := initTestAdaptorI2c() 80 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 81 82 // act 83 count, err := c.Write(data) 84 85 // assert 86 gobottest.Assert(t, err, nil) 87 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 88 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 89 gobottest.Assert(t, count, dataLen) 90 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, data) 91 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 92 } 93 94 func TestDigisparkAdaptorI2cWriteByte(t *testing.T) { 95 // arrange 96 data := byte(0x02) 97 a := initTestAdaptorI2c() 98 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 99 100 // act 101 err = c.WriteByte(data) 102 103 // assert 104 gobottest.Assert(t, err, nil) 105 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 106 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 107 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{data}) 108 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 109 } 110 111 func TestDigisparkAdaptorI2cWriteByteData(t *testing.T) { 112 // arrange 113 reg := uint8(0x03) 114 data := byte(0x09) 115 a := initTestAdaptorI2c() 116 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 117 118 // act 119 err = c.WriteByteData(reg, data) 120 121 // assert 122 gobottest.Assert(t, err, nil) 123 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 124 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 125 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg, data}) 126 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 127 } 128 129 func TestDigisparkAdaptorI2cWriteWordData(t *testing.T) { 130 // arrange 131 reg := uint8(0x04) 132 data := uint16(0x0508) 133 a := initTestAdaptorI2c() 134 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 135 136 // act 137 err = c.WriteWordData(reg, data) 138 139 // assert 140 gobottest.Assert(t, err, nil) 141 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 142 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 143 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg, 0x08, 0x05}) 144 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 145 } 146 147 func TestDigisparkAdaptorI2cRead(t *testing.T) { 148 // arrange 149 data := []byte{0, 1, 2, 3, 4} 150 dataLen := len(data) 151 a := initTestAdaptorI2c() 152 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 153 154 // act 155 count, err := c.Read(data) 156 157 // assert 158 gobottest.Assert(t, count, dataLen) 159 gobottest.Assert(t, err, nil) 160 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 161 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 162 gobottest.Assert(t, data, i2cData[:dataLen]) 163 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 164 } 165 166 func TestDigisparkAdaptorI2cReadByte(t *testing.T) { 167 // arrange 168 a := initTestAdaptorI2c() 169 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 170 171 // act 172 data, err := c.ReadByte() 173 174 // assert 175 gobottest.Assert(t, err, nil) 176 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 177 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 178 gobottest.Assert(t, data, i2cData[0]) 179 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 180 } 181 182 func TestDigisparkAdaptorI2cReadByteData(t *testing.T) { 183 // arrange 184 reg := uint8(0x04) 185 a := initTestAdaptorI2c() 186 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 187 188 // act 189 data, err := c.ReadByteData(reg) 190 191 // assert 192 gobottest.Assert(t, err, nil) 193 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 194 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 195 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg}) 196 gobottest.Assert(t, data, i2cData[0]) 197 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 198 } 199 200 func TestDigisparkAdaptorI2cReadWordData(t *testing.T) { 201 // arrange 202 reg := uint8(0x05) 203 // 2 bytes of i2cData are used swapped 204 expectedValue := uint16(0x0405) 205 a := initTestAdaptorI2c() 206 c, err := a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 207 208 // act 209 data, err := c.ReadWordData(reg) 210 211 // assert 212 gobottest.Assert(t, err, nil) 213 gobottest.Assert(t, a.littleWire.(*i2cMock).startWasSend, true) 214 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 215 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg}) 216 gobottest.Assert(t, data, expectedValue) 217 gobottest.Assert(t, a.littleWire.(*i2cMock).stopWasSend, true) 218 } 219 220 func TestDigisparkAdaptorI2cUpdateDelay(t *testing.T) { 221 // arrange 222 var c i2c.Connection 223 var err error 224 a := initTestAdaptorI2c() 225 c, err = a.GetConnection(availableI2cAddress, a.GetDefaultBus()) 226 227 // act 228 err = c.(*digisparkI2cConnection).UpdateDelay(uint(100)) 229 230 // assert 231 gobottest.Assert(t, err, nil) 232 gobottest.Assert(t, a.littleWire.(*i2cMock).duration, uint(100)) 233 } 234 235 // setup mock for i2c tests 236 func (l *i2cMock) i2cInit() error { 237 l.direction = maxUint8 238 return l.error() 239 } 240 241 func (l *i2cMock) i2cStart(address7bit uint8, direction uint8) error { 242 if address7bit != availableI2cAddress { 243 return fmt.Errorf("Invalid address, only %d is supported", availableI2cAddress) 244 } 245 if err := l.error(); err != nil { 246 return err 247 } 248 l.direction = direction 249 l.startWasSend = true 250 return nil 251 } 252 253 func (l *i2cMock) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error { 254 l.dataWritten = append(l.dataWritten, sendBuffer...) 255 if endWithStop > 0 { 256 l.stopWasSend = true 257 } 258 return l.error() 259 } 260 261 func (l *i2cMock) i2cRead(readBuffer []byte, length int, endWithStop uint8) error { 262 if len(readBuffer) < length { 263 length = len(readBuffer) 264 } 265 if len(i2cData) < length { 266 length = len(i2cData) 267 } 268 copy(readBuffer[:length], i2cData[:length]) 269 270 if endWithStop > 0 { 271 l.stopWasSend = true 272 } 273 return l.error() 274 } 275 276 func (l *i2cMock) i2cUpdateDelay(duration uint) error { 277 l.duration = duration 278 return l.error() 279 } 280 281 // GPIO, PWM and servo functions unused in this test scenarios 282 func (l *i2cMock) digitalWrite(pin uint8, state uint8) error { return nil } 283 func (l *i2cMock) pinMode(pin uint8, mode uint8) error { return nil } 284 func (l *i2cMock) pwmInit() error { return nil } 285 func (l *i2cMock) pwmStop() error { return nil } 286 func (l *i2cMock) pwmUpdateCompare(channelA uint8, channelB uint8) error { return nil } 287 func (l *i2cMock) pwmUpdatePrescaler(value uint) error { return nil } 288 func (l *i2cMock) servoInit() error { return nil } 289 func (l *i2cMock) servoUpdateLocation(locationA uint8, locationB uint8) error { return nil } 290 func (l *i2cMock) error() error { return nil }