2016-12-07 02:16:33 +08:00
|
|
|
package ollie
|
|
|
|
|
2016-12-23 16:36:47 +08:00
|
|
|
import (
|
2019-01-23 21:30:55 +08:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2017-02-02 23:43:50 +08:00
|
|
|
"testing"
|
2019-01-23 21:30:55 +08:00
|
|
|
"time"
|
2016-12-23 16:36:47 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/sphero"
|
2016-12-23 16:36:47 +08:00
|
|
|
)
|
2016-12-07 02:16:33 +08:00
|
|
|
|
|
|
|
var _ gobot.Driver = (*Driver)(nil)
|
2016-12-23 16:36:47 +08:00
|
|
|
|
|
|
|
func initTestOllieDriver() *Driver {
|
2017-04-05 18:57:50 +08:00
|
|
|
d := NewDriver(NewBleTestAdaptor())
|
2016-12-23 16:36:47 +08:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOllieDriver(t *testing.T) {
|
|
|
|
d := initTestOllieDriver()
|
2017-04-05 18:57:50 +08:00
|
|
|
d.SetName("NewName")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "NewName", d.Name())
|
2017-04-05 18:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOllieDriverStartAndHalt(t *testing.T) {
|
|
|
|
d := initTestOllieDriver()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
|
|
|
require.NoError(t, d.Halt())
|
2016-12-23 16:36:47 +08:00
|
|
|
}
|
2019-01-23 21:30:55 +08:00
|
|
|
|
|
|
|
func TestLocatorData(t *testing.T) {
|
|
|
|
d := initTestOllieDriver()
|
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
x1 byte
|
|
|
|
x2 byte
|
|
|
|
y1 byte
|
|
|
|
y2 byte
|
|
|
|
x int16
|
|
|
|
y int16
|
|
|
|
}{
|
|
|
|
{0x00, 0x05, 0x00, 0x05, 5, 5},
|
|
|
|
{0x00, 0x00, 0x00, 0x00, 0, 0},
|
|
|
|
{0x00, 0x0A, 0x00, 0xF0, 10, 240},
|
|
|
|
{0x01, 0x00, 0x01, 0x00, 256, 256},
|
|
|
|
{0xFF, 0xFE, 0xFF, 0xFE, -1, -1},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, point := range tables {
|
2023-10-21 02:50:42 +08:00
|
|
|
// 0x0B is the locator ID
|
2023-11-12 02:55:47 +08:00
|
|
|
packet := []byte{
|
|
|
|
0xFF, 0xFF, 0x00, 0x00, 0x0B, point.x1, point.x2, point.y1, point.y2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
}
|
2019-01-23 21:30:55 +08:00
|
|
|
|
|
|
|
d.GetLocatorData(func(p Point2D) {
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, point.y, p.Y)
|
2019-01-23 21:30:55 +08:00
|
|
|
})
|
|
|
|
d.HandleResponses(packet, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataStreaming(t *testing.T) {
|
|
|
|
d := initTestOllieDriver()
|
|
|
|
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.SetDataStreamingConfig(sphero.DefaultDataStreamingConfig())
|
2019-01-23 21:30:55 +08:00
|
|
|
|
2023-11-12 02:55:47 +08:00
|
|
|
responseChan := make(chan bool)
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.On("sensordata", func(data interface{}) {
|
2019-01-23 21:30:55 +08:00
|
|
|
cont := data.(DataStreamingPacket)
|
|
|
|
fmt.Printf("got streaming packet: %+v \n", cont)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, int16(10), cont.RawAccX)
|
2023-11-12 02:55:47 +08:00
|
|
|
responseChan <- true
|
2019-01-23 21:30:55 +08:00
|
|
|
})
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// example data packet
|
|
|
|
p1 := []string{
|
|
|
|
"FFFE030053000A003900FAFFFE0007FFFF000000",
|
2019-01-23 21:30:55 +08:00
|
|
|
"000000000000000000FFECFFFB00010000004B01",
|
|
|
|
"BD1034FFFF000300000000000000000000000000",
|
|
|
|
"0000002701FDE500560000000000000065000000",
|
|
|
|
"0000000000000071",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, elem := range p1 {
|
|
|
|
var bytes []byte
|
|
|
|
for i := 0; i < len([]rune(elem)); i += 2 {
|
|
|
|
a := []rune(elem)[i : i+2]
|
|
|
|
b, _ := strconv.ParseUint(string(a), 16, 16)
|
|
|
|
c := uint16(b)
|
|
|
|
bytes = append(bytes, byte(c))
|
|
|
|
}
|
|
|
|
d.HandleResponses(bytes, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:50:42 +08:00
|
|
|
// send empty packet to indicate start of next message
|
2019-01-23 21:30:55 +08:00
|
|
|
d.HandleResponses([]byte{0xFF}, nil)
|
2023-11-12 02:55:47 +08:00
|
|
|
select {
|
|
|
|
case <-responseChan:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2023-10-21 02:50:42 +08:00
|
|
|
t.Error("no response received")
|
2019-01-23 21:30:55 +08:00
|
|
|
}
|
|
|
|
}
|