2017-03-01 08:23:50 +08:00
|
|
|
package microbit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-04-06 03:56:41 +08:00
|
|
|
"time"
|
2017-03-01 08:23:50 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*AccelerometerDriver)(nil)
|
|
|
|
|
|
|
|
func initTestAccelerometerDriver() *AccelerometerDriver {
|
2017-04-05 18:40:17 +08:00
|
|
|
d := NewAccelerometerDriver(NewBleTestAdaptor())
|
2017-03-01 08:23:50 +08:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccelerometerDriver(t *testing.T) {
|
|
|
|
d := initTestAccelerometerDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Accelerometer"), true)
|
2017-04-05 18:40:17 +08:00
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccelerometerDriverStartAndHalt(t *testing.T) {
|
|
|
|
d := initTestAccelerometerDriver()
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2017-03-01 08:23:50 +08:00
|
|
|
}
|
2017-04-06 03:56:41 +08:00
|
|
|
|
|
|
|
func TestAccelerometerDriverReadData(t *testing.T) {
|
2023-05-19 20:16:22 +08:00
|
|
|
sem := make(chan bool)
|
2017-04-06 03:56:41 +08:00
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewAccelerometerDriver(a)
|
|
|
|
d.Start()
|
|
|
|
d.On(Accelerometer, func(data interface{}) {
|
|
|
|
gobottest.Assert(t, data.(*AccelerometerData).X, float32(8.738))
|
|
|
|
gobottest.Assert(t, data.(*AccelerometerData).Y, float32(8.995))
|
|
|
|
gobottest.Assert(t, data.(*AccelerometerData).Z, float32(9.252))
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
a.TestReceiveNotification([]byte{0x22, 0x22, 0x23, 0x23, 0x24, 0x24}, nil)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("Microbit Event \"Accelerometer\" was not published")
|
|
|
|
}
|
|
|
|
}
|