gobot.io/x/gobot@v1.16.0/drivers/i2c/wiichuck_driver_test.go (about)

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