Skip to content

Commit

Permalink
ble: test examples to excercise multiple BLE devices at once
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 5f93b1e commit b8b3f4e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/ble_multiple_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
To run this example, pass the BLE addresses or BLE names as first param:
go run examples/ble_multiple.go 2B-1234 2B-5678
NOTE: sudo is required to use BLE in Linux
*/

package main

import (
"fmt"
"os"

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

func NewSwarmBot(port string) *gobot.Robot {
bleAdaptor := ble.NewClientAdaptor(port)
access := ble.NewGenericAccessDriver(bleAdaptor)

work := func() {
fmt.Println("Device name:", access.GetDeviceName())
fmt.Println("Appearance:", access.GetAppearance())
}

robot := gobot.NewRobot("bot "+port,
[]gobot.Connection{bleAdaptor},
[]gobot.Device{access},
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()
}
51 changes: 51 additions & 0 deletions examples/ble_multiple_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
To run this example, pass the BLE addresses or BLE names as first param:
go run examples/ble_multiple.go 2B-1234 2B-5678
NOTE: sudo is required to use BLE in Linux
*/

package main

import (
"fmt"
"os"

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

func NewSwarmBot(port string) *gobot.Robot {
bleAdaptor := ble.NewClientAdaptor(port)
info := ble.NewDeviceInformationDriver(bleAdaptor)

work := func() {
fmt.Println("Model number:", info.GetModelNumber())
fmt.Println("Firmware rev:", info.GetFirmwareRevision())
fmt.Println("Hardware rev:", info.GetHardwareRevision())
fmt.Println("Manufacturer name:", info.GetManufacturerName())
fmt.Println("PnPId:", info.GetPnPId())
}

robot := gobot.NewRobot("bot "+port,
[]gobot.Connection{bleAdaptor},
[]gobot.Device{info},
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()
}

0 comments on commit b8b3f4e

Please sign in to comment.