aboutsummaryrefslogtreecommitdiff
path: root/car/elegoo/main.go
blob: e44fb6a9ce00ca6d053aa45c3bd8e02f883096ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

//go:generate sh -c "protoc --go_out=. *.proto"
//go:generate sh -c "protoc --nanopb_out=.. *.proto"

import (
	"io"
	"log"
	"time"

	"github.com/golang/protobuf/proto"
	"github.com/tarm/serial"
)

func Write(w io.Writer, buf []byte) {
	sz := proto.EncodeVarint(uint64(len(buf)))
	w.Write(append(sz, buf...))
}

func Read(r io.Reader) []byte {
	buf := make([]byte, 1)
	r.Read(buf)
	sz, _ := proto.DecodeVarint(buf)
	nbuf := make([]byte, int(sz))
	io.ReadFull(r, nbuf)
	return nbuf
}

func varint(b []byte) []byte {
	return append([]byte{byte(len(b))}, b...)
}

// /dev/cu.Elegoo-DevB
// /dev/cu.usbmodem1421

func main() {
	c := &serial.Config{
		Name: "/dev/cu.usbmodem1421",
		Baud: 57600,
	}
	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()

	cmd := &Command{
		SetSpeed: &Speed{
			Left:  100,
			Right: 120,
		},
	}
	buf, err := proto.Marshal(cmd)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Send: %x", buf)
	//Write(s, buf)

	go func() {
		for {
			buf := Read(s)
			log.Printf("Got: %x", buf)
			evt := &Event{}
			proto.Unmarshal(buf, evt)
			log.Println(evt)
		}
	}()

	time.Sleep(time.Minute)
}