Skip to content

Commit

Permalink
[ble] Correct examples to use client adaptor
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jul 9, 2016
1 parent 9630662 commit 5abc5d2
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/ble_battery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"fmt"
"time"
"os"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/ble"
)

func main() {
gbot := gobot.NewGobot()

bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1])
bleAdaptor := ble.NewBLEClientAdaptor("ble", os.Args[1])
battery := ble.NewBLEBatteryDriver(bleAdaptor, "battery")

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/ble_device_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
gbot := gobot.NewGobot()

bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1])
bleAdaptor := ble.NewBLEClientAdaptor("ble", os.Args[1])
info := ble.NewBLEDeviceInformationDriver(bleAdaptor, "info")

work := func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/minidrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func main() {
gbot := gobot.NewGobot()

bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1])
bleAdaptor := ble.NewBLEClientAdaptor("ble", os.Args[1])
drone := ble.NewBLEMinidroneDriver(bleAdaptor, "drone")

work := func() {
Expand Down
29 changes: 29 additions & 0 deletions examples/ollie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"os"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/ble"
)

func main() {
gbot := gobot.NewGobot()

bleAdaptor := ble.NewBLEClientAdaptor("ble", os.Args[1])
ollie := ble.NewSpheroOllieDriver(bleAdaptor, "ollie")

work := func() {
ollie.SetRGB(0, 255, 0)
}

robot := gobot.NewRobot("ollieBot",
[]gobot.Connection{bleAdaptor},
[]gobot.Device{ollie},
work,
)

gbot.AddRobot(robot)

gbot.Start()
}
2 changes: 1 addition & 1 deletion platforms/ble/ble_client_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (b *BLEClientAdaptor) ConnectHandler(p gatt.Peripheral, err error) {

b.peripheral = p

if err := p.SetMTU(500); err != nil {
if err := p.SetMTU(250); err != nil {
fmt.Printf("Failed to set MTU, err: %s\n", err)
}

Expand Down
133 changes: 133 additions & 0 deletions platforms/ble/ollie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package ble

import (
"bytes"
"fmt"

"github.com/hybridgroup/gobot"
)

var _ gobot.Driver = (*SpheroOllieDriver)(nil)

type SpheroOllieDriver struct {
name string
connection gobot.Connection
gobot.Eventer
}

const (
// service IDs
SpheroBLEService = "22bb746f2bb075542d6f726568705327"

// characteristic IDs
WakeCharacteristic = "22bb746f2bbf75542d6f726568705327"
TXPowerCharacteristic = "22bb746f2bb275542d6f726568705327"
AntiDosCharacteristic = "22bb746f2bbd75542d6f726568705327"
RobotControlService = "22bb746f2ba075542d6f726568705327"
CommandsCharacteristic = "22bb746f2ba175542d6f726568705327"
ResponseCharacteristic = "22bb746f2ba675542d6f726568705327"
)

// NewSpheroOllieDriver creates a SpheroOllieDriver by name
func NewSpheroOllieDriver(a *BLEClientAdaptor, name string) *SpheroOllieDriver {
n := &SpheroOllieDriver{
name: name,
connection: a,
Eventer: gobot.NewEventer(),
}

return n
}
func (b *SpheroOllieDriver) Connection() gobot.Connection { return b.connection }
func (b *SpheroOllieDriver) Name() string { return b.name }

// adaptor returns BLE adaptor
func (b *SpheroOllieDriver) adaptor() *BLEClientAdaptor {
return b.Connection().(*BLEClientAdaptor)
}

// Start tells driver to get ready to do work
func (b *SpheroOllieDriver) Start() (errs []error) {
b.Init()

return
}

// Halt stops Ollie driver (void)
func (b *SpheroOllieDriver) Halt() (errs []error) {
return
}

func (b *SpheroOllieDriver) Init() (err error) {
b.AntiDOSOff()
b.SetTXPower(7)
b.Wake()

// subscribe to Sphero response notifications
b.adaptor().Subscribe(RobotControlService, ResponseCharacteristic, b.HandleResponses)

return
}

// Turns off Anti-DOS code so we can control Ollie
func (b *SpheroOllieDriver) AntiDOSOff() (err error) {
str := "011i3"
buf := &bytes.Buffer{}
buf.WriteString(str)

err = b.adaptor().WriteCharacteristic(SpheroBLEService, AntiDosCharacteristic, buf.Bytes())
if err != nil {
fmt.Println("AntiDOSOff error:", err)
return err
}

return
}

// Wakes Ollie up so we can play
func (b *SpheroOllieDriver) Wake() (err error) {
buf := []byte{0x01}

err = b.adaptor().WriteCharacteristic(SpheroBLEService, WakeCharacteristic, buf)
if err != nil {
fmt.Println("Wake error:", err)
return err
}

return
}

// Sets transmit level
func (b *SpheroOllieDriver) SetTXPower(level int) (err error) {
buf := []byte{byte(level)}

err = b.adaptor().WriteCharacteristic(SpheroBLEService, TXPowerCharacteristic, buf)
if err != nil {
fmt.Println("SetTXLevel error:", err)
return err
}

return
}

// Handle responses returned from Ollie
func (b *SpheroOllieDriver) HandleResponses(data []byte, e error) {
fmt.Println("response data:", data)

return
}

// SetRGB sets the Ollie to the given r, g, and b values
func (s *SpheroOllieDriver) SetRGB(r uint8, g uint8, b uint8) {
fmt.Println("setrgb")
}

// Tells the Ollie to roll
func (s *SpheroOllieDriver) Roll(speed uint8, heading uint16) {
fmt.Println("roll", speed, heading)
}

// Tells the Ollie to stop
func (s *SpheroOllieDriver) Stop() {
s.Roll(0, 0)
}

0 comments on commit 5abc5d2

Please sign in to comment.