gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/wiichuck_driver_test.go (about) 1 package i2c 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 8 "gobot.io/x/gobot/v2" 9 "gobot.io/x/gobot/v2/gobottest" 10 ) 11 12 // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver 13 // and tests all implementations, so no further tests needed here for gobot.Driver interface 14 var _ gobot.Driver = (*WiichuckDriver)(nil) 15 16 func initTestWiichuckDriverWithStubbedAdaptor() *WiichuckDriver { 17 d := NewWiichuckDriver(newI2cTestAdaptor()) 18 if err := d.Start(); err != nil { 19 panic(err) 20 } 21 return d 22 } 23 24 func TestNewWiichuckDriver(t *testing.T) { 25 var di interface{} = NewWiichuckDriver(newI2cTestAdaptor()) 26 d, ok := di.(*WiichuckDriver) 27 if !ok { 28 t.Errorf("NewWiichuckDriver() should have returned a *WiichuckDriver") 29 } 30 gobottest.Refute(t, d.Driver, nil) 31 gobottest.Assert(t, strings.HasPrefix(d.Name(), "Wiichuck"), true) 32 gobottest.Assert(t, d.defaultAddress, 0x52) 33 gobottest.Assert(t, d.interval, 10*time.Millisecond) 34 } 35 36 func TestWiichuckDriverStart(t *testing.T) { 37 a := newI2cTestAdaptor() 38 d := NewWiichuckDriver(a) 39 a.Testi2cReadImpl(func(b []byte) (int, error) { 40 copy(b, []byte{1, 2, 3, 4, 5, 6}) 41 return 6, nil 42 }) 43 numberOfCyclesForEvery := 3 44 d.interval = 1 * time.Millisecond 45 sem := make(chan bool) 46 47 gobottest.Assert(t, d.Start(), nil) 48 49 go func() { 50 for { 51 time.Sleep(time.Duration(numberOfCyclesForEvery) * time.Millisecond) 52 j := d.Joystick() 53 if (j["sy_origin"] == float64(44)) && 54 (j["sx_origin"] == float64(45)) { 55 sem <- true 56 return 57 } 58 } 59 }() 60 61 select { 62 case <-sem: 63 case <-time.After(100 * time.Millisecond): 64 t.Errorf("origin not read correctly") 65 } 66 67 } 68 69 func TestWiichuckDriverHalt(t *testing.T) { 70 d := initTestWiichuckDriverWithStubbedAdaptor() 71 gobottest.Assert(t, d.Halt(), nil) 72 } 73 74 func TestWiichuckDriverCanParse(t *testing.T) { 75 d := initTestWiichuckDriverWithStubbedAdaptor() 76 77 // ------ When value is not encrypted 78 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 79 d.update(decryptedValue) 80 81 // - This should be done by WiichuckDriver.parse 82 gobottest.Assert(t, d.data["sx"], float64(45)) 83 gobottest.Assert(t, d.data["sy"], float64(44)) 84 gobottest.Assert(t, d.data["z"], float64(0)) 85 gobottest.Assert(t, d.data["c"], float64(0)) 86 } 87 88 func TestWiichuckDriverCanAdjustOrigins(t *testing.T) { 89 d := initTestWiichuckDriverWithStubbedAdaptor() 90 91 // ------ When value is not encrypted 92 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 93 d.update(decryptedValue) 94 95 // - This should be done by WiichuckDriver.adjustOrigins 96 gobottest.Assert(t, d.Joystick()["sx_origin"], float64(45)) 97 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(44)) 98 } 99 100 func TestWiichuckDriverCButton(t *testing.T) { 101 d := initTestWiichuckDriverWithStubbedAdaptor() 102 103 // ------ When value is not encrypted 104 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 105 d.update(decryptedValue) 106 107 // - This should be done by WiichuckDriver.updateButtons 108 done := make(chan bool) 109 110 d.On(d.Event(C), func(data interface{}) { 111 gobottest.Assert(t, data, true) 112 done <- true 113 }) 114 115 d.update(decryptedValue) 116 117 select { 118 case <-done: 119 case <-time.After(10 * time.Second): 120 t.Errorf("Did not receive 'C' event") 121 } 122 } 123 124 func TestWiichuckDriverZButton(t *testing.T) { 125 d := initTestWiichuckDriverWithStubbedAdaptor() 126 127 // ------ When value is not encrypted 128 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 129 d.update(decryptedValue) 130 131 done := make(chan bool) 132 133 d.On(d.Event(Z), func(data interface{}) { 134 gobottest.Assert(t, data, true) 135 done <- true 136 }) 137 138 d.update(decryptedValue) 139 140 select { 141 case <-done: 142 case <-time.After(10 * time.Second): 143 t.Errorf("Did not receive 'Z' event") 144 } 145 } 146 147 func TestWiichuckDriverUpdateJoystick(t *testing.T) { 148 d := initTestWiichuckDriverWithStubbedAdaptor() 149 150 // ------ When value is not encrypted 151 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 152 153 // - This should be done by WiichuckDriver.updateJoystick 154 expectedData := map[string]float64{ 155 "x": float64(0), 156 "y": float64(0), 157 } 158 159 done := make(chan bool) 160 161 d.On(d.Event(Joystick), func(data interface{}) { 162 gobottest.Assert(t, data, expectedData) 163 done <- true 164 }) 165 166 d.update(decryptedValue) 167 168 select { 169 case <-done: 170 case <-time.After(10 * time.Second): 171 t.Errorf("Did not receive 'Joystick' event") 172 } 173 } 174 175 func TestWiichuckDriverEncrypted(t *testing.T) { 176 d := initTestWiichuckDriverWithStubbedAdaptor() 177 encryptedValue := []byte{1, 1, 2, 2, 3, 3} 178 179 d.update(encryptedValue) 180 181 gobottest.Assert(t, d.data["sx"], float64(0)) 182 gobottest.Assert(t, d.data["sy"], float64(0)) 183 gobottest.Assert(t, d.data["z"], float64(0)) 184 gobottest.Assert(t, d.data["c"], float64(0)) 185 186 gobottest.Assert(t, d.Joystick()["sx_origin"], float64(-1)) 187 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(-1)) 188 } 189 190 func TestWiichuckDriverSetJoystickDefaultValue(t *testing.T) { 191 d := initTestWiichuckDriverWithStubbedAdaptor() 192 193 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(-1)) 194 195 d.setJoystickDefaultValue("sy_origin", float64(2)) 196 197 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(2)) 198 199 // when current default value is not -1 it keeps the current value 200 d.setJoystickDefaultValue("sy_origin", float64(20)) 201 202 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(2)) 203 } 204 205 func TestWiichuckDriverCalculateJoystickValue(t *testing.T) { 206 d := initTestWiichuckDriverWithStubbedAdaptor() 207 208 gobottest.Assert(t, d.calculateJoystickValue(float64(20), float64(5)), float64(15)) 209 gobottest.Assert(t, d.calculateJoystickValue(float64(1), float64(2)), float64(-1)) 210 gobottest.Assert(t, d.calculateJoystickValue(float64(10), float64(5)), float64(5)) 211 gobottest.Assert(t, d.calculateJoystickValue(float64(5), float64(10)), float64(-5)) 212 } 213 214 func TestWiichuckDriverIsEncrypted(t *testing.T) { 215 d := initTestWiichuckDriverWithStubbedAdaptor() 216 217 encryptedValue := []byte{1, 1, 2, 2, 3, 3} 218 gobottest.Assert(t, d.isEncrypted(encryptedValue), true) 219 220 encryptedValue = []byte{42, 42, 24, 24, 30, 30} 221 gobottest.Assert(t, d.isEncrypted(encryptedValue), true) 222 223 decryptedValue := []byte{1, 2, 3, 4, 5, 6} 224 gobottest.Assert(t, d.isEncrypted(decryptedValue), false) 225 226 decryptedValue = []byte{1, 1, 2, 2, 5, 6} 227 gobottest.Assert(t, d.isEncrypted(decryptedValue), false) 228 229 decryptedValue = []byte{1, 1, 2, 3, 3, 3} 230 gobottest.Assert(t, d.isEncrypted(decryptedValue), false) 231 } 232 233 func TestWiichuckDriverDecode(t *testing.T) { 234 d := initTestWiichuckDriverWithStubbedAdaptor() 235 236 gobottest.Assert(t, d.decode(byte(0)), float64(46)) 237 gobottest.Assert(t, d.decode(byte(100)), float64(138)) 238 gobottest.Assert(t, d.decode(byte(200)), float64(246)) 239 gobottest.Assert(t, d.decode(byte(254)), float64(0)) 240 } 241 242 func TestWiichuckDriverParse(t *testing.T) { 243 d := initTestWiichuckDriverWithStubbedAdaptor() 244 245 gobottest.Assert(t, d.data["sx"], float64(0)) 246 gobottest.Assert(t, d.data["sy"], float64(0)) 247 gobottest.Assert(t, d.data["z"], float64(0)) 248 gobottest.Assert(t, d.data["c"], float64(0)) 249 250 // First pass 251 d.parse([]byte{12, 23, 34, 45, 56, 67}) 252 253 gobottest.Assert(t, d.data["sx"], float64(50)) 254 gobottest.Assert(t, d.data["sy"], float64(23)) 255 gobottest.Assert(t, d.data["z"], float64(1)) 256 gobottest.Assert(t, d.data["c"], float64(2)) 257 258 // Second pass 259 d.parse([]byte{70, 81, 92, 103, 204, 205}) 260 261 gobottest.Assert(t, d.data["sx"], float64(104)) 262 gobottest.Assert(t, d.data["sy"], float64(93)) 263 gobottest.Assert(t, d.data["z"], float64(1)) 264 gobottest.Assert(t, d.data["c"], float64(0)) 265 } 266 267 func TestWiichuckDriverAdjustOrigins(t *testing.T) { 268 d := initTestWiichuckDriverWithStubbedAdaptor() 269 270 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(-1)) 271 gobottest.Assert(t, d.Joystick()["sx_origin"], float64(-1)) 272 273 // First pass 274 d.parse([]byte{1, 2, 3, 4, 5, 6}) 275 d.adjustOrigins() 276 277 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(44)) 278 gobottest.Assert(t, d.Joystick()["sx_origin"], float64(45)) 279 280 // Second pass 281 d = initTestWiichuckDriverWithStubbedAdaptor() 282 283 d.parse([]byte{61, 72, 83, 94, 105, 206}) 284 d.adjustOrigins() 285 286 gobottest.Assert(t, d.Joystick()["sy_origin"], float64(118)) 287 gobottest.Assert(t, d.Joystick()["sx_origin"], float64(65)) 288 } 289 290 func TestWiichuckDriverSetName(t *testing.T) { 291 d := initTestWiichuckDriverWithStubbedAdaptor() 292 d.SetName("TESTME") 293 gobottest.Assert(t, d.Name(), "TESTME") 294 } 295 296 func TestWiichuckDriverOptions(t *testing.T) { 297 d := NewWiichuckDriver(newI2cTestAdaptor(), WithBus(2)) 298 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 299 }