forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathendpoint_custom.go
More file actions
74 lines (61 loc) · 1.74 KB
/
endpoint_custom.go
File metadata and controls
74 lines (61 loc) · 1.74 KB
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
// +build ignore
package main
import (
"fmt"
"github.com/team-rocos/gomavlib"
"github.com/team-rocos/gomavlib/dialects/ardupilotmega"
)
// 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 == false {
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 a custom endpoint
// - understands ardupilotmega dialect
// - writes messages with given system id
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 write to 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 every message we receive
for evt := range node.Events() {
if frm, ok := evt.(*gomavlib.EventFrame); ok {
fmt.Printf("received: id=%d, %+v\n", frm.Message().GetId(), frm.Message())
}
}
}