From fc3669937463b329a815f148106241883020388e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 29 Dec 2016 21:08:59 +0100 Subject: Lights --- cmd/experimental/main.go | 49 ++++++++++++++++++++++++++++++++---------------- protocol.go | 16 ++++++++++++++-- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/cmd/experimental/main.go b/cmd/experimental/main.go index 7c9b72a..4e93d23 100644 --- a/cmd/experimental/main.go +++ b/cmd/experimental/main.go @@ -42,23 +42,22 @@ func onConnect(p gatt.Peripheral, err error) { return } for _, s := range ss { - log.Println("Service", s.UUID()) cs, err := p.DiscoverCharacteristics(nil, s) if err != nil { log.Println("Discover Service", err) return } for _, c := range cs { - log.Println("Characteristic", c.UUID()) - ds, err := p.DiscoverDescriptors(nil, c) - if err != nil { - log.Println("Discover Descriptors", err) - return - } - for _, d := range ds { - log.Println("Descriptor", d.UUID()) - } - log.Println("Properties", c.Properties()) + /* + ds, err := p.DiscoverDescriptors(nil, c) + if err != nil { + log.Println("Discover Descriptors", err) + return + } + for _, d := range ds { + log.Println("Descriptor", d.UUID()) + } + */ if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 { if err := p.SetNotifyValue(c, onNotify); err != nil { log.Println("Set notify", err) @@ -69,18 +68,36 @@ func onConnect(p gatt.Peripheral, err error) { //c.HandleReadFunc(onRead) //log.Println("Send") //c.SetValue([]byte{0x01, 0x18}) - p.WriteCharacteristic(c, []byte{0x01, 0x90}, false) // sdk - p.WriteCharacteristic(c, []byte{0x01, 0x16}, true) // ping - p.WriteCharacteristic(c, []byte{0x01, 0x18}, true) // version - p.WriteCharacteristic(c, []byte{0x01, 0x1a}, true) // battery + //p.WriteCharacteristic(c, []byte{0x01, 0x90}, false) // sdk + p.WriteCharacteristic(c, []byte{0x01, 0x16}, false) // ping + p.WriteCharacteristic(c, []byte{0x01, 0x18}, false) // version + p.WriteCharacteristic(c, []byte{0x01, 0x1a}, false) // battery p.WriteCharacteristic(c, []byte{0x02, 0x1d, 0xff}, true) // lights + + lc := &anki.VehicleLightConfig{} + lm := &anki.VehicleMsgLightsPattern{} + lc.Set(anki.LightFrontL, anki.EffectSteady, 10, 0, 0) + lm.Set(*lc) + lc.Set(anki.LightFrontR, anki.EffectSteady, 10, 0, 0) + lm.Set(*lc) + lc.Set(anki.LightTail, anki.EffectSteady, 10, 0, 0) + lm.Set(*lc) + lb := anki.Encode(lm) + p.WriteCharacteristic(c, lb, true) + + ss := &anki.VehicleMsgSetSpeed{} + ss.Set(25000, 25000) + b := anki.Encode(ss) + p.WriteCharacteristic(c, b, true) } } } } func onNotify(c *gatt.Characteristic, b []byte, err error) { - log.Printf("notify: % X | %q\n", b, b) + if err != nil { + log.Println(c.UUID(), err) + } id, payload := anki.SplitMsg(b) log.Printf("ID: %v | % X\n", id, payload) } diff --git a/protocol.go b/protocol.go index 273967f..5c39db4 100644 --- a/protocol.go +++ b/protocol.go @@ -1,6 +1,9 @@ package anki -import "encoding/binary" +import ( + "bytes" + "encoding/binary" +) // VehicleMsgMaxSize = 20 // VehicleMsgPayloadMaxSize = 18 @@ -67,6 +70,15 @@ func SplitMsg(b []byte) (id ID, payload []byte) { return ID(b[1]), b[2:] } +func Encode(v interface{}) []byte { + buf := new(bytes.Buffer) + binary.Write(buf, binary.LittleEndian, v) + b := buf.Bytes()[:] + z := make([]byte, len(b)) + copy(z, b) // FIXME workaround "cgo argument has Go pointer to Go pointer" + return z +} + type VehicleMsgVersionResponse struct { Size uint8 MsgID ID @@ -304,7 +316,7 @@ func (v *VehicleLightConfig) Set(channel, effect uint8, start, end uint8, cycles if cpm > VehicleMaxLightTime { cpm = VehicleMaxLightTime } - v.Cycles = uint8(cpm / 6) + v.Cycles = uint8(cpm / 6) // BUG integer division } type VehicleMsgLightsPattern struct { -- cgit v1.2.3