diff --git a/examples/microbit_accelerometer.go b/examples/microbit_accelerometer.go new file mode 100644 index 00000000..4da0611b --- /dev/null +++ b/examples/microbit_accelerometer.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "os" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/platforms/ble" + "gobot.io/x/gobot/platforms/microbit" +) + +func main() { + bleAdaptor := ble.NewClientAdaptor(os.Args[1]) + ubit := microbit.NewAccelerometerDriver(bleAdaptor) + + work := func() { + ubit.On(microbit.Accelerometer, func(data interface{}) { + fmt.Println("Accelerometer", data) + }) + } + + robot := gobot.NewRobot("buttonBot", + []gobot.Connection{bleAdaptor}, + []gobot.Device{ubit}, + work, + ) + + robot.Start() +} diff --git a/examples/microbit_buttons_leds.go b/examples/microbit_buttons_leds.go index 7e8e45a2..d65bfccf 100644 --- a/examples/microbit_buttons_leds.go +++ b/examples/microbit_buttons_leds.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "os" "gobot.io/x/gobot" @@ -16,7 +15,6 @@ func main() { work := func() { buttons.On(microbit.ButtonA, func(data interface{}) { - fmt.Println("a", data) if data.([]byte)[0] == 1 { leds.UpLeftArrow() return @@ -26,7 +24,6 @@ func main() { }) buttons.On(microbit.ButtonB, func(data interface{}) { - fmt.Println("b", data) if data.([]byte)[0] == 1 { leds.UpRightArrow() return diff --git a/platforms/microbit/accelerometer_driver.go b/platforms/microbit/accelerometer_driver.go new file mode 100644 index 00000000..e39cc7e5 --- /dev/null +++ b/platforms/microbit/accelerometer_driver.go @@ -0,0 +1,92 @@ +package microbit + +import ( + "bytes" + "encoding/binary" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/platforms/ble" +) + +// AccelerometerDriver is the Gobot driver for the Microbit's built-in accelerometer +type AccelerometerDriver struct { + name string + connection gobot.Connection + gobot.Eventer +} + +type RawAccelerometerData struct { + X int16 + Y int16 + Z int16 +} + +type AccelerometerData struct { + X float32 + Y float32 + Z float32 +} + +const ( + // BLE services + accelerometerService = "e95d0753251d470aa062fa1922dfa9a8" + + // BLE characteristics + accelerometerCharacteristic = "e95dca4b251d470aa062fa1922dfa9a8" + + // Accelerometer event + Accelerometer = "accelerometer" +) + +// NewAccelerometerDriver creates a Microbit AccelerometerDriver +func NewAccelerometerDriver(a *ble.ClientAdaptor) *AccelerometerDriver { + n := &AccelerometerDriver{ + name: gobot.DefaultName("Microbit Accelerometer"), + connection: a, + Eventer: gobot.NewEventer(), + } + + n.AddEvent(Accelerometer) + + return n +} + +// Connection returns the BLE connection +func (b *AccelerometerDriver) Connection() gobot.Connection { return b.connection } + +// Name returns the Driver Name +func (b *AccelerometerDriver) Name() string { return b.name } + +// SetName sets the Driver Name +func (b *AccelerometerDriver) SetName(n string) { b.name = n } + +// adaptor returns BLE adaptor +func (b *AccelerometerDriver) adaptor() *ble.ClientAdaptor { + return b.Connection().(*ble.ClientAdaptor) +} + +// Start tells driver to get ready to do work +func (b *AccelerometerDriver) Start() (err error) { + // subscribe to button A notifications + b.adaptor().Subscribe(accelerometerCharacteristic, func(data []byte, e error) { + a := &RawAccelerometerData{X: 0, Y: 0, Z: 0} + + buf := bytes.NewBuffer(data) + binary.Read(buf, binary.LittleEndian, &a.X) + binary.Read(buf, binary.LittleEndian, &a.Y) + binary.Read(buf, binary.LittleEndian, &a.Z) + + result := &AccelerometerData{X: float32(a.X) / 1000.0, + Y: float32(a.Y) / 1000.0, + Z: float32(a.Z) / 1000.0} + + b.Publish(b.Event(Accelerometer), result) + }) + + return +} + +// Halt stops LED driver (void) +func (b *AccelerometerDriver) Halt() (err error) { + return +} diff --git a/platforms/microbit/accelerometer_driver_test.go b/platforms/microbit/accelerometer_driver_test.go new file mode 100644 index 00000000..159d231d --- /dev/null +++ b/platforms/microbit/accelerometer_driver_test.go @@ -0,0 +1,23 @@ +package microbit + +import ( + "strings" + "testing" + + "gobot.io/x/gobot" + "gobot.io/x/gobot/gobottest" + + "gobot.io/x/gobot/platforms/ble" +) + +var _ gobot.Driver = (*AccelerometerDriver)(nil) + +func initTestAccelerometerDriver() *AccelerometerDriver { + d := NewAccelerometerDriver(ble.NewClientAdaptor("D7:99:5A:26:EC:38")) + return d +} + +func TestAccelerometerDriver(t *testing.T) { + d := initTestAccelerometerDriver() + gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Accelerometer"), true) +}