-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
75 lines (62 loc) · 1.82 KB
/
main.go
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
72
73
74
75
package main
import (
"fmt"
"log"
"github.com/bluenviron/gomavlib/v3"
"github.com/bluenviron/gomavlib/v3/pkg/dialects/ardupilotmega"
)
// this example shows how to:
// 1) create a custom endpoint from a io.ReadWriteCloser
// 2) create a node which communicates with the custom endpoint
// 3) print incoming messages
// this is an example struct that implements io.ReadWriteCloser.
// it does not read anything and prints what it receives.
// the only requirement is that Close() must release Read().
type CustomEndpoint struct {
readChan chan []byte
}
func NewCustomEndpoint() *CustomEndpoint {
return &CustomEndpoint{
readChan: make(chan []byte),
}
}
func (c *CustomEndpoint) Close() error {
close(c.readChan)
return nil
}
func (c *CustomEndpoint) Read(buf []byte) (int, error) {
read, ok := <-c.readChan
if !ok {
return 0, fmt.Errorf("all right")
}
n := copy(buf, read)
return n, nil
}
func (c *CustomEndpoint) Write(buf []byte) (int, error) {
return len(buf), nil
}
func main() {
// allocate the custom endpoint
endpoint := NewCustomEndpoint()
// create a node which communicates with the custom endpoint
node, err := gomavlib.NewNode(gomavlib.NodeConf{
Endpoints: []gomavlib.EndpointConf{
gomavlib.EndpointCustom{endpoint},
},
Dialect: ardupilotmega.Dialect,
OutVersion: gomavlib.V2, // change to V1 if you're unable to communicate with the target
OutSystemID: 10,
})
if err != nil {
panic(err)
}
defer node.Close()
// queue a dummy message
endpoint.readChan <- []byte("\xfd\t\x01\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x02\x03\x05\x03\xd9\xd1\x01\x02\x00\x00\x00\x00\x00\x0eG\x04\x0c\xef\x9b")
// print incoming messages
for evt := range node.Events() {
if frm, ok := evt.(*gomavlib.EventFrame); ok {
log.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message())
}
}
}