gobot.io/x/gobot/v2@v2.1.0/platforms/sphero/ollie/ollie_driver_test.go (about)

     1  package ollie
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"gobot.io/x/gobot/v2"
    11  	"gobot.io/x/gobot/v2/gobottest"
    12  	"gobot.io/x/gobot/v2/platforms/sphero"
    13  )
    14  
    15  var _ gobot.Driver = (*Driver)(nil)
    16  
    17  func initTestOllieDriver() *Driver {
    18  	d := NewDriver(NewBleTestAdaptor())
    19  	return d
    20  }
    21  
    22  func TestOllieDriver(t *testing.T) {
    23  	d := initTestOllieDriver()
    24  	d.SetName("NewName")
    25  	gobottest.Assert(t, d.Name(), "NewName")
    26  }
    27  
    28  func TestOllieDriverStartAndHalt(t *testing.T) {
    29  	d := initTestOllieDriver()
    30  	gobottest.Assert(t, d.Start(), nil)
    31  	gobottest.Assert(t, d.Halt(), nil)
    32  }
    33  
    34  func TestLocatorData(t *testing.T) {
    35  	d := initTestOllieDriver()
    36  
    37  	tables := []struct {
    38  		x1 byte
    39  		x2 byte
    40  		y1 byte
    41  		y2 byte
    42  		x  int16
    43  		y  int16
    44  	}{
    45  		{0x00, 0x05, 0x00, 0x05, 5, 5},
    46  		{0x00, 0x00, 0x00, 0x00, 0, 0},
    47  		{0x00, 0x0A, 0x00, 0xF0, 10, 240},
    48  		{0x01, 0x00, 0x01, 0x00, 256, 256},
    49  		{0xFF, 0xFE, 0xFF, 0xFE, -1, -1},
    50  	}
    51  
    52  	for _, point := range tables {
    53  		//0x0B is the locator ID
    54  		packet := []byte{0xFF, 0xFF, 0x00, 0x00, 0x0B, point.x1, point.x2, point.y1, point.y2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    55  
    56  		d.GetLocatorData(func(p Point2D) {
    57  			gobottest.Assert(t, p.Y, point.y)
    58  		})
    59  		d.HandleResponses(packet, nil)
    60  	}
    61  }
    62  
    63  func TestDataStreaming(t *testing.T) {
    64  	d := initTestOllieDriver()
    65  
    66  	d.SetDataStreamingConfig(sphero.DefaultDataStreamingConfig())
    67  
    68  	response := false
    69  	d.On("sensordata", func(data interface{}) {
    70  		cont := data.(DataStreamingPacket)
    71  		fmt.Printf("got streaming packet: %+v \n", cont)
    72  		gobottest.Assert(t, cont.RawAccX, int16(10))
    73  		response = true
    74  	})
    75  
    76  	//example data packet
    77  	p1 := []string{"FFFE030053000A003900FAFFFE0007FFFF000000",
    78  		"000000000000000000FFECFFFB00010000004B01",
    79  		"BD1034FFFF000300000000000000000000000000",
    80  		"0000002701FDE500560000000000000065000000",
    81  		"0000000000000071",
    82  	}
    83  
    84  	for _, elem := range p1 {
    85  		var bytes []byte
    86  		for i := 0; i < len([]rune(elem)); i += 2 {
    87  			a := []rune(elem)[i : i+2]
    88  			b, _ := strconv.ParseUint(string(a), 16, 16)
    89  			c := uint16(b)
    90  			bytes = append(bytes, byte(c))
    91  		}
    92  		d.HandleResponses(bytes, nil)
    93  
    94  	}
    95  
    96  	//send empty packet to indicate start of next message
    97  	d.HandleResponses([]byte{0xFF}, nil)
    98  	time.Sleep(10 * time.Millisecond)
    99  	if response == false {
   100  		t.Error("no response recieved")
   101  	}
   102  }
   103  
   104  func parseBytes(s string) (f byte) {
   105  	i, err := strconv.ParseUint(s, 16, 32)
   106  	if err != nil {
   107  		return
   108  	}
   109  
   110  	f = byte(math.Float32frombits(uint32(i)))
   111  
   112  	return
   113  }