Switching to currantlabs fork of gatt, and some related refactoring

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-07-03 10:51:20 +02:00
parent a589be6491
commit 9b779e40ab
2 changed files with 17 additions and 17 deletions

View File

@ -2,7 +2,9 @@ package main
import (
"fmt"
"time"
"os"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/ble"
)
@ -10,11 +12,13 @@ import (
func main() {
gbot := gobot.NewGobot()
bleAdaptor := ble.NewBLEAdaptor("ble", "D0:39:72:C9:9E:5A")
bleAdaptor := ble.NewBLEAdaptor("ble", os.Args[1])
battery := ble.NewBLEBatteryDriver(bleAdaptor, "battery")
work := func() {
fmt.Println("Battery level:", battery.GetBatteryLevel())
gobot.Every(5*time.Second, func() {
fmt.Println("Battery level:", battery.GetBatteryLevel())
})
}
robot := gobot.NewRobot("bleBot",

View File

@ -3,7 +3,7 @@ package ble
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/paypal/gatt"
"github.com/currantlabs/gatt"
"log"
"strings"
)
@ -24,8 +24,7 @@ type BLEAdaptor struct {
peripheral gatt.Peripheral
services map[string]*BLEService
connected bool
ready chan struct{}
//connect func(string) (io.ReadWriteCloser, error)
ready chan struct{}
}
// NewBLEAdaptor returns a new BLEAdaptor given a name and uuid
@ -36,9 +35,6 @@ func NewBLEAdaptor(name string, uuid string) *BLEAdaptor {
connected: false,
ready: make(chan struct{}),
services: make(map[string]*BLEService),
// connect: func(port string) (io.ReadWriteCloser, error) {
// return serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
// },
}
}
@ -58,12 +54,12 @@ func (b *BLEAdaptor) Connect() (errs []error) {
// Register handlers.
device.Handle(
gatt.PeripheralDiscovered(b.onDiscovered),
gatt.PeripheralConnected(b.onConnected),
gatt.PeripheralDisconnected(b.onDisconnected),
gatt.PeripheralDiscovered(b.DiscoveryHandler),
gatt.PeripheralConnected(b.ConnectHandler),
gatt.PeripheralDisconnected(b.DisconnectHandler),
)
device.Init(b.onStateChanged)
device.Init(b.StateChangeHandler)
<-b.ready
// TODO: make sure peripheral currently exists for this UUID before returning
return nil
@ -109,7 +105,7 @@ func (b *BLEAdaptor) ReadCharacteristic(sUUID string, cUUID string) (data []byte
return val, nil
}
func (b *BLEAdaptor) onStateChanged(d gatt.Device, s gatt.State) {
func (b *BLEAdaptor) StateChangeHandler(d gatt.Device, s gatt.State) {
fmt.Println("State:", s)
switch s {
case gatt.StatePoweredOn:
@ -121,7 +117,7 @@ func (b *BLEAdaptor) onStateChanged(d gatt.Device, s gatt.State) {
}
}
func (b *BLEAdaptor) onDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
func (b *BLEAdaptor) DiscoveryHandler(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
id := strings.ToUpper(b.UUID())
if strings.ToUpper(p.ID()) != id {
return
@ -134,7 +130,7 @@ func (b *BLEAdaptor) onDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi
p.Device().Connect(p)
}
func (b *BLEAdaptor) onConnected(p gatt.Peripheral, err error) {
func (b *BLEAdaptor) ConnectHandler(p gatt.Peripheral, err error) {
fmt.Printf("\nConnected Peripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name())
b.peripheral = p
@ -168,7 +164,7 @@ func (b *BLEAdaptor) onConnected(p gatt.Peripheral, err error) {
//defer p.Device().CancelConnection(p)
}
func (b *BLEAdaptor) onDisconnected(p gatt.Peripheral, err error) {
func (b *BLEAdaptor) DisconnectHandler(p gatt.Peripheral, err error) {
fmt.Println("Disconnected")
}