hybridgroup.gobot/platforms/mavlink/mavlink_adaptor.go

66 lines
1.4 KiB
Go
Raw Permalink Normal View History

2014-08-01 14:39:52 +08:00
package mavlink
import (
"io"
"go.bug.st/serial"
"gobot.io/x/gobot/v2"
common "gobot.io/x/gobot/v2/platforms/mavlink/common"
2014-08-01 14:39:52 +08:00
)
// Adaptor is a Mavlink transport adaptor.
type BaseAdaptor interface {
gobot.Connection
io.Writer
ReadMAVLinkPacket() (*common.MAVLinkPacket, error)
}
// Adaptor is a Mavlink-over-serial adaptor.
type Adaptor struct {
name string
port string
2014-08-01 14:39:52 +08:00
sp io.ReadWriteCloser
connect func(string) (io.ReadWriteCloser, error)
2014-08-01 14:39:52 +08:00
}
// NewAdaptor creates a new mavlink adaptor with specified port
func NewAdaptor(port string) *Adaptor {
return &Adaptor{
name: "Mavlink",
port: port,
connect: func(port string) (io.ReadWriteCloser, error) {
return serial.Open(port, &serial.Mode{BaudRate: 57600})
2014-08-01 14:39:52 +08:00
},
}
}
func (m *Adaptor) Name() string { return m.name }
func (m *Adaptor) SetName(n string) { m.name = n }
func (m *Adaptor) Port() string { return m.port }
// Connect returns true if connection to device is successful
func (m *Adaptor) Connect() error {
sp, err := m.connect(m.Port())
if err != nil {
return err
}
m.sp = sp
return nil
2014-08-01 14:39:52 +08:00
}
// Finalize returns true if connection to devices is closed successfully
func (m *Adaptor) Finalize() error {
return m.sp.Close()
2014-08-01 14:39:52 +08:00
}
func (m *Adaptor) ReadMAVLinkPacket() (*common.MAVLinkPacket, error) {
return common.ReadMAVLinkPacket(m.sp)
}
func (m *Adaptor) Write(b []byte) (int, error) {
return m.sp.Write(b)
}