gobot.io/x/gobot/v2@v2.1.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/v2/drivers/i2c" 9 "gobot.io/x/gobot/v2/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 writeStartWasSend bool 24 writeStopWasSend bool 25 readStartWasSend bool 26 readStopWasSend bool 27 } 28 29 func initTestAdaptorI2c() *Adaptor { 30 a := NewAdaptor() 31 a.connect = func(a *Adaptor) (err error) { return nil } 32 a.littleWire = new(i2cMock) 33 return a 34 } 35 36 func TestDigisparkAdaptorI2cGetI2cConnection(t *testing.T) { 37 // arrange 38 var c i2c.Connection 39 var err error 40 a := initTestAdaptorI2c() 41 42 // act 43 c, err = a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 44 45 // assert 46 gobottest.Assert(t, err, nil) 47 gobottest.Refute(t, c, nil) 48 } 49 50 func TestDigisparkAdaptorI2cGetI2cConnectionFailWithInvalidBus(t *testing.T) { 51 // arrange 52 a := initTestAdaptorI2c() 53 54 // act 55 c, err := a.GetI2cConnection(0x40, 1) 56 57 // assert 58 gobottest.Assert(t, err, errors.New("Invalid bus number 1, only 0 is supported")) 59 gobottest.Assert(t, c, nil) 60 } 61 62 func TestDigisparkAdaptorI2cStartFailWithWrongAddress(t *testing.T) { 63 // arrange 64 data := []byte{0, 1, 2, 3, 4} 65 a := initTestAdaptorI2c() 66 c, _ := a.GetI2cConnection(0x39, a.DefaultI2cBus()) 67 68 // act 69 count, err := c.Write(data) 70 71 // assert 72 gobottest.Assert(t, count, 0) 73 gobottest.Assert(t, err, fmt.Errorf("Invalid address, only %d is supported", availableI2cAddress)) 74 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, maxUint8) 75 } 76 77 func TestDigisparkAdaptorI2cWrite(t *testing.T) { 78 // arrange 79 data := []byte{0, 1, 2, 3, 4} 80 dataLen := len(data) 81 a := initTestAdaptorI2c() 82 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 83 84 // act 85 count, err := c.Write(data) 86 87 // assert 88 gobottest.Assert(t, err, nil) 89 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 90 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, false) 91 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 92 gobottest.Assert(t, count, dataLen) 93 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, data) 94 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, true) 95 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, false) 96 } 97 98 func TestDigisparkAdaptorI2cWriteByte(t *testing.T) { 99 // arrange 100 data := byte(0x02) 101 a := initTestAdaptorI2c() 102 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 103 104 // act 105 err := c.WriteByte(data) 106 107 // assert 108 gobottest.Assert(t, err, nil) 109 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 110 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, false) 111 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 112 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{data}) 113 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, true) 114 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, false) 115 } 116 117 func TestDigisparkAdaptorI2cWriteByteData(t *testing.T) { 118 // arrange 119 reg := uint8(0x03) 120 data := byte(0x09) 121 a := initTestAdaptorI2c() 122 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 123 124 // act 125 err := c.WriteByteData(reg, data) 126 127 // assert 128 gobottest.Assert(t, err, nil) 129 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 130 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, false) 131 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 132 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg, data}) 133 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, true) 134 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, false) 135 } 136 137 func TestDigisparkAdaptorI2cWriteWordData(t *testing.T) { 138 // arrange 139 reg := uint8(0x04) 140 data := uint16(0x0508) 141 a := initTestAdaptorI2c() 142 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 143 144 // act 145 err := c.WriteWordData(reg, data) 146 147 // assert 148 gobottest.Assert(t, err, nil) 149 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 150 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, false) 151 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 152 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg, 0x08, 0x05}) 153 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, true) 154 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, false) 155 } 156 157 func TestDigisparkAdaptorI2cWriteBlockData(t *testing.T) { 158 // arrange 159 reg := uint8(0x05) 160 data := []byte{0x80, 0x81, 0x82} 161 a := initTestAdaptorI2c() 162 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 163 164 // act 165 err := c.WriteBlockData(reg, data) 166 167 // assert 168 gobottest.Assert(t, err, nil) 169 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 170 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, false) 171 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(0)) 172 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, append([]byte{reg}, data...)) 173 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, true) 174 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, false) 175 } 176 177 func TestDigisparkAdaptorI2cRead(t *testing.T) { 178 // arrange 179 data := []byte{0, 1, 2, 3, 4} 180 dataLen := len(data) 181 a := initTestAdaptorI2c() 182 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 183 184 // act 185 count, err := c.Read(data) 186 187 // assert 188 gobottest.Assert(t, count, dataLen) 189 gobottest.Assert(t, err, nil) 190 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, false) 191 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, true) 192 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 193 gobottest.Assert(t, data, i2cData[:dataLen]) 194 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, false) 195 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, true) 196 } 197 198 func TestDigisparkAdaptorI2cReadByte(t *testing.T) { 199 // arrange 200 a := initTestAdaptorI2c() 201 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 202 203 // act 204 data, err := c.ReadByte() 205 206 // assert 207 gobottest.Assert(t, err, nil) 208 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, false) 209 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, true) 210 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 211 gobottest.Assert(t, data, i2cData[0]) 212 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, false) 213 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, true) 214 } 215 216 func TestDigisparkAdaptorI2cReadByteData(t *testing.T) { 217 // arrange 218 reg := uint8(0x04) 219 a := initTestAdaptorI2c() 220 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 221 222 // act 223 data, err := c.ReadByteData(reg) 224 225 // assert 226 gobottest.Assert(t, err, nil) 227 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 228 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, true) 229 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 230 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg}) 231 gobottest.Assert(t, data, i2cData[0]) 232 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, false) 233 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, true) 234 } 235 236 func TestDigisparkAdaptorI2cReadWordData(t *testing.T) { 237 // arrange 238 reg := uint8(0x05) 239 // 2 bytes of i2cData are used swapped 240 expectedValue := uint16(0x0405) 241 a := initTestAdaptorI2c() 242 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 243 244 // act 245 data, err := c.ReadWordData(reg) 246 247 // assert 248 gobottest.Assert(t, err, nil) 249 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 250 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, true) 251 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 252 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg}) 253 gobottest.Assert(t, data, expectedValue) 254 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, false) 255 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, true) 256 } 257 258 func TestDigisparkAdaptorI2cReadBlockData(t *testing.T) { 259 // arrange 260 reg := uint8(0x05) 261 data := []byte{0, 0, 0, 0, 0} 262 dataLen := len(data) 263 a := initTestAdaptorI2c() 264 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 265 266 // act 267 err := c.ReadBlockData(reg, data) 268 269 // assert 270 gobottest.Assert(t, err, nil) 271 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStartWasSend, true) 272 gobottest.Assert(t, a.littleWire.(*i2cMock).readStartWasSend, true) 273 gobottest.Assert(t, a.littleWire.(*i2cMock).direction, uint8(1)) 274 gobottest.Assert(t, a.littleWire.(*i2cMock).dataWritten, []byte{reg}) 275 gobottest.Assert(t, data, i2cData[:dataLen]) 276 gobottest.Assert(t, a.littleWire.(*i2cMock).writeStopWasSend, false) 277 gobottest.Assert(t, a.littleWire.(*i2cMock).readStopWasSend, true) 278 } 279 280 func TestDigisparkAdaptorI2cUpdateDelay(t *testing.T) { 281 // arrange 282 a := initTestAdaptorI2c() 283 c, _ := a.GetI2cConnection(availableI2cAddress, a.DefaultI2cBus()) 284 285 // act 286 err := c.(*digisparkI2cConnection).UpdateDelay(uint(100)) 287 288 // assert 289 gobottest.Assert(t, err, nil) 290 gobottest.Assert(t, a.littleWire.(*i2cMock).duration, uint(100)) 291 } 292 293 // setup mock for i2c tests 294 func (l *i2cMock) i2cInit() error { 295 l.direction = maxUint8 296 return l.error() 297 } 298 299 func (l *i2cMock) i2cStart(address7bit uint8, direction uint8) error { 300 if address7bit != availableI2cAddress { 301 return fmt.Errorf("Invalid address, only %d is supported", availableI2cAddress) 302 } 303 if err := l.error(); err != nil { 304 return err 305 } 306 l.direction = direction 307 if direction == 1 { 308 l.readStartWasSend = true 309 } else { 310 l.writeStartWasSend = true 311 } 312 return nil 313 } 314 315 func (l *i2cMock) i2cWrite(sendBuffer []byte, length int, endWithStop uint8) error { 316 l.dataWritten = append(l.dataWritten, sendBuffer...) 317 if endWithStop > 0 { 318 l.writeStopWasSend = true 319 } 320 return l.error() 321 } 322 323 func (l *i2cMock) i2cRead(readBuffer []byte, length int, endWithStop uint8) error { 324 if len(readBuffer) < length { 325 length = len(readBuffer) 326 } 327 if len(i2cData) < length { 328 length = len(i2cData) 329 } 330 copy(readBuffer[:length], i2cData[:length]) 331 332 if endWithStop > 0 { 333 l.readStopWasSend = true 334 } 335 return l.error() 336 } 337 338 func (l *i2cMock) i2cUpdateDelay(duration uint) error { 339 l.duration = duration 340 return l.error() 341 } 342 343 // GPIO, PWM and servo functions unused in this test scenarios 344 func (l *i2cMock) digitalWrite(pin uint8, state uint8) error { return nil } 345 func (l *i2cMock) pinMode(pin uint8, mode uint8) error { return nil } 346 func (l *i2cMock) pwmInit() error { return nil } 347 func (l *i2cMock) pwmStop() error { return nil } 348 func (l *i2cMock) pwmUpdateCompare(channelA uint8, channelB uint8) error { return nil } 349 func (l *i2cMock) pwmUpdatePrescaler(value uint) error { return nil } 350 func (l *i2cMock) servoInit() error { return nil } 351 func (l *i2cMock) servoUpdateLocation(locationA uint8, locationB uint8) error { return nil } 352 func (l *i2cMock) error() error { return nil }