github.com/bluenviron/gomavlib/v2@v2.2.1-0.20240308101627-2c07e3da629c/examples/dialect-custom/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/bluenviron/gomavlib/v2"
     7  	"github.com/bluenviron/gomavlib/v2/pkg/dialect"
     8  	"github.com/bluenviron/gomavlib/v2/pkg/message"
     9  )
    10  
    11  // this example shows how to:
    12  // 1) create a custom dialect from a list of messages
    13  // 2) create a node which understands the custom dialect
    14  // 3) print incoming messages
    15  
    16  // this is a custom message.
    17  // It must be prefixed with "Message" and implement the message.Message interface.
    18  type MessageCustom struct {
    19  	Param1 uint8
    20  	Param2 uint8
    21  	Param3 uint32
    22  }
    23  
    24  func (*MessageCustom) GetID() uint32 {
    25  	return 304
    26  }
    27  
    28  func main() {
    29  	// create a custom dialect from a list of messages
    30  	dialect := &dialect.Dialect{3, []message.Message{
    31  		&MessageCustom{},
    32  	}}
    33  
    34  	// create a node which understands the custom dialect
    35  	node, err := gomavlib.NewNode(gomavlib.NodeConf{
    36  		Endpoints: []gomavlib.EndpointConf{
    37  			gomavlib.EndpointSerial{
    38  				Device: "/dev/ttyUSB0",
    39  				Baud:   57600,
    40  			},
    41  		},
    42  		Dialect:     dialect,
    43  		OutVersion:  gomavlib.V2, // change to V1 if you're unable to communicate with the target
    44  		OutSystemID: 10,
    45  	})
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	defer node.Close()
    50  
    51  	// print incoming messages
    52  	for evt := range node.Events() {
    53  		if frm, ok := evt.(*gomavlib.EventFrame); ok {
    54  			log.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message())
    55  		}
    56  	}
    57  }