microbit: add accelerometer implementation

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-03-01 01:23:50 +01:00
parent ec9de10a10
commit 3fdf0639e1
4 changed files with 144 additions and 3 deletions

View File

@ -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()
}

View File

@ -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

View File

@ -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
}

View File

@ -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)
}