aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-01-25 03:11:01 +0100
committerDimitri Sokolyuk <demon@dim13.org>2017-01-25 03:11:01 +0100
commit9cbf4e50a3b0b8f20740de79e33afb9902d7e307 (patch)
treea61909413fc5f35a62ba66c7dc362b2a6440b372
parent427998e3506be484caeec94ab720cefbfebf35f2 (diff)
wip
-rw-r--r--car/car.ino1
-rw-r--r--car/elegoo/main.go32
2 files changed, 17 insertions, 16 deletions
diff --git a/car/car.ino b/car/car.ino
index 68c07f1..27d892f 100644
--- a/car/car.ino
+++ b/car/car.ino
@@ -144,6 +144,7 @@ void loop() {
int d = distance();
if (d > 0) {
evt.Distance = d;
+ evt.has_Distance = true;
pb_encode_delimited(&output, Event_fields, &evt);
}
diff --git a/car/elegoo/main.go b/car/elegoo/main.go
index 9171102..0ea3502 100644
--- a/car/elegoo/main.go
+++ b/car/elegoo/main.go
@@ -10,25 +10,21 @@ import (
)
func Write(w io.Writer, buf []byte) {
- w.Write([]byte{byte(len(buf))})
+ sz := proto.EncodeVarint(uint64(len(buf)))
+ w.Write(sz)
w.Write(buf)
}
func Read(r io.Reader) []byte {
- buf := [32]byte{}
- n, _ := r.Read(buf[:])
- return buf[:n]
- /*
- sz := [1]byte{}
- r.Read(sz[:])
- log.Println(sz)
- if l := int(sz[0]); l > 0 {
- buf := make([]byte, l)
- r.Read(buf)
- return buf
- }
- return nil
- */
+ buf := [10]byte{}
+ r.Read(buf[:])
+ sz, n := proto.DecodeVarint(buf[:])
+
+ nbuf := make([]byte, int(sz))
+ copy(nbuf, buf[int(n):])
+
+ io.ReadFull(r, nbuf[int(10-n):])
+ return nbuf
}
func varint(b []byte) []byte {
@@ -70,7 +66,11 @@ func main() {
go func() {
for {
- log.Printf("Got: %v", Read(s))
+ buf := Read(s)
+ log.Printf("Got: %v", buf)
+ evt := &Event{}
+ proto.Unmarshal(buf, evt)
+ log.Println(evt)
}
}()