[ble] Startup Pcmd

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-07-04 22:53:54 +02:00
parent eedc5081ed
commit a95af64ecb
3 changed files with 21 additions and 14 deletions

View File

@ -17,7 +17,8 @@ func main() {
work := func() {
drone.Init()
drone.StartPcmd()
gobot.On(drone.Event("battery"), func(data interface{}) {
fmt.Printf("battery: %d\n", data)
})

View File

@ -118,9 +118,9 @@ func (b *BLEAdaptor) WriteCharacteristic(sUUID string, cUUID string, data []byte
return
}
// SubscribeNotify subscribes to the BLE device for the
// Subscribe subscribes to notifications from the BLE device for the
// requested service and characteristic
func (b *BLEAdaptor) SubscribeNotify(sUUID string, cUUID string, f func([]byte, error)) (err error) {
func (b *BLEAdaptor) Subscribe(sUUID string, cUUID string, f func([]byte, error)) (err error) {
if !b.connected {
log.Fatalf("Cannot subscribe to BLE device until connected")
return

View File

@ -95,21 +95,15 @@ func (b *BLEMinidroneDriver) Start() (errs []error) {
func (b *BLEMinidroneDriver) Halt() (errs []error) { return }
func (b *BLEMinidroneDriver) Init() (err error) {
b.stepsfa0b++
buf := []byte{0x04, byte(b.stepsfa0b), 0x00, 0x04, 0x01, 0x00, 0x32, 0x30, 0x31, 0x34, 0x2D, 0x31, 0x30, 0x2D, 0x32, 0x38, 0x00}
err = b.adaptor().WriteCharacteristic("9a66fa000800919111e4012d1540cb8e", "9a66fa0b0800919111e4012d1540cb8e", buf)
if err != nil {
fmt.Println("init error:", err)
return err
}
b.GenerateAllStates()
// setup battery notifications
b.adaptor().SubscribeNotify("9a66fb000800919111e4012d1540cb8e", "9a66fb0f0800919111e4012d1540cb8e", func(data []byte, e error) {
// subscribe to battery notifications
b.adaptor().Subscribe("9a66fb000800919111e4012d1540cb8e", "9a66fb0f0800919111e4012d1540cb8e", func(data []byte, e error) {
gobot.Publish(b.Event(Battery), data[len(data)-1])
})
// setup flying status notifications
b.adaptor().SubscribeNotify("9a66fb000800919111e4012d1540cb8e", "9a66fb0e0800919111e4012d1540cb8e", func(data []byte, e error) {
// subscribe to flying status notifications
b.adaptor().Subscribe("9a66fb000800919111e4012d1540cb8e", "9a66fb0e0800919111e4012d1540cb8e", func(data []byte, e error) {
gobot.Publish(b.Event(Status), data[6])
if (data[6] == 1 || data[6] == 2) && !b.flying {
b.flying = true
@ -123,6 +117,18 @@ func (b *BLEMinidroneDriver) Init() (err error) {
return
}
func (b *BLEMinidroneDriver) GenerateAllStates() (err error) {
b.stepsfa0b++
buf := []byte{0x04, byte(b.stepsfa0b), 0x00, 0x04, 0x01, 0x00, 0x32, 0x30, 0x31, 0x34, 0x2D, 0x31, 0x30, 0x2D, 0x32, 0x38, 0x00}
err = b.adaptor().WriteCharacteristic("9a66fa000800919111e4012d1540cb8e", "9a66fa0b0800919111e4012d1540cb8e", buf)
if err != nil {
fmt.Println("GenerateAllStates error:", err)
return err
}
return
}
func (b *BLEMinidroneDriver) TakeOff() (err error) {
b.stepsfa0b++
buf := []byte{0x02, byte(b.stepsfa0b) & 0xff, 0x02, 0x00, 0x01, 0x00}