Skip to content

Commit

Permalink
ble: more WIP on multiple ble devices
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Feb 14, 2017
1 parent 3344273 commit 5f93b1e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
53 changes: 53 additions & 0 deletions examples/ollie_multiple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
To run this example, pass the BLE address or BLE name as first param:
go run examples/ollie_multiple.go 2B-1234 2B-5678
NOTE: sudo is required to use BLE in Linux
*/

package main

import (
"os"
"time"

"gobot.io/x/gobot"
"gobot.io/x/gobot/api"
"gobot.io/x/gobot/platforms/ble"
"gobot.io/x/gobot/platforms/sphero/ollie"
)

func NewSwarmBot(port string) *gobot.Robot {
bleAdaptor := ble.NewClientAdaptor(port)
ollieDriver := ollie.NewDriver(bleAdaptor)

work := func() {
gobot.Every(1*time.Second, func() {
ollieDriver.SetRGB(uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
uint8(gobot.Rand(255)),
)
})
}

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

return robot
}

func main() {
master := gobot.NewMaster()
api.NewAPI(master).Start()

for _, port := range os.Args[1:] {
bot := NewSwarmBot(port)
master.AddRobot(bot)
}

master.Start()
}
61 changes: 50 additions & 11 deletions platforms/ble/ble_client_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@ package ble

import (
"context"
"fmt"
"log"
"strings"
"time"
"sync"

"gobot.io/x/gobot"

blelib "github.com/currantlabs/ble"
"github.com/pkg/errors"
)

// newBLEDevice is constructor about blelib HCI device connection
func newBLEDevice(impl string) (d blelib.Device, err error) {
return defaultDevice(impl)
var currentDevice *blelib.Device
var bleMutex sync.Mutex
var bleCtx context.Context

// getBLEDevice is singleton for blelib HCI device connection
func getBLEDevice(impl string) (d *blelib.Device, err error) {
if currentDevice != nil {
fmt.Println("getBLEDevice", currentDevice)
return currentDevice, nil
}

fmt.Println("getBLEDevice defaultDevice")
dev, e := defaultDevice(impl)
if e != nil {
fmt.Println("can't get device")
return nil, errors.Wrap(e, "can't get device")
}
fmt.Println("getBLEDevice SetDefaultDevice")
blelib.SetDefaultDevice(dev)

currentDevice = &dev
d = &dev
return
}

// ClientAdaptor represents a Client Connection to a BLE Peripheral
Expand All @@ -23,7 +44,7 @@ type ClientAdaptor struct {
address string

addr blelib.Addr
device blelib.Device
device *blelib.Device
client blelib.Client
profile *blelib.Profile

Expand Down Expand Up @@ -51,26 +72,35 @@ func (b *ClientAdaptor) Address() string { return b.address }

// Connect initiates a connection to the BLE peripheral. Returns true on successful connection.
func (b *ClientAdaptor) Connect() (err error) {
d, err := newBLEDevice("default")
bleMutex.Lock()
defer bleMutex.Unlock()

b.device, err = getBLEDevice("default")
if err != nil {
return errors.Wrap(err, "can't new device")
return errors.Wrap(err, "can't connect")
}
blelib.SetDefaultDevice(d)
b.device = d

var cln blelib.Client

ctx := blelib.WithSigHandler(context.WithTimeout(context.Background(), 3*time.Second))
cln, err = blelib.Connect(ctx, filter(b.Address()))
//ctx := blelib.WithSigHandler(context.WithTimeout(context.Background(), 5*time.Second))
if bleCtx == nil {
bleCtx = context.Background()
}

fmt.Println("Connect", b.Address())
//ctx := blelib.WithSigHandler(context.WithTimeout(bleCtx, 10*time.Second))
cln, err = blelib.Connect(context.Background(), filter(b.Address()))
if err != nil {
return errors.Wrap(err, "can't connect")
}

fmt.Println("Connected", b.Address())
b.addr = cln.Address()
b.address = cln.Address().String()
b.SetName(cln.Name())
b.client = cln

fmt.Println("DiscoverProfile", b.Address())
p, err := b.client.DiscoverProfile(true)
if err != nil {
return errors.Wrap(err, "can't discover profile")
Expand Down Expand Up @@ -110,6 +140,9 @@ func (b *ClientAdaptor) ReadCharacteristic(cUUID string) (data []byte, err error
return
}

// bleMutex.Lock()
// defer bleMutex.Unlock()

uuid, _ := blelib.Parse(cUUID)

if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
Expand All @@ -127,6 +160,9 @@ func (b *ClientAdaptor) WriteCharacteristic(cUUID string, data []byte) (err erro
return
}

// bleMutex.Lock()
// defer bleMutex.Unlock()

uuid, _ := blelib.Parse(cUUID)

if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
Expand All @@ -144,6 +180,9 @@ func (b *ClientAdaptor) Subscribe(cUUID string, f func([]byte, error)) (err erro
return
}

// bleMutex.Lock()
// defer bleMutex.Unlock()

uuid, _ := blelib.Parse(cUUID)

if u := b.profile.Find(blelib.NewCharacteristic(uuid)); u != nil {
Expand Down

0 comments on commit 5f93b1e

Please sign in to comment.