From 7e62f728fe33fd81ccadf11eca43d424a9260d09 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 30 Dec 2016 03:26:51 +0100 Subject: tweak around --- cmd/experimental/main.go | 78 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 23 deletions(-) (limited to 'cmd') diff --git a/cmd/experimental/main.go b/cmd/experimental/main.go index d70c512..e82a366 100644 --- a/cmd/experimental/main.go +++ b/cmd/experimental/main.go @@ -59,23 +59,27 @@ func onConnect(p gatt.Peripheral, err error) { log.Println("Descriptor", d.UUID()) } */ + //log.Println("Properties", c.Properties()) if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 { if err := p.SetNotifyValue(c, onNotify); err != nil { log.Println("Set notify", err) return } } - if (c.Properties() & gatt.CharWrite) != 0 { - //p.WriteCharacteristic(c, []byte{0x01, 0x90}, true) // sdk + if (c.Properties() & (gatt.CharWrite | gatt.CharWriteNR)) != 0 { + //p.WriteCharacteristic(c, []byte{0x03, 0x90, 0x01, 0x00}, true) // sdk ? + //p.WriteCharacteristic(c, []byte{0x01, 0x8c}, true) // reset stats ? + go func(c *gatt.Characteristic) { t := time.NewTicker(time.Second * 10) defer t.Stop() for range t.C { - log.Println("Ping") + //log.Println("Ping") p.WriteCharacteristic(c, []byte{0x01, 0x16}, true) // ping time.Sleep(10 * time.Second) } }(c) + //p.WriteCharacteristic(c, []byte{0x01, 0x18}, true) // version //p.WriteCharacteristic(c, []byte{0x01, 0x1a}, true) // battery @@ -85,23 +89,29 @@ func onConnect(p gatt.Peripheral, err error) { lm.Set(*lc) lc.Set(anki.LightFrontR, anki.EffectSteady, 10, 0, 0) lm.Set(*lc) - lc.Set(anki.LightTail, anki.EffectSteady, 10, 0, 0) + lc.Set(anki.LightTail, anki.EffectSteady, 5, 0, 0) lm.Set(*lc) lb := anki.Encode(lm) p.WriteCharacteristic(c, lb, true) - time.Sleep(time.Second) - ss := &anki.VehicleMsgSetSpeed{} - ss.Set(500, 2500) - b := anki.Encode(ss) - p.WriteCharacteristic(c, b, true) + go func(c *gatt.Characteristic) { + for i := 1; i <= 5; i++ { + time.Sleep(5 * time.Second) + log.Println("Set speed") + ss := &anki.VehicleMsgSetSpeed{} + ss.Set(anki.Speed(100*i), 1000) + b := anki.Encode(ss) + p.WriteCharacteristic(c, b, true) + } + }(c) go func(c *gatt.Characteristic) { - time.Sleep(5 * time.Second) + return // XXX disable for now + time.Sleep(15 * time.Second) log.Println("Change lane") cl := &anki.VehicleMsgChangeLane{} cl.Set(100, 100, 0.0) - b = anki.Encode(cl) + b := anki.Encode(cl) p.WriteCharacteristic(c, b, true) }(c) } @@ -115,22 +125,44 @@ func onNotify(c *gatt.Characteristic, b []byte, err error) { } id, payload := anki.SplitMsg(b) switch id { + case anki.VehicleMsgV2CPingResponse: + // ignore + case anki.VehicleMsgV2CCollisionDetected: + v := anki.VehicleMsgCollisionDetected{} + anki.Decode(b, &v) + log.Printf("%+v", v) + case anki.VehicleMsgV2CCycleOvertime: + v := anki.VehicleMsgCycleOvertime{} + anki.Decode(b, &v) + log.Printf("%+v", v) + case anki.VehicleMsgV2CBatteryLevelResponse: + v := anki.VehicleMsgBatteryLevelResponse{} + anki.Decode(b, &v) + log.Printf("%+v", v) + case anki.VehicleMsgV2CVersionResponse: + v := anki.VehicleMsgVersionResponse{} + anki.Decode(b, &v) + log.Printf("%+v", v) + case anki.VehicleMsgV2CSpeedUpdate: + v := anki.VehicleMsgSpeedUpdate{} + anki.Decode(b, &v) + log.Printf("%+v", v) case anki.VehicleMsgV2CLaneChangeUpdate: - lu := anki.VehicleMsgLaneChangeUpdate{} - anki.Decode(b, &lu) - log.Printf("%+v", lu) + v := anki.VehicleMsgLaneChangeUpdate{} + anki.Decode(b, &v) + log.Printf("%+v", v) case anki.VehicleMsgV2CStatusUpdate: - su := anki.VehicleMsgStatusUpdate{} - anki.Decode(b, &su) - log.Printf("%+v", su) + v := anki.VehicleMsgStatusUpdate{} + anki.Decode(b, &v) + log.Printf("%+v", v) case anki.VehicleMsgV2CLocalizationTransitionUpdate: - tu := anki.VehicleMsgLocalizationTransitionUpdate{} - anki.Decode(b, &tu) - log.Printf("%+v", tu) + v := anki.VehicleMsgLocalizationTransitionUpdate{} + anki.Decode(b, &v) + log.Printf("%+v", v) case anki.VehicleMsgV2CLocalizationPositionUpdate: - pu := anki.VehicleLocalizationPositionUpdate{} - anki.Decode(b, &pu) - log.Printf("%+v", pu) + v := anki.VehicleLocalizationPositionUpdate{} + anki.Decode(b, &v) + log.Printf("%+v", v) default: log.Printf("ID: %v | % X\n", id, payload) } -- cgit v1.2.3