microbit: add magnetometer driver
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
b538f4db80
commit
88a260a221
|
@ -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.NewMagnetometerDriver(bleAdaptor)
|
||||
|
||||
work := func() {
|
||||
ubit.On(microbit.Magnetometer, func(data interface{}) {
|
||||
fmt.Println("Magnetometer", data)
|
||||
})
|
||||
}
|
||||
|
||||
robot := gobot.NewRobot("magnetoBot",
|
||||
[]gobot.Connection{bleAdaptor},
|
||||
[]gobot.Device{ubit},
|
||||
work,
|
||||
)
|
||||
|
||||
robot.Start()
|
||||
}
|
|
@ -7,6 +7,12 @@ The [Microbit](http://microbit.org/) is a tiny computer with built-in Bluetooth
|
|||
go get -d -u gobot.io/x/gobot/... && go install gobot.io/x/gobot/platforms/microbit
|
||||
```
|
||||
|
||||
You must install the Microbit firmware from [@sandeepmistry] located at [https://github.com/sandeepmistry/node-bbc-microbit](https://github.com/sandeepmistry/node-bbc-microbit) to use the Microbit with Gobot. This firmware is based on the micro:bit template, but with a few changes.
|
||||
|
||||
You can either use the [Gort](https://gort.io) command line tool's `gort microbit` commands, or follow the firmware installation instructions at [https://github.com/sandeepmistry/node-bbc-microbit#flashing-microbit-firmware](https://github.com/sandeepmistry/node-bbc-microbit#flashing-microbit-firmware).
|
||||
|
||||
The source code for the firmware is located at [https://github.com/sandeepmistry/node-bbc-microbit/files/546610/node-bbc-microbit_zip_nrf51_microbit.zip](https://github.com/sandeepmistry/node-bbc-microbit/files/546610/node-bbc-microbit_zip_nrf51_microbit.zip) however you do not need this source code to install the firmware using the installation instructions.
|
||||
|
||||
## How to Use
|
||||
|
||||
The Gobot platform for the Microbit includes several different drivers, each one corresponding to a different capability:
|
||||
|
|
|
@ -67,7 +67,7 @@ func (b *AccelerometerDriver) adaptor() *ble.ClientAdaptor {
|
|||
|
||||
// Start tells driver to get ready to do work
|
||||
func (b *AccelerometerDriver) Start() (err error) {
|
||||
// subscribe to button A notifications
|
||||
// subscribe to accelerometer notifications
|
||||
b.adaptor().Subscribe(accelerometerCharacteristic, func(data []byte, e error) {
|
||||
a := &RawAccelerometerData{X: 0, Y: 0, Z: 0}
|
||||
|
||||
|
@ -76,7 +76,8 @@ func (b *AccelerometerDriver) Start() (err error) {
|
|||
binary.Read(buf, binary.LittleEndian, &a.Y)
|
||||
binary.Read(buf, binary.LittleEndian, &a.Z)
|
||||
|
||||
result := &AccelerometerData{X: float32(a.X) / 1000.0,
|
||||
result := &AccelerometerData{
|
||||
X: float32(a.X) / 1000.0,
|
||||
Y: float32(a.Y) / 1000.0,
|
||||
Z: float32(a.Z) / 1000.0}
|
||||
|
||||
|
|
|
@ -0,0 +1,93 @@
|
|||
package microbit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
|
||||
"gobot.io/x/gobot"
|
||||
"gobot.io/x/gobot/platforms/ble"
|
||||
)
|
||||
|
||||
// MagnetometerDriver is the Gobot driver for the Microbit's built-in magnetometer
|
||||
type MagnetometerDriver struct {
|
||||
name string
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
type RawMagnetometerData struct {
|
||||
X int16
|
||||
Y int16
|
||||
Z int16
|
||||
}
|
||||
|
||||
type MagnetometerData struct {
|
||||
X float32
|
||||
Y float32
|
||||
Z float32
|
||||
}
|
||||
|
||||
const (
|
||||
// BLE services
|
||||
magnetometerService = "e95df2d8251d470aa062fa1922dfa9a8"
|
||||
|
||||
// BLE characteristics
|
||||
magnetometerCharacteristic = "e95dfb11251d470aa062fa1922dfa9a8"
|
||||
|
||||
// Magnetometer event
|
||||
Magnetometer = "magnetometer"
|
||||
)
|
||||
|
||||
// NewMagnetometerDriver creates a Microbit MagnetometerDriver
|
||||
func NewMagnetometerDriver(a *ble.ClientAdaptor) *MagnetometerDriver {
|
||||
n := &MagnetometerDriver{
|
||||
name: gobot.DefaultName("Microbit Magnetometer"),
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
|
||||
n.AddEvent(Magnetometer)
|
||||
|
||||
return n
|
||||
}
|
||||
|
||||
// Connection returns the BLE connection
|
||||
func (b *MagnetometerDriver) Connection() gobot.Connection { return b.connection }
|
||||
|
||||
// Name returns the Driver Name
|
||||
func (b *MagnetometerDriver) Name() string { return b.name }
|
||||
|
||||
// SetName sets the Driver Name
|
||||
func (b *MagnetometerDriver) SetName(n string) { b.name = n }
|
||||
|
||||
// adaptor returns BLE adaptor
|
||||
func (b *MagnetometerDriver) adaptor() *ble.ClientAdaptor {
|
||||
return b.Connection().(*ble.ClientAdaptor)
|
||||
}
|
||||
|
||||
// Start tells driver to get ready to do work
|
||||
func (b *MagnetometerDriver) Start() (err error) {
|
||||
// subscribe to magnetometer notifications
|
||||
b.adaptor().Subscribe(magnetometerCharacteristic, func(data []byte, e error) {
|
||||
a := &RawMagnetometerData{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 := &MagnetometerData{
|
||||
X: float32(a.X) / 1000.0,
|
||||
Y: float32(a.Y) / 1000.0,
|
||||
Z: float32(a.Z) / 1000.0}
|
||||
|
||||
b.Publish(b.Event(Magnetometer), result)
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Halt stops LED driver (void)
|
||||
func (b *MagnetometerDriver) Halt() (err error) {
|
||||
return
|
||||
}
|
|
@ -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 = (*MagnetometerDriver)(nil)
|
||||
|
||||
func initTestMagnetometerDriver() *MagnetometerDriver {
|
||||
d := NewMagnetometerDriver(ble.NewClientAdaptor("D7:99:5A:26:EC:38"))
|
||||
return d
|
||||
}
|
||||
|
||||
func TestMagnetometerDriver(t *testing.T) {
|
||||
d := initTestMagnetometerDriver()
|
||||
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Magnetometer"), true)
|
||||
}
|
Loading…
Reference in New Issue