hybridgroup.gobot/platforms/megapi/megapi_adaptor.go

88 lines
2.0 KiB
Go

package megapi
import (
"io"
"time"
"go.bug.st/serial"
"gobot.io/x/gobot/v2"
)
var _ gobot.Adaptor = (*Adaptor)(nil)
// Adaptor is the Gobot adaptor for the MakeBlock MegaPi board
type Adaptor struct {
name string
port string
connection io.ReadWriteCloser
serialMode *serial.Mode
writeBytesChannel chan []byte
finalizeChannel chan struct{}
}
// NewAdaptor returns a new MegaPi Adaptor with specified serial port used to talk to the MegaPi with a baud rate of 115200
func NewAdaptor(device string) *Adaptor {
c := &serial.Mode{BaudRate: 115200}
return &Adaptor{
name: "MegaPi",
connection: nil,
port: device,
serialMode: c,
writeBytesChannel: make(chan []byte),
finalizeChannel: make(chan struct{}),
}
}
// Name returns the name of this adaptor
func (megaPi *Adaptor) Name() string {
return megaPi.name
}
// SetName sets the name of this adaptor
func (megaPi *Adaptor) SetName(n string) {
megaPi.name = n
}
// Connect starts a connection to the board
func (megaPi *Adaptor) Connect() error {
if megaPi.connection == nil {
sp, err := serial.Open(megaPi.port, megaPi.serialMode)
if err != nil {
return err
}
// sleeping is required to give the board a chance to reset
time.Sleep(2 * time.Second)
megaPi.connection = sp
}
// kick off thread to send bytes to the board
go func() {
for {
select {
case bytes := <-megaPi.writeBytesChannel:
if _, err := megaPi.connection.Write(bytes); err != nil {
panic(err)
}
time.Sleep(10 * time.Millisecond)
case <-megaPi.finalizeChannel:
megaPi.finalizeChannel <- struct{}{}
return
default:
time.Sleep(10 * time.Millisecond)
}
}
}()
return nil
}
// Finalize terminates the connection to the board
func (megaPi *Adaptor) Finalize() error {
megaPi.finalizeChannel <- struct{}{}
<-megaPi.finalizeChannel
if err := megaPi.connection.Close(); err != nil {
return err
}
return nil
}