2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert,dupl // ok here
|
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
|
|
|
|
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-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2017-03-01 08:23:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "Microbit Accelerometer"))
|
2017-04-05 18:40:17 +08:00
|
|
|
d.SetName("NewName")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "NewName", d.Name())
|
2017-04-05 18:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccelerometerDriverStartAndHalt(t *testing.T) {
|
|
|
|
d := initTestAccelerometerDriver()
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
|
|
|
require.NoError(t, d.Halt())
|
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)
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = d.Start()
|
|
|
|
_ = d.On(Accelerometer, func(data interface{}) {
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, float32(8.738), data.(*AccelerometerData).X, 0.0)
|
|
|
|
assert.InDelta(t, float32(8.995), data.(*AccelerometerData).Y, 0.0)
|
|
|
|
assert.InDelta(t, float32(9.252), data.(*AccelerometerData).Z, 0.0)
|
2017-04-06 03:56:41 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|